Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
7.1 KiB

  1. #ifndef __FIXED_H__
  2. #define __FIXED_H__
  3. #include <math.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #ifndef PI
  8. #define PI (3.1415926535897932384626433832795028841971693993751)
  9. #endif
  10. /* Default table size for precomputed sincos table */
  11. #define FA_TABLE_SIZE 360
  12. /*
  13. Flags for initialization
  14. FA_CARTESIAN_Y - Y axis is positive up
  15. */
  16. #define FA_DEFAULT 0
  17. #define FA_CARTESIAN_Y 1
  18. #if defined(FX_DOUBLE) || defined(FX_SINGLE)
  19. #ifdef FX_DOUBLE
  20. typedef double FxValue;
  21. #define FX_MAX_VALUE (1e100)
  22. #define FX_MIN_VALUE (1e-10)
  23. #else
  24. typedef float FxValue;
  25. #define FX_MAX_VALUE (1e38f)
  26. #define FX_MIN_VALUE (1e-7f)
  27. #endif
  28. #define FxVal(i) ((FxValue)(i))
  29. #define FxInt(v) ((int)(v))
  30. #define FxFltVal(f) ((FxValue)(f))
  31. #define FxFlt(v) ((double)(v))
  32. #define FxPromote(v) (v)
  33. #define FxDemote(v) (v)
  34. #define FxMul(a, b) ((a)*(b))
  35. #define FxDemotedMul(a, b) FxMul(a, b)
  36. #define FxDiv(a, b) ((a)/(b))
  37. #define FxDemotedDiv(a, b) FxDiv(a, b)
  38. #define FxMulToInt(a, b) FxInt((a)*(b))
  39. #define FxDivToInt(a, b) ((int)((a)/(b)))
  40. #define FxMulDiv(a, m, d) (((a)*(m))/(d))
  41. #define FxSqrt(v) ((FxValue)sqrt((double)(v)))
  42. #define FxDemotedSqrt(v) FxSqrt(v)
  43. typedef FxValue FaAngle;
  44. #define FaAng(a) (a)
  45. #define FaSin(v) ((FxValue)-sin((double)(v)))
  46. #define FaCos(v) ((FxValue)cos((double)(v)))
  47. #define FaAdd(a, d) ((a)+(d))
  48. FaAngle FaNorm(FaAngle a);
  49. #define FaDeg(da) ((da)*(FxValue)(PI/180.0))
  50. #define FaRad(ra) (ra)
  51. #define FaAngVal(aa) (aa)
  52. #define FaFltDegVal(a) ((a)*180.0/PI)
  53. #define FaFltRadVal(a) (a)
  54. #define FxInitialize(table_size, flags) ((flags) == FA_DEFAULT)
  55. #define FxEnd()
  56. #else
  57. /* If integer sqrt isn't interesting, define FX_PRECISE_SQRT
  58. and the floating point sqrt will be used */
  59. #ifndef FX_SHIFT
  60. #define FX_SHIFT 10
  61. #endif
  62. #define FX_MULT (1L << FX_SHIFT)
  63. typedef long FxValue;
  64. #define FX_MAX_VALUE (0x7fffffff)
  65. #define FX_MIN_VALUE (1)
  66. #define FxVal(i) FxPromote((FxValue)(i))
  67. #define FxInt(v) ((int)FxDemote(v))
  68. #define FxFltVal(f) ((FxValue)((f)*(double)FX_MULT))
  69. #define FxFlt(v) (((double)(v))/(double)FX_MULT)
  70. #define FxPromote(v) ((v) << FX_SHIFT)
  71. #define FxDemote(v) ((v) >> FX_SHIFT)
  72. #if FX_SHIFT != 16
  73. /* These can overflow if the shift and numbers are too large */
  74. #define FxMul(a, b) FxDemote((a)*(b))
  75. #define FxDiv(a, b) (FxPromote(a)/(b))
  76. #define FxMulToInt(a, b) FxInt(FxDemote((a)*(b)))
  77. #define FxMulDiv(a, m, d) (((a)*(m))/(d))
  78. #else
  79. /* For FX_SHIFT == 16 and certain platforms, assembly routines are
  80. provided which do 64-bit intermediate math, preserving accuracy
  81. There is still a danger of overflow if the results don't fit in
  82. 32 bits, though */
  83. FxValue FxMul(FxValue a, FxValue b);
  84. FxValue FxDiv(FxValue a, FxValue b);
  85. int FxMulToInt(FxValue a, FxValue b);
  86. FxValue FxMulDiv(FxValue a, FxValue m, FxValue d);
  87. #endif
  88. #ifndef FX_PRECISE_SQRT
  89. FxValue FxSqrt(FxValue v);
  90. /* Computing the square root of a demoted value leaves it out of
  91. adjustment by sqrt(FX_MULT) so shift by FX_SHIFT/2 to
  92. restore fixed point
  93. FX_SHIFT should be even for this to work */
  94. #define FxDemotedSqrt(v) (FxSqrt(FxDemote(v)) << (FX_SHIFT/2))
  95. #else
  96. #define FxSqrt(v) FxFltVal(sqrt(FxFlt(v)))
  97. #define FxDemotedSqrt(v) FxSqrt(v)
  98. #endif
  99. #define FxDemotedMul(a, b) (FxDemote(a)*(b))
  100. #define FxDemotedDiv(a, b) FxPromote((a)/(b))
  101. #define FxDivToInt(a, b) ((int)((a)/(b)))
  102. /* One unit of angle is the table quantum
  103. One unit of angle equals 360/_fa_table_size degrees */
  104. typedef FxValue FaAngle;
  105. extern int _fa_table_size;
  106. extern FxValue *_fa_sines;
  107. extern FxValue *_fa_cosines;
  108. #define FaAng(a) FxDemote(a)
  109. #define FaSin(a) _fa_sines[(int)FaAng(a)]
  110. #define FaCos(a) _fa_cosines[(int)FaAng(a)]
  111. FaAngle FaAdd(FaAngle a, FaAngle d);
  112. FaAngle FaBisectingAngle(FaAngle f, FaAngle t);
  113. #define FaNorm(a) FaAdd(FxVal(0), a)
  114. #define FaDeg(da) \
  115. FaNorm(FxMulDiv(FxVal(da), _fa_table_size, 360))
  116. #define FaRad(ra) FaNorm(FxFltVal((ra)*_fa_table_size/PI2))
  117. #define FaAngVal(aa) FaNorm(FxVal(aa))
  118. #define FaFltDegVal(ang) FxFltVal(FxMulDiv(ang, 360, _fa_table_size))
  119. #define FaFltRadVal(ang) (FxFltVal(ang)*PI2/_fa_table_size)
  120. BOOL FxInitialize(int table_size, ULONG flags);
  121. void FxEnd(void);
  122. #endif
  123. typedef struct _FxPt2
  124. {
  125. FxValue x, y;
  126. } FxPt2;
  127. typedef FxPt2 FxVec2;
  128. typedef struct _FxBox2
  129. {
  130. FxPt2 min, max;
  131. } FxBox2;
  132. typedef struct _FxPt3
  133. {
  134. FxValue x, y, z;
  135. } FxPt3;
  136. typedef FxPt3 FxVec3;
  137. typedef struct _FxBox3
  138. {
  139. FxPt3 min, max;
  140. } FxBox3;
  141. typedef struct _FxPt4
  142. {
  143. FxValue x, y, z, w;
  144. } FxPt4;
  145. typedef FxPt4 FxVec4;
  146. void FxBBox2Empty(FxBox2 *bb);
  147. void FxBBox2AddPt(FxBox2 *bb, FxPt2 *pt);
  148. void FxBBox3Empty(FxBox3 *bb);
  149. void FxBBox3AddPt(FxBox3 *bb, FxPt3 *pt);
  150. #define FxV2Set(v, xv, yv) \
  151. ((v)->x = (xv), (v)->y = (yv))
  152. #define FxV2Add(a, b, r) \
  153. ((r)->x = (a)->x+(b)->x, (r)->y = (a)->y+(b)->y)
  154. #define FxV2Sub(a, b, r) \
  155. ((r)->x = (a)->x-(b)->x, (r)->y = (a)->y-(b)->y)
  156. #define FxV2Dot(a, b) \
  157. (FxMul((a)->x, (b)->x)+FxMul((a)->y, (b)->y))
  158. #define FxV2Neg(v, r) \
  159. ((r)->x = -(v)->x, (r)->y = -(v)->y)
  160. #define FxV2NegV(v) FxV2Neg(v, v)
  161. #define FxV2NormV(v) FxV2Norm(v, v)
  162. FxValue FxV2Len(FxVec2 *v);
  163. FxValue FxV2Norm(FxVec2 *v, FxVec2 *r);
  164. #define FxvV2Set(v, xv, yv) FxV2Set(&(v), xv, yv)
  165. #define FxvV2Add(a, b, r) FxV2Add(&(a), &(b), &(r))
  166. #define FxvV2Sub(a, b, r) FxV2Sub(&(a), &(b), &(r))
  167. #define FxvV2Dot(a, b) FxV2Dot(&(a), &(b))
  168. #define FxvV2Neg(v, r) FxV2Neg(&(v), &(r))
  169. #define FxvV2NegV(v) FxV2NegV(&(v))
  170. #define FxvV2Len(v) FxV2Len(&(v))
  171. #define FxvV2Norm(v, r) FxV2Norm(&(v), &(r))
  172. #define FxvV2NormV(v) FxV2NormV(&(v))
  173. #define FxV3Set(v, xv, yv, zv) \
  174. ((v)->x = (xv), (v)->y = (yv), (v)->z = (zv))
  175. #define FxV3Add(a, b, r) \
  176. ((r)->x = (a)->x+(b)->x, (r)->y = (a)->y+(b)->y, (r)->z = (a)->z+(b)->z)
  177. #define FxV3Sub(a, b, r) \
  178. ((r)->x = (a)->x-(b)->x, (r)->y = (a)->y-(b)->y, (r)->z = (a)->z-(b)->z)
  179. #define FxV3Dot(a, b) \
  180. (FxMul((a)->x, (b)->x)+FxMul((a)->y, (b)->y)+FxMul((a)->z, (b)->z))
  181. #define FxV3Neg(v, r) \
  182. ((r)->x = -(v)->x, (r)->y = -(v)->y, (r)->z = -(v)->z)
  183. #define FxV3NegV(v) FxV3Neg(v, v)
  184. #define FxV3Cross(a, b, r) \
  185. ((r)->x = (a)->y*(b)->z-(b)->y*(a)->z,\
  186. (r)->y = (a)->z*(b)->x-(b)->z*(a)->x,\
  187. (r)->z = (a)->x*(b)->y-(b)->x*(a)->y)
  188. #define FxV3NormV(v) FxV3Norm(v, v)
  189. FxValue FxV3Len(FxVec3 *v);
  190. FxValue FxV3Norm(FxVec3 *v, FxVec3 *r);
  191. #define FxvV3Set(v, xv, yv, zv) FxV3Set(&(v), xv, yv, zv)
  192. #define FxvV3Add(a, b, r) FxV3Add(&(a), &(b), &(r))
  193. #define FxvV3Sub(a, b, r) FxV3Sub(&(a), &(b), &(r))
  194. #define FxvV3Dot(a, b) FxV3Dot(&(a), &(b))
  195. #define FxvV3Neg(v, r) FxV3Neg(&(v), &(r))
  196. #define FxvV3NegV(v) FxV3NegV(&(v))
  197. #define FxvV3Cross(a, b, r) FxV3Cross(&(a), &(b), &(r))
  198. #define FxvV3Len(v) FxV3Len(&(v))
  199. #define FxvV3Norm(v, r) FxV3Norm(&(v), &(r))
  200. #define FxvV3NormV(v) FxV3NormV(&(v))
  201. typedef FxValue FxMatrix2[2][2];
  202. typedef FxValue FxMatrix3[3][3];
  203. typedef FxValue FxMatrix4[4][4];
  204. typedef FxValue FxTMatrix2[2][3];
  205. typedef FxValue FxTMatrix3[3][4];
  206. void FxT2Ident(FxTMatrix2 m);
  207. void FxT2Mul(FxTMatrix2 a, FxTMatrix2 b, FxTMatrix2 r);
  208. void FxT2Vec2(FxTMatrix2 m, int n, FxVec2 *f, FxVec2 *t);
  209. void FxT3Ident(FxTMatrix3 m);
  210. void FxT3Mul(FxTMatrix3 a, FxTMatrix3 b, FxTMatrix3 r);
  211. void FxT3Vec3(FxTMatrix3 m, int n, FxVec3 *f, FxVec3 *t);
  212. #ifdef __cplusplus
  213. }
  214. #endif
  215. #endif