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