xref: /netbsd-src/lib/libm/src/math_private.h (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
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.23 2016/09/19 22:05:05 christos Exp $
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 ARM ports are little endian except for the FPA word order which is
38  * big endian.
39  */
40 
41 #if (BYTE_ORDER == BIG_ENDIAN) || (defined(__arm__) && !defined(__VFP_FP__))
42 
43 typedef union
44 {
45   double value;
46   struct
47   {
48     u_int32_t msw;
49     u_int32_t lsw;
50   } parts;
51   struct {
52     u_int64_t w;
53   } xparts;
54 } ieee_double_shape_type;
55 
56 #endif
57 
58 #if (BYTE_ORDER == LITTLE_ENDIAN) && \
59     !(defined(__arm__) && !defined(__VFP_FP__))
60 
61 typedef union
62 {
63   double value;
64   struct
65   {
66     u_int32_t lsw;
67     u_int32_t msw;
68   } parts;
69   struct {
70     u_int64_t w;
71   } xparts;
72 } ieee_double_shape_type;
73 
74 #endif
75 
76 /* Get two 32 bit ints from a double.  */
77 
78 #define EXTRACT_WORDS(ix0,ix1,d)				\
79 do {								\
80   ieee_double_shape_type ew_u;					\
81   ew_u.value = (d);						\
82   (ix0) = ew_u.parts.msw;					\
83   (ix1) = ew_u.parts.lsw;					\
84 } while (/*CONSTCOND*/0)
85 
86 /* Get a 64-bit int from a double. */
87 #define EXTRACT_WORD64(ix,d)					\
88 do {								\
89   ieee_double_shape_type ew_u;					\
90   ew_u.value = (d);						\
91   (ix) = ew_u.xparts.w;						\
92 } while (/*CONSTCOND*/0)
93 
94 
95 /* Get the more significant 32 bit int from a double.  */
96 
97 #define GET_HIGH_WORD(i,d)					\
98 do {								\
99   ieee_double_shape_type gh_u;					\
100   gh_u.value = (d);						\
101   (i) = gh_u.parts.msw;						\
102 } while (/*CONSTCOND*/0)
103 
104 /* Get the less significant 32 bit int from a double.  */
105 
106 #define GET_LOW_WORD(i,d)					\
107 do {								\
108   ieee_double_shape_type gl_u;					\
109   gl_u.value = (d);						\
110   (i) = gl_u.parts.lsw;						\
111 } while (/*CONSTCOND*/0)
112 
113 /* Set a double from two 32 bit ints.  */
114 
115 #define INSERT_WORDS(d,ix0,ix1)					\
116 do {								\
117   ieee_double_shape_type iw_u;					\
118   iw_u.parts.msw = (ix0);					\
119   iw_u.parts.lsw = (ix1);					\
120   (d) = iw_u.value;						\
121 } while (/*CONSTCOND*/0)
122 
123 /* Set a double from a 64-bit int. */
124 #define INSERT_WORD64(d,ix)					\
125 do {								\
126   ieee_double_shape_type iw_u;					\
127   iw_u.xparts.w = (ix);						\
128   (d) = iw_u.value;						\
129 } while (/*CONSTCOND*/0)
130 
131 
132 /* Set the more significant 32 bits of a double from an int.  */
133 
134 #define SET_HIGH_WORD(d,v)					\
135 do {								\
136   ieee_double_shape_type sh_u;					\
137   sh_u.value = (d);						\
138   sh_u.parts.msw = (v);						\
139   (d) = sh_u.value;						\
140 } while (/*CONSTCOND*/0)
141 
142 /* Set the less significant 32 bits of a double from an int.  */
143 
144 #define SET_LOW_WORD(d,v)					\
145 do {								\
146   ieee_double_shape_type sl_u;					\
147   sl_u.value = (d);						\
148   sl_u.parts.lsw = (v);						\
149   (d) = sl_u.value;						\
150 } while (/*CONSTCOND*/0)
151 
152 /* A union which permits us to convert between a float and a 32 bit
153    int.  */
154 
155 typedef union
156 {
157   float value;
158   u_int32_t word;
159 } ieee_float_shape_type;
160 
161 /* Get a 32 bit int from a float.  */
162 
163 #define GET_FLOAT_WORD(i,d)					\
164 do {								\
165   ieee_float_shape_type gf_u;					\
166   gf_u.value = (d);						\
167   (i) = gf_u.word;						\
168 } while (/*CONSTCOND*/0)
169 
170 /* Set a float from a 32 bit int.  */
171 
172 #define SET_FLOAT_WORD(d,i)					\
173 do {								\
174   ieee_float_shape_type sf_u;					\
175   sf_u.word = (i);						\
176   (d) = sf_u.value;						\
177 } while (/*CONSTCOND*/0)
178 
179 /*
180  * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
181  */
182 #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
183 #define	STRICT_ASSIGN(type, lval, rval)	((lval) = (rval))
184 #else
185 #define	STRICT_ASSIGN(type, lval, rval) do {	\
186 	volatile type __lval;			\
187 						\
188 	if (sizeof(type) >= sizeof(long double))	\
189 		(lval) = (rval);		\
190 	else {					\
191 		__lval = (rval);		\
192 		(lval) = __lval;		\
193 	}					\
194 } while (/*CONSTCOND*/0)
195 #endif
196 
197 #ifdef	_COMPLEX_H
198 
199 /*
200  * Quoting from ISO/IEC 9899:TC2:
201  *
202  * 6.2.5.13 Types
203  * Each complex type has the same representation and alignment requirements as
204  * an array type containing exactly two elements of the corresponding real type;
205  * the first element is equal to the real part, and the second element to the
206  * imaginary part, of the complex number.
207  */
208 typedef union {
209 	float complex z;
210 	float parts[2];
211 } float_complex;
212 
213 typedef union {
214 	double complex z;
215 	double parts[2];
216 } double_complex;
217 
218 typedef union {
219 	long double complex z;
220 	long double parts[2];
221 } long_double_complex;
222 
223 #define	REAL_PART(z)	((z).parts[0])
224 #define	IMAG_PART(z)	((z).parts[1])
225 
226 /*
227  * Inline functions that can be used to construct complex values.
228  *
229  * The C99 standard intends x+I*y to be used for this, but x+I*y is
230  * currently unusable in general since gcc introduces many overflow,
231  * underflow, sign and efficiency bugs by rewriting I*y as
232  * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
233  * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
234  * to -0.0+I*0.0.
235  *
236  * The C11 standard introduced the macros CMPLX(), CMPLXF() and CMPLXL()
237  * to construct complex values.  Compilers that conform to the C99
238  * standard require the following functions to avoid the above issues.
239  */
240 
241 #ifndef CMPLXF
242 static __inline float complex
243 CMPLXF(float x, float y)
244 {
245 	float_complex z;
246 
247 	REAL_PART(z) = x;
248 	IMAG_PART(z) = y;
249 	return (z.z);
250 }
251 #endif
252 
253 #ifndef CMPLX
254 static __inline double complex
255 CMPLX(double x, double y)
256 {
257 	double_complex z;
258 
259 	REAL_PART(z) = x;
260 	IMAG_PART(z) = y;
261 	return (z.z);
262 }
263 #endif
264 
265 #ifndef CMPLXL
266 static __inline long double complex
267 CMPLXL(long double x, long double y)
268 {
269 	long_double_complex z;
270 
271 	REAL_PART(z) = x;
272 	IMAG_PART(z) = y;
273 	return (z.z);
274 }
275 #endif
276 
277 #endif	/* _COMPLEX_H */
278 
279 /* ieee style elementary functions */
280 extern double __ieee754_sqrt __P((double));
281 extern double __ieee754_acos __P((double));
282 extern double __ieee754_acosh __P((double));
283 extern double __ieee754_log __P((double));
284 extern double __ieee754_atanh __P((double));
285 extern double __ieee754_asin __P((double));
286 extern double __ieee754_atan2 __P((double,double));
287 extern double __ieee754_exp __P((double));
288 extern double __ieee754_cosh __P((double));
289 extern double __ieee754_fmod __P((double,double));
290 extern double __ieee754_pow __P((double,double));
291 extern double __ieee754_lgamma_r __P((double,int *));
292 extern double __ieee754_gamma_r __P((double,int *));
293 extern double __ieee754_lgamma __P((double));
294 extern double __ieee754_gamma __P((double));
295 extern double __ieee754_log10 __P((double));
296 extern double __ieee754_log2 __P((double));
297 extern double __ieee754_sinh __P((double));
298 extern double __ieee754_hypot __P((double,double));
299 extern double __ieee754_j0 __P((double));
300 extern double __ieee754_j1 __P((double));
301 extern double __ieee754_y0 __P((double));
302 extern double __ieee754_y1 __P((double));
303 extern double __ieee754_jn __P((int,double));
304 extern double __ieee754_yn __P((int,double));
305 extern double __ieee754_remainder __P((double,double));
306 extern int32_t __ieee754_rem_pio2 __P((double,double*));
307 extern double __ieee754_scalb __P((double,double));
308 
309 /* fdlibm kernel function */
310 extern double __kernel_standard __P((double,double,int));
311 extern double __kernel_sin __P((double,double,int));
312 extern double __kernel_cos __P((double,double));
313 extern double __kernel_tan __P((double,double,int));
314 extern int    __kernel_rem_pio2 __P((double*,double*,int,int,int,const int32_t*));
315 
316 
317 /* ieee style elementary float functions */
318 extern float __ieee754_sqrtf __P((float));
319 extern float __ieee754_acosf __P((float));
320 extern float __ieee754_acoshf __P((float));
321 extern float __ieee754_logf __P((float));
322 extern float __ieee754_atanhf __P((float));
323 extern float __ieee754_asinf __P((float));
324 extern float __ieee754_atan2f __P((float,float));
325 extern float __ieee754_expf __P((float));
326 extern float __ieee754_coshf __P((float));
327 extern float __ieee754_fmodf __P((float,float));
328 extern float __ieee754_powf __P((float,float));
329 extern float __ieee754_lgammaf_r __P((float,int *));
330 extern float __ieee754_gammaf_r __P((float,int *));
331 extern float __ieee754_lgammaf __P((float));
332 extern float __ieee754_gammaf __P((float));
333 extern float __ieee754_log10f __P((float));
334 extern float __ieee754_log2f __P((float));
335 extern float __ieee754_sinhf __P((float));
336 extern float __ieee754_hypotf __P((float,float));
337 extern float __ieee754_j0f __P((float));
338 extern float __ieee754_j1f __P((float));
339 extern float __ieee754_y0f __P((float));
340 extern float __ieee754_y1f __P((float));
341 extern float __ieee754_jnf __P((int,float));
342 extern float __ieee754_ynf __P((int,float));
343 extern float __ieee754_remainderf __P((float,float));
344 extern int32_t __ieee754_rem_pio2f __P((float,float*));
345 extern float __ieee754_scalbf __P((float,float));
346 
347 /* float versions of fdlibm kernel functions */
348 extern float __kernel_sinf __P((float,float,int));
349 extern float __kernel_cosf __P((float,float));
350 extern float __kernel_tanf __P((float,float,int));
351 extern int   __kernel_rem_pio2f __P((float*,float*,int,int,int,const int32_t*));
352 
353 /* ieee style elementary long double functions */
354 extern long double __ieee754_fmodl(long double, long double);
355 extern long double __ieee754_sqrtl(long double);
356 
357 /*
358  * TRUNC() is a macro that sets the trailing 27 bits in the mantissa of an
359  * IEEE double variable to zero.  It must be expression-like for syntactic
360  * reasons, and we implement this expression using an inline function
361  * instead of a pure macro to avoid depending on the gcc feature of
362  * statement-expressions.
363  */
364 #define	TRUNC(d)	(_b_trunc(&(d)))
365 
366 static __inline void
367 _b_trunc(volatile double *_dp)
368 {
369 	uint32_t _lw;
370 
371 	GET_LOW_WORD(_lw, *_dp);
372 	SET_LOW_WORD(*_dp, _lw & 0xf8000000);
373 }
374 
375 struct Double {
376 	double	a;
377 	double	b;
378 };
379 
380 /*
381  * Functions internal to the math package, yet not static.
382  */
383 double	__exp__D(double, double);
384 struct Double __log__D(double);
385 
386 #endif /* _MATH_PRIVATE_H_ */
387