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>
22 #if HAVE_MACHINE_ENDIAN_H
23 #include <machine/endian.h>
24 #elif HAVE_SYS_ENDIAN_H && HOST_ANDROID
25 /* Android unified headers don't have machine/endian.h */
26 #include <sys/endian.h>
30 * The original fdlibm code used statements like:
31 * n0 = ((*(int*)&one)>>29)^1; * index of high word *
32 * ix0 = *(n0+(int*)&x); * high word of x *
33 * ix1 = *((1-n0)+(int*)&x); * low word of x *
34 * to dig two 32 bit words out of the 64 bit IEEE floating point
35 * value. That is non-ANSI, and, moreover, the gcc instruction
36 * scheduler gets it wrong. We instead use the following macros.
37 * Unlike the original code, we determine the endianness at compile
38 * time, not at run time; I don't see much benefit to selecting
39 * endianness at run time.
43 * A union which permits us to convert between a double and two 32 bit
48 #if defined(__VFP_FP__) || defined(__ARM_EABI__)
49 #define IEEE_WORD_ORDER BYTE_ORDER
51 #define IEEE_WORD_ORDER BIG_ENDIAN
54 #define IEEE_WORD_ORDER BYTE_ORDER
57 #if IEEE_WORD_ORDER == BIG_ENDIAN
71 } ieee_double_shape_type;
75 #if IEEE_WORD_ORDER == LITTLE_ENDIAN
89 } ieee_double_shape_type;
93 /* Get two 32 bit ints from a double. */
95 #define EXTRACT_WORDS(ix0,ix1,d) \
97 ieee_double_shape_type ew_u; \
99 (ix0) = ew_u.parts.msw; \
100 (ix1) = ew_u.parts.lsw; \
103 /* Get a 64-bit int from a double. */
104 #define EXTRACT_WORD64(ix,d) \
106 ieee_double_shape_type ew_u; \
108 (ix) = ew_u.xparts.w; \
111 /* Get the more significant 32 bit int from a double. */
113 #define GET_HIGH_WORD(i,d) \
115 ieee_double_shape_type gh_u; \
117 (i) = gh_u.parts.msw; \
120 /* Get the less significant 32 bit int from a double. */
122 #define GET_LOW_WORD(i,d) \
124 ieee_double_shape_type gl_u; \
126 (i) = gl_u.parts.lsw; \
129 /* Set a double from two 32 bit ints. */
131 #define INSERT_WORDS(d,ix0,ix1) \
133 ieee_double_shape_type iw_u; \
134 iw_u.parts.msw = (ix0); \
135 iw_u.parts.lsw = (ix1); \
139 /* Set a double from a 64-bit int. */
140 #define INSERT_WORD64(d,ix) \
142 ieee_double_shape_type iw_u; \
143 iw_u.xparts.w = (ix); \
147 /* Set the more significant 32 bits of a double from an int. */
149 #define SET_HIGH_WORD(d,v) \
151 ieee_double_shape_type sh_u; \
153 sh_u.parts.msw = (v); \
157 /* Set the less significant 32 bits of a double from an int. */
159 #define SET_LOW_WORD(d,v) \
161 ieee_double_shape_type sl_u; \
163 sl_u.parts.lsw = (v); \
168 * A union which permits us to convert between a float and a 32 bit
175 /* FIXME: Assumes 32 bit int. */
177 } ieee_float_shape_type;
179 /* Get a 32 bit int from a float. */
181 #define GET_FLOAT_WORD(i,d) \
183 ieee_float_shape_type gf_u; \
188 /* Set a float from a 32 bit int. */
190 #define SET_FLOAT_WORD(d,i) \
192 ieee_float_shape_type sf_u; \
198 * Get expsign and mantissa as 16 bit and 64 bit ints from an 80 bit long
202 #define EXTRACT_LDBL80_WORDS(ix0,ix1,d) \
204 union IEEEl2bits ew_u; \
206 (ix0) = ew_u.xbits.expsign; \
207 (ix1) = ew_u.xbits.man; \
211 * Get expsign and mantissa as one 16 bit and two 64 bit ints from a 128 bit
215 #define EXTRACT_LDBL128_WORDS(ix0,ix1,ix2,d) \
217 union IEEEl2bits ew_u; \
219 (ix0) = ew_u.xbits.expsign; \
220 (ix1) = ew_u.xbits.manh; \
221 (ix2) = ew_u.xbits.manl; \
224 /* Get expsign as a 16 bit int from a long double. */
226 #define GET_LDBL_EXPSIGN(i,d) \
228 union IEEEl2bits ge_u; \
230 (i) = ge_u.xbits.expsign; \
234 * Set an 80 bit long double from a 16 bit int expsign and a 64 bit int
238 #define INSERT_LDBL80_WORDS(d,ix0,ix1) \
240 union IEEEl2bits iw_u; \
241 iw_u.xbits.expsign = (ix0); \
242 iw_u.xbits.man = (ix1); \
247 * Set a 128 bit long double from a 16 bit int expsign and two 64 bit ints
248 * comprising the mantissa.
251 #define INSERT_LDBL128_WORDS(d,ix0,ix1,ix2) \
253 union IEEEl2bits iw_u; \
254 iw_u.xbits.expsign = (ix0); \
255 iw_u.xbits.manh = (ix1); \
256 iw_u.xbits.manl = (ix2); \
260 /* Set expsign of a long double from a 16 bit int. */
262 #define SET_LDBL_EXPSIGN(d,v) \
264 union IEEEl2bits se_u; \
266 se_u.xbits.expsign = (v); \
271 /* Long double constants are broken on i386. */
272 #define LD80C(m, ex, v) { \
273 .xbits.man = __CONCAT(m, ULL), \
274 .xbits.expsign = (0x3fff + (ex)) | ((v) < 0 ? 0x8000 : 0), \
277 /* The above works on non-i386 too, but we use this to check v. */
278 #define LD80C(m, ex, v) { .e = (v), }
281 #ifdef FLT_EVAL_METHOD
283 * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
285 #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
286 #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
288 #define STRICT_ASSIGN(type, lval, rval) do { \
289 volatile type __lval; \
291 if (sizeof(type) >= sizeof(long double)) \
299 #endif /* FLT_EVAL_METHOD */
301 /* Support switching the mode to FP_PE if necessary. */
302 #if defined(__i386__) && !defined(NO_FPSETPREC)
304 long double __retval; \
307 if ((__oprec = fpgetprec()) != FP_PE) \
309 #define RETURNI(x) do { \
311 if (__oprec != FP_PE) \
312 fpsetprec(__oprec); \
317 #define RETURNI(x) RETURNF(x)
320 /* Default return statement if hack*_t() is not used. */
321 #define RETURNF(v) return (v)
324 * 2sum gives the same result as 2sumF without requiring |a| >= |b| or
325 * a == 0, but is slower.
327 #define _2sum(a, b) do { \
328 __typeof(a) __s, __w; \
332 (b) = ((a) - (__w - __s)) + ((b) - __s); \
339 * "Normalize" the terms in the infinite-precision expression a + b for
340 * the sum of 2 floating point values so that b is as small as possible
341 * relative to 'a'. (The resulting 'a' is the value of the expression in
342 * the same precision as 'a' and the resulting b is the rounding error.)
343 * |a| must be >= |b| or 0, b's type must be no larger than 'a's type, and
344 * exponent overflow or underflow must not occur. This uses a Theorem of
345 * Dekker (1971). See Knuth (1981) 4.2.2 Theorem C. The name "TwoSum"
346 * is apparently due to Skewchuk (1997).
348 * For this to always work, assignment of a + b to 'a' must not retain any
349 * extra precision in a + b. This is required by C standards but broken
350 * in many compilers. The brokenness cannot be worked around using
351 * STRICT_ASSIGN() like we do elsewhere, since the efficiency of this
352 * algorithm would be destroyed by non-null strict assignments. (The
353 * compilers are correct to be broken -- the efficiency of all floating
354 * point code calculations would be destroyed similarly if they forced the
357 * Fortunately, a case that works well can usually be arranged by building
358 * any extra precision into the type of 'a' -- 'a' should have type float_t,
359 * double_t or long double. b's type should be no larger than 'a's type.
360 * Callers should use these types with scopes as large as possible, to
361 * reduce their own extra-precision and efficiciency problems. In
362 * particular, they shouldn't convert back and forth just to call here.
365 #define _2sumF(a, b) do { \
367 volatile __typeof(a) __ia, __ib, __r, __vw; \
371 assert(__ia == 0 || fabsl(__ia) >= fabsl(__ib)); \
374 (b) = ((a) - __w) + (b); \
377 /* The next 2 assertions are weak if (a) is already long double. */ \
378 assert((long double)__ia + __ib == (long double)(a) + (b)); \
379 __vw = __ia + __ib; \
382 assert(__vw == (a) && __r == (b)); \
385 #define _2sumF(a, b) do { \
389 (b) = ((a) - __w) + (b); \
395 * Set x += c, where x is represented in extra precision as a + b.
396 * x must be sufficiently normalized and sufficiently larger than c,
397 * and the result is then sufficiently normalized.
399 * The details of ordering are that |a| must be >= |c| (so that (a, c)
400 * can be normalized without extra work to swap 'a' with c). The details of
401 * the normalization are that b must be small relative to the normalized 'a'.
402 * Normalization of (a, c) makes the normalized c tiny relative to the
403 * normalized a, so b remains small relative to 'a' in the result. However,
404 * b need not ever be tiny relative to 'a'. For example, b might be about
405 * 2**20 times smaller than 'a' to give about 20 extra bits of precision.
406 * That is usually enough, and adding c (which by normalization is about
407 * 2**53 times smaller than a) cannot change b significantly. However,
408 * cancellation of 'a' with c in normalization of (a, c) may reduce 'a'
409 * significantly relative to b. The caller must ensure that significant
410 * cancellation doesn't occur, either by having c of the same sign as 'a',
411 * or by having |c| a few percent smaller than |a|. Pre-normalization of
414 * This is is a variant of an algorithm of Kahan (see Knuth (1981) 4.2.2
415 * exercise 19). We gain considerable efficiency by requiring the terms to
416 * be sufficiently normalized and sufficiently increasing.
418 #define _3sumF(a, b, c) do { \
422 _2sumF(__tmp, (a)); \
428 * Common routine to process the arguments to nan(), nanf(), and nanl().
430 void _scan_nan(uint32_t *__words, int __num_words, const char *__s);
435 * C99 specifies that complex numbers have the same representation as
436 * an array of two elements, where the first element is the real part
437 * and the second element is the imaginary part.
448 long double complex f;
450 } long_double_complex;
451 #define REALPART(z) ((z).a[0])
452 #define IMAGPART(z) ((z).a[1])
455 * Inline functions that can be used to construct complex values.
457 * The C99 standard intends x+I*y to be used for this, but x+I*y is
458 * currently unusable in general since gcc introduces many overflow,
459 * underflow, sign and efficiency bugs by rewriting I*y as
460 * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
461 * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
464 static __inline float complex
465 cpackf(float x, float y)
474 static __inline double complex
475 cpack(double x, double y)
484 static __inline long double complex
485 cpackl(long double x, long double y)
487 long_double_complex z;
493 #endif /* _COMPLEX_H */
495 #ifdef __GNUCLIKE_ASM
497 /* Asm versions of some functions. */
505 asm("cvtsd2si %1,%0" : "=r" (n) : "x" (x));
508 #define HAVE_EFFICIENT_IRINT
517 asm("fistl %0" : "=m" (n) : "t" (x));
520 #define HAVE_EFFICIENT_IRINT
523 #if defined(__amd64__) || defined(__i386__)
525 irintl(long double x)
529 asm("fistl %0" : "=m" (n) : "t" (x));
532 #define HAVE_EFFICIENT_IRINTL
535 #endif /* __GNUCLIKE_ASM */
538 #if defined(__amd64__) || defined(__i386__)
539 #define breakpoint() asm("int $3")
543 #define breakpoint() raise(SIGTRAP)
547 /* Write a pari script to test things externally. */
551 #ifndef DOPRINT_SWIZZLE
552 #define DOPRINT_SWIZZLE 0
557 #define DOPRINT_START(xp) do { \
561 /* Hack to give more-problematic args. */ \
562 EXTRACT_LDBL80_WORDS(__hx, __lx, *xp); \
563 __lx ^= DOPRINT_SWIZZLE; \
564 INSERT_LDBL80_WORDS(*xp, __hx, __lx); \
565 printf("x = %.21Lg; ", (long double)*xp); \
567 #define DOPRINT_END1(v) \
568 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
569 #define DOPRINT_END2(hi, lo) \
570 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
571 (long double)(hi), (long double)(lo))
573 #elif defined(DOPRINT_D64)
575 #define DOPRINT_START(xp) do { \
576 uint32_t __hx, __lx; \
578 EXTRACT_WORDS(__hx, __lx, *xp); \
579 __lx ^= DOPRINT_SWIZZLE; \
580 INSERT_WORDS(*xp, __hx, __lx); \
581 printf("x = %.21Lg; ", (long double)*xp); \
583 #define DOPRINT_END1(v) \
584 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
585 #define DOPRINT_END2(hi, lo) \
586 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
587 (long double)(hi), (long double)(lo))
589 #elif defined(DOPRINT_F32)
591 #define DOPRINT_START(xp) do { \
594 GET_FLOAT_WORD(__hx, *xp); \
595 __hx ^= DOPRINT_SWIZZLE; \
596 SET_FLOAT_WORD(*xp, __hx); \
597 printf("x = %.21Lg; ", (long double)*xp); \
599 #define DOPRINT_END1(v) \
600 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
601 #define DOPRINT_END2(hi, lo) \
602 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
603 (long double)(hi), (long double)(lo))
605 #else /* !DOPRINT_LD80 && !DOPRINT_D64 (LD128 only) */
607 #ifndef DOPRINT_SWIZZLE_HIGH
608 #define DOPRINT_SWIZZLE_HIGH 0
611 #define DOPRINT_START(xp) do { \
612 uint64_t __lx, __llx; \
615 EXTRACT_LDBL128_WORDS(__hx, __lx, __llx, *xp); \
616 __llx ^= DOPRINT_SWIZZLE; \
617 __lx ^= DOPRINT_SWIZZLE_HIGH; \
618 INSERT_LDBL128_WORDS(*xp, __hx, __lx, __llx); \
619 printf("x = %.36Lg; ", (long double)*xp); \
621 #define DOPRINT_END1(v) \
622 printf("y = %.36Lg; z = 0; show(x, y, z);\n", (long double)(v))
623 #define DOPRINT_END2(hi, lo) \
624 printf("y = %.36Lg; z = %.36Lg; show(x, y, z);\n", \
625 (long double)(hi), (long double)(lo))
627 #endif /* DOPRINT_LD80 */
630 #define DOPRINT_START(xp)
631 #define DOPRINT_END1(v)
632 #define DOPRINT_END2(hi, lo)
635 #define RETURNP(x) do { \
639 #define RETURNPI(x) do { \
643 #define RETURN2P(x, y) do { \
644 DOPRINT_END2((x), (y)); \
645 RETURNF((x) + (y)); \
647 #define RETURN2PI(x, y) do { \
648 DOPRINT_END2((x), (y)); \
649 RETURNI((x) + (y)); \
652 #define RETURNSP(rp) do { \
655 RETURN2P((rp)->hi, (rp)->lo); \
657 #define RETURNSPI(rp) do { \
659 RETURNPI((rp)->hi); \
660 RETURN2PI((rp)->hi, (rp)->lo); \
663 #define SUM2P(x, y) ({ \
664 const __typeof (x) __x = (x); \
665 const __typeof (y) __y = (y); \
667 DOPRINT_END2(__x, __y); \
672 * ieee style elementary functions
674 * We rename functions here to improve other sources' diffability
677 #define __ieee754_sqrt sqrt
678 #define __ieee754_acos acos
679 #define __ieee754_acosh acosh
680 #define __ieee754_log log
681 #define __ieee754_log2 log2
682 #define __ieee754_atanh atanh
683 #define __ieee754_asin asin
684 #define __ieee754_atan2 atan2
685 #define __ieee754_exp exp
686 #define __ieee754_cosh cosh
687 #define __ieee754_fmod fmod
688 #define __ieee754_pow pow
689 #define __ieee754_lgamma lgamma
690 #define __ieee754_gamma gamma
691 #define __ieee754_lgamma_r lgamma_r
692 #define __ieee754_gamma_r gamma_r
693 #define __ieee754_log10 log10
694 #define __ieee754_sinh sinh
695 #define __ieee754_hypot hypot
696 #define __ieee754_j0 j0
697 #define __ieee754_j1 j1
698 #define __ieee754_y0 y0
699 #define __ieee754_y1 y1
700 #define __ieee754_jn jn
701 #define __ieee754_yn yn
702 #define __ieee754_remainder remainder
703 #define __ieee754_scalb scalb
704 #define __ieee754_sqrtf sqrtf
705 #define __ieee754_acosf acosf
706 #define __ieee754_acoshf acoshf
707 #define __ieee754_logf logf
708 #define __ieee754_atanhf atanhf
709 #define __ieee754_asinf asinf
710 #define __ieee754_atan2f atan2f
711 #define __ieee754_expf expf
712 #define __ieee754_coshf coshf
713 #define __ieee754_fmodf fmodf
714 #define __ieee754_powf powf
715 #define __ieee754_lgammaf lgammaf
716 #define __ieee754_gammaf gammaf
717 #define __ieee754_lgammaf_r lgammaf_r
718 #define __ieee754_gammaf_r gammaf_r
719 #define __ieee754_log10f log10f
720 #define __ieee754_log2f log2f
721 #define __ieee754_sinhf sinhf
722 #define __ieee754_hypotf hypotf
723 #define __ieee754_j0f j0f
724 #define __ieee754_j1f j1f
725 #define __ieee754_y0f y0f
726 #define __ieee754_y1f y1f
727 #define __ieee754_jnf jnf
728 #define __ieee754_ynf ynf
729 #define __ieee754_remainderf remainderf
730 #define __ieee754_scalbf scalbf
732 /* fdlibm kernel function */
733 int __kernel_rem_pio2(double*,double*,int,int,int);
735 /* double precision kernel functions */
736 #ifndef INLINE_REM_PIO2
737 int __ieee754_rem_pio2(double,double*);
739 double __kernel_sin(double,double,int);
740 double __kernel_cos(double,double);
741 double __kernel_tan(double,double,int);
742 double __ldexp_exp(double,int);
744 double complex __ldexp_cexp(double complex,int);
747 /* float precision kernel functions */
748 #ifndef INLINE_REM_PIO2F
749 int __ieee754_rem_pio2f(float,double*);
751 #ifndef INLINE_KERNEL_SINDF
752 float __kernel_sindf(double);
754 #ifndef INLINE_KERNEL_COSDF
755 float __kernel_cosdf(double);
757 #ifndef INLINE_KERNEL_TANDF
758 float __kernel_tandf(double,int);
760 float __ldexp_expf(float,int);
762 float complex __ldexp_cexpf(float complex,int);
765 /* long double precision kernel functions */
766 long double __kernel_sinl(long double, long double, int);
767 long double __kernel_cosl(long double, long double);
768 long double __kernel_tanl(long double, long double, int);
770 #endif /* !_MATH_PRIVATE_H_ */