88a7eb479e53e9253adbe08f79481d64f66946b6
[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 == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
32 union ldshape {
33         long double f;
34         struct {
35                 uint64_t lo;
36                 uint32_t mid;
37                 uint16_t top;
38                 uint16_t se;
39         } i;
40         struct {
41                 uint64_t lo;
42                 uint64_t hi;
43         } i2;
44 };
45 #else
46 #error Unsupported long double representation
47 #endif
48
49 #define FORCE_EVAL(x) do {                        \
50         if (sizeof(x) == sizeof(float)) {         \
51                 volatile float __x;               \
52                 __x = (x);                        \
53         } else if (sizeof(x) == sizeof(double)) { \
54                 volatile double __x;              \
55                 __x = (x);                        \
56         } else {                                  \
57                 volatile long double __x;         \
58                 __x = (x);                        \
59         }                                         \
60 } while(0)
61
62 /* Get two 32 bit ints from a double.  */
63 #define EXTRACT_WORDS(hi,lo,d)                    \
64 do {                                              \
65   union {double f; uint64_t i;} __u;              \
66   __u.f = (d);                                    \
67   (hi) = __u.i >> 32;                             \
68   (lo) = (uint32_t)__u.i;                         \
69 } while (0)
70
71 /* Get the more significant 32 bit int from a double.  */
72 #define GET_HIGH_WORD(hi,d)                       \
73 do {                                              \
74   union {double f; uint64_t i;} __u;              \
75   __u.f = (d);                                    \
76   (hi) = __u.i >> 32;                             \
77 } while (0)
78
79 /* Get the less significant 32 bit int from a double.  */
80 #define GET_LOW_WORD(lo,d)                        \
81 do {                                              \
82   union {double f; uint64_t i;} __u;              \
83   __u.f = (d);                                    \
84   (lo) = (uint32_t)__u.i;                         \
85 } while (0)
86
87 /* Set a double from two 32 bit ints.  */
88 #define INSERT_WORDS(d,hi,lo)                     \
89 do {                                              \
90   union {double f; uint64_t i;} __u;              \
91   __u.i = ((uint64_t)(hi)<<32) | (uint32_t)(lo);  \
92   (d) = __u.f;                                    \
93 } while (0)
94
95 /* Set the more significant 32 bits of a double from an int.  */
96 #define SET_HIGH_WORD(d,hi)                       \
97 do {                                              \
98   union {double f; uint64_t i;} __u;              \
99   __u.f = (d);                                    \
100   __u.i &= 0xffffffff;                            \
101   __u.i |= (uint64_t)(hi) << 32;                  \
102   (d) = __u.f;                                    \
103 } while (0)
104
105 /* Set the less significant 32 bits of a double from an int.  */
106 #define SET_LOW_WORD(d,lo)                        \
107 do {                                              \
108   union {double f; uint64_t i;} __u;              \
109   __u.f = (d);                                    \
110   __u.i &= 0xffffffff00000000ull;                 \
111   __u.i |= (uint32_t)(lo);                        \
112   (d) = __u.f;                                    \
113 } while (0)
114
115 /* Get a 32 bit int from a float.  */
116 #define GET_FLOAT_WORD(w,d)                       \
117 do {                                              \
118   union {float f; uint32_t i;} __u;               \
119   __u.f = (d);                                    \
120   (w) = __u.i;                                    \
121 } while (0)
122
123 /* Set a float from a 32 bit int.  */
124 #define SET_FLOAT_WORD(d,w)                       \
125 do {                                              \
126   union {float f; uint32_t i;} __u;               \
127   __u.i = (w);                                    \
128   (d) = __u.f;                                    \
129 } while (0)
130
131 #undef __CMPLX
132 #undef CMPLX
133 #undef CMPLXF
134 #undef CMPLXL
135
136 #define __CMPLX(x, y, t) \
137         ((union { _Complex t __z; t __xy[2]; }){.__xy = {(x),(y)}}.__z)
138
139 #define CMPLX(x, y) __CMPLX(x, y, double)
140 #define CMPLXF(x, y) __CMPLX(x, y, float)
141 #define CMPLXL(x, y) __CMPLX(x, y, long double)
142
143 /* fdlibm kernel functions */
144
145 int    __rem_pio2_large(double*,double*,int,int,int);
146
147 int    __rem_pio2(double,double*);
148 double __sin(double,double,int);
149 double __cos(double,double);
150 double __tan(double,double,int);
151 double __expo2(double);
152 double complex __ldexp_cexp(double complex,int);
153
154 int    __rem_pio2f(float,double*);
155 float  __sindf(double);
156 float  __cosdf(double);
157 float  __tandf(double,int);
158 float  __expo2f(float);
159 float complex __ldexp_cexpf(float complex,int);
160
161 int __rem_pio2l(long double, long double *);
162 long double __sinl(long double, long double, int);
163 long double __cosl(long double, long double);
164 long double __tanl(long double, long double, int);
165
166 /* polynomial evaluation */
167 long double __polevll(long double, const long double *, int);
168 long double __p1evll(long double, const long double *, int);
169
170 #endif