move and deduplicate declarations of __vdsosym to make it checkable
[musl] / src / internal / libm.h
1 /* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12
13 #ifndef _LIBM_H
14 #define _LIBM_H
15
16 #include <stdint.h>
17 #include <float.h>
18 #include <math.h>
19 #include <complex.h>
20 #include <endian.h>
21
22 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
23 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
24 union ldshape {
25         long double f;
26         struct {
27                 uint64_t m;
28                 uint16_t se;
29         } i;
30 };
31 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
32 /* This is the m68k variant of 80-bit long double, and this definition only works
33  * on archs where the alignment requirement of uint64_t is <= 4. */
34 union ldshape {
35         long double f;
36         struct {
37                 uint16_t se;
38                 uint16_t pad;
39                 uint64_t m;
40         } i;
41 };
42 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
43 union ldshape {
44         long double f;
45         struct {
46                 uint64_t lo;
47                 uint32_t mid;
48                 uint16_t top;
49                 uint16_t se;
50         } i;
51         struct {
52                 uint64_t lo;
53                 uint64_t hi;
54         } i2;
55 };
56 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
57 union ldshape {
58         long double f;
59         struct {
60                 uint16_t se;
61                 uint16_t top;
62                 uint32_t mid;
63                 uint64_t lo;
64         } i;
65         struct {
66                 uint64_t hi;
67                 uint64_t lo;
68         } i2;
69 };
70 #else
71 #error Unsupported long double representation
72 #endif
73
74 #define FORCE_EVAL(x) do {                        \
75         if (sizeof(x) == sizeof(float)) {         \
76                 volatile float __x;               \
77                 __x = (x);                        \
78         } else if (sizeof(x) == sizeof(double)) { \
79                 volatile double __x;              \
80                 __x = (x);                        \
81         } else {                                  \
82                 volatile long double __x;         \
83                 __x = (x);                        \
84         }                                         \
85 } while(0)
86
87 /* Get two 32 bit ints from a double.  */
88 #define EXTRACT_WORDS(hi,lo,d)                    \
89 do {                                              \
90   union {double f; uint64_t i;} __u;              \
91   __u.f = (d);                                    \
92   (hi) = __u.i >> 32;                             \
93   (lo) = (uint32_t)__u.i;                         \
94 } while (0)
95
96 /* Get the more significant 32 bit int from a double.  */
97 #define GET_HIGH_WORD(hi,d)                       \
98 do {                                              \
99   union {double f; uint64_t i;} __u;              \
100   __u.f = (d);                                    \
101   (hi) = __u.i >> 32;                             \
102 } while (0)
103
104 /* Get the less significant 32 bit int from a double.  */
105 #define GET_LOW_WORD(lo,d)                        \
106 do {                                              \
107   union {double f; uint64_t i;} __u;              \
108   __u.f = (d);                                    \
109   (lo) = (uint32_t)__u.i;                         \
110 } while (0)
111
112 /* Set a double from two 32 bit ints.  */
113 #define INSERT_WORDS(d,hi,lo)                     \
114 do {                                              \
115   union {double f; uint64_t i;} __u;              \
116   __u.i = ((uint64_t)(hi)<<32) | (uint32_t)(lo);  \
117   (d) = __u.f;                                    \
118 } while (0)
119
120 /* Set the more significant 32 bits of a double from an int.  */
121 #define SET_HIGH_WORD(d,hi)                       \
122 do {                                              \
123   union {double f; uint64_t i;} __u;              \
124   __u.f = (d);                                    \
125   __u.i &= 0xffffffff;                            \
126   __u.i |= (uint64_t)(hi) << 32;                  \
127   (d) = __u.f;                                    \
128 } while (0)
129
130 /* Set the less significant 32 bits of a double from an int.  */
131 #define SET_LOW_WORD(d,lo)                        \
132 do {                                              \
133   union {double f; uint64_t i;} __u;              \
134   __u.f = (d);                                    \
135   __u.i &= 0xffffffff00000000ull;                 \
136   __u.i |= (uint32_t)(lo);                        \
137   (d) = __u.f;                                    \
138 } while (0)
139
140 /* Get a 32 bit int from a float.  */
141 #define GET_FLOAT_WORD(w,d)                       \
142 do {                                              \
143   union {float f; uint32_t i;} __u;               \
144   __u.f = (d);                                    \
145   (w) = __u.i;                                    \
146 } while (0)
147
148 /* Set a float from a 32 bit int.  */
149 #define SET_FLOAT_WORD(d,w)                       \
150 do {                                              \
151   union {float f; uint32_t i;} __u;               \
152   __u.i = (w);                                    \
153   (d) = __u.f;                                    \
154 } while (0)
155
156 #undef __CMPLX
157 #undef CMPLX
158 #undef CMPLXF
159 #undef CMPLXL
160
161 #define __CMPLX(x, y, t) \
162         ((union { _Complex t __z; t __xy[2]; }){.__xy = {(x),(y)}}.__z)
163
164 #define CMPLX(x, y) __CMPLX(x, y, double)
165 #define CMPLXF(x, y) __CMPLX(x, y, float)
166 #define CMPLXL(x, y) __CMPLX(x, y, long double)
167
168 /* fdlibm kernel functions */
169
170 int    __rem_pio2_large(double*,double*,int,int,int);
171
172 int    __rem_pio2(double,double*);
173 double __sin(double,double,int);
174 double __cos(double,double);
175 double __tan(double,double,int);
176 double __expo2(double);
177 double complex __ldexp_cexp(double complex,int);
178
179 int    __rem_pio2f(float,double*);
180 float  __sindf(double);
181 float  __cosdf(double);
182 float  __tandf(double,int);
183 float  __expo2f(float);
184 float complex __ldexp_cexpf(float complex,int);
185
186 int __rem_pio2l(long double, long double *);
187 long double __sinl(long double, long double, int);
188 long double __cosl(long double, long double);
189 long double __tanl(long double, long double, int);
190
191 /* polynomial evaluation */
192 long double __polevll(long double, const long double *, int);
193 long double __p1evll(long double, const long double *, int);
194
195 #endif