math: add asuint, asuint64, asfloat and asdouble
[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 #define asuint(f) ((union{float _f; uint32_t _i;}){f})._i
87 #define asfloat(i) ((union{uint32_t _i; float _f;}){i})._f
88 #define asuint64(f) ((union{double _f; uint64_t _i;}){f})._i
89 #define asdouble(i) ((union{uint64_t _i; double _f;}){i})._f
90
91 /* Get two 32 bit ints from a double.  */
92 #define EXTRACT_WORDS(hi,lo,d)                    \
93 do {                                              \
94   uint64_t __u = asuint64(d);                     \
95   (hi) = __u >> 32;                               \
96   (lo) = (uint32_t)__u;                           \
97 } while (0)
98
99 /* Get the more significant 32 bit int from a double.  */
100 #define GET_HIGH_WORD(hi,d)                       \
101 do {                                              \
102   (hi) = asuint64(d) >> 32;                       \
103 } while (0)
104
105 /* Get the less significant 32 bit int from a double.  */
106 #define GET_LOW_WORD(lo,d)                        \
107 do {                                              \
108   (lo) = (uint32_t)asuint64(d);                   \
109 } while (0)
110
111 /* Set a double from two 32 bit ints.  */
112 #define INSERT_WORDS(d,hi,lo)                     \
113 do {                                              \
114   (d) = asdouble(((uint64_t)(hi)<<32) | (uint32_t)(lo)); \
115 } while (0)
116
117 /* Set the more significant 32 bits of a double from an int.  */
118 #define SET_HIGH_WORD(d,hi)                       \
119   INSERT_WORDS(d, hi, (uint32_t)asuint64(d))
120
121 /* Set the less significant 32 bits of a double from an int.  */
122 #define SET_LOW_WORD(d,lo)                        \
123   INSERT_WORDS(d, asuint64(d)>>32, lo)
124
125 /* Get a 32 bit int from a float.  */
126 #define GET_FLOAT_WORD(w,d)                       \
127 do {                                              \
128   (w) = asuint(d);                                \
129 } while (0)
130
131 /* Set a float from a 32 bit int.  */
132 #define SET_FLOAT_WORD(d,w)                       \
133 do {                                              \
134   (d) = asfloat(w);                               \
135 } while (0)
136
137 /* fdlibm kernel functions */
138
139 hidden int    __rem_pio2_large(double*,double*,int,int,int);
140
141 hidden int    __rem_pio2(double,double*);
142 hidden double __sin(double,double,int);
143 hidden double __cos(double,double);
144 hidden double __tan(double,double,int);
145 hidden double __expo2(double);
146
147 hidden int    __rem_pio2f(float,double*);
148 hidden float  __sindf(double);
149 hidden float  __cosdf(double);
150 hidden float  __tandf(double,int);
151 hidden float  __expo2f(float);
152
153 hidden int __rem_pio2l(long double, long double *);
154 hidden long double __sinl(long double, long double, int);
155 hidden long double __cosl(long double, long double);
156 hidden long double __tanl(long double, long double, int);
157
158 /* polynomial evaluation */
159 hidden long double __polevll(long double, const long double *, int);
160 hidden long double __p1evll(long double, const long double *, int);
161
162 extern int __signgam;
163 hidden double __lgamma_r(double, int *);
164 hidden float __lgammaf_r(float, int *);
165
166 #endif