fdlibm.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * This file is adapted from from newlib-nano-2, the newlib/libm/common/fdlib.h,
  5. * available from https://github.com/32bitmicro/newlib-nano-2. The main change
  6. * is removal of anything to do with double precision.
  7. *
  8. * Appropriate copyright headers are reproduced below.
  9. */
  10. /* @(#)fdlibm.h 5.1 93/09/24 */
  11. /*
  12. * ====================================================
  13. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  14. *
  15. * Developed at SunPro, a Sun Microsystems, Inc. business.
  16. * Permission to use, copy, modify, and distribute this
  17. * software is freely granted, provided that this notice
  18. * is preserved.
  19. * ====================================================
  20. */
  21. #include <math.h>
  22. /* Default to XOPEN_MODE. */
  23. #define _XOPEN_MODE
  24. /* Most routines need to check whether a float is finite, infinite, or not a
  25. number, and many need to know whether the result of an operation will
  26. overflow. These conditions depend on whether the largest exponent is
  27. used for NaNs & infinities, or whether it's used for finite numbers. The
  28. macros below wrap up that kind of information:
  29. FLT_UWORD_IS_FINITE(X)
  30. True if a positive float with bitmask X is finite.
  31. FLT_UWORD_IS_NAN(X)
  32. True if a positive float with bitmask X is not a number.
  33. FLT_UWORD_IS_INFINITE(X)
  34. True if a positive float with bitmask X is +infinity.
  35. FLT_UWORD_MAX
  36. The bitmask of FLT_MAX.
  37. FLT_UWORD_HALF_MAX
  38. The bitmask of FLT_MAX/2.
  39. FLT_UWORD_EXP_MAX
  40. The bitmask of the largest finite exponent (129 if the largest
  41. exponent is used for finite numbers, 128 otherwise).
  42. FLT_UWORD_LOG_MAX
  43. The bitmask of log(FLT_MAX), rounded down. This value is the largest
  44. input that can be passed to exp() without producing overflow.
  45. FLT_UWORD_LOG_2MAX
  46. The bitmask of log(2*FLT_MAX), rounded down. This value is the
  47. largest input than can be passed to cosh() without producing
  48. overflow.
  49. FLT_LARGEST_EXP
  50. The largest biased exponent that can be used for finite numbers
  51. (255 if the largest exponent is used for finite numbers, 254
  52. otherwise) */
  53. #ifdef _FLT_LARGEST_EXPONENT_IS_NORMAL
  54. #define FLT_UWORD_IS_FINITE(x) 1
  55. #define FLT_UWORD_IS_NAN(x) 0
  56. #define FLT_UWORD_IS_INFINITE(x) 0
  57. #define FLT_UWORD_MAX 0x7fffffff
  58. #define FLT_UWORD_EXP_MAX 0x43010000
  59. #define FLT_UWORD_LOG_MAX 0x42b2d4fc
  60. #define FLT_UWORD_LOG_2MAX 0x42b437e0
  61. #define HUGE ((float)0X1.FFFFFEP128)
  62. #else
  63. #define FLT_UWORD_IS_FINITE(x) ((x)<0x7f800000L)
  64. #define FLT_UWORD_IS_NAN(x) ((x)>0x7f800000L)
  65. #define FLT_UWORD_IS_INFINITE(x) ((x)==0x7f800000L)
  66. #define FLT_UWORD_MAX 0x7f7fffffL
  67. #define FLT_UWORD_EXP_MAX 0x43000000
  68. #define FLT_UWORD_LOG_MAX 0x42b17217
  69. #define FLT_UWORD_LOG_2MAX 0x42b2d4fc
  70. #define HUGE ((float)3.40282346638528860e+38)
  71. #endif
  72. #define FLT_UWORD_HALF_MAX (FLT_UWORD_MAX-(1L<<23))
  73. #define FLT_LARGEST_EXP (FLT_UWORD_MAX>>23)
  74. /* Many routines check for zero and subnormal numbers. Such things depend
  75. on whether the target supports denormals or not:
  76. FLT_UWORD_IS_ZERO(X)
  77. True if a positive float with bitmask X is +0. Without denormals,
  78. any float with a zero exponent is a +0 representation. With
  79. denormals, the only +0 representation is a 0 bitmask.
  80. FLT_UWORD_IS_SUBNORMAL(X)
  81. True if a non-zero positive float with bitmask X is subnormal.
  82. (Routines should check for zeros first.)
  83. FLT_UWORD_MIN
  84. The bitmask of the smallest float above +0. Call this number
  85. REAL_FLT_MIN...
  86. FLT_UWORD_EXP_MIN
  87. The bitmask of the float representation of REAL_FLT_MIN's exponent.
  88. FLT_UWORD_LOG_MIN
  89. The bitmask of |log(REAL_FLT_MIN)|, rounding down.
  90. FLT_SMALLEST_EXP
  91. REAL_FLT_MIN's exponent - EXP_BIAS (1 if denormals are not supported,
  92. -22 if they are).
  93. */
  94. #ifdef _FLT_NO_DENORMALS
  95. #define FLT_UWORD_IS_ZERO(x) ((x)<0x00800000L)
  96. #define FLT_UWORD_IS_SUBNORMAL(x) 0
  97. #define FLT_UWORD_MIN 0x00800000
  98. #define FLT_UWORD_EXP_MIN 0x42fc0000
  99. #define FLT_UWORD_LOG_MIN 0x42aeac50
  100. #define FLT_SMALLEST_EXP 1
  101. #else
  102. #define FLT_UWORD_IS_ZERO(x) ((x)==0)
  103. #define FLT_UWORD_IS_SUBNORMAL(x) ((x)<0x00800000L)
  104. #define FLT_UWORD_MIN 0x00000001
  105. #define FLT_UWORD_EXP_MIN 0x43160000
  106. #define FLT_UWORD_LOG_MIN 0x42cff1b5
  107. #define FLT_SMALLEST_EXP -22
  108. #endif
  109. #ifdef __STDC__
  110. #undef __P
  111. #define __P(p) p
  112. #else
  113. #define __P(p) ()
  114. #endif
  115. /*
  116. * set X_TLOSS = pi*2**52, which is possibly defined in <values.h>
  117. * (one may replace the following line by "#include <values.h>")
  118. */
  119. #define X_TLOSS 1.41484755040568800000e+16
  120. /* Functions that are not documented, and are not in <math.h>. */
  121. /* Undocumented float functions. */
  122. #ifdef _SCALB_INT
  123. extern float scalbf __P((float, int));
  124. #else
  125. extern float scalbf __P((float, float));
  126. #endif
  127. extern float significandf __P((float));
  128. /* ieee style elementary float functions */
  129. extern float __ieee754_sqrtf __P((float));
  130. extern float __ieee754_acosf __P((float));
  131. extern float __ieee754_acoshf __P((float));
  132. extern float __ieee754_logf __P((float));
  133. extern float __ieee754_atanhf __P((float));
  134. extern float __ieee754_asinf __P((float));
  135. extern float __ieee754_atan2f __P((float,float));
  136. extern float __ieee754_expf __P((float));
  137. extern float __ieee754_coshf __P((float));
  138. extern float __ieee754_fmodf __P((float,float));
  139. extern float __ieee754_powf __P((float,float));
  140. extern float __ieee754_lgammaf_r __P((float,int *));
  141. extern float __ieee754_gammaf_r __P((float,int *));
  142. extern float __ieee754_log10f __P((float));
  143. extern float __ieee754_sinhf __P((float));
  144. extern float __ieee754_hypotf __P((float,float));
  145. extern float __ieee754_j0f __P((float));
  146. extern float __ieee754_j1f __P((float));
  147. extern float __ieee754_y0f __P((float));
  148. extern float __ieee754_y1f __P((float));
  149. extern float __ieee754_jnf __P((int,float));
  150. extern float __ieee754_ynf __P((int,float));
  151. extern float __ieee754_remainderf __P((float,float));
  152. extern __int32_t __ieee754_rem_pio2f __P((float,float*));
  153. #ifdef _SCALB_INT
  154. extern float __ieee754_scalbf __P((float,int));
  155. #else
  156. extern float __ieee754_scalbf __P((float,float));
  157. #endif
  158. /* float versions of fdlibm kernel functions */
  159. extern float __kernel_sinf __P((float,float,int));
  160. extern float __kernel_cosf __P((float,float));
  161. extern float __kernel_tanf __P((float,float,int));
  162. extern int __kernel_rem_pio2f __P((float*,float*,int,int,int,const __int32_t*));
  163. /* A union which permits us to convert between a float and a 32 bit
  164. int. */
  165. typedef union
  166. {
  167. float value;
  168. __uint32_t word;
  169. } ieee_float_shape_type;
  170. /* Get a 32 bit int from a float. */
  171. #define GET_FLOAT_WORD(i,d) \
  172. do { \
  173. ieee_float_shape_type gf_u; \
  174. gf_u.value = (d); \
  175. (i) = gf_u.word; \
  176. } while (0)
  177. /* Set a float from a 32 bit int. */
  178. #define SET_FLOAT_WORD(d,i) \
  179. do { \
  180. ieee_float_shape_type sf_u; \
  181. sf_u.word = (i); \
  182. (d) = sf_u.value; \
  183. } while (0)
  184. /* Macros to avoid undefined behaviour that can arise if the amount
  185. of a shift is exactly equal to the size of the shifted operand. */
  186. #define SAFE_LEFT_SHIFT(op,amt) \
  187. (((amt) < 8 * sizeof(op)) ? ((op) << (amt)) : 0)
  188. #define SAFE_RIGHT_SHIFT(op,amt) \
  189. (((amt) < 8 * sizeof(op)) ? ((op) >> (amt)) : 0)