math: remove old longdbl.h
[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 #include "libc.h"
23
24 union fshape {
25         float value;
26         uint32_t bits;
27 };
28
29 union dshape {
30         double value;
31         uint64_t bits;
32 };
33
34 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
35 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
36 union ldshape {
37         long double f;
38         struct {
39                 uint64_t m;
40                 uint16_t se;
41         } i;
42 };
43 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
44 union ldshape {
45         long double f;
46         struct {
47                 uint64_t lo;
48                 uint32_t mid;
49                 uint16_t top;
50                 uint16_t se;
51         } i;
52         struct {
53                 uint64_t lo;
54                 uint64_t hi;
55         } i2;
56 };
57 #else
58 #error Unsupported long double representation
59 #endif
60
61 #define FORCE_EVAL(x) do {                          \
62         if (sizeof(x) == sizeof(float)) {           \
63                 volatile float __x;                 \
64                 __x = (x);                          \
65         } else if (sizeof(x) == sizeof(double)) {   \
66                 volatile double __x;                \
67                 __x = (x);                          \
68         } else {                                    \
69                 volatile long double __x;           \
70                 __x = (x);                          \
71         }                                           \
72 } while(0)
73
74 /* Get two 32 bit ints from a double.  */
75 #define EXTRACT_WORDS(hi,lo,d)                                  \
76 do {                                                            \
77   union dshape __u;                                             \
78   __u.value = (d);                                              \
79   (hi) = __u.bits >> 32;                                        \
80   (lo) = (uint32_t)__u.bits;                                    \
81 } while (0)
82
83 /* Get a 64 bit int from a double.  */
84 #define EXTRACT_WORD64(i,d)                                     \
85 do {                                                            \
86   union dshape __u;                                             \
87   __u.value = (d);                                              \
88   (i) = __u.bits;                                               \
89 } while (0)
90
91 /* Get the more significant 32 bit int from a double.  */
92 #define GET_HIGH_WORD(i,d)                                      \
93 do {                                                            \
94   union dshape __u;                                             \
95   __u.value = (d);                                              \
96   (i) = __u.bits >> 32;                                         \
97 } while (0)
98
99 /* Get the less significant 32 bit int from a double.  */
100 #define GET_LOW_WORD(i,d)                                       \
101 do {                                                            \
102   union dshape __u;                                             \
103   __u.value = (d);                                              \
104   (i) = (uint32_t)__u.bits;                                     \
105 } while (0)
106
107 /* Set a double from two 32 bit ints.  */
108 #define INSERT_WORDS(d,hi,lo)                                   \
109 do {                                                            \
110   union dshape __u;                                             \
111   __u.bits = ((uint64_t)(hi) << 32) | (uint32_t)(lo);           \
112   (d) = __u.value;                                              \
113 } while (0)
114
115 /* Set a double from a 64 bit int.  */
116 #define INSERT_WORD64(d,i)                                      \
117 do {                                                            \
118   union dshape __u;                                             \
119   __u.bits = (i);                                               \
120   (d) = __u.value;                                              \
121 } while (0)
122
123 /* Set the more significant 32 bits of a double from an int.  */
124 #define SET_HIGH_WORD(d,hi)                                     \
125 do {                                                            \
126   union dshape __u;                                             \
127   __u.value = (d);                                              \
128   __u.bits &= 0xffffffff;                                       \
129   __u.bits |= (uint64_t)(hi) << 32;                             \
130   (d) = __u.value;                                              \
131 } while (0)
132
133 /* Set the less significant 32 bits of a double from an int.  */
134 #define SET_LOW_WORD(d,lo)                                      \
135 do {                                                            \
136   union dshape __u;                                             \
137   __u.value = (d);                                              \
138   __u.bits &= 0xffffffff00000000ull;                            \
139   __u.bits |= (uint32_t)(lo);                                   \
140   (d) = __u.value;                                              \
141 } while (0)
142
143 /* Get a 32 bit int from a float.  */
144 #define GET_FLOAT_WORD(i,d)                                     \
145 do {                                                            \
146   union fshape __u;                                             \
147   __u.value = (d);                                              \
148   (i) = __u.bits;                                               \
149 } while (0)
150
151 /* Set a float from a 32 bit int.  */
152 #define SET_FLOAT_WORD(d,i)                                     \
153 do {                                                            \
154   union fshape __u;                                             \
155   __u.bits = (i);                                               \
156   (d) = __u.value;                                              \
157 } while (0)
158
159 /* fdlibm kernel functions */
160
161 int    __rem_pio2_large(double*,double*,int,int,int);
162
163 int    __rem_pio2(double,double*);
164 double __sin(double,double,int);
165 double __cos(double,double);
166 double __tan(double,double,int);
167 double __expo2(double);
168 double complex __ldexp_cexp(double complex,int);
169
170 int    __rem_pio2f(float,double*);
171 float  __sindf(double);
172 float  __cosdf(double);
173 float  __tandf(double,int);
174 float  __expo2f(float);
175 float complex __ldexp_cexpf(float complex,int);
176
177 int __rem_pio2l(long double, long double *);
178 long double __sinl(long double, long double, int);
179 long double __cosl(long double, long double);
180 long double __tanl(long double, long double, int);
181
182 /* polynomial evaluation */
183 long double __polevll(long double, const long double *, int);
184 long double __p1evll(long double, const long double *, int);
185
186 #if 0
187 /* Attempt to get strict C99 semantics for assignment with non-C99 compilers. */
188 #define STRICT_ASSIGN(type, lval, rval) do {    \
189         volatile type __v = (rval);             \
190         (lval) = __v;                           \
191 } while (0)
192 #else
193 /* Should work with -fexcess-precision=standard (>=gcc-4.5) or -ffloat-store */
194 #define STRICT_ASSIGN(type, lval, rval) ((lval) = (type)(rval))
195 #endif
196
197 #endif