xref: /openbsd-src/lib/libm/src/math_private.h (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: math_private.h,v 1.6 2002/02/16 21:27:27 millert Exp $	*/
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 /*
14  * from: @(#)fdlibm.h 5.1 93/09/24
15  */
16 
17 #ifndef _MATH_PRIVATE_H_
18 #define _MATH_PRIVATE_H_
19 
20 #include <sys/types.h>
21 
22 /* The original fdlibm code used statements like:
23 	n0 = ((*(int*)&one)>>29)^1;		* index of high word *
24 	ix0 = *(n0+(int*)&x);			* high word of x *
25 	ix1 = *((1-n0)+(int*)&x);		* low word of x *
26    to dig two 32 bit words out of the 64 bit IEEE floating point
27    value.  That is non-ANSI, and, moreover, the gcc instruction
28    scheduler gets it wrong.  We instead use the following macros.
29    Unlike the original code, we determine the endianness at compile
30    time, not at run time; I don't see much benefit to selecting
31    endianness at run time.  */
32 
33 /* A union which permits us to convert between a double and two 32 bit
34    ints.  */
35 
36 /*
37  * The arm32 port is little endian except for the FP word order which is
38  * big endian.
39  */
40 
41 #if (BYTE_ORDER == BIG_ENDIAN) || defined(arm32)
42 
43 typedef union
44 {
45   double value;
46   struct
47   {
48     u_int32_t msw;
49     u_int32_t lsw;
50   } parts;
51 } ieee_double_shape_type;
52 
53 #endif
54 
55 #if (BYTE_ORDER == LITTLE_ENDIAN) && !defined(arm32)
56 
57 typedef union
58 {
59   double value;
60   struct
61   {
62     u_int32_t lsw;
63     u_int32_t msw;
64   } parts;
65 } ieee_double_shape_type;
66 
67 #endif
68 
69 /* Get two 32 bit ints from a double.  */
70 
71 #define EXTRACT_WORDS(ix0,ix1,d)				\
72 do {								\
73   ieee_double_shape_type ew_u;					\
74   ew_u.value = (d);						\
75   (ix0) = ew_u.parts.msw;					\
76   (ix1) = ew_u.parts.lsw;					\
77 } while (0)
78 
79 /* Get the more significant 32 bit int from a double.  */
80 
81 #define GET_HIGH_WORD(i,d)					\
82 do {								\
83   ieee_double_shape_type gh_u;					\
84   gh_u.value = (d);						\
85   (i) = gh_u.parts.msw;						\
86 } while (0)
87 
88 /* Get the less significant 32 bit int from a double.  */
89 
90 #define GET_LOW_WORD(i,d)					\
91 do {								\
92   ieee_double_shape_type gl_u;					\
93   gl_u.value = (d);						\
94   (i) = gl_u.parts.lsw;						\
95 } while (0)
96 
97 /* Set a double from two 32 bit ints.  */
98 
99 #define INSERT_WORDS(d,ix0,ix1)					\
100 do {								\
101   ieee_double_shape_type iw_u;					\
102   iw_u.parts.msw = (ix0);					\
103   iw_u.parts.lsw = (ix1);					\
104   (d) = iw_u.value;						\
105 } while (0)
106 
107 /* Set the more significant 32 bits of a double from an int.  */
108 
109 #define SET_HIGH_WORD(d,v)					\
110 do {								\
111   ieee_double_shape_type sh_u;					\
112   sh_u.value = (d);						\
113   sh_u.parts.msw = (v);						\
114   (d) = sh_u.value;						\
115 } while (0)
116 
117 /* Set the less significant 32 bits of a double from an int.  */
118 
119 #define SET_LOW_WORD(d,v)					\
120 do {								\
121   ieee_double_shape_type sl_u;					\
122   sl_u.value = (d);						\
123   sl_u.parts.lsw = (v);						\
124   (d) = sl_u.value;						\
125 } while (0)
126 
127 /* A union which permits us to convert between a float and a 32 bit
128    int.  */
129 
130 typedef union
131 {
132   float value;
133   u_int32_t word;
134 } ieee_float_shape_type;
135 
136 /* Get a 32 bit int from a float.  */
137 
138 #define GET_FLOAT_WORD(i,d)					\
139 do {								\
140   ieee_float_shape_type gf_u;					\
141   gf_u.value = (d);						\
142   (i) = gf_u.word;						\
143 } while (0)
144 
145 /* Set a float from a 32 bit int.  */
146 
147 #define SET_FLOAT_WORD(d,i)					\
148 do {								\
149   ieee_float_shape_type sf_u;					\
150   sf_u.word = (i);						\
151   (d) = sf_u.value;						\
152 } while (0)
153 
154 /* ieee style elementary functions */
155 extern double __ieee754_sqrt(double);
156 extern double __ieee754_acos(double);
157 extern double __ieee754_acosh(double);
158 extern double __ieee754_log(double);
159 extern double __ieee754_atanh(double);
160 extern double __ieee754_asin(double);
161 extern double __ieee754_atan2(double,double);
162 extern double __ieee754_exp(double);
163 extern double __ieee754_cosh(double);
164 extern double __ieee754_fmod(double,double);
165 extern double __ieee754_pow(double,double);
166 extern double __ieee754_lgamma_r(double,int *);
167 extern double __ieee754_gamma_r(double,int *);
168 extern double __ieee754_lgamma(double);
169 extern double __ieee754_gamma(double);
170 extern double __ieee754_log10(double);
171 extern double __ieee754_sinh(double);
172 extern double __ieee754_hypot(double,double);
173 extern double __ieee754_j0(double);
174 extern double __ieee754_j1(double);
175 extern double __ieee754_y0(double);
176 extern double __ieee754_y1(double);
177 extern double __ieee754_jn(int,double);
178 extern double __ieee754_yn(int,double);
179 extern double __ieee754_remainder(double,double);
180 extern int    __ieee754_rem_pio2(double,double*);
181 extern double __ieee754_scalb(double,double);
182 
183 /* fdlibm kernel function */
184 extern double __kernel_standard(double,double,int);
185 extern double __kernel_sin(double,double,int);
186 extern double __kernel_cos(double,double);
187 extern double __kernel_tan(double,double,int);
188 extern int    __kernel_rem_pio2(double*,double*,int,int,int,const int*);
189 
190 
191 /* ieee style elementary float functions */
192 extern float __ieee754_sqrtf(float);
193 extern float __ieee754_acosf(float);
194 extern float __ieee754_acoshf(float);
195 extern float __ieee754_logf(float);
196 extern float __ieee754_atanhf(float);
197 extern float __ieee754_asinf(float);
198 extern float __ieee754_atan2f(float,float);
199 extern float __ieee754_expf(float);
200 extern float __ieee754_coshf(float);
201 extern float __ieee754_fmodf(float,float);
202 extern float __ieee754_powf(float,float);
203 extern float __ieee754_lgammaf_r(float,int *);
204 extern float __ieee754_gammaf_r(float,int *);
205 extern float __ieee754_lgammaf(float);
206 extern float __ieee754_gammaf(float);
207 extern float __ieee754_log10f(float);
208 extern float __ieee754_sinhf(float);
209 extern float __ieee754_hypotf(float,float);
210 extern float __ieee754_j0f(float);
211 extern float __ieee754_j1f(float);
212 extern float __ieee754_y0f(float);
213 extern float __ieee754_y1f(float);
214 extern float __ieee754_jnf(int,float);
215 extern float __ieee754_ynf(int,float);
216 extern float __ieee754_remainderf(float,float);
217 extern int   __ieee754_rem_pio2f(float,float*);
218 extern float __ieee754_scalbf(float,float);
219 
220 /* float versions of fdlibm kernel functions */
221 extern float __kernel_sinf(float,float,int);
222 extern float __kernel_cosf(float,float);
223 extern float __kernel_tanf(float,float,int);
224 extern int   __kernel_rem_pio2f(float*,float*,int,int,int,const int*);
225 
226 #endif /* _MATH_PRIVATE_H_ */
227