libm.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifndef _LIBM_H
  2. #define _LIBM_H
  3. #include <stdint.h>
  4. #include <float.h>
  5. #include <math.h>
  6. #include <endian.h>
  7. #include "fp_arch.h"
  8. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  9. #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
  10. union ldshape {
  11. long double f;
  12. struct {
  13. uint64_t m;
  14. uint16_t se;
  15. } i;
  16. };
  17. #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
  18. /* This is the m68k variant of 80-bit long double, and this definition only works
  19. * on archs where the alignment requirement of uint64_t is <= 4. */
  20. union ldshape {
  21. long double f;
  22. struct {
  23. uint16_t se;
  24. uint16_t pad;
  25. uint64_t m;
  26. } i;
  27. };
  28. #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
  29. union ldshape {
  30. long double f;
  31. struct {
  32. uint64_t lo;
  33. uint32_t mid;
  34. uint16_t top;
  35. uint16_t se;
  36. } i;
  37. struct {
  38. uint64_t lo;
  39. uint64_t hi;
  40. } i2;
  41. };
  42. #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
  43. union ldshape {
  44. long double f;
  45. struct {
  46. uint16_t se;
  47. uint16_t top;
  48. uint32_t mid;
  49. uint64_t lo;
  50. } i;
  51. struct {
  52. uint64_t hi;
  53. uint64_t lo;
  54. } i2;
  55. };
  56. #else
  57. #error Unsupported long double representation
  58. #endif
  59. /* Support non-nearest rounding mode. */
  60. #define WANT_ROUNDING 1
  61. /* Support signaling NaNs. */
  62. #define WANT_SNAN 0
  63. #if WANT_SNAN
  64. #error SNaN is unsupported
  65. #else
  66. #define issignalingf_inline(x) 0
  67. #define issignaling_inline(x) 0
  68. #endif
  69. #ifndef TOINT_INTRINSICS
  70. #define TOINT_INTRINSICS 0
  71. #endif
  72. #if TOINT_INTRINSICS
  73. /* Round x to nearest int in all rounding modes, ties have to be rounded
  74. consistently with converttoint so the results match. If the result
  75. would be outside of [-2^31, 2^31-1] then the semantics is unspecified. */
  76. static double_t roundtoint(double_t);
  77. /* Convert x to nearest int in all rounding modes, ties have to be rounded
  78. consistently with roundtoint. If the result is not representible in an
  79. int32_t then the semantics is unspecified. */
  80. static int32_t converttoint(double_t);
  81. #endif
  82. /* Helps static branch prediction so hot path can be better optimized. */
  83. #ifdef __GNUC__
  84. #define predict_true(x) __builtin_expect(!!(x), 1)
  85. #define predict_false(x) __builtin_expect(x, 0)
  86. #else
  87. #define predict_true(x) (x)
  88. #define predict_false(x) (x)
  89. #endif
  90. /* Evaluate an expression as the specified type. With standard excess
  91. precision handling a type cast or assignment is enough (with
  92. -ffloat-store an assignment is required, in old compilers argument
  93. passing and return statement may not drop excess precision). */
  94. static inline float eval_as_float(float x)
  95. {
  96. float y = x;
  97. return y;
  98. }
  99. static inline double eval_as_double(double x)
  100. {
  101. double y = x;
  102. return y;
  103. }
  104. /* fp_barrier returns its input, but limits code transformations
  105. as if it had a side-effect (e.g. observable io) and returned
  106. an arbitrary value. */
  107. #ifndef fp_barrierf
  108. #define fp_barrierf fp_barrierf
  109. static inline float fp_barrierf(float x)
  110. {
  111. volatile float y = x;
  112. return y;
  113. }
  114. #endif
  115. #ifndef fp_barrier
  116. #define fp_barrier fp_barrier
  117. static inline double fp_barrier(double x)
  118. {
  119. volatile double y = x;
  120. return y;
  121. }
  122. #endif
  123. #ifndef fp_barrierl
  124. #define fp_barrierl fp_barrierl
  125. static inline long double fp_barrierl(long double x)
  126. {
  127. volatile long double y = x;
  128. return y;
  129. }
  130. #endif
  131. /* fp_force_eval ensures that the input value is computed when that's
  132. otherwise unused. To prevent the constant folding of the input
  133. expression, an additional fp_barrier may be needed or a compilation
  134. mode that does so (e.g. -frounding-math in gcc). Then it can be
  135. used to evaluate an expression for its fenv side-effects only. */
  136. #ifndef fp_force_evalf
  137. #define fp_force_evalf fp_force_evalf
  138. static inline void fp_force_evalf(float x)
  139. {
  140. volatile float y;
  141. y = x;
  142. }
  143. #endif
  144. #ifndef fp_force_eval
  145. #define fp_force_eval fp_force_eval
  146. static inline void fp_force_eval(double x)
  147. {
  148. volatile double y;
  149. y = x;
  150. }
  151. #endif
  152. #ifndef fp_force_evall
  153. #define fp_force_evall fp_force_evall
  154. static inline void fp_force_evall(long double x)
  155. {
  156. volatile long double y;
  157. y = x;
  158. }
  159. #endif
  160. #define FORCE_EVAL(x) do { \
  161. if (sizeof(x) == sizeof(float)) { \
  162. fp_force_evalf(x); \
  163. } else if (sizeof(x) == sizeof(double)) { \
  164. fp_force_eval(x); \
  165. } else { \
  166. fp_force_evall(x); \
  167. } \
  168. } while(0)
  169. #define asuint(f) ((union{float _f; uint32_t _i;}){f})._i
  170. #define asfloat(i) ((union{uint32_t _i; float _f;}){i})._f
  171. #define asuint64(f) ((union{double _f; uint64_t _i;}){f})._i
  172. #define asdouble(i) ((union{uint64_t _i; double _f;}){i})._f
  173. #define EXTRACT_WORDS(hi,lo,d) \
  174. do { \
  175. uint64_t __u = asuint64(d); \
  176. (hi) = __u >> 32; \
  177. (lo) = (uint32_t)__u; \
  178. } while (0)
  179. #define GET_HIGH_WORD(hi,d) \
  180. do { \
  181. (hi) = asuint64(d) >> 32; \
  182. } while (0)
  183. #define GET_LOW_WORD(lo,d) \
  184. do { \
  185. (lo) = (uint32_t)asuint64(d); \
  186. } while (0)
  187. #define INSERT_WORDS(d,hi,lo) \
  188. do { \
  189. (d) = asdouble(((uint64_t)(hi)<<32) | (uint32_t)(lo)); \
  190. } while (0)
  191. #define SET_HIGH_WORD(d,hi) \
  192. INSERT_WORDS(d, hi, (uint32_t)asuint64(d))
  193. #define SET_LOW_WORD(d,lo) \
  194. INSERT_WORDS(d, asuint64(d)>>32, lo)
  195. #define GET_FLOAT_WORD(w,d) \
  196. do { \
  197. (w) = asuint(d); \
  198. } while (0)
  199. #define SET_FLOAT_WORD(d,w) \
  200. do { \
  201. (d) = asfloat(w); \
  202. } while (0)
  203. hidden int __rem_pio2_large(double*,double*,int,int,int);
  204. hidden int __rem_pio2(double,double*);
  205. hidden double __sin(double,double,int);
  206. hidden double __cos(double,double);
  207. hidden double __tan(double,double,int);
  208. hidden double __expo2(double);
  209. hidden int __rem_pio2f(float,double*);
  210. hidden float __sindf(double);
  211. hidden float __cosdf(double);
  212. hidden float __tandf(double,int);
  213. hidden float __expo2f(float);
  214. hidden int __rem_pio2l(long double, long double *);
  215. hidden long double __sinl(long double, long double, int);
  216. hidden long double __cosl(long double, long double);
  217. hidden long double __tanl(long double, long double, int);
  218. hidden long double __polevll(long double, const long double *, int);
  219. hidden long double __p1evll(long double, const long double *, int);
  220. extern int __signgam;
  221. hidden double __lgamma_r(double, int *);
  222. hidden float __lgammaf_r(float, int *);
  223. /* error handling functions */
  224. hidden float __math_xflowf(uint32_t, float);
  225. hidden float __math_uflowf(uint32_t);
  226. hidden float __math_oflowf(uint32_t);
  227. hidden float __math_divzerof(uint32_t);
  228. hidden float __math_invalidf(float);
  229. hidden double __math_xflow(uint32_t, double);
  230. hidden double __math_uflow(uint32_t);
  231. hidden double __math_oflow(uint32_t);
  232. hidden double __math_divzero(uint32_t);
  233. hidden double __math_invalid(double);
  234. #endif