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
16 #ifndef _MATH_PRIVATE_H_
17 #define _MATH_PRIVATE_H_
19 #include <sys/types.h>
20 #include <machine/endian.h>
23 * The original fdlibm code used statements like:
24 * n0 = ((*(int*)&one)>>29)^1; * index of high word *
25 * ix0 = *(n0+(int*)&x); * high word of x *
26 * ix1 = *((1-n0)+(int*)&x); * low word of x *
27 * to dig two 32 bit words out of the 64 bit IEEE floating point
28 * value. That is non-ANSI, and, moreover, the gcc instruction
29 * scheduler gets it wrong. We instead use the following macros.
30 * Unlike the original code, we determine the endianness at compile
31 * time, not at run time; I don't see much benefit to selecting
32 * endianness at run time.
36 * A union which permits us to convert between a double and two 32 bit
41 #if defined(__VFP_FP__) || defined(__ARM_EABI__)
42 #define IEEE_WORD_ORDER BYTE_ORDER
44 #define IEEE_WORD_ORDER BIG_ENDIAN
47 #define IEEE_WORD_ORDER BYTE_ORDER
50 /* A union which permits us to convert between a long double and
53 #if IEEE_WORD_ORDER == BIG_ENDIAN
68 } ieee_quad_shape_type;
72 #if IEEE_WORD_ORDER == LITTLE_ENDIAN
87 } ieee_quad_shape_type;
91 #if IEEE_WORD_ORDER == BIG_ENDIAN
105 } ieee_double_shape_type;
109 #if IEEE_WORD_ORDER == LITTLE_ENDIAN
123 } ieee_double_shape_type;
127 /* Get two 32 bit ints from a double. */
129 #define EXTRACT_WORDS(ix0,ix1,d) \
131 ieee_double_shape_type ew_u; \
133 (ix0) = ew_u.parts.msw; \
134 (ix1) = ew_u.parts.lsw; \
137 /* Get a 64-bit int from a double. */
138 #define EXTRACT_WORD64(ix,d) \
140 ieee_double_shape_type ew_u; \
142 (ix) = ew_u.xparts.w; \
145 /* Get the more significant 32 bit int from a double. */
147 #define GET_HIGH_WORD(i,d) \
149 ieee_double_shape_type gh_u; \
151 (i) = gh_u.parts.msw; \
154 /* Get the less significant 32 bit int from a double. */
156 #define GET_LOW_WORD(i,d) \
158 ieee_double_shape_type gl_u; \
160 (i) = gl_u.parts.lsw; \
163 /* Set a double from two 32 bit ints. */
165 #define INSERT_WORDS(d,ix0,ix1) \
167 ieee_double_shape_type iw_u; \
168 iw_u.parts.msw = (ix0); \
169 iw_u.parts.lsw = (ix1); \
173 /* Set a double from a 64-bit int. */
174 #define INSERT_WORD64(d,ix) \
176 ieee_double_shape_type iw_u; \
177 iw_u.xparts.w = (ix); \
181 /* Set the more significant 32 bits of a double from an int. */
183 #define SET_HIGH_WORD(d,v) \
185 ieee_double_shape_type sh_u; \
187 sh_u.parts.msw = (v); \
191 /* Set the less significant 32 bits of a double from an int. */
193 #define SET_LOW_WORD(d,v) \
195 ieee_double_shape_type sl_u; \
197 sl_u.parts.lsw = (v); \
202 * A union which permits us to convert between a float and a 32 bit
209 /* FIXME: Assumes 32 bit int. */
211 } ieee_float_shape_type;
213 /* Get a 32 bit int from a float. */
215 #define GET_FLOAT_WORD(i,d) \
217 ieee_float_shape_type gf_u; \
222 /* Set a float from a 32 bit int. */
224 #define SET_FLOAT_WORD(d,i) \
226 ieee_float_shape_type sf_u; \
232 * Get expsign and mantissa as 16 bit and 64 bit ints from an 80 bit long
236 #define EXTRACT_LDBL80_WORDS(ix0,ix1,d) \
238 union IEEEl2bits ew_u; \
240 (ix0) = ew_u.xbits.expsign; \
241 (ix1) = ew_u.xbits.man; \
245 * Get expsign and mantissa as one 16 bit and two 64 bit ints from a 128 bit
249 #define EXTRACT_LDBL128_WORDS(ix0,ix1,ix2,d) \
251 union IEEEl2bits ew_u; \
253 (ix0) = ew_u.xbits.expsign; \
254 (ix1) = ew_u.xbits.manh; \
255 (ix2) = ew_u.xbits.manl; \
258 /* Get expsign as a 16 bit int from a long double. */
260 #define GET_LDBL_EXPSIGN(i,d) \
262 union IEEEl2bits ge_u; \
264 (i) = ge_u.xbits.expsign; \
268 * Set an 80 bit long double from a 16 bit int expsign and a 64 bit int
272 #define INSERT_LDBL80_WORDS(d,ix0,ix1) \
274 union IEEEl2bits iw_u; \
275 iw_u.xbits.expsign = (ix0); \
276 iw_u.xbits.man = (ix1); \
281 * Set a 128 bit long double from a 16 bit int expsign and two 64 bit ints
282 * comprising the mantissa.
285 #define INSERT_LDBL128_WORDS(d,ix0,ix1,ix2) \
287 union IEEEl2bits iw_u; \
288 iw_u.xbits.expsign = (ix0); \
289 iw_u.xbits.manh = (ix1); \
290 iw_u.xbits.manl = (ix2); \
294 /* Set expsign of a long double from a 16 bit int. */
296 #define SET_LDBL_EXPSIGN(d,v) \
298 union IEEEl2bits se_u; \
300 se_u.xbits.expsign = (v); \
305 /* Long double constants are broken on i386. */
306 #define LD80C(m, ex, v) { \
307 .xbits.man = __CONCAT(m, ULL), \
308 .xbits.expsign = (0x3fff + (ex)) | ((v) < 0 ? 0x8000 : 0), \
311 /* The above works on non-i386 too, but we use this to check v. */
312 #define LD80C(m, ex, v) { .e = (v), }
315 #ifdef FLT_EVAL_METHOD
317 * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
319 #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
320 #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
322 #define STRICT_ASSIGN(type, lval, rval) do { \
323 volatile type __lval; \
325 if (sizeof(type) >= sizeof(long double)) \
333 #endif /* FLT_EVAL_METHOD */
335 /* Support switching the mode to FP_PE if necessary. */
336 #if defined(__i386__) && !defined(NO_FPSETPREC)
337 #define ENTERI() ENTERIT(long double)
338 #define ENTERIT(returntype) \
339 returntype __retval; \
342 if ((__oprec = fpgetprec()) != FP_PE) \
344 #define RETURNI(x) do { \
346 if (__oprec != FP_PE) \
347 fpsetprec(__oprec); \
353 if ((__oprec = fpgetprec()) != FP_PE) \
355 #define RETURNV() do { \
356 if (__oprec != FP_PE) \
357 fpsetprec(__oprec); \
363 #define RETURNI(x) RETURNF(x)
365 #define RETURNV() return
368 /* Default return statement if hack*_t() is not used. */
369 #define RETURNF(v) return (v)
372 * 2sum gives the same result as 2sumF without requiring |a| >= |b| or
373 * a == 0, but is slower.
375 #define _2sum(a, b) do { \
376 __typeof(a) __s, __w; \
380 (b) = ((a) - (__w - __s)) + ((b) - __s); \
387 * "Normalize" the terms in the infinite-precision expression a + b for
388 * the sum of 2 floating point values so that b is as small as possible
389 * relative to 'a'. (The resulting 'a' is the value of the expression in
390 * the same precision as 'a' and the resulting b is the rounding error.)
391 * |a| must be >= |b| or 0, b's type must be no larger than 'a's type, and
392 * exponent overflow or underflow must not occur. This uses a Theorem of
393 * Dekker (1971). See Knuth (1981) 4.2.2 Theorem C. The name "TwoSum"
394 * is apparently due to Skewchuk (1997).
396 * For this to always work, assignment of a + b to 'a' must not retain any
397 * extra precision in a + b. This is required by C standards but broken
398 * in many compilers. The brokenness cannot be worked around using
399 * STRICT_ASSIGN() like we do elsewhere, since the efficiency of this
400 * algorithm would be destroyed by non-null strict assignments. (The
401 * compilers are correct to be broken -- the efficiency of all floating
402 * point code calculations would be destroyed similarly if they forced the
405 * Fortunately, a case that works well can usually be arranged by building
406 * any extra precision into the type of 'a' -- 'a' should have type float_t,
407 * double_t or long double. b's type should be no larger than 'a's type.
408 * Callers should use these types with scopes as large as possible, to
409 * reduce their own extra-precision and efficiciency problems. In
410 * particular, they shouldn't convert back and forth just to call here.
413 #define _2sumF(a, b) do { \
415 volatile __typeof(a) __ia, __ib, __r, __vw; \
419 assert(__ia == 0 || fabsl(__ia) >= fabsl(__ib)); \
422 (b) = ((a) - __w) + (b); \
425 /* The next 2 assertions are weak if (a) is already long double. */ \
426 assert((long double)__ia + __ib == (long double)(a) + (b)); \
427 __vw = __ia + __ib; \
430 assert(__vw == (a) && __r == (b)); \
433 #define _2sumF(a, b) do { \
437 (b) = ((a) - __w) + (b); \
443 * Set x += c, where x is represented in extra precision as a + b.
444 * x must be sufficiently normalized and sufficiently larger than c,
445 * and the result is then sufficiently normalized.
447 * The details of ordering are that |a| must be >= |c| (so that (a, c)
448 * can be normalized without extra work to swap 'a' with c). The details of
449 * the normalization are that b must be small relative to the normalized 'a'.
450 * Normalization of (a, c) makes the normalized c tiny relative to the
451 * normalized a, so b remains small relative to 'a' in the result. However,
452 * b need not ever be tiny relative to 'a'. For example, b might be about
453 * 2**20 times smaller than 'a' to give about 20 extra bits of precision.
454 * That is usually enough, and adding c (which by normalization is about
455 * 2**53 times smaller than a) cannot change b significantly. However,
456 * cancellation of 'a' with c in normalization of (a, c) may reduce 'a'
457 * significantly relative to b. The caller must ensure that significant
458 * cancellation doesn't occur, either by having c of the same sign as 'a',
459 * or by having |c| a few percent smaller than |a|. Pre-normalization of
462 * This is a variant of an algorithm of Kahan (see Knuth (1981) 4.2.2
463 * exercise 19). We gain considerable efficiency by requiring the terms to
464 * be sufficiently normalized and sufficiently increasing.
466 #define _3sumF(a, b, c) do { \
470 _2sumF(__tmp, (a)); \
476 * Common routine to process the arguments to nan(), nanf(), and nanl().
478 void _scan_nan(uint32_t *__words, int __num_words, const char *__s);
481 * Mix 0, 1 or 2 NaNs. First add 0 to each arg. This normally just turns
482 * signaling NaNs into quiet NaNs by setting a quiet bit. We do this
483 * because we want to never return a signaling NaN, and also because we
484 * don't want the quiet bit to affect the result. Then mix the converted
485 * args using the specified operation.
487 * When one arg is NaN, the result is typically that arg quieted. When both
488 * args are NaNs, the result is typically the quietening of the arg whose
489 * mantissa is largest after quietening. When neither arg is NaN, the
490 * result may be NaN because it is indeterminate, or finite for subsequent
491 * construction of a NaN as the indeterminate 0.0L/0.0L.
493 * Technical complications: the result in bits after rounding to the final
494 * precision might depend on the runtime precision and/or on compiler
495 * optimizations, especially when different register sets are used for
496 * different precisions. Try to make the result not depend on at least the
497 * runtime precision by always doing the main mixing step in long double
498 * precision. Try to reduce dependencies on optimizations by adding the
499 * the 0's in different precisions (unless everything is in long double
502 #define nan_mix(x, y) (nan_mix_op((x), (y), +))
503 #define nan_mix_op(x, y, op) (((x) + 0.0L) op ((y) + 0))
508 * C99 specifies that complex numbers have the same representation as
509 * an array of two elements, where the first element is the real part
510 * and the second element is the imaginary part.
521 long double complex f;
523 } long_double_complex;
524 #define REALPART(z) ((z).a[0])
525 #define IMAGPART(z) ((z).a[1])
528 * Inline functions that can be used to construct complex values.
530 * The C99 standard intends x+I*y to be used for this, but x+I*y is
531 * currently unusable in general since gcc introduces many overflow,
532 * underflow, sign and efficiency bugs by rewriting I*y as
533 * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
534 * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
537 * The C11 standard introduced the macros CMPLX(), CMPLXF() and CMPLXL()
538 * to construct complex values. Compilers that conform to the C99
539 * standard require the following functions to avoid the above issues.
543 static __inline float complex
544 CMPLXF(float x, float y)
555 static __inline double complex
556 CMPLX(double x, double y)
567 static __inline long double complex
568 CMPLXL(long double x, long double y)
570 long_double_complex z;
578 #endif /* _COMPLEX_H */
581 * The rnint() family rounds to the nearest integer for a restricted range
582 * range of args (up to about 2**MANT_DIG). We assume that the current
583 * rounding mode is FE_TONEAREST so that this can be done efficiently.
584 * Extra precision causes more problems in practice, and we only centralize
585 * this here to reduce those problems, and have not solved the efficiency
586 * problems. The exp2() family uses a more delicate version of this that
587 * requires extracting bits from the intermediate value, so it is not
588 * centralized here and should copy any solution of the efficiency problems.
595 * This casts to double to kill any extra precision. This depends
596 * on the cast being applied to a double_t to avoid compiler bugs
597 * (this is a cleaner version of STRICT_ASSIGN()). This is
598 * inefficient if there actually is extra precision, but is hard
599 * to improve on. We use double_t in the API to minimise conversions
600 * for just calling here. Note that we cannot easily change the
601 * magic number to the one that works directly with double_t, since
602 * the rounding precision is variable at runtime on x86 so the
603 * magic number would need to be variable. Assuming that the
604 * rounding precision is always the default is too fragile. This
605 * and many other complications will move when the default is
608 return ((double)(x + 0x1.8p52) - 0x1.8p52);
615 * As for rnint(), except we could just call that to handle the
616 * extra precision case, usually without losing efficiency.
618 return ((float)(x + 0x1.8p23F) - 0x1.8p23F);
623 * The complications for extra precision are smaller for rnintl() since it
624 * can safely assume that the rounding precision has been increased from
625 * its default to FP_PE on x86. We don't exploit that here to get small
626 * optimizations from limiting the range to double. We just need it for
627 * the magic number to work with long doubles. ld128 callers should use
628 * rnint() instead of this if possible. ld80 callers should prefer
629 * rnintl() since for amd64 this avoids swapping the register set, while
630 * for i386 it makes no difference (assuming FP_PE), and for other arches
631 * it makes little difference.
633 static inline long double
634 rnintl(long double x)
636 return (x + __CONCAT(0x1.8p, LDBL_MANT_DIG) / 2 -
637 __CONCAT(0x1.8p, LDBL_MANT_DIG) / 2);
639 #endif /* LDBL_MANT_DIG */
642 * irint() and i64rint() give the same result as casting to their integer
643 * return type provided their arg is a floating point integer. They can
644 * sometimes be more efficient because no rounding is required.
646 #if defined(amd64) || defined(__i386__)
648 (sizeof(x) == sizeof(float) && \
649 sizeof(__float_t) == sizeof(long double) ? irintf(x) : \
650 sizeof(x) == sizeof(double) && \
651 sizeof(__double_t) == sizeof(long double) ? irintd(x) : \
652 sizeof(x) == sizeof(long double) ? irintl(x) : (int)(x))
654 #define irint(x) ((int)(x))
657 #define i64rint(x) ((int64_t)(x)) /* only needed for ld128 so not opt. */
659 #if defined(__i386__)
665 __asm("fistl %0" : "=m" (n) : "t" (x));
674 __asm("fistl %0" : "=m" (n) : "t" (x));
679 #if defined(__amd64__) || defined(__i386__)
681 irintl(long double x)
685 __asm("fistl %0" : "=m" (n) : "t" (x));
691 * The following are fast floor macros for 0 <= |x| < 0x1p(N-1), where
692 * N is the precision of the type of x. These macros are used in the
693 * half-cycle trignometric functions (e.g., sinpi(x)).
695 #define FFLOORF(x, j0, ix) do { \
696 (j0) = (((ix) >> 23) & 0xff) - 0x7f; \
697 (ix) &= ~(0x007fffff >> (j0)); \
698 SET_FLOAT_WORD((x), (ix)); \
701 #define FFLOOR(x, j0, ix, lx) do { \
702 (j0) = (((ix) >> 20) & 0x7ff) - 0x3ff; \
704 (ix) &= ~(0x000fffff >> (j0)); \
707 (lx) &= ~((uint32_t)0xffffffff >> ((j0) - 20)); \
709 INSERT_WORDS((x), (ix), (lx)); \
712 #define FFLOORL80(x, j0, ix, lx) do { \
713 j0 = ix - 0x3fff + 1; \
715 (lx) = ((lx) >> 32) << 32; \
716 (lx) &= ~((((lx) << 32)-1) >> (j0)); \
719 _m = (uint64_t)-1 >> (j0); \
720 if ((lx) & _m) (lx) &= ~_m; \
722 INSERT_LDBL80_WORDS((x), (ix), (lx)); \
725 #define FFLOORL128(x, ai, ar) do { \
726 union IEEEl2bits u; \
730 e = u.bits.exp - 16383; \
732 m = ((1llu << 49) - 1) >> (e + 1); \
736 m = (uint64_t)-1 >> (e - 48); \
744 #if defined(__amd64__) || defined(__i386__)
745 #define breakpoint() asm("int $3")
749 #define breakpoint() raise(SIGTRAP)
754 #define RETURNSP(rp) do { \
757 RETURNF((rp)->hi + (rp)->lo); \
759 #define RETURNSPI(rp) do { \
762 RETURNI((rp)->hi + (rp)->lo); \
766 #define SUM2P(x, y) ({ \
767 const __typeof (x) __x = (x); \
768 const __typeof (y) __y = (y); \
772 /* fdlibm kernel function */
773 int __kernel_rem_pio2(double*,double*,int,int,int);
775 /* double precision kernel functions */
776 #ifndef INLINE_REM_PIO2
777 int __ieee754_rem_pio2(double,double*);
779 double __kernel_sin(double,double,int);
780 double __kernel_cos(double,double);
781 double __kernel_tan(double,double,int);
782 double __ldexp_exp(double,int);
784 double complex __ldexp_cexp(double complex,int);
787 /* float precision kernel functions */
788 #ifndef INLINE_REM_PIO2F
789 int __ieee754_rem_pio2f(float,double*);
791 #ifndef INLINE_KERNEL_SINDF
792 float __kernel_sindf(double);
794 #ifndef INLINE_KERNEL_COSDF
795 float __kernel_cosdf(double);
797 #ifndef INLINE_KERNEL_TANDF
798 float __kernel_tandf(double,int);
800 float __ldexp_expf(float,int);
802 float complex __ldexp_cexpf(float complex,int);
805 /* long double precision kernel functions */
806 long double __kernel_sinl(long double, long double, int);
807 long double __kernel_cosl(long double, long double);
808 long double __kernel_tanl(long double, long double, int);
810 #endif /* !_MATH_PRIVATE_H_ */