math: add macros for static branch prediction hints
[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 /* Helps static branch prediction so hot path can be better optimized.  */
63 #ifdef __GNUC__
64 #define predict_true(x) __builtin_expect(!!(x), 1)
65 #define predict_false(x) __builtin_expect(x, 0)
66 #else
67 #define predict_true(x) (x)
68 #define predict_false(x) (x)
69 #endif
70
71 /* Evaluate an expression as the specified type. With standard excess
72    precision handling a type cast or assignment is enough (with
73    -ffloat-store an assignment is required, in old compilers argument
74    passing and return statement may not drop excess precision).  */
75
76 static inline float eval_as_float(float x)
77 {
78         float y = x;
79         return y;
80 }
81
82 static inline double eval_as_double(double x)
83 {
84         double y = x;
85         return y;
86 }
87
88 /* fp_barrier returns its input, but limits code transformations
89    as if it had a side-effect (e.g. observable io) and returned
90    an arbitrary value.  */
91
92 #ifndef fp_barrierf
93 #define fp_barrierf fp_barrierf
94 static inline float fp_barrierf(float x)
95 {
96         volatile float y = x;
97         return y;
98 }
99 #endif
100
101 #ifndef fp_barrier
102 #define fp_barrier fp_barrier
103 static inline double fp_barrier(double x)
104 {
105         volatile double y = x;
106         return y;
107 }
108 #endif
109
110 #ifndef fp_barrierl
111 #define fp_barrierl fp_barrierl
112 static inline long double fp_barrierl(long double x)
113 {
114         volatile long double y = x;
115         return y;
116 }
117 #endif
118
119 /* fp_force_eval ensures that the input value is computed when that's
120    otherwise unused.  To prevent the constant folding of the input
121    expression, an additional fp_barrier may be needed or a compilation
122    mode that does so (e.g. -frounding-math in gcc). Then it can be
123    used to evaluate an expression for its fenv side-effects only.   */
124
125 #ifndef fp_force_evalf
126 #define fp_force_evalf fp_force_evalf
127 static inline void fp_force_evalf(float x)
128 {
129         volatile float y = x;
130 }
131 #endif
132
133 #ifndef fp_force_eval
134 #define fp_force_eval fp_force_eval
135 static inline void fp_force_eval(double x)
136 {
137         volatile double y = x;
138 }
139 #endif
140
141 #ifndef fp_force_evall
142 #define fp_force_evall fp_force_evall
143 static inline void fp_force_evall(long double x)
144 {
145         volatile long double y = x;
146 }
147 #endif
148
149 #define FORCE_EVAL(x) do {                        \
150         if (sizeof(x) == sizeof(float)) {         \
151                 fp_force_evalf(x);                \
152         } else if (sizeof(x) == sizeof(double)) { \
153                 fp_force_eval(x);                 \
154         } else {                                  \
155                 fp_force_evall(x);                \
156         }                                         \
157 } while(0)
158
159 #define asuint(f) ((union{float _f; uint32_t _i;}){f})._i
160 #define asfloat(i) ((union{uint32_t _i; float _f;}){i})._f
161 #define asuint64(f) ((union{double _f; uint64_t _i;}){f})._i
162 #define asdouble(i) ((union{uint64_t _i; double _f;}){i})._f
163
164 #define EXTRACT_WORDS(hi,lo,d)                    \
165 do {                                              \
166   uint64_t __u = asuint64(d);                     \
167   (hi) = __u >> 32;                               \
168   (lo) = (uint32_t)__u;                           \
169 } while (0)
170
171 #define GET_HIGH_WORD(hi,d)                       \
172 do {                                              \
173   (hi) = asuint64(d) >> 32;                       \
174 } while (0)
175
176 #define GET_LOW_WORD(lo,d)                        \
177 do {                                              \
178   (lo) = (uint32_t)asuint64(d);                   \
179 } while (0)
180
181 #define INSERT_WORDS(d,hi,lo)                     \
182 do {                                              \
183   (d) = asdouble(((uint64_t)(hi)<<32) | (uint32_t)(lo)); \
184 } while (0)
185
186 #define SET_HIGH_WORD(d,hi)                       \
187   INSERT_WORDS(d, hi, (uint32_t)asuint64(d))
188
189 #define SET_LOW_WORD(d,lo)                        \
190   INSERT_WORDS(d, asuint64(d)>>32, lo)
191
192 #define GET_FLOAT_WORD(w,d)                       \
193 do {                                              \
194   (w) = asuint(d);                                \
195 } while (0)
196
197 #define SET_FLOAT_WORD(d,w)                       \
198 do {                                              \
199   (d) = asfloat(w);                               \
200 } while (0)
201
202 hidden int    __rem_pio2_large(double*,double*,int,int,int);
203
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
210 hidden int    __rem_pio2f(float,double*);
211 hidden float  __sindf(double);
212 hidden float  __cosdf(double);
213 hidden float  __tandf(double,int);
214 hidden float  __expo2f(float);
215
216 hidden int __rem_pio2l(long double, long double *);
217 hidden long double __sinl(long double, long double, int);
218 hidden long double __cosl(long double, long double);
219 hidden long double __tanl(long double, long double, int);
220
221 hidden long double __polevll(long double, const long double *, int);
222 hidden long double __p1evll(long double, const long double *, int);
223
224 extern int __signgam;
225 hidden double __lgamma_r(double, int *);
226 hidden float __lgammaf_r(float, int *);
227
228 /* error handling functions */
229 hidden float __math_xflowf(uint32_t, float);
230 hidden float __math_uflowf(uint32_t);
231 hidden float __math_oflowf(uint32_t);
232 hidden float __math_divzerof(uint32_t);
233 hidden float __math_invalidf(float);
234 hidden double __math_xflow(uint32_t, double);
235 hidden double __math_uflow(uint32_t);
236 hidden double __math_oflow(uint32_t);
237 hidden double __math_divzero(uint32_t);
238 hidden double __math_invalid(double);
239
240 #endif