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
14 * $FreeBSD: head/lib/msun/src/math_private.h 251292 2013-06-03 09:14:31Z das $
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
41 #define IEEE_WORD_ORDER BYTE_ORDER
43 #if IEEE_WORD_ORDER == BIG_ENDIAN
57 } ieee_double_shape_type;
61 #if IEEE_WORD_ORDER == LITTLE_ENDIAN
75 } ieee_double_shape_type;
79 /* Get two 32 bit ints from a double. */
81 #define EXTRACT_WORDS(ix0,ix1,d) \
83 ieee_double_shape_type ew_u; \
85 (ix0) = ew_u.parts.msw; \
86 (ix1) = ew_u.parts.lsw; \
89 /* Get a 64-bit int from a double. */
90 #define EXTRACT_WORD64(ix,d) \
92 ieee_double_shape_type ew_u; \
94 (ix) = ew_u.xparts.w; \
97 /* Get the more significant 32 bit int from a double. */
99 #define GET_HIGH_WORD(i,d) \
101 ieee_double_shape_type gh_u; \
103 (i) = gh_u.parts.msw; \
106 /* Get the less significant 32 bit int from a double. */
108 #define GET_LOW_WORD(i,d) \
110 ieee_double_shape_type gl_u; \
112 (i) = gl_u.parts.lsw; \
115 /* Set a double from two 32 bit ints. */
117 #define INSERT_WORDS(d,ix0,ix1) \
119 ieee_double_shape_type iw_u; \
120 iw_u.parts.msw = (ix0); \
121 iw_u.parts.lsw = (ix1); \
125 /* Set a double from a 64-bit int. */
126 #define INSERT_WORD64(d,ix) \
128 ieee_double_shape_type iw_u; \
129 iw_u.xparts.w = (ix); \
133 /* Set the more significant 32 bits of a double from an int. */
135 #define SET_HIGH_WORD(d,v) \
137 ieee_double_shape_type sh_u; \
139 sh_u.parts.msw = (v); \
143 /* Set the less significant 32 bits of a double from an int. */
145 #define SET_LOW_WORD(d,v) \
147 ieee_double_shape_type sl_u; \
149 sl_u.parts.lsw = (v); \
154 * A union which permits us to convert between a float and a 32 bit
161 /* FIXME: Assumes 32 bit int. */
163 } ieee_float_shape_type;
165 /* Get a 32 bit int from a float. */
167 #define GET_FLOAT_WORD(i,d) \
169 ieee_float_shape_type gf_u; \
174 /* Set a float from a 32 bit int. */
176 #define SET_FLOAT_WORD(d,i) \
178 ieee_float_shape_type sf_u; \
184 * Get expsign and mantissa as 16 bit and 64 bit ints from an 80 bit long
188 #define EXTRACT_LDBL80_WORDS(ix0,ix1,d) \
190 union IEEEl2bits ew_u; \
192 (ix0) = ew_u.xbits.expsign; \
193 (ix1) = ew_u.xbits.man; \
197 * Get expsign and mantissa as one 16 bit and two 64 bit ints from a 128 bit
201 #define EXTRACT_LDBL128_WORDS(ix0,ix1,ix2,d) \
203 union IEEEl2bits ew_u; \
205 (ix0) = ew_u.xbits.expsign; \
206 (ix1) = ew_u.xbits.manh; \
207 (ix2) = ew_u.xbits.manl; \
210 /* Get expsign as a 16 bit int from a long double. */
212 #define GET_LDBL_EXPSIGN(i,d) \
214 union IEEEl2bits ge_u; \
216 (i) = ge_u.xbits.expsign; \
220 * Set an 80 bit long double from a 16 bit int expsign and a 64 bit int
224 #define INSERT_LDBL80_WORDS(d,ix0,ix1) \
226 union IEEEl2bits iw_u; \
227 iw_u.xbits.expsign = (ix0); \
228 iw_u.xbits.man = (ix1); \
233 * Set a 128 bit long double from a 16 bit int expsign and two 64 bit ints
234 * comprising the mantissa.
237 #define INSERT_LDBL128_WORDS(d,ix0,ix1,ix2) \
239 union IEEEl2bits iw_u; \
240 iw_u.xbits.expsign = (ix0); \
241 iw_u.xbits.manh = (ix1); \
242 iw_u.xbits.manl = (ix2); \
246 /* Set expsign of a long double from a 16 bit int. */
248 #define SET_LDBL_EXPSIGN(d,v) \
250 union IEEEl2bits se_u; \
252 se_u.xbits.expsign = (v); \
257 /* Long double constants are broken on i386. */
258 #define LD80C(m, ex, v) { \
259 .xbits.man = __CONCAT(m, ULL), \
260 .xbits.expsign = (0x3fff + (ex)) | ((v) < 0 ? 0x8000 : 0), \
263 /* The above works on non-i386 too, but we use this to check v. */
264 #define LD80C(m, ex, v) { .e = (v), }
267 #ifdef FLT_EVAL_METHOD
269 * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
271 #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
272 #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
274 #define STRICT_ASSIGN(type, lval, rval) do { \
275 volatile type __lval; \
277 if (sizeof(type) >= sizeof(long double)) \
285 #endif /* FLT_EVAL_METHOD */
287 /* Support switching the mode to FP_PE if necessary. */
288 #if defined(__i386__) && !defined(NO_FPSETPREC)
290 long double __retval; \
293 if ((__oprec = fpgetprec()) != FP_PE) \
295 #define RETURNI(x) do { \
297 if (__oprec != FP_PE) \
298 fpsetprec(__oprec); \
303 #define RETURNI(x) RETURNF(x)
306 /* Default return statement if hack*_t() is not used. */
307 #define RETURNF(v) return (v)
310 * 2sum gives the same result as 2sumF without requiring |a| >= |b| or
311 * a == 0, but is slower.
313 #define _2sum(a, b) do { \
314 __typeof(a) __s, __w; \
318 (b) = ((a) - (__w - __s)) + ((b) - __s); \
325 * "Normalize" the terms in the infinite-precision expression a + b for
326 * the sum of 2 floating point values so that b is as small as possible
327 * relative to 'a'. (The resulting 'a' is the value of the expression in
328 * the same precision as 'a' and the resulting b is the rounding error.)
329 * |a| must be >= |b| or 0, b's type must be no larger than 'a's type, and
330 * exponent overflow or underflow must not occur. This uses a Theorem of
331 * Dekker (1971). See Knuth (1981) 4.2.2 Theorem C. The name "TwoSum"
332 * is apparently due to Skewchuk (1997).
334 * For this to always work, assignment of a + b to 'a' must not retain any
335 * extra precision in a + b. This is required by C standards but broken
336 * in many compilers. The brokenness cannot be worked around using
337 * STRICT_ASSIGN() like we do elsewhere, since the efficiency of this
338 * algorithm would be destroyed by non-null strict assignments. (The
339 * compilers are correct to be broken -- the efficiency of all floating
340 * point code calculations would be destroyed similarly if they forced the
343 * Fortunately, a case that works well can usually be arranged by building
344 * any extra precision into the type of 'a' -- 'a' should have type float_t,
345 * double_t or long double. b's type should be no larger than 'a's type.
346 * Callers should use these types with scopes as large as possible, to
347 * reduce their own extra-precision and efficiciency problems. In
348 * particular, they shouldn't convert back and forth just to call here.
351 #define _2sumF(a, b) do { \
353 volatile __typeof(a) __ia, __ib, __r, __vw; \
357 assert(__ia == 0 || fabsl(__ia) >= fabsl(__ib)); \
360 (b) = ((a) - __w) + (b); \
363 /* The next 2 assertions are weak if (a) is already long double. */ \
364 assert((long double)__ia + __ib == (long double)(a) + (b)); \
365 __vw = __ia + __ib; \
368 assert(__vw == (a) && __r == (b)); \
371 #define _2sumF(a, b) do { \
375 (b) = ((a) - __w) + (b); \
381 * Set x += c, where x is represented in extra precision as a + b.
382 * x must be sufficiently normalized and sufficiently larger than c,
383 * and the result is then sufficiently normalized.
385 * The details of ordering are that |a| must be >= |c| (so that (a, c)
386 * can be normalized without extra work to swap 'a' with c). The details of
387 * the normalization are that b must be small relative to the normalized 'a'.
388 * Normalization of (a, c) makes the normalized c tiny relative to the
389 * normalized a, so b remains small relative to 'a' in the result. However,
390 * b need not ever be tiny relative to 'a'. For example, b might be about
391 * 2**20 times smaller than 'a' to give about 20 extra bits of precision.
392 * That is usually enough, and adding c (which by normalization is about
393 * 2**53 times smaller than a) cannot change b significantly. However,
394 * cancellation of 'a' with c in normalization of (a, c) may reduce 'a'
395 * significantly relative to b. The caller must ensure that significant
396 * cancellation doesn't occur, either by having c of the same sign as 'a',
397 * or by having |c| a few percent smaller than |a|. Pre-normalization of
400 * This is is a variant of an algorithm of Kahan (see Knuth (1981) 4.2.2
401 * exercise 19). We gain considerable efficiency by requiring the terms to
402 * be sufficiently normalized and sufficiently increasing.
404 #define _3sumF(a, b, c) do { \
408 _2sumF(__tmp, (a)); \
414 * Common routine to process the arguments to nan(), nanf(), and nanl().
416 void _scan_nan(uint32_t *__words, int __num_words, const char *__s);
421 * C99 specifies that complex numbers have the same representation as
422 * an array of two elements, where the first element is the real part
423 * and the second element is the imaginary part.
434 long double complex f;
436 } long_double_complex;
437 #define REALPART(z) ((z).a[0])
438 #define IMAGPART(z) ((z).a[1])
441 * Inline functions that can be used to construct complex values.
443 * The C99 standard intends x+I*y to be used for this, but x+I*y is
444 * currently unusable in general since gcc introduces many overflow,
445 * underflow, sign and efficiency bugs by rewriting I*y as
446 * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
447 * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
450 static __inline float complex
451 cpackf(float x, float y)
460 static __inline double complex
461 cpack(double x, double y)
470 static __inline long double complex
471 cpackl(long double x, long double y)
473 long_double_complex z;
479 #endif /* _COMPLEX_H */
483 /* Asm versions of some functions. */
491 asm("cvtsd2si %1,%0" : "=r" (n) : "x" (x));
494 #define HAVE_EFFICIENT_IRINT
503 asm("fistl %0" : "=m" (n) : "t" (x));
506 #define HAVE_EFFICIENT_IRINT
509 #if defined(__x86_64__) || defined(__i386__)
511 irintl(long double x)
515 asm("fistl %0" : "=m" (n) : "t" (x));
518 #define HAVE_EFFICIENT_IRINTL
521 #endif /* __GNUC__ */
524 #if defined(__amd64__) || defined(__i386__)
525 #define breakpoint() asm("int $3")
529 #define breakpoint() raise(SIGTRAP)
533 /* Write a pari script to test things externally. */
537 #ifndef DOPRINT_SWIZZLE
538 #define DOPRINT_SWIZZLE 0
543 #define DOPRINT_START(xp) do { \
547 /* Hack to give more-problematic args. */ \
548 EXTRACT_LDBL80_WORDS(__hx, __lx, *xp); \
549 __lx ^= DOPRINT_SWIZZLE; \
550 INSERT_LDBL80_WORDS(*xp, __hx, __lx); \
551 printf("x = %.21Lg; ", (long double)*xp); \
553 #define DOPRINT_END1(v) \
554 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
555 #define DOPRINT_END2(hi, lo) \
556 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
557 (long double)(hi), (long double)(lo))
559 #elif defined(DOPRINT_D64)
561 #define DOPRINT_START(xp) do { \
562 uint32_t __hx, __lx; \
564 EXTRACT_WORDS(__hx, __lx, *xp); \
565 __lx ^= DOPRINT_SWIZZLE; \
566 INSERT_WORDS(*xp, __hx, __lx); \
567 printf("x = %.21Lg; ", (long double)*xp); \
569 #define DOPRINT_END1(v) \
570 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
571 #define DOPRINT_END2(hi, lo) \
572 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
573 (long double)(hi), (long double)(lo))
575 #elif defined(DOPRINT_F32)
577 #define DOPRINT_START(xp) do { \
580 GET_FLOAT_WORD(__hx, *xp); \
581 __hx ^= DOPRINT_SWIZZLE; \
582 SET_FLOAT_WORD(*xp, __hx); \
583 printf("x = %.21Lg; ", (long double)*xp); \
585 #define DOPRINT_END1(v) \
586 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
587 #define DOPRINT_END2(hi, lo) \
588 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
589 (long double)(hi), (long double)(lo))
591 #else /* !DOPRINT_LD80 && !DOPRINT_D64 (LD128 only) */
593 #ifndef DOPRINT_SWIZZLE_HIGH
594 #define DOPRINT_SWIZZLE_HIGH 0
597 #define DOPRINT_START(xp) do { \
598 uint64_t __lx, __llx; \
601 EXTRACT_LDBL128_WORDS(__hx, __lx, __llx, *xp); \
602 __llx ^= DOPRINT_SWIZZLE; \
603 __lx ^= DOPRINT_SWIZZLE_HIGH; \
604 INSERT_LDBL128_WORDS(*xp, __hx, __lx, __llx); \
605 printf("x = %.36Lg; ", (long double)*xp); \
607 #define DOPRINT_END1(v) \
608 printf("y = %.36Lg; z = 0; show(x, y, z);\n", (long double)(v))
609 #define DOPRINT_END2(hi, lo) \
610 printf("y = %.36Lg; z = %.36Lg; show(x, y, z);\n", \
611 (long double)(hi), (long double)(lo))
613 #endif /* DOPRINT_LD80 */
616 #define DOPRINT_START(xp)
617 #define DOPRINT_END1(v)
618 #define DOPRINT_END2(hi, lo)
621 #define RETURNP(x) do { \
625 #define RETURNPI(x) do { \
629 #define RETURN2P(x, y) do { \
630 DOPRINT_END2((x), (y)); \
631 RETURNF((x) + (y)); \
633 #define RETURN2PI(x, y) do { \
634 DOPRINT_END2((x), (y)); \
635 RETURNI((x) + (y)); \
638 #define RETURNSP(rp) do { \
641 RETURN2P((rp)->hi, (rp)->lo); \
643 #define RETURNSPI(rp) do { \
645 RETURNPI((rp)->hi); \
646 RETURN2PI((rp)->hi, (rp)->lo); \
649 #define SUM2P(x, y) ({ \
650 const __typeof (x) __x = (x); \
651 const __typeof (y) __y = (y); \
653 DOPRINT_END2(__x, __y); \
658 * ieee style elementary functions
660 * We rename functions here to improve other sources' diffability
663 #define __ieee754_sqrt sqrt
664 #define __ieee754_acos acos
665 #define __ieee754_acosh acosh
666 #define __ieee754_log log
667 #define __ieee754_log2 log2
668 #define __ieee754_atanh atanh
669 #define __ieee754_asin asin
670 #define __ieee754_atan2 atan2
671 #define __ieee754_exp exp
672 #define __ieee754_cosh cosh
673 #define __ieee754_fmod fmod
674 #define __ieee754_pow pow
675 #define __ieee754_lgamma lgamma
676 #define __ieee754_gamma gamma
677 #define __ieee754_lgamma_r lgamma_r
678 #define __ieee754_gamma_r gamma_r
679 #define __ieee754_log10 log10
680 #define __ieee754_sinh sinh
681 #define __ieee754_hypot hypot
682 #define __ieee754_j0 j0
683 #define __ieee754_j1 j1
684 #define __ieee754_y0 y0
685 #define __ieee754_y1 y1
686 #define __ieee754_jn jn
687 #define __ieee754_yn yn
688 #define __ieee754_remainder remainder
689 #define __ieee754_scalb scalb
690 #define __ieee754_sqrtf sqrtf
691 #define __ieee754_acosf acosf
692 #define __ieee754_acoshf acoshf
693 #define __ieee754_logf logf
694 #define __ieee754_atanhf atanhf
695 #define __ieee754_asinf asinf
696 #define __ieee754_atan2f atan2f
697 #define __ieee754_expf expf
698 #define __ieee754_coshf coshf
699 #define __ieee754_fmodf fmodf
700 #define __ieee754_powf powf
701 #define __ieee754_lgammaf lgammaf
702 #define __ieee754_gammaf gammaf
703 #define __ieee754_lgammaf_r lgammaf_r
704 #define __ieee754_gammaf_r gammaf_r
705 #define __ieee754_log10f log10f
706 #define __ieee754_log2f log2f
707 #define __ieee754_sinhf sinhf
708 #define __ieee754_hypotf hypotf
709 #define __ieee754_j0f j0f
710 #define __ieee754_j1f j1f
711 #define __ieee754_y0f y0f
712 #define __ieee754_y1f y1f
713 #define __ieee754_jnf jnf
714 #define __ieee754_ynf ynf
715 #define __ieee754_remainderf remainderf
716 #define __ieee754_scalbf scalbf
718 /* fdlibm kernel function */
719 int __kernel_rem_pio2(double*,double*,int,int,int);
721 /* double precision kernel functions */
722 #ifndef INLINE_REM_PIO2
723 int __ieee754_rem_pio2(double,double*);
725 double __kernel_sin(double,double,int);
726 double __kernel_cos(double,double);
727 double __kernel_tan(double,double,int);
728 double __ldexp_exp(double,int);
730 double complex __ldexp_cexp(double complex,int);
733 /* float precision kernel functions */
734 #ifndef INLINE_REM_PIO2F
735 int __ieee754_rem_pio2f(float,double*);
737 #ifndef INLINE_KERNEL_SINDF
738 float __kernel_sindf(double);
740 #ifndef INLINE_KERNEL_COSDF
741 float __kernel_cosdf(double);
743 #ifndef INLINE_KERNEL_TANDF
744 float __kernel_tandf(double,int);
746 float __ldexp_expf(float,int);
748 float complex __ldexp_cexpf(float complex,int);
751 /* long double precision kernel functions */
752 long double __kernel_sinl(long double, long double, int);
753 long double __kernel_cosl(long double, long double);
754 long double __kernel_tanl(long double, long double, int);
756 #endif /* !_MATH_PRIVATE_H_ */