first commit of the new libm!
[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
21 #include "longdbl.h"
22
23 union fshape {
24         float value;
25         uint32_t bits;
26 };
27
28 union dshape {
29         double value;
30         uint64_t bits;
31 };
32
33 /* Get two 32 bit ints from a double.  */
34 #define EXTRACT_WORDS(hi,lo,d)                                  \
35 do {                                                            \
36   union dshape __u;                                             \
37   __u.value = (d);                                              \
38   (hi) = __u.bits >> 32;                                        \
39   (lo) = (uint32_t)__u.bits;                                    \
40 } while (0)
41
42 /* Get a 64 bit int from a double.  */
43 #define EXTRACT_WORD64(i,d)                                     \
44 do {                                                            \
45   union dshape __u;                                             \
46   __u.value = (d);                                              \
47   (i) = __u.bits;                                               \
48 } while (0)
49
50 /* Get the more significant 32 bit int from a double.  */
51 #define GET_HIGH_WORD(i,d)                                      \
52 do {                                                            \
53   union dshape __u;                                             \
54   __u.value = (d);                                              \
55   (i) = __u.bits >> 32;                                         \
56 } while (0)
57
58 /* Get the less significant 32 bit int from a double.  */
59 #define GET_LOW_WORD(i,d)                                       \
60 do {                                                            \
61   union dshape __u;                                             \
62   __u.value = (d);                                              \
63   (i) = (uint32_t)__u.bits;                                     \
64 } while (0)
65
66 /* Set a double from two 32 bit ints.  */
67 #define INSERT_WORDS(d,hi,lo)                                   \
68 do {                                                            \
69   union dshape __u;                                             \
70   __u.bits = ((uint64_t)(hi) << 32) | (uint32_t)(lo);           \
71   (d) = __u.value;                                              \
72 } while (0)
73
74 /* Set a double from a 64 bit int.  */
75 #define INSERT_WORD64(d,i)                                      \
76 do {                                                            \
77   union dshape __u;                                             \
78   __u.bits = (i);                                               \
79   (d) = __u.value;                                              \
80 } while (0)
81
82 /* Set the more significant 32 bits of a double from an int.  */
83 #define SET_HIGH_WORD(d,hi)                                     \
84 do {                                                            \
85   union dshape __u;                                             \
86   __u.value = (d);                                              \
87   __u.bits &= 0xffffffff;                                       \
88   __u.bits |= (uint64_t)(hi) << 32;                             \
89   (d) = __u.value;                                              \
90 } while (0)
91
92 /* Set the less significant 32 bits of a double from an int.  */
93 #define SET_LOW_WORD(d,lo)                                      \
94 do {                                                            \
95   union dshape __u;                                             \
96   __u.value = (d);                                              \
97   __u.bits &= 0xffffffff00000000ull;                            \
98   __u.bits |= (uint32_t)(lo);                                   \
99   (d) = __u.value;                                              \
100 } while (0)
101
102 /* Get a 32 bit int from a float.  */
103 #define GET_FLOAT_WORD(i,d)                                     \
104 do {                                                            \
105   union fshape __u;                                             \
106   __u.value = (d);                                              \
107   (i) = __u.bits;                                               \
108 } while (0)
109
110 /* Set a float from a 32 bit int.  */
111 #define SET_FLOAT_WORD(d,i)                                     \
112 do {                                                            \
113   union fshape __u;                                             \
114   __u.bits = (i);                                               \
115   (d) = __u.value;                                              \
116 } while (0)
117
118 /* fdlibm kernel functions */
119
120 int    __rem_pio2_large(double*,double*,int,int,int);
121
122 int    __rem_pio2(double,double*);
123 double __sin(double,double,int);
124 double __cos(double,double);
125 double __tan(double,double,int);
126 double __expo2(double);
127 double complex __ldexp_cexp(double complex,int);
128
129 int    __rem_pio2f(float,double*);
130 float  __sindf(double);
131 float  __cosdf(double);
132 float  __tandf(double,int);
133 float  __expo2f(float);
134 float complex __ldexp_cexpf(float complex,int);
135
136 long double __sinl(long double, long double, int);
137 long double __cosl(long double, long double);
138 long double __tanl(long double, long double, int);
139
140 /* polynomial evaluation */
141 long double __polevll(long double, long double *, int);
142 long double __p1evll(long double, long double *, int);
143
144 // FIXME: not needed when -fexcess-precision=standard is supported (>=gcc4.5)
145 /*
146  * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
147  */
148 #if 1
149 #define STRICT_ASSIGN(type, lval, rval) do {    \
150         volatile type __v = (rval);             \
151         (lval) = __v;                           \
152 } while (0)
153 #else
154 #define STRICT_ASSIGN(type, lval, rval) ((lval) = (type)(rval))
155 #endif
156
157
158 /* complex */
159
160 union dcomplex {
161         double complex z;
162         double a[2];
163 };
164 union fcomplex {
165         float complex z;
166         float a[2];
167 };
168 union lcomplex {
169         long double complex z;
170         long double a[2];
171 };
172
173 // FIXME: move to complex.h ?
174 #define creal(z) ((double)(z))
175 #define crealf(z) ((float)(z))
176 #define creall(z) ((long double)(z))
177 #define cimag(z) ((union dcomplex){(z)}.a[1])
178 #define cimagf(z) ((union fcomplex){(z)}.a[1])
179 #define cimagl(z) ((union lcomplex){(z)}.a[1])
180
181 /* x + y*I is not supported properly by gcc */
182 #define cpack(x,y) ((union dcomplex){.a={(x),(y)}}.z)
183 #define cpackf(x,y) ((union fcomplex){.a={(x),(y)}}.z)
184 #define cpackl(x,y) ((union lcomplex){.a={(x),(y)}}.z)
185
186 #endif