math: new powf
[musl] / src / internal / libm.h
1 #ifndef _LIBM_H
2 #define _LIBM_H
3
4 #include <stdint.h>
5 #include <float.h>
6 #include <math.h>
7 #include <endian.h>
8 #include "fp_arch.h"
9
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 };
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 };
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 };
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 };
58 #else
59 #error Unsupported long double representation
60 #endif
61
62 /* Support non-nearest rounding mode.  */
63 #define WANT_ROUNDING 1
64 /* Support signaling NaNs.  */
65 #define WANT_SNAN 0
66
67 #if WANT_SNAN
68 #error SNaN is unsupported
69 #else
70 #define issignalingf_inline(x) 0
71 #endif
72
73 #ifndef TOINT_INTRINSICS
74 #define TOINT_INTRINSICS 0
75 #endif
76
77 #if TOINT_INTRINSICS
78 /* Round x to nearest int in all rounding modes, ties have to be rounded
79    consistently with converttoint so the results match.  If the result
80    would be outside of [-2^31, 2^31-1] then the semantics is unspecified.  */
81 static double_t roundtoint(double_t);
82
83 /* Convert x to nearest int in all rounding modes, ties have to be rounded
84    consistently with roundtoint.  If the result is not representible in an
85    int32_t then the semantics is unspecified.  */
86 static int32_t converttoint(double_t);
87 #endif
88
89 /* Helps static branch prediction so hot path can be better optimized.  */
90 #ifdef __GNUC__
91 #define predict_true(x) __builtin_expect(!!(x), 1)
92 #define predict_false(x) __builtin_expect(x, 0)
93 #else
94 #define predict_true(x) (x)
95 #define predict_false(x) (x)
96 #endif
97
98 /* Evaluate an expression as the specified type. With standard excess
99    precision handling a type cast or assignment is enough (with
100    -ffloat-store an assignment is required, in old compilers argument
101    passing and return statement may not drop excess precision).  */
102
103 static inline float eval_as_float(float x)
104 {
105         float y = x;
106         return y;
107 }
108
109 static inline double eval_as_double(double x)
110 {
111         double y = x;
112         return y;
113 }
114
115 /* fp_barrier returns its input, but limits code transformations
116    as if it had a side-effect (e.g. observable io) and returned
117    an arbitrary value.  */
118
119 #ifndef fp_barrierf
120 #define fp_barrierf fp_barrierf
121 static inline float fp_barrierf(float x)
122 {
123         volatile float y = x;
124         return y;
125 }
126 #endif
127
128 #ifndef fp_barrier
129 #define fp_barrier fp_barrier
130 static inline double fp_barrier(double x)
131 {
132         volatile double y = x;
133         return y;
134 }
135 #endif
136
137 #ifndef fp_barrierl
138 #define fp_barrierl fp_barrierl
139 static inline long double fp_barrierl(long double x)
140 {
141         volatile long double y = x;
142         return y;
143 }
144 #endif
145
146 /* fp_force_eval ensures that the input value is computed when that's
147    otherwise unused.  To prevent the constant folding of the input
148    expression, an additional fp_barrier may be needed or a compilation
149    mode that does so (e.g. -frounding-math in gcc). Then it can be
150    used to evaluate an expression for its fenv side-effects only.   */
151
152 #ifndef fp_force_evalf
153 #define fp_force_evalf fp_force_evalf
154 static inline void fp_force_evalf(float x)
155 {
156         volatile float y = x;
157 }
158 #endif
159
160 #ifndef fp_force_eval
161 #define fp_force_eval fp_force_eval
162 static inline void fp_force_eval(double x)
163 {
164         volatile double y = x;
165 }
166 #endif
167
168 #ifndef fp_force_evall
169 #define fp_force_evall fp_force_evall
170 static inline void fp_force_evall(long double x)
171 {
172         volatile long double y = x;
173 }
174 #endif
175
176 #define FORCE_EVAL(x) do {                        \
177         if (sizeof(x) == sizeof(float)) {         \
178                 fp_force_evalf(x);                \
179         } else if (sizeof(x) == sizeof(double)) { \
180                 fp_force_eval(x);                 \
181         } else {                                  \
182                 fp_force_evall(x);                \
183         }                                         \
184 } while(0)
185
186 #define asuint(f) ((union{float _f; uint32_t _i;}){f})._i
187 #define asfloat(i) ((union{uint32_t _i; float _f;}){i})._f
188 #define asuint64(f) ((union{double _f; uint64_t _i;}){f})._i
189 #define asdouble(i) ((union{uint64_t _i; double _f;}){i})._f
190
191 #define EXTRACT_WORDS(hi,lo,d)                    \
192 do {                                              \
193   uint64_t __u = asuint64(d);                     \
194   (hi) = __u >> 32;                               \
195   (lo) = (uint32_t)__u;                           \
196 } while (0)
197
198 #define GET_HIGH_WORD(hi,d)                       \
199 do {                                              \
200   (hi) = asuint64(d) >> 32;                       \
201 } while (0)
202
203 #define GET_LOW_WORD(lo,d)                        \
204 do {                                              \
205   (lo) = (uint32_t)asuint64(d);                   \
206 } while (0)
207
208 #define INSERT_WORDS(d,hi,lo)                     \
209 do {                                              \
210   (d) = asdouble(((uint64_t)(hi)<<32) | (uint32_t)(lo)); \
211 } while (0)
212
213 #define SET_HIGH_WORD(d,hi)                       \
214   INSERT_WORDS(d, hi, (uint32_t)asuint64(d))
215
216 #define SET_LOW_WORD(d,lo)                        \
217   INSERT_WORDS(d, asuint64(d)>>32, lo)
218
219 #define GET_FLOAT_WORD(w,d)                       \
220 do {                                              \
221   (w) = asuint(d);                                \
222 } while (0)
223
224 #define SET_FLOAT_WORD(d,w)                       \
225 do {                                              \
226   (d) = asfloat(w);                               \
227 } while (0)
228
229 hidden int    __rem_pio2_large(double*,double*,int,int,int);
230
231 hidden int    __rem_pio2(double,double*);
232 hidden double __sin(double,double,int);
233 hidden double __cos(double,double);
234 hidden double __tan(double,double,int);
235 hidden double __expo2(double);
236
237 hidden int    __rem_pio2f(float,double*);
238 hidden float  __sindf(double);
239 hidden float  __cosdf(double);
240 hidden float  __tandf(double,int);
241 hidden float  __expo2f(float);
242
243 hidden int __rem_pio2l(long double, long double *);
244 hidden long double __sinl(long double, long double, int);
245 hidden long double __cosl(long double, long double);
246 hidden long double __tanl(long double, long double, int);
247
248 hidden long double __polevll(long double, const long double *, int);
249 hidden long double __p1evll(long double, const long double *, int);
250
251 extern int __signgam;
252 hidden double __lgamma_r(double, int *);
253 hidden float __lgammaf_r(float, int *);
254
255 /* error handling functions */
256 hidden float __math_xflowf(uint32_t, float);
257 hidden float __math_uflowf(uint32_t);
258 hidden float __math_oflowf(uint32_t);
259 hidden float __math_divzerof(uint32_t);
260 hidden float __math_invalidf(float);
261 hidden double __math_xflow(uint32_t, double);
262 hidden double __math_uflow(uint32_t);
263 hidden double __math_oflow(uint32_t);
264 hidden double __math_divzero(uint32_t);
265 hidden double __math_invalid(double);
266
267 #endif