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