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