6e2d19008d8fe6babc31234cf8f585fbfc836736
[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 <endian.h>
20
21 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
22 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
23 union ldshape {
24         long double f;
25         struct {
26                 uint64_t m;
27                 uint16_t se;
28         } i;
29 };
30 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
31 /* This is the m68k variant of 80-bit long double, and this definition only works
32  * on archs where the alignment requirement of uint64_t is <= 4. */
33 union ldshape {
34         long double f;
35         struct {
36                 uint16_t se;
37                 uint16_t pad;
38                 uint64_t m;
39         } i;
40 };
41 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
42 union ldshape {
43         long double f;
44         struct {
45                 uint64_t lo;
46                 uint32_t mid;
47                 uint16_t top;
48                 uint16_t se;
49         } i;
50         struct {
51                 uint64_t lo;
52                 uint64_t hi;
53         } i2;
54 };
55 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
56 union ldshape {
57         long double f;
58         struct {
59                 uint16_t se;
60                 uint16_t top;
61                 uint32_t mid;
62                 uint64_t lo;
63         } i;
64         struct {
65                 uint64_t hi;
66                 uint64_t lo;
67         } i2;
68 };
69 #else
70 #error Unsupported long double representation
71 #endif
72
73 #define FORCE_EVAL(x) do {                        \
74         if (sizeof(x) == sizeof(float)) {         \
75                 volatile float __x;               \
76                 __x = (x);                        \
77         } else if (sizeof(x) == sizeof(double)) { \
78                 volatile double __x;              \
79                 __x = (x);                        \
80         } else {                                  \
81                 volatile long double __x;         \
82                 __x = (x);                        \
83         }                                         \
84 } while(0)
85
86 /* Get two 32 bit ints from a double.  */
87 #define EXTRACT_WORDS(hi,lo,d)                    \
88 do {                                              \
89   union {double f; uint64_t i;} __u;              \
90   __u.f = (d);                                    \
91   (hi) = __u.i >> 32;                             \
92   (lo) = (uint32_t)__u.i;                         \
93 } while (0)
94
95 /* Get the more significant 32 bit int from a double.  */
96 #define GET_HIGH_WORD(hi,d)                       \
97 do {                                              \
98   union {double f; uint64_t i;} __u;              \
99   __u.f = (d);                                    \
100   (hi) = __u.i >> 32;                             \
101 } while (0)
102
103 /* Get the less significant 32 bit int from a double.  */
104 #define GET_LOW_WORD(lo,d)                        \
105 do {                                              \
106   union {double f; uint64_t i;} __u;              \
107   __u.f = (d);                                    \
108   (lo) = (uint32_t)__u.i;                         \
109 } while (0)
110
111 /* Set a double from two 32 bit ints.  */
112 #define INSERT_WORDS(d,hi,lo)                     \
113 do {                                              \
114   union {double f; uint64_t i;} __u;              \
115   __u.i = ((uint64_t)(hi)<<32) | (uint32_t)(lo);  \
116   (d) = __u.f;                                    \
117 } while (0)
118
119 /* Set the more significant 32 bits of a double from an int.  */
120 #define SET_HIGH_WORD(d,hi)                       \
121 do {                                              \
122   union {double f; uint64_t i;} __u;              \
123   __u.f = (d);                                    \
124   __u.i &= 0xffffffff;                            \
125   __u.i |= (uint64_t)(hi) << 32;                  \
126   (d) = __u.f;                                    \
127 } while (0)
128
129 /* Set the less significant 32 bits of a double from an int.  */
130 #define SET_LOW_WORD(d,lo)                        \
131 do {                                              \
132   union {double f; uint64_t i;} __u;              \
133   __u.f = (d);                                    \
134   __u.i &= 0xffffffff00000000ull;                 \
135   __u.i |= (uint32_t)(lo);                        \
136   (d) = __u.f;                                    \
137 } while (0)
138
139 /* Get a 32 bit int from a float.  */
140 #define GET_FLOAT_WORD(w,d)                       \
141 do {                                              \
142   union {float f; uint32_t i;} __u;               \
143   __u.f = (d);                                    \
144   (w) = __u.i;                                    \
145 } while (0)
146
147 /* Set a float from a 32 bit int.  */
148 #define SET_FLOAT_WORD(d,w)                       \
149 do {                                              \
150   union {float f; uint32_t i;} __u;               \
151   __u.i = (w);                                    \
152   (d) = __u.f;                                    \
153 } while (0)
154
155 /* fdlibm kernel functions */
156
157 hidden int    __rem_pio2_large(double*,double*,int,int,int);
158
159 hidden int    __rem_pio2(double,double*);
160 hidden double __sin(double,double,int);
161 hidden double __cos(double,double);
162 hidden double __tan(double,double,int);
163 hidden double __expo2(double);
164
165 hidden int    __rem_pio2f(float,double*);
166 hidden float  __sindf(double);
167 hidden float  __cosdf(double);
168 hidden float  __tandf(double,int);
169 hidden float  __expo2f(float);
170
171 hidden int __rem_pio2l(long double, long double *);
172 hidden long double __sinl(long double, long double, int);
173 hidden long double __cosl(long double, long double);
174 hidden long double __tanl(long double, long double, int);
175
176 /* polynomial evaluation */
177 hidden long double __polevll(long double, const long double *, int);
178 hidden long double __p1evll(long double, const long double *, int);
179
180 extern int __signgam;
181 hidden double __lgamma_r(double, int *);
182 hidden float __lgammaf_r(float, int *);
183
184 #endif