faa8834f9dff08ed08b0aa32a8a946963da3e3d7
[dragonfly.git] / lib / libm / src / math_private.h
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11
12 /*
13  * from: @(#)fdlibm.h 5.1 93/09/24
14  * $NetBSD: math_private.h,v 1.11 2001/02/21 18:09:26 bjh21 Exp $
15  * $DragonFly: src/lib/libm/src/math_private.h,v 1.1 2005/07/26 21:15:20 joerg Exp $
16  */
17
18 #ifndef _MATH_PRIVATE_H_
19 #define _MATH_PRIVATE_H_
20
21 #include <sys/types.h>
22
23 /* The original fdlibm code used statements like:
24         n0 = ((*(int*)&one)>>29)^1;             * index of high word *
25         ix0 = *(n0+(int*)&x);                   * high word of x *
26         ix1 = *((1-n0)+(int*)&x);               * low word of x *
27    to dig two 32 bit words out of the 64 bit IEEE floating point
28    value.  That is non-ANSI, and, moreover, the gcc instruction
29    scheduler gets it wrong.  We instead use the following macros.
30    Unlike the original code, we determine the endianness at compile
31    time, not at run time; I don't see much benefit to selecting
32    endianness at run time.  */
33
34 /* A union which permits us to convert between a double and two 32 bit
35    ints.  */
36
37 /*
38  * The ARM ports are little endian except for the FPA word order which is
39  * big endian.
40  */
41
42 #if (BYTE_ORDER == BIG_ENDIAN) || (defined(__arm__) && !defined(__VFP_FP__))
43
44 typedef union
45 {
46   double value;
47   struct
48   {
49     u_int32_t msw;
50     u_int32_t lsw;
51   } parts;
52 } ieee_double_shape_type;
53
54 #endif
55
56 #if (BYTE_ORDER == LITTLE_ENDIAN) && \
57     !(defined(__arm__) && !defined(__VFP_FP__))
58
59 typedef union
60 {
61   double value;
62   struct
63   {
64     u_int32_t lsw;
65     u_int32_t msw;
66   } parts;
67 } ieee_double_shape_type;
68
69 #endif
70
71 /* Get two 32 bit ints from a double.  */
72
73 #define EXTRACT_WORDS(ix0,ix1,d)                                \
74 do {                                                            \
75   ieee_double_shape_type ew_u;                                  \
76   ew_u.value = (d);                                             \
77   (ix0) = ew_u.parts.msw;                                       \
78   (ix1) = ew_u.parts.lsw;                                       \
79 } while (0)
80
81 /* Get the more significant 32 bit int from a double.  */
82
83 #define GET_HIGH_WORD(i,d)                                      \
84 do {                                                            \
85   ieee_double_shape_type gh_u;                                  \
86   gh_u.value = (d);                                             \
87   (i) = gh_u.parts.msw;                                         \
88 } while (0)
89
90 /* Get the less significant 32 bit int from a double.  */
91
92 #define GET_LOW_WORD(i,d)                                       \
93 do {                                                            \
94   ieee_double_shape_type gl_u;                                  \
95   gl_u.value = (d);                                             \
96   (i) = gl_u.parts.lsw;                                         \
97 } while (0)
98
99 /* Set a double from two 32 bit ints.  */
100
101 #define INSERT_WORDS(d,ix0,ix1)                                 \
102 do {                                                            \
103   ieee_double_shape_type iw_u;                                  \
104   iw_u.parts.msw = (ix0);                                       \
105   iw_u.parts.lsw = (ix1);                                       \
106   (d) = iw_u.value;                                             \
107 } while (0)
108
109 /* Set the more significant 32 bits of a double from an int.  */
110
111 #define SET_HIGH_WORD(d,v)                                      \
112 do {                                                            \
113   ieee_double_shape_type sh_u;                                  \
114   sh_u.value = (d);                                             \
115   sh_u.parts.msw = (v);                                         \
116   (d) = sh_u.value;                                             \
117 } while (0)
118
119 /* Set the less significant 32 bits of a double from an int.  */
120
121 #define SET_LOW_WORD(d,v)                                       \
122 do {                                                            \
123   ieee_double_shape_type sl_u;                                  \
124   sl_u.value = (d);                                             \
125   sl_u.parts.lsw = (v);                                         \
126   (d) = sl_u.value;                                             \
127 } while (0)
128
129 /* A union which permits us to convert between a float and a 32 bit
130    int.  */
131
132 typedef union
133 {
134   float value;
135   u_int32_t word;
136 } ieee_float_shape_type;
137
138 /* Get a 32 bit int from a float.  */
139
140 #define GET_FLOAT_WORD(i,d)                                     \
141 do {                                                            \
142   ieee_float_shape_type gf_u;                                   \
143   gf_u.value = (d);                                             \
144   (i) = gf_u.word;                                              \
145 } while (0)
146
147 /* Set a float from a 32 bit int.  */
148
149 #define SET_FLOAT_WORD(d,i)                                     \
150 do {                                                            \
151   ieee_float_shape_type sf_u;                                   \
152   sf_u.word = (i);                                              \
153   (d) = sf_u.value;                                             \
154 } while (0)
155
156 #ifdef _COMPLEX_H
157
158 /*
159  * C99 specifies that complex numbers have the same representation as
160  * an array of two elements, where the first element is the real part
161  * and the second element is the imaginary part.
162  */
163 typedef union {
164         float complex f;
165         float a[2];
166 } float_complex;
167 typedef union {
168         double complex f;
169         double a[2];
170 } double_complex;
171 typedef union {
172         long double complex f;
173         long double a[2];
174 } long_double_complex;
175 #define REALPART(z)     ((z).a[0])
176 #define IMAGPART(z)     ((z).a[1])
177
178 /*
179  * Inline functions that can be used to construct complex values.
180  *
181  * The C99 standard intends x+I*y to be used for this, but x+I*y is
182  * currently unusable in general since gcc introduces many overflow,
183  * underflow, sign and efficiency bugs by rewriting I*y as
184  * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
185  * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
186  * to -0.0+I*0.0.
187  */
188 static __inline float complex
189 cpackf(float x, float y)
190 {
191         float_complex z;
192
193         REALPART(z) = x;
194         IMAGPART(z) = y;
195         return (z.f);
196 }
197
198 static __inline double complex
199 cpack(double x, double y)
200 {
201         double_complex z;
202
203         REALPART(z) = x;
204         IMAGPART(z) = y;
205         return (z.f);
206 }
207
208 static __inline long double complex
209 cpackl(long double x, long double y)
210 {
211         long_double_complex z;
212
213         REALPART(z) = x;
214         IMAGPART(z) = y;
215         return (z.f);
216 }
217
218 #endif /* _COMPLEX_H */
219
220 __BEGIN_DECLS
221 #pragma GCC visibility push(hidden)
222
223 /* ieee style elementary functions */
224 int     __libm_rem_pio2(double, double*);
225
226 /* fdlibm kernel function */
227 double  __kernel_sin(double, double, int);
228 double  __kernel_cos(double, double);
229 double  __kernel_tan(double, double, int);
230 int     __kernel_rem_pio2(double*, double*, int, int, int, const int*);
231
232
233 /* ieee style elementary float functions */
234 int     __libm_rem_pio2f(float,float*);
235
236 /* float versions of fdlibm kernel functions */
237 float   __kernel_sinf(float, float, int);
238 float   __kernel_cosf(float, float);
239 float   __kernel_tanf(float, float, int);
240 int     __kernel_rem_pio2f(float*, float*, int, int, int, const int*);
241
242 #pragma GCC visibility pop
243 __END_DECLS
244
245 #endif /* _MATH_PRIVATE_H_ */