Leaked source code of windows server 2003
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.

321 lines
15 KiB

  1. //
  2. // Copyright (c) 2000, Intel Corporation
  3. // All rights reserved.
  4. //
  5. // Contributed 2/2/2000 by John Harrison, Ted Kubaska, Bob Norin, Shane Story,
  6. // and Ping Tak Peter Tang of the Computational Software Lab, Intel Corporation.
  7. //
  8. // WARRANTY DISCLAIMER
  9. //
  10. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  11. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  12. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  13. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
  14. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  15. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  16. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  17. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  18. // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
  19. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. //
  22. // Intel Corporation is the author of this code, and requests that all
  23. // problem reports or change requests be submitted to it directly at
  24. // http://developer.intel.com/opensource.
  25. //
  26. typedef enum
  27. {
  28. logl_zero=0, logl_negative, /* 0, 1 */
  29. log_zero, log_negative, /* 2, 3 */
  30. logf_zero, logf_negative, /* 4, 5 */
  31. log10l_zero, log10l_negative, /* 6, 7 */
  32. log10_zero, log10_negative, /* 8, 9 */
  33. log10f_zero, log10f_negative, /* 10, 11 */
  34. expl_overflow, expl_underflow, /* 12, 13 */
  35. exp_overflow, exp_underflow, /* 14, 15 */
  36. expf_overflow, expf_underflow, /* 16, 17 */
  37. powl_overflow, powl_underflow, /* 18, 19 */
  38. powl_zero_to_zero, /* 20 */
  39. powl_zero_to_negative, /* 21 */
  40. powl_neg_to_non_integer, /* 22 */
  41. powl_nan_to_zero, /* 23 */
  42. pow_overflow, pow_underflow, /* 24, 25 */
  43. pow_zero_to_zero, /* 26 */
  44. pow_zero_to_negative, /* 27 */
  45. pow_neg_to_non_integer, /* 28 */
  46. pow_nan_to_zero, /* 29 */
  47. powf_overflow, powf_underflow, /* 30, 31 */
  48. powf_zero_to_zero, /* 32 */
  49. powf_zero_to_negative, /* 33 */
  50. powf_neg_to_non_integer, /* 34 */
  51. powf_nan_to_zero, /* 35 */
  52. atan2l_zero, /* 36 */
  53. atan2_zero, /* 37 */
  54. atan2f_zero, /* 38 */
  55. expm1l_overflow, /* 39 */
  56. expm1l_underflow, /* 40 */
  57. expm1_overflow, /* 41 */
  58. expm1_underflow, /* 42 */
  59. expm1f_overflow, /* 43 */
  60. expm1f_underflow, /* 44 */
  61. hypotl_overflow, /* 45 */
  62. hypot_overflow, /* 46 */
  63. hypotf_overflow, /* 47 */
  64. sqrtl_negative, /* 48 */
  65. sqrt_negative, /* 49 */
  66. sqrtf_negative, /* 50 */
  67. scalbl_overflow, scalbl_underflow, /* 51,52 */
  68. scalb_overflow, scalb_underflow, /* 53,54 */
  69. scalbf_overflow, scalbf_underflow, /* 55,56 */
  70. acosl_gt_one, acos_gt_one, acosf_gt_one, /* 57, 58, 59 */
  71. asinl_gt_one, asin_gt_one, asinf_gt_one, /* 60, 61, 62 */
  72. coshl_overflow, cosh_overflow, coshf_overflow, /* 63, 64, 65 */
  73. y0l_zero, y0l_negative,y0l_gt_loss, /* 66, 67, 68 */
  74. y0_zero, y0_negative,y0_gt_loss, /* 69, 70, 71 */
  75. y0f_zero, y0f_negative,y0f_gt_loss, /* 72, 73, 74 */
  76. y1l_zero, y1l_negative,y1l_gt_loss, /* 75, 76, 77 */
  77. y1_zero, y1_negative,y1_gt_loss, /* 78, 79, 80 */
  78. y1f_zero, y1f_negative,y1f_gt_loss, /* 81, 82, 83 */
  79. ynl_zero, ynl_negative,ynl_gt_loss, /* 84, 85, 86 */
  80. yn_zero, yn_negative,yn_gt_loss, /* 87, 88, 89 */
  81. ynf_zero, ynf_negative,ynf_gt_loss, /* 90, 91, 92 */
  82. j0l_gt_loss, /* 93 */
  83. j0_gt_loss, /* 94 */
  84. j0f_gt_loss, /* 95 */
  85. j1l_gt_loss, /* 96 */
  86. j1_gt_loss, /* 97 */
  87. j1f_gt_loss, /* 98 */
  88. jnl_gt_loss, /* 99 */
  89. jn_gt_loss, /* 100 */
  90. jnf_gt_loss, /* 101 */
  91. lgammal_overflow, lgammal_negative,lgammal_reserve, /* 102, 103, 104 */
  92. lgamma_overflow, lgamma_negative,lgamma_reserve, /* 105, 106, 107 */
  93. lgammaf_overflow, lgammaf_negative, lgammaf_reserve,/* 108, 109, 110 */
  94. gammal_overflow,gammal_negative, gammal_reserve, /* 111, 112, 113 */
  95. gamma_overflow, gamma_negative, gamma_reserve, /* 114, 115, 116 */
  96. gammaf_overflow,gammaf_negative,gammaf_reserve, /* 117, 118, 119 */
  97. fmodl_by_zero, /* 120 */
  98. fmod_by_zero, /* 121 */
  99. fmodf_by_zero, /* 122 */
  100. remainderl_by_zero, /* 123 */
  101. remainder_by_zero, /* 124 */
  102. remainderf_by_zero, /* 125 */
  103. sinhl_overflow, sinh_overflow, sinhf_overflow, /* 126, 127, 128 */
  104. atanhl_gt_one, atanhl_eq_one, /* 129, 130 */
  105. atanh_gt_one, atanh_eq_one, /* 131, 132 */
  106. atanhf_gt_one, atanhf_eq_one, /* 133, 134 */
  107. acoshl_lt_one, /* 135 */
  108. acosh_lt_one, /* 136 */
  109. acoshf_lt_one, /* 137 */
  110. log1pl_zero, log1pl_negative, /* 138, 139 */
  111. log1p_zero, log1p_negative, /* 140, 141 */
  112. log1pf_zero, log1pf_negative, /* 142, 143 */
  113. ldexpl_overflow, ldexpl_underflow, /* 144, 145 */
  114. ldexp_overflow, ldexp_underflow, /* 146, 147 */
  115. ldexpf_overflow, ldexpf_underflow, /* 148, 149 */
  116. logbl_zero, logb_zero, logbf_zero, /* 150, 151,152 */
  117. nextafterl_overflow, nextafter_overflow,
  118. nextafterf_overflow, /* 153, 154,155 */
  119. ilogbl_zero, ilogb_zero, ilogbf_zero, /* 156, 157,158 */
  120. exp2l_overflow, exp2l_underflow, /* 159, 160 */
  121. exp2_overflow, exp2_underflow, /* 161, 162 */
  122. exp2f_overflow, exp2f_underflow, /* 163, 164 */
  123. exp10l_overflow, exp10_overflow,
  124. exp10f_overflow, /* 165, 166, 167 */
  125. log2l_zero, log2l_negative, /* 168, 169 */
  126. log2_zero, log2_negative, /* 170, 171 */
  127. log2f_zero, log2f_negative, /* 172, 173 */
  128. log_nan = 1000, /* 1000 */
  129. log10_nan, /* 1001 */
  130. exp_nan, /* 1002 */
  131. atan_nan, /* 1003 */
  132. ceil_nan, /* 1004 */
  133. floor_nan, /* 1005 */
  134. pow_nan, /* 1006 */
  135. modf_nan /* 1007 */
  136. } error_types;
  137. void __libm_error_support(void*,void*,void*,error_types);
  138. #define BIAS_64 1023
  139. #define EXPINF_64 2047
  140. #define DOUBLE_HEX(HI, LO) 0x ## LO, 0x ## HI
  141. static const unsigned INF[] = {
  142. DOUBLE_HEX(7ff00000, 00000000),
  143. DOUBLE_HEX(fff00000, 00000000)
  144. };
  145. static const unsigned BIG[] = {
  146. DOUBLE_HEX(7fe00000, 00000000),
  147. DOUBLE_HEX(00200000, 00000000)
  148. };
  149. static const unsigned ZERO[] = {
  150. DOUBLE_HEX(00000000, 00000000),
  151. DOUBLE_HEX(80000000, 00000000)
  152. };
  153. static const unsigned INF_32[] = {0x7f800000,0xff800000 };
  154. static const unsigned NAN_32[] = {0x7fc00000,0xffc00000};
  155. static const unsigned ZERO_32[] = { 0, 0x80000000 };
  156. static const float libm_largef[] = { 1.0e+30f, -1.0e+30f };
  157. static const float libm_smallf[] = { 1.0e-30f, -1.0e-30f };
  158. static const double libm_small[] = { 1.0e-300, -1.0e-300 };
  159. #define INVALID (*((double*)&ZERO[0]) * *((double*)&INF[0]))
  160. #define INVALID_32 *(float *)&ZERO_32[0] * *(float *)&INF_32[0]
  161. #define LIBM_OVERFLOW (*((double*)&BIG[0]) * *((double*)&BIG[0]))
  162. #define LIBM_UNDERFLOW (*((double*)&BIG[2]) * *((double*)&BIG[2]))
  163. #define LIBM_OVERFLOWF (libm_largef[0] * libm_largef[0])
  164. #define LIBM_UNDERFLOWF (libm_smallf[0] * libm_smallf[0])
  165. #define PINF *((double*)&INF[0])
  166. #define NINF -PINF
  167. #define PINF_DZ (1.0/(*((double*)&ZERO[0]))
  168. struct _exception
  169. {
  170. int type;
  171. char *name;
  172. double arg1, arg2, retval;
  173. };
  174. #define MATHERR_D _matherr
  175. #define EXC_DECL_D _exception
  176. extern int MATHERR_D(struct EXC_DECL_D*);
  177. /* Set these appropriately to make thread Safe */
  178. #define ERRNO_RANGE errno = ERANGE
  179. #define ERRNO_DOMAIN errno = EDOM
  180. extern int (*_pmatherr)(struct EXC_DECL_D*);
  181. // This is a run-time variable and may affect
  182. // floating point behavior of the libm functions
  183. #define BIAS_32 127
  184. #define BIAS_64 1023
  185. #define BIAS_80 16383
  186. #define BIAS_128 16383
  187. #define MAXEXP_32 254
  188. #define MAXEXP_64 2046
  189. #define MAXEXP_80 32766
  190. #define MAXEXP_128 32766
  191. #define EXPINF_32 255
  192. #define EXPINF_64 2047
  193. #define EXPINF_80 32767
  194. #define EXPINF_128 32767
  195. /*****************************/
  196. /* memory format definitions */
  197. /*****************************/
  198. /*/// if you use /Zp2 option, or the following pragma... then the
  199. //// 80 bit FP objects will occupy only 10 bytes in memory - otherwise
  200. //// more generous alignments will "waste" memory.
  201. //// Use of -DPACKFP80 is NOT recommended.
  202. //// The same effect can be obtained using /Zp2 (again, NOT recommended).
  203. //// Otherwise the alignment will be a word, forcing the size to be
  204. //// 12 bytes (this means some memory will be wasted in exchange
  205. //// for better performance).
  206. //// Unfortunately, because -DPACKFP80 is implemented the same as /Zp2,
  207. //// all structures will be packed tighter in the entire program. */
  208. #ifdef PACKFP80
  209. #pragma warning(disable:4103)
  210. #pragma pack(2)
  211. #endif
  212. struct fp32 { /*// sign:1 exponent:8 significand:23 (implied leading 1)*/
  213. unsigned significand:23;
  214. unsigned exponent:8;
  215. unsigned sign:1;
  216. };
  217. struct fp64 { /*/ sign:1 exponent:11 significand:52 (implied leading 1)*/
  218. unsigned lo_significand:32;
  219. unsigned hi_significand:20;
  220. unsigned exponent:11;
  221. unsigned sign:1;
  222. };
  223. struct fp80 { /*/ sign:1 exponent:15 significand:64 (NO implied bits) */
  224. unsigned lo_significand;
  225. unsigned hi_significand;
  226. unsigned exponent:15;
  227. unsigned sign:1;
  228. };
  229. #ifdef __cplusplus
  230. extern "C" {
  231. #endif
  232. #define NONZERO_SIGNIFICAND(X) ((X)->hi_significand != 0 || (X)->lo_significand != 0)
  233. #define ZERO_SIGNIFICAND(X) ((X)->hi_significand == 0 && (X)->lo_significand == 0)
  234. #define SET_ZERO_SIGNIFICAND(X) (X)->hi_significand = 0; (X)->lo_significand = 0
  235. #define SET_LO_SIGNIFICAND(X, LO) (X)->lo_significand = (LO)
  236. #define SET_HI_SIGNIFICAND(X, HI) (X)->hi_significand = (HI)
  237. #define SET_SIGNIFICAND(X, HI, LO) (X)->hi_significand = (HI);(X)->lo_significand = (LO)
  238. #define SIGNIFICAND_EQUAL(X, HI, LO) (((X)->hi_significand == 0x ## HI) && ((X)->lo_significand == 0x ## LO))
  239. #define SIGNIFICAND_GREATER(X, HI, LO) ((X)->hi_significand > 0x ## HI) || \
  240. (((X)->hi_significand == 0x ## HI) && ((X)->lo_significand > 0x ## LO))
  241. #define SIGNIFICAND_GREATER_EQ(X, HI, LO) ((X)->hi_significand > 0x ## HI) || \
  242. (((X)->hi_significand == 0x ## HI) && ((X)->lo_significand >= 0x ## LO))
  243. #define SIGNIFICAND_LESS(X, HI, LO) ((X)->hi_significand < 0x ## HI) || \
  244. (((X)->hi_significand == 0x ## HI) && ((X)->lo_significand < 0x ## LO))
  245. #define SIGNIFICAND_LESS_EQ(X, HI, LO) ((X)->hi_significand < 0x ## HI) || \
  246. (((X)->hi_significand == 0x ## HI) && ((X)->lo_significand <= 0x ## LO))
  247. #define HI_SIGNIFICAND_LESS(X, HI) ((X)->hi_significand < 0x ## HI)
  248. #define ISSIGNIFICAND_EQUAL(X, Y) (((X)->hi_significand == (Y)->hi_significand) && ((X)->lo_significand == (Y)->lo_significand))
  249. #define ISSIGNIFICAND_GREATER(X, Y) ((X)->hi_significand > (Y)->hi_significand) || \
  250. (((X)->hi_significand == (Y)->hi_significand) && ((X)->lo_significand > (Y)->lo_significand))
  251. #define ADD_ULP(X) if(!++(X)->lo_significand) { \
  252. if(!++(X)->hi_significand) { \
  253. (X)->exponent++; }}
  254. #define SUB_ULP(X) if(!(X)->lo_significand--) { \
  255. if(!(X)->hi_significand--) { \
  256. (X)->exponent--; }}
  257. #define CLEAR_LOW_BITS(X,MASK) (X)->lo_significand &= 0x ## MASK
  258. #define FIX_N_HI_BITS(X, NBIT) (X)->lo_significand = 0; (X)->hi_significand &= (0xfffff << (20 - NBIT))
  259. #define HI_BITS_TO_INTEGER(X, NBIT) (1 << (NBIT)) | ((X)->hi_significand >> (20 - (NBIT)))
  260. #ifdef BIG_ENDIAN
  261. #define DOUBLE_HEX(HI, LO) 0x ## HI, 0x ## LO
  262. #define HI_WORD(NUM) (*((unsigned *)(NUM))) & 0x7fffffff
  263. #else
  264. #define DOUBLE_HEX(HI, LO) 0x ## LO, 0x ## HI
  265. #define HI_WORD(NUM) (*(((unsigned *)(NUM))+1)) & 0x7fffffff
  266. #endif
  267. #define SIGN_EXPAND(val,num) ((val) << (32-(num))) >> (32-(num)) /* sign expand of 'num' LSBs */
  268. #define VALUE_EQUAL(X,EXP,HI,LO) (((X)->exponent == (EXP)) && SIGNIFICAND_EQUAL(X, HI, LO))
  269. #define VALUE_GREATER(X,EXP,HI,LO) (((X)->exponent > (EXP)) || \
  270. (((X)->exponent == (EXP)) && (SIGNIFICAND_GREATER(X, HI, LO))))
  271. #define VALUE_GREATER_EQ(X,EXP,HI,LO) (((X)->exponent > (EXP)) || \
  272. (((X)->exponent == (EXP)) && (SIGNIFICAND_GREATER_EQ(X, HI, LO))))
  273. #define VALUE_LESS(X,EXP,HI,LO) (((X)->exponent < (EXP)) || \
  274. (((X)->exponent == (EXP)) && (SIGNIFICAND_LESS(X, HI, LO))))
  275. #define VALUE_LESS_EQ(X,EXP,HI,LO) (((X)->exponent < (EXP)) || \
  276. (((X)->exponent == (EXP)) && (SIGNIFICAND_LESS_EQ(X, HI, LO))))
  277. #define ISVALUE_EQUAL(X, Y) \
  278. (((X)->exponent == (Y)->exponent) && ISSIGNIFICAND_EQUAL(X, Y))
  279. #define ISVALUE_GREATER(X, Y) \
  280. (((X)->exponent > (Y)->exponent) || (((X)->exponent == (Y)->exponent) && ISSIGNIFICAND_GREATER(X, Y)))
  281. #define VALUE_GREATERF(X,EXP,SIG) (((X)->exponent > (EXP)) || \
  282. (((X)->exponent == (EXP)) && ((X)->significand > 0x##SIG)))
  283. #define VALUE_GREATER_EQF(X,EXP,SIG) (((X)->exponent > (EXP)) || \
  284. (((X)->exponent == (EXP)) && ((X)->significand >= 0x##SIG)))
  285. #define VALUE_LESSF(X,EXP,SIG) (((X)->exponent < (EXP)) || \
  286. (((X)->exponent == (EXP)) && ((X)->significand < 0x##SIG)))
  287. #define VALUE_LESS_EQF(X,EXP,SIG) (((X)->exponent < (EXP)) || \
  288. (((X)->exponent == (EXP)) && ((X)->significand <= 0x##SIG)))