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 /* A union which permits us to convert between a long double and
54 #if IEEE_WORD_ORDER == BIG_ENDIAN
69 } ieee_quad_shape_type;
73 #if IEEE_WORD_ORDER == LITTLE_ENDIAN
88 } ieee_quad_shape_type;
92 #if IEEE_WORD_ORDER == BIG_ENDIAN
106 } ieee_double_shape_type;
110 #if IEEE_WORD_ORDER == LITTLE_ENDIAN
124 } ieee_double_shape_type;
128 /* Get two 32 bit ints from a double. */
130 #define EXTRACT_WORDS(ix0,ix1,d) \
132 ieee_double_shape_type ew_u; \
134 (ix0) = ew_u.parts.msw; \
135 (ix1) = ew_u.parts.lsw; \
138 /* Get a 64-bit int from a double. */
139 #define EXTRACT_WORD64(ix,d) \
141 ieee_double_shape_type ew_u; \
143 (ix) = ew_u.xparts.w; \
146 /* Get the more significant 32 bit int from a double. */
148 #define GET_HIGH_WORD(i,d) \
150 ieee_double_shape_type gh_u; \
152 (i) = gh_u.parts.msw; \
155 /* Get the less significant 32 bit int from a double. */
157 #define GET_LOW_WORD(i,d) \
159 ieee_double_shape_type gl_u; \
161 (i) = gl_u.parts.lsw; \
164 /* Set a double from two 32 bit ints. */
166 #define INSERT_WORDS(d,ix0,ix1) \
168 ieee_double_shape_type iw_u; \
169 iw_u.parts.msw = (ix0); \
170 iw_u.parts.lsw = (ix1); \
174 /* Set a double from a 64-bit int. */
175 #define INSERT_WORD64(d,ix) \
177 ieee_double_shape_type iw_u; \
178 iw_u.xparts.w = (ix); \
182 /* Set the more significant 32 bits of a double from an int. */
184 #define SET_HIGH_WORD(d,v) \
186 ieee_double_shape_type sh_u; \
188 sh_u.parts.msw = (v); \
192 /* Set the less significant 32 bits of a double from an int. */
194 #define SET_LOW_WORD(d,v) \
196 ieee_double_shape_type sl_u; \
198 sl_u.parts.lsw = (v); \
203 * A union which permits us to convert between a float and a 32 bit
210 /* FIXME: Assumes 32 bit int. */
212 } ieee_float_shape_type;
214 /* Get a 32 bit int from a float. */
216 #define GET_FLOAT_WORD(i,d) \
218 ieee_float_shape_type gf_u; \
223 /* Set a float from a 32 bit int. */
225 #define SET_FLOAT_WORD(d,i) \
227 ieee_float_shape_type sf_u; \
233 * Get expsign and mantissa as 16 bit and 64 bit ints from an 80 bit long
237 #define EXTRACT_LDBL80_WORDS(ix0,ix1,d) \
239 union IEEEl2bits ew_u; \
241 (ix0) = ew_u.xbits.expsign; \
242 (ix1) = ew_u.xbits.man; \
246 * Get expsign and mantissa as one 16 bit and two 64 bit ints from a 128 bit
250 #define EXTRACT_LDBL128_WORDS(ix0,ix1,ix2,d) \
252 union IEEEl2bits ew_u; \
254 (ix0) = ew_u.xbits.expsign; \
255 (ix1) = ew_u.xbits.manh; \
256 (ix2) = ew_u.xbits.manl; \
259 /* Get expsign as a 16 bit int from a long double. */
261 #define GET_LDBL_EXPSIGN(i,d) \
263 union IEEEl2bits ge_u; \
265 (i) = ge_u.xbits.expsign; \
269 * Set an 80 bit long double from a 16 bit int expsign and a 64 bit int
273 #define INSERT_LDBL80_WORDS(d,ix0,ix1) \
275 union IEEEl2bits iw_u; \
276 iw_u.xbits.expsign = (ix0); \
277 iw_u.xbits.man = (ix1); \
282 * Set a 128 bit long double from a 16 bit int expsign and two 64 bit ints
283 * comprising the mantissa.
286 #define INSERT_LDBL128_WORDS(d,ix0,ix1,ix2) \
288 union IEEEl2bits iw_u; \
289 iw_u.xbits.expsign = (ix0); \
290 iw_u.xbits.manh = (ix1); \
291 iw_u.xbits.manl = (ix2); \
295 /* Set expsign of a long double from a 16 bit int. */
297 #define SET_LDBL_EXPSIGN(d,v) \
299 union IEEEl2bits se_u; \
301 se_u.xbits.expsign = (v); \
306 /* Long double constants are broken on i386. */
307 #define LD80C(m, ex, v) { \
308 .xbits.man = __CONCAT(m, ULL), \
309 .xbits.expsign = (0x3fff + (ex)) | ((v) < 0 ? 0x8000 : 0), \
312 /* The above works on non-i386 too, but we use this to check v. */
313 #define LD80C(m, ex, v) { .e = (v), }
316 #ifdef FLT_EVAL_METHOD
318 * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
320 #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
321 #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
323 #define STRICT_ASSIGN(type, lval, rval) do { \
324 volatile type __lval; \
326 if (sizeof(type) >= sizeof(long double)) \
334 #endif /* FLT_EVAL_METHOD */
336 /* Support switching the mode to FP_PE if necessary. */
337 #if defined(__i386__) && !defined(NO_FPSETPREC)
338 #define ENTERI() ENTERIT(long double)
339 #define ENTERIT(returntype) \
340 returntype __retval; \
343 if ((__oprec = fpgetprec()) != FP_PE) \
345 #define RETURNI(x) do { \
347 if (__oprec != FP_PE) \
348 fpsetprec(__oprec); \
354 if ((__oprec = fpgetprec()) != FP_PE) \
356 #define RETURNV() do { \
357 if (__oprec != FP_PE) \
358 fpsetprec(__oprec); \
364 #define RETURNI(x) RETURNF(x)
366 #define RETURNV() return
369 /* Default return statement if hack*_t() is not used. */
370 #define RETURNF(v) return (v)
373 * 2sum gives the same result as 2sumF without requiring |a| >= |b| or
374 * a == 0, but is slower.
376 #define _2sum(a, b) do { \
377 __typeof(a) __s, __w; \
381 (b) = ((a) - (__w - __s)) + ((b) - __s); \
388 * "Normalize" the terms in the infinite-precision expression a + b for
389 * the sum of 2 floating point values so that b is as small as possible
390 * relative to 'a'. (The resulting 'a' is the value of the expression in
391 * the same precision as 'a' and the resulting b is the rounding error.)
392 * |a| must be >= |b| or 0, b's type must be no larger than 'a's type, and
393 * exponent overflow or underflow must not occur. This uses a Theorem of
394 * Dekker (1971). See Knuth (1981) 4.2.2 Theorem C. The name "TwoSum"
395 * is apparently due to Skewchuk (1997).
397 * For this to always work, assignment of a + b to 'a' must not retain any
398 * extra precision in a + b. This is required by C standards but broken
399 * in many compilers. The brokenness cannot be worked around using
400 * STRICT_ASSIGN() like we do elsewhere, since the efficiency of this
401 * algorithm would be destroyed by non-null strict assignments. (The
402 * compilers are correct to be broken -- the efficiency of all floating
403 * point code calculations would be destroyed similarly if they forced the
406 * Fortunately, a case that works well can usually be arranged by building
407 * any extra precision into the type of 'a' -- 'a' should have type float_t,
408 * double_t or long double. b's type should be no larger than 'a's type.
409 * Callers should use these types with scopes as large as possible, to
410 * reduce their own extra-precision and efficiciency problems. In
411 * particular, they shouldn't convert back and forth just to call here.
414 #define _2sumF(a, b) do { \
416 volatile __typeof(a) __ia, __ib, __r, __vw; \
420 assert(__ia == 0 || fabsl(__ia) >= fabsl(__ib)); \
423 (b) = ((a) - __w) + (b); \
426 /* The next 2 assertions are weak if (a) is already long double. */ \
427 assert((long double)__ia + __ib == (long double)(a) + (b)); \
428 __vw = __ia + __ib; \
431 assert(__vw == (a) && __r == (b)); \
434 #define _2sumF(a, b) do { \
438 (b) = ((a) - __w) + (b); \
444 * Set x += c, where x is represented in extra precision as a + b.
445 * x must be sufficiently normalized and sufficiently larger than c,
446 * and the result is then sufficiently normalized.
448 * The details of ordering are that |a| must be >= |c| (so that (a, c)
449 * can be normalized without extra work to swap 'a' with c). The details of
450 * the normalization are that b must be small relative to the normalized 'a'.
451 * Normalization of (a, c) makes the normalized c tiny relative to the
452 * normalized a, so b remains small relative to 'a' in the result. However,
453 * b need not ever be tiny relative to 'a'. For example, b might be about
454 * 2**20 times smaller than 'a' to give about 20 extra bits of precision.
455 * That is usually enough, and adding c (which by normalization is about
456 * 2**53 times smaller than a) cannot change b significantly. However,
457 * cancellation of 'a' with c in normalization of (a, c) may reduce 'a'
458 * significantly relative to b. The caller must ensure that significant
459 * cancellation doesn't occur, either by having c of the same sign as 'a',
460 * or by having |c| a few percent smaller than |a|. Pre-normalization of
463 * This is is a variant of an algorithm of Kahan (see Knuth (1981) 4.2.2
464 * exercise 19). We gain considerable efficiency by requiring the terms to
465 * be sufficiently normalized and sufficiently increasing.
467 #define _3sumF(a, b, c) do { \
471 _2sumF(__tmp, (a)); \
477 * Common routine to process the arguments to nan(), nanf(), and nanl().
479 void _scan_nan(uint32_t *__words, int __num_words, const char *__s);
482 * Mix 1 or 2 NaNs. First add 0 to each arg. This normally just turns
483 * signaling NaNs into quiet NaNs by setting a quiet bit. We do this
484 * because we want to never return a signaling NaN, and also because we
485 * don't want the quiet bit to affect the result. Then mix the converted
486 * args using addition. The result is typically the arg whose mantissa
487 * bits (considered as in integer) are largest.
489 * Technical complications: the result in bits might depend on the precision
490 * and/or on compiler optimizations, especially when different register sets
491 * are used for different precisions. Try to make the result not depend on
492 * at least the precision by always doing the main mixing step in long double
493 * precision. Try to reduce dependencies on optimizations by adding the
494 * the 0's in different precisions (unless everything is in long double
497 #define nan_mix(x, y) (((x) + 0.0L) + ((y) + 0))
502 * C99 specifies that complex numbers have the same representation as
503 * an array of two elements, where the first element is the real part
504 * and the second element is the imaginary part.
515 long double complex f;
517 } long_double_complex;
518 #define REALPART(z) ((z).a[0])
519 #define IMAGPART(z) ((z).a[1])
522 * Inline functions that can be used to construct complex values.
524 * The C99 standard intends x+I*y to be used for this, but x+I*y is
525 * currently unusable in general since gcc introduces many overflow,
526 * underflow, sign and efficiency bugs by rewriting I*y as
527 * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
528 * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
531 * The C11 standard introduced the macros CMPLX(), CMPLXF() and CMPLXL()
532 * to construct complex values. Compilers that conform to the C99
533 * standard require the following functions to avoid the above issues.
537 static __inline float complex
538 CMPLXF(float x, float y)
549 static __inline double complex
550 CMPLX(double x, double y)
561 static __inline long double complex
562 CMPLXL(long double x, long double y)
564 long_double_complex z;
572 #endif /* _COMPLEX_H */
574 #ifdef __GNUCLIKE_ASM
576 /* Asm versions of some functions. */
584 asm("cvtsd2si %1,%0" : "=r" (n) : "x" (x));
587 #define HAVE_EFFICIENT_IRINT
596 asm("fistl %0" : "=m" (n) : "t" (x));
599 #define HAVE_EFFICIENT_IRINT
602 #if defined(__amd64__) || defined(__i386__)
604 irintl(long double x)
608 asm("fistl %0" : "=m" (n) : "t" (x));
611 #define HAVE_EFFICIENT_IRINTL
614 #endif /* __GNUCLIKE_ASM */
617 #if defined(__amd64__) || defined(__i386__)
618 #define breakpoint() asm("int $3")
622 #define breakpoint() raise(SIGTRAP)
626 /* Write a pari script to test things externally. */
630 #ifndef DOPRINT_SWIZZLE
631 #define DOPRINT_SWIZZLE 0
636 #define DOPRINT_START(xp) do { \
640 /* Hack to give more-problematic args. */ \
641 EXTRACT_LDBL80_WORDS(__hx, __lx, *xp); \
642 __lx ^= DOPRINT_SWIZZLE; \
643 INSERT_LDBL80_WORDS(*xp, __hx, __lx); \
644 printf("x = %.21Lg; ", (long double)*xp); \
646 #define DOPRINT_END1(v) \
647 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
648 #define DOPRINT_END2(hi, lo) \
649 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
650 (long double)(hi), (long double)(lo))
652 #elif defined(DOPRINT_D64)
654 #define DOPRINT_START(xp) do { \
655 uint32_t __hx, __lx; \
657 EXTRACT_WORDS(__hx, __lx, *xp); \
658 __lx ^= DOPRINT_SWIZZLE; \
659 INSERT_WORDS(*xp, __hx, __lx); \
660 printf("x = %.21Lg; ", (long double)*xp); \
662 #define DOPRINT_END1(v) \
663 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
664 #define DOPRINT_END2(hi, lo) \
665 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
666 (long double)(hi), (long double)(lo))
668 #elif defined(DOPRINT_F32)
670 #define DOPRINT_START(xp) do { \
673 GET_FLOAT_WORD(__hx, *xp); \
674 __hx ^= DOPRINT_SWIZZLE; \
675 SET_FLOAT_WORD(*xp, __hx); \
676 printf("x = %.21Lg; ", (long double)*xp); \
678 #define DOPRINT_END1(v) \
679 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
680 #define DOPRINT_END2(hi, lo) \
681 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
682 (long double)(hi), (long double)(lo))
684 #else /* !DOPRINT_LD80 && !DOPRINT_D64 (LD128 only) */
686 #ifndef DOPRINT_SWIZZLE_HIGH
687 #define DOPRINT_SWIZZLE_HIGH 0
690 #define DOPRINT_START(xp) do { \
691 uint64_t __lx, __llx; \
694 EXTRACT_LDBL128_WORDS(__hx, __lx, __llx, *xp); \
695 __llx ^= DOPRINT_SWIZZLE; \
696 __lx ^= DOPRINT_SWIZZLE_HIGH; \
697 INSERT_LDBL128_WORDS(*xp, __hx, __lx, __llx); \
698 printf("x = %.36Lg; ", (long double)*xp); \
700 #define DOPRINT_END1(v) \
701 printf("y = %.36Lg; z = 0; show(x, y, z);\n", (long double)(v))
702 #define DOPRINT_END2(hi, lo) \
703 printf("y = %.36Lg; z = %.36Lg; show(x, y, z);\n", \
704 (long double)(hi), (long double)(lo))
706 #endif /* DOPRINT_LD80 */
709 #define DOPRINT_START(xp)
710 #define DOPRINT_END1(v)
711 #define DOPRINT_END2(hi, lo)
714 #define RETURNP(x) do { \
718 #define RETURNPI(x) do { \
722 #define RETURN2P(x, y) do { \
723 DOPRINT_END2((x), (y)); \
724 RETURNF((x) + (y)); \
726 #define RETURN2PI(x, y) do { \
727 DOPRINT_END2((x), (y)); \
728 RETURNI((x) + (y)); \
731 #define RETURNSP(rp) do { \
734 RETURN2P((rp)->hi, (rp)->lo); \
736 #define RETURNSPI(rp) do { \
738 RETURNPI((rp)->hi); \
739 RETURN2PI((rp)->hi, (rp)->lo); \
742 #define SUM2P(x, y) ({ \
743 const __typeof (x) __x = (x); \
744 const __typeof (y) __y = (y); \
746 DOPRINT_END2(__x, __y); \
751 * ieee style elementary functions
753 * We rename functions here to improve other sources' diffability
756 #define __ieee754_sqrt sqrt
757 #define __ieee754_acos acos
758 #define __ieee754_acosh acosh
759 #define __ieee754_log log
760 #define __ieee754_log2 log2
761 #define __ieee754_atanh atanh
762 #define __ieee754_asin asin
763 #define __ieee754_atan2 atan2
764 #define __ieee754_exp exp
765 #define __ieee754_cosh cosh
766 #define __ieee754_fmod fmod
767 #define __ieee754_pow pow
768 #define __ieee754_lgamma lgamma
769 #define __ieee754_gamma gamma
770 #define __ieee754_lgamma_r lgamma_r
771 #define __ieee754_gamma_r gamma_r
772 #define __ieee754_log10 log10
773 #define __ieee754_sinh sinh
774 #define __ieee754_hypot hypot
775 #define __ieee754_j0 j0
776 #define __ieee754_j1 j1
777 #define __ieee754_y0 y0
778 #define __ieee754_y1 y1
779 #define __ieee754_jn jn
780 #define __ieee754_yn yn
781 #define __ieee754_remainder remainder
782 #define __ieee754_scalb scalb
783 #define __ieee754_sqrtf sqrtf
784 #define __ieee754_acosf acosf
785 #define __ieee754_acoshf acoshf
786 #define __ieee754_logf logf
787 #define __ieee754_atanhf atanhf
788 #define __ieee754_asinf asinf
789 #define __ieee754_atan2f atan2f
790 #define __ieee754_expf expf
791 #define __ieee754_coshf coshf
792 #define __ieee754_fmodf fmodf
793 #define __ieee754_powf powf
794 #define __ieee754_lgammaf lgammaf
795 #define __ieee754_gammaf gammaf
796 #define __ieee754_lgammaf_r lgammaf_r
797 #define __ieee754_gammaf_r gammaf_r
798 #define __ieee754_log10f log10f
799 #define __ieee754_log2f log2f
800 #define __ieee754_sinhf sinhf
801 #define __ieee754_hypotf hypotf
802 #define __ieee754_j0f j0f
803 #define __ieee754_j1f j1f
804 #define __ieee754_y0f y0f
805 #define __ieee754_y1f y1f
806 #define __ieee754_jnf jnf
807 #define __ieee754_ynf ynf
808 #define __ieee754_remainderf remainderf
809 #define __ieee754_scalbf scalbf
811 /* fdlibm kernel function */
812 int __kernel_rem_pio2(double*,double*,int,int,int);
814 /* double precision kernel functions */
815 #ifndef INLINE_REM_PIO2
816 int __ieee754_rem_pio2(double,double*);
818 double __kernel_sin(double,double,int);
819 double __kernel_cos(double,double);
820 double __kernel_tan(double,double,int);
821 double __ldexp_exp(double,int);
823 double complex __ldexp_cexp(double complex,int);
826 /* float precision kernel functions */
827 #ifndef INLINE_REM_PIO2F
828 int __ieee754_rem_pio2f(float,double*);
830 #ifndef INLINE_KERNEL_SINDF
831 float __kernel_sindf(double);
833 #ifndef INLINE_KERNEL_COSDF
834 float __kernel_cosdf(double);
836 #ifndef INLINE_KERNEL_TANDF
837 float __kernel_tandf(double,int);
839 float __ldexp_expf(float,int);
841 float complex __ldexp_cexpf(float complex,int);
844 /* long double precision kernel functions */
845 long double __kernel_sinl(long double, long double, int);
846 long double __kernel_cosl(long double, long double);
847 long double __kernel_tanl(long double, long double, int);
849 long double __p1evll(long double, void *, int);
850 long double __polevll(long double, void *, int);
852 #endif /* !_MATH_PRIVATE_H_ */