math: cosmetic cleanup (use explicit union instead of fshape and dshape)
[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 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
25 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
26 union ldshape {
27         long double f;
28         struct {
29                 uint64_t m;
30                 uint16_t se;
31         } i;
32 };
33 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
34 union ldshape {
35         long double f;
36         struct {
37                 uint64_t lo;
38                 uint32_t mid;
39                 uint16_t top;
40                 uint16_t se;
41         } i;
42         struct {
43                 uint64_t lo;
44                 uint64_t hi;
45         } i2;
46 };
47 #else
48 #error Unsupported long double representation
49 #endif
50
51 #define FORCE_EVAL(x) do {                        \
52         if (sizeof(x) == sizeof(float)) {         \
53                 volatile float __x;               \
54                 __x = (x);                        \
55         } else if (sizeof(x) == sizeof(double)) { \
56                 volatile double __x;              \
57                 __x = (x);                        \
58         } else {                                  \
59                 volatile long double __x;         \
60                 __x = (x);                        \
61         }                                         \
62 } while(0)
63
64 /* Get two 32 bit ints from a double.  */
65 #define EXTRACT_WORDS(hi,lo,d)                    \
66 do {                                              \
67   union {double f; uint64_t i;} __u;              \
68   __u.f = (d);                                    \
69   (hi) = __u.i >> 32;                             \
70   (lo) = (uint32_t)__u.i;                         \
71 } while (0)
72
73 /* Get the more significant 32 bit int from a double.  */
74 #define GET_HIGH_WORD(hi,d)                       \
75 do {                                              \
76   union {double f; uint64_t i;} __u;              \
77   __u.f = (d);                                    \
78   (hi) = __u.i >> 32;                             \
79 } while (0)
80
81 /* Get the less significant 32 bit int from a double.  */
82 #define GET_LOW_WORD(lo,d)                        \
83 do {                                              \
84   union {double f; uint64_t i;} __u;              \
85   __u.f = (d);                                    \
86   (lo) = (uint32_t)__u.i;                         \
87 } while (0)
88
89 /* Set a double from two 32 bit ints.  */
90 #define INSERT_WORDS(d,hi,lo)                     \
91 do {                                              \
92   union {double f; uint64_t i;} __u;              \
93   __u.i = ((uint64_t)(hi)<<32) | (uint32_t)(lo);  \
94   (d) = __u.f;                                    \
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 {double f; uint64_t i;} __u;              \
101   __u.f = (d);                                    \
102   __u.i &= 0xffffffff;                            \
103   __u.i |= (uint64_t)(hi) << 32;                  \
104   (d) = __u.f;                                    \
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 {double f; uint64_t i;} __u;              \
111   __u.f = (d);                                    \
112   __u.i &= 0xffffffff00000000ull;                 \
113   __u.i |= (uint32_t)(lo);                        \
114   (d) = __u.f;                                    \
115 } while (0)
116
117 /* Get a 32 bit int from a float.  */
118 #define GET_FLOAT_WORD(w,d)                       \
119 do {                                              \
120   union {float f; uint32_t i;} __u;               \
121   __u.f = (d);                                    \
122   (w) = __u.i;                                    \
123 } while (0)
124
125 /* Set a float from a 32 bit int.  */
126 #define SET_FLOAT_WORD(d,w)                       \
127 do {                                              \
128   union {float f; uint32_t i;} __u;               \
129   __u.i = (w);                                    \
130   (d) = __u.f;                                    \
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 #endif