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