2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
9 * ====================================================
13 * from: @(#)fdlibm.h 5.1 93/09/24
17 #ifndef _MATH_PRIVATE_H_
18 #define _MATH_PRIVATE_H_
20 #include <sys/types.h>
21 #include <machine/endian.h>
24 * The original fdlibm code used statements like:
25 * n0 = ((*(int*)&one)>>29)^1; * index of high word *
26 * ix0 = *(n0+(int*)&x); * high word of x *
27 * ix1 = *((1-n0)+(int*)&x); * low word of x *
28 * to dig two 32 bit words out of the 64 bit IEEE floating point
29 * value. That is non-ANSI, and, moreover, the gcc instruction
30 * scheduler gets it wrong. We instead use the following macros.
31 * Unlike the original code, we determine the endianness at compile
32 * time, not at run time; I don't see much benefit to selecting
33 * endianness at run time.
37 * A union which permits us to convert between a double and two 32 bit
42 #if defined(__VFP_FP__) || defined(__ARM_EABI__)
43 #define IEEE_WORD_ORDER BYTE_ORDER
45 #define IEEE_WORD_ORDER BIG_ENDIAN
48 #define IEEE_WORD_ORDER BYTE_ORDER
51 #if IEEE_WORD_ORDER == BIG_ENDIAN
65 } ieee_double_shape_type;
69 #if IEEE_WORD_ORDER == LITTLE_ENDIAN
83 } ieee_double_shape_type;
87 /* Get two 32 bit ints from a double. */
89 #define EXTRACT_WORDS(ix0,ix1,d) \
91 ieee_double_shape_type ew_u; \
93 (ix0) = ew_u.parts.msw; \
94 (ix1) = ew_u.parts.lsw; \
97 /* Get a 64-bit int from a double. */
98 #define EXTRACT_WORD64(ix,d) \
100 ieee_double_shape_type ew_u; \
102 (ix) = ew_u.xparts.w; \
105 /* Get the more significant 32 bit int from a double. */
107 #define GET_HIGH_WORD(i,d) \
109 ieee_double_shape_type gh_u; \
111 (i) = gh_u.parts.msw; \
114 /* Get the less significant 32 bit int from a double. */
116 #define GET_LOW_WORD(i,d) \
118 ieee_double_shape_type gl_u; \
120 (i) = gl_u.parts.lsw; \
123 /* Set a double from two 32 bit ints. */
125 #define INSERT_WORDS(d,ix0,ix1) \
127 ieee_double_shape_type iw_u; \
128 iw_u.parts.msw = (ix0); \
129 iw_u.parts.lsw = (ix1); \
133 /* Set a double from a 64-bit int. */
134 #define INSERT_WORD64(d,ix) \
136 ieee_double_shape_type iw_u; \
137 iw_u.xparts.w = (ix); \
141 /* Set the more significant 32 bits of a double from an int. */
143 #define SET_HIGH_WORD(d,v) \
145 ieee_double_shape_type sh_u; \
147 sh_u.parts.msw = (v); \
151 /* Set the less significant 32 bits of a double from an int. */
153 #define SET_LOW_WORD(d,v) \
155 ieee_double_shape_type sl_u; \
157 sl_u.parts.lsw = (v); \
162 * A union which permits us to convert between a float and a 32 bit
169 /* FIXME: Assumes 32 bit int. */
171 } ieee_float_shape_type;
173 /* Get a 32 bit int from a float. */
175 #define GET_FLOAT_WORD(i,d) \
177 ieee_float_shape_type gf_u; \
182 /* Set a float from a 32 bit int. */
184 #define SET_FLOAT_WORD(d,i) \
186 ieee_float_shape_type sf_u; \
192 * Get expsign and mantissa as 16 bit and 64 bit ints from an 80 bit long
196 #define EXTRACT_LDBL80_WORDS(ix0,ix1,d) \
198 union IEEEl2bits ew_u; \
200 (ix0) = ew_u.xbits.expsign; \
201 (ix1) = ew_u.xbits.man; \
205 * Get expsign and mantissa as one 16 bit and two 64 bit ints from a 128 bit
209 #define EXTRACT_LDBL128_WORDS(ix0,ix1,ix2,d) \
211 union IEEEl2bits ew_u; \
213 (ix0) = ew_u.xbits.expsign; \
214 (ix1) = ew_u.xbits.manh; \
215 (ix2) = ew_u.xbits.manl; \
218 /* Get expsign as a 16 bit int from a long double. */
220 #define GET_LDBL_EXPSIGN(i,d) \
222 union IEEEl2bits ge_u; \
224 (i) = ge_u.xbits.expsign; \
228 * Set an 80 bit long double from a 16 bit int expsign and a 64 bit int
232 #define INSERT_LDBL80_WORDS(d,ix0,ix1) \
234 union IEEEl2bits iw_u; \
235 iw_u.xbits.expsign = (ix0); \
236 iw_u.xbits.man = (ix1); \
241 * Set a 128 bit long double from a 16 bit int expsign and two 64 bit ints
242 * comprising the mantissa.
245 #define INSERT_LDBL128_WORDS(d,ix0,ix1,ix2) \
247 union IEEEl2bits iw_u; \
248 iw_u.xbits.expsign = (ix0); \
249 iw_u.xbits.manh = (ix1); \
250 iw_u.xbits.manl = (ix2); \
254 /* Set expsign of a long double from a 16 bit int. */
256 #define SET_LDBL_EXPSIGN(d,v) \
258 union IEEEl2bits se_u; \
260 se_u.xbits.expsign = (v); \
265 /* Long double constants are broken on i386. */
266 #define LD80C(m, ex, v) { \
267 .xbits.man = __CONCAT(m, ULL), \
268 .xbits.expsign = (0x3fff + (ex)) | ((v) < 0 ? 0x8000 : 0), \
271 /* The above works on non-i386 too, but we use this to check v. */
272 #define LD80C(m, ex, v) { .e = (v), }
275 #ifdef FLT_EVAL_METHOD
277 * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
279 #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
280 #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
282 #define STRICT_ASSIGN(type, lval, rval) do { \
283 volatile type __lval; \
285 if (sizeof(type) >= sizeof(long double)) \
293 #endif /* FLT_EVAL_METHOD */
295 /* Support switching the mode to FP_PE if necessary. */
296 #if defined(__i386__) && !defined(NO_FPSETPREC)
298 long double __retval; \
301 if ((__oprec = fpgetprec()) != FP_PE) \
303 #define RETURNI(x) do { \
305 if (__oprec != FP_PE) \
306 fpsetprec(__oprec); \
311 #define RETURNI(x) RETURNF(x)
314 /* Default return statement if hack*_t() is not used. */
315 #define RETURNF(v) return (v)
318 * 2sum gives the same result as 2sumF without requiring |a| >= |b| or
319 * a == 0, but is slower.
321 #define _2sum(a, b) do { \
322 __typeof(a) __s, __w; \
326 (b) = ((a) - (__w - __s)) + ((b) - __s); \
333 * "Normalize" the terms in the infinite-precision expression a + b for
334 * the sum of 2 floating point values so that b is as small as possible
335 * relative to 'a'. (The resulting 'a' is the value of the expression in
336 * the same precision as 'a' and the resulting b is the rounding error.)
337 * |a| must be >= |b| or 0, b's type must be no larger than 'a's type, and
338 * exponent overflow or underflow must not occur. This uses a Theorem of
339 * Dekker (1971). See Knuth (1981) 4.2.2 Theorem C. The name "TwoSum"
340 * is apparently due to Skewchuk (1997).
342 * For this to always work, assignment of a + b to 'a' must not retain any
343 * extra precision in a + b. This is required by C standards but broken
344 * in many compilers. The brokenness cannot be worked around using
345 * STRICT_ASSIGN() like we do elsewhere, since the efficiency of this
346 * algorithm would be destroyed by non-null strict assignments. (The
347 * compilers are correct to be broken -- the efficiency of all floating
348 * point code calculations would be destroyed similarly if they forced the
351 * Fortunately, a case that works well can usually be arranged by building
352 * any extra precision into the type of 'a' -- 'a' should have type float_t,
353 * double_t or long double. b's type should be no larger than 'a's type.
354 * Callers should use these types with scopes as large as possible, to
355 * reduce their own extra-precision and efficiciency problems. In
356 * particular, they shouldn't convert back and forth just to call here.
359 #define _2sumF(a, b) do { \
361 volatile __typeof(a) __ia, __ib, __r, __vw; \
365 assert(__ia == 0 || fabsl(__ia) >= fabsl(__ib)); \
368 (b) = ((a) - __w) + (b); \
371 /* The next 2 assertions are weak if (a) is already long double. */ \
372 assert((long double)__ia + __ib == (long double)(a) + (b)); \
373 __vw = __ia + __ib; \
376 assert(__vw == (a) && __r == (b)); \
379 #define _2sumF(a, b) do { \
383 (b) = ((a) - __w) + (b); \
389 * Set x += c, where x is represented in extra precision as a + b.
390 * x must be sufficiently normalized and sufficiently larger than c,
391 * and the result is then sufficiently normalized.
393 * The details of ordering are that |a| must be >= |c| (so that (a, c)
394 * can be normalized without extra work to swap 'a' with c). The details of
395 * the normalization are that b must be small relative to the normalized 'a'.
396 * Normalization of (a, c) makes the normalized c tiny relative to the
397 * normalized a, so b remains small relative to 'a' in the result. However,
398 * b need not ever be tiny relative to 'a'. For example, b might be about
399 * 2**20 times smaller than 'a' to give about 20 extra bits of precision.
400 * That is usually enough, and adding c (which by normalization is about
401 * 2**53 times smaller than a) cannot change b significantly. However,
402 * cancellation of 'a' with c in normalization of (a, c) may reduce 'a'
403 * significantly relative to b. The caller must ensure that significant
404 * cancellation doesn't occur, either by having c of the same sign as 'a',
405 * or by having |c| a few percent smaller than |a|. Pre-normalization of
408 * This is is a variant of an algorithm of Kahan (see Knuth (1981) 4.2.2
409 * exercise 19). We gain considerable efficiency by requiring the terms to
410 * be sufficiently normalized and sufficiently increasing.
412 #define _3sumF(a, b, c) do { \
416 _2sumF(__tmp, (a)); \
422 * Common routine to process the arguments to nan(), nanf(), and nanl().
424 void _scan_nan(uint32_t *__words, int __num_words, const char *__s);
429 * C99 specifies that complex numbers have the same representation as
430 * an array of two elements, where the first element is the real part
431 * and the second element is the imaginary part.
442 long double complex f;
444 } long_double_complex;
445 #define REALPART(z) ((z).a[0])
446 #define IMAGPART(z) ((z).a[1])
449 * Inline functions that can be used to construct complex values.
451 * The C99 standard intends x+I*y to be used for this, but x+I*y is
452 * currently unusable in general since gcc introduces many overflow,
453 * underflow, sign and efficiency bugs by rewriting I*y as
454 * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
455 * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
458 * The C11 standard introduced the macros CMPLX(), CMPLXF() and CMPLXL()
459 * to construct complex values. The functions below are modelled after
460 * these macros, with the exception that they cannot be used to
461 * construct compile-time complex values.
465 static __inline float complex
466 CMPLXF(float x, float y)
477 static __inline double complex
478 CMPLX(double x, double y)
489 static __inline long double complex
490 CMPLXL(long double x, long double y)
492 long_double_complex z;
500 #endif /* _COMPLEX_H */
502 #ifdef __GNUCLIKE_ASM
504 /* Asm versions of some functions. */
512 asm("cvtsd2si %1,%0" : "=r" (n) : "x" (x));
515 #define HAVE_EFFICIENT_IRINT
524 asm("fistl %0" : "=m" (n) : "t" (x));
527 #define HAVE_EFFICIENT_IRINT
530 #if defined(__amd64__) || defined(__i386__)
532 irintl(long double x)
536 asm("fistl %0" : "=m" (n) : "t" (x));
539 #define HAVE_EFFICIENT_IRINTL
542 #endif /* __GNUCLIKE_ASM */
545 #if defined(__amd64__) || defined(__i386__)
546 #define breakpoint() asm("int $3")
550 #define breakpoint() raise(SIGTRAP)
554 /* Write a pari script to test things externally. */
558 #ifndef DOPRINT_SWIZZLE
559 #define DOPRINT_SWIZZLE 0
564 #define DOPRINT_START(xp) do { \
568 /* Hack to give more-problematic args. */ \
569 EXTRACT_LDBL80_WORDS(__hx, __lx, *xp); \
570 __lx ^= DOPRINT_SWIZZLE; \
571 INSERT_LDBL80_WORDS(*xp, __hx, __lx); \
572 printf("x = %.21Lg; ", (long double)*xp); \
574 #define DOPRINT_END1(v) \
575 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
576 #define DOPRINT_END2(hi, lo) \
577 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
578 (long double)(hi), (long double)(lo))
580 #elif defined(DOPRINT_D64)
582 #define DOPRINT_START(xp) do { \
583 uint32_t __hx, __lx; \
585 EXTRACT_WORDS(__hx, __lx, *xp); \
586 __lx ^= DOPRINT_SWIZZLE; \
587 INSERT_WORDS(*xp, __hx, __lx); \
588 printf("x = %.21Lg; ", (long double)*xp); \
590 #define DOPRINT_END1(v) \
591 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
592 #define DOPRINT_END2(hi, lo) \
593 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
594 (long double)(hi), (long double)(lo))
596 #elif defined(DOPRINT_F32)
598 #define DOPRINT_START(xp) do { \
601 GET_FLOAT_WORD(__hx, *xp); \
602 __hx ^= DOPRINT_SWIZZLE; \
603 SET_FLOAT_WORD(*xp, __hx); \
604 printf("x = %.21Lg; ", (long double)*xp); \
606 #define DOPRINT_END1(v) \
607 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
608 #define DOPRINT_END2(hi, lo) \
609 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
610 (long double)(hi), (long double)(lo))
612 #else /* !DOPRINT_LD80 && !DOPRINT_D64 (LD128 only) */
614 #ifndef DOPRINT_SWIZZLE_HIGH
615 #define DOPRINT_SWIZZLE_HIGH 0
618 #define DOPRINT_START(xp) do { \
619 uint64_t __lx, __llx; \
622 EXTRACT_LDBL128_WORDS(__hx, __lx, __llx, *xp); \
623 __llx ^= DOPRINT_SWIZZLE; \
624 __lx ^= DOPRINT_SWIZZLE_HIGH; \
625 INSERT_LDBL128_WORDS(*xp, __hx, __lx, __llx); \
626 printf("x = %.36Lg; ", (long double)*xp); \
628 #define DOPRINT_END1(v) \
629 printf("y = %.36Lg; z = 0; show(x, y, z);\n", (long double)(v))
630 #define DOPRINT_END2(hi, lo) \
631 printf("y = %.36Lg; z = %.36Lg; show(x, y, z);\n", \
632 (long double)(hi), (long double)(lo))
634 #endif /* DOPRINT_LD80 */
637 #define DOPRINT_START(xp)
638 #define DOPRINT_END1(v)
639 #define DOPRINT_END2(hi, lo)
642 #define RETURNP(x) do { \
646 #define RETURNPI(x) do { \
650 #define RETURN2P(x, y) do { \
651 DOPRINT_END2((x), (y)); \
652 RETURNF((x) + (y)); \
654 #define RETURN2PI(x, y) do { \
655 DOPRINT_END2((x), (y)); \
656 RETURNI((x) + (y)); \
659 #define RETURNSP(rp) do { \
662 RETURN2P((rp)->hi, (rp)->lo); \
664 #define RETURNSPI(rp) do { \
666 RETURNPI((rp)->hi); \
667 RETURN2PI((rp)->hi, (rp)->lo); \
670 #define SUM2P(x, y) ({ \
671 const __typeof (x) __x = (x); \
672 const __typeof (y) __y = (y); \
674 DOPRINT_END2(__x, __y); \
679 * ieee style elementary functions
681 * We rename functions here to improve other sources' diffability
684 #define __ieee754_sqrt sqrt
685 #define __ieee754_acos acos
686 #define __ieee754_acosh acosh
687 #define __ieee754_log log
688 #define __ieee754_log2 log2
689 #define __ieee754_atanh atanh
690 #define __ieee754_asin asin
691 #define __ieee754_atan2 atan2
692 #define __ieee754_exp exp
693 #define __ieee754_cosh cosh
694 #define __ieee754_fmod fmod
695 #define __ieee754_pow pow
696 #define __ieee754_lgamma lgamma
697 #define __ieee754_gamma gamma
698 #define __ieee754_lgamma_r lgamma_r
699 #define __ieee754_gamma_r gamma_r
700 #define __ieee754_log10 log10
701 #define __ieee754_sinh sinh
702 #define __ieee754_hypot hypot
703 #define __ieee754_j0 j0
704 #define __ieee754_j1 j1
705 #define __ieee754_y0 y0
706 #define __ieee754_y1 y1
707 #define __ieee754_jn jn
708 #define __ieee754_yn yn
709 #define __ieee754_remainder remainder
710 #define __ieee754_scalb scalb
711 #define __ieee754_sqrtf sqrtf
712 #define __ieee754_acosf acosf
713 #define __ieee754_acoshf acoshf
714 #define __ieee754_logf logf
715 #define __ieee754_atanhf atanhf
716 #define __ieee754_asinf asinf
717 #define __ieee754_atan2f atan2f
718 #define __ieee754_expf expf
719 #define __ieee754_coshf coshf
720 #define __ieee754_fmodf fmodf
721 #define __ieee754_powf powf
722 #define __ieee754_lgammaf lgammaf
723 #define __ieee754_gammaf gammaf
724 #define __ieee754_lgammaf_r lgammaf_r
725 #define __ieee754_gammaf_r gammaf_r
726 #define __ieee754_log10f log10f
727 #define __ieee754_log2f log2f
728 #define __ieee754_sinhf sinhf
729 #define __ieee754_hypotf hypotf
730 #define __ieee754_j0f j0f
731 #define __ieee754_j1f j1f
732 #define __ieee754_y0f y0f
733 #define __ieee754_y1f y1f
734 #define __ieee754_jnf jnf
735 #define __ieee754_ynf ynf
736 #define __ieee754_remainderf remainderf
737 #define __ieee754_scalbf scalbf
739 /* fdlibm kernel function */
740 int __kernel_rem_pio2(double*,double*,int,int,int);
742 /* double precision kernel functions */
743 #ifndef INLINE_REM_PIO2
744 int __ieee754_rem_pio2(double,double*);
746 double __kernel_sin(double,double,int);
747 double __kernel_cos(double,double);
748 double __kernel_tan(double,double,int);
749 double __ldexp_exp(double,int);
751 double complex __ldexp_cexp(double complex,int);
754 /* float precision kernel functions */
755 #ifndef INLINE_REM_PIO2F
756 int __ieee754_rem_pio2f(float,double*);
758 #ifndef INLINE_KERNEL_SINDF
759 float __kernel_sindf(double);
761 #ifndef INLINE_KERNEL_COSDF
762 float __kernel_cosdf(double);
764 #ifndef INLINE_KERNEL_TANDF
765 float __kernel_tandf(double,int);
767 float __ldexp_expf(float,int);
769 float complex __ldexp_cexpf(float complex,int);
772 /* long double precision kernel functions */
773 long double __kernel_sinl(long double, long double, int);
774 long double __kernel_cosl(long double, long double);
775 long double __kernel_tanl(long double, long double, int);
777 #endif /* !_MATH_PRIVATE_H_ */