Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / ld / math_private.h
blob6d0183e3cda6f71ce465f2c3d09f1716b90e9dc3
1 /*
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
8 * is preserved.
9 * ====================================================
13 * from: @(#)fdlibm.h 5.1 93/09/24
14 * $FreeBSD$
17 #ifndef _MATH_PRIVATE_H_
18 #define _MATH_PRIVATE_H_
20 #include <sys/types.h>
21 #include <machine/endian.h>
24 * __double_t and __float_t are defined elsewhere in
25 * freebsd and used to define double_t and float_t.
26 * Newlib has already went through the process of
27 * defining double_t and float_t so we should be able
28 * to use them to define __double_t and __float_t for
29 * this file.
31 typedef double_t __double_t;
32 typedef float_t __float_t;
35 * Necessary to disable Protection Enabled specific
36 * Free-bsd source.
38 #define NO_FPSETPREC
41 * The original fdlibm code used statements like:
42 * n0 = ((*(int*)&one)>>29)^1; * index of high word *
43 * ix0 = *(n0+(int*)&x); * high word of x *
44 * ix1 = *((1-n0)+(int*)&x); * low word of x *
45 * to dig two 32 bit words out of the 64 bit IEEE floating point
46 * value. That is non-ANSI, and, moreover, the gcc instruction
47 * scheduler gets it wrong. We instead use the following macros.
48 * Unlike the original code, we determine the endianness at compile
49 * time, not at run time; I don't see much benefit to selecting
50 * endianness at run time.
54 * A union which permits us to convert between a double and two 32 bit
55 * ints.
58 #ifdef __arm__
59 #if defined(__VFP_FP__) || defined(__ARM_EABI__)
60 #define IEEE_WORD_ORDER BYTE_ORDER
61 #else
62 #define IEEE_WORD_ORDER BIG_ENDIAN
63 #endif
64 #else /* __arm__ */
65 #define IEEE_WORD_ORDER BYTE_ORDER
66 #endif
68 /* A union which permits us to convert between a long double and
69 four 32 bit ints. */
71 #if IEEE_WORD_ORDER == BIG_ENDIAN
73 typedef union
75 long double value;
76 struct {
77 u_int32_t mswhi;
78 u_int32_t mswlo;
79 u_int32_t lswhi;
80 u_int32_t lswlo;
81 } parts32;
82 struct {
83 u_int64_t msw;
84 u_int64_t lsw;
85 } parts64;
86 } ieee_quad_shape_type;
88 #endif
90 #if IEEE_WORD_ORDER == LITTLE_ENDIAN
92 typedef union
94 long double value;
95 struct {
96 u_int32_t lswlo;
97 u_int32_t lswhi;
98 u_int32_t mswlo;
99 u_int32_t mswhi;
100 } parts32;
101 struct {
102 u_int64_t lsw;
103 u_int64_t msw;
104 } parts64;
105 } ieee_quad_shape_type;
107 #endif
109 #if IEEE_WORD_ORDER == BIG_ENDIAN
111 typedef union
113 double value;
114 struct
116 u_int32_t msw;
117 u_int32_t lsw;
118 } parts;
119 struct
121 u_int64_t w;
122 } xparts;
123 } ieee_double_shape_type;
125 #endif
127 #if IEEE_WORD_ORDER == LITTLE_ENDIAN
129 typedef union
131 double value;
132 struct
134 u_int32_t lsw;
135 u_int32_t msw;
136 } parts;
137 struct
139 u_int64_t w;
140 } xparts;
141 } ieee_double_shape_type;
143 #endif
145 /* Get two 32 bit ints from a double. */
147 #define EXTRACT_WORDS(ix0,ix1,d) \
148 do { \
149 ieee_double_shape_type ew_u; \
150 ew_u.value = (d); \
151 (ix0) = ew_u.parts.msw; \
152 (ix1) = ew_u.parts.lsw; \
153 } while (0)
155 /* Get a 64-bit int from a double. */
156 #define EXTRACT_WORD64(ix,d) \
157 do { \
158 ieee_double_shape_type ew_u; \
159 ew_u.value = (d); \
160 (ix) = ew_u.xparts.w; \
161 } while (0)
163 /* Get the more significant 32 bit int from a double. */
165 #define GET_HIGH_WORD(i,d) \
166 do { \
167 ieee_double_shape_type gh_u; \
168 gh_u.value = (d); \
169 (i) = gh_u.parts.msw; \
170 } while (0)
172 /* Get the less significant 32 bit int from a double. */
174 #define GET_LOW_WORD(i,d) \
175 do { \
176 ieee_double_shape_type gl_u; \
177 gl_u.value = (d); \
178 (i) = gl_u.parts.lsw; \
179 } while (0)
181 /* Set a double from two 32 bit ints. */
183 #define INSERT_WORDS(d,ix0,ix1) \
184 do { \
185 ieee_double_shape_type iw_u; \
186 iw_u.parts.msw = (ix0); \
187 iw_u.parts.lsw = (ix1); \
188 (d) = iw_u.value; \
189 } while (0)
191 /* Set a double from a 64-bit int. */
192 #define INSERT_WORD64(d,ix) \
193 do { \
194 ieee_double_shape_type iw_u; \
195 iw_u.xparts.w = (ix); \
196 (d) = iw_u.value; \
197 } while (0)
199 /* Set the more significant 32 bits of a double from an int. */
201 #define SET_HIGH_WORD(d,v) \
202 do { \
203 ieee_double_shape_type sh_u; \
204 sh_u.value = (d); \
205 sh_u.parts.msw = (v); \
206 (d) = sh_u.value; \
207 } while (0)
209 /* Set the less significant 32 bits of a double from an int. */
211 #define SET_LOW_WORD(d,v) \
212 do { \
213 ieee_double_shape_type sl_u; \
214 sl_u.value = (d); \
215 sl_u.parts.lsw = (v); \
216 (d) = sl_u.value; \
217 } while (0)
220 * A union which permits us to convert between a float and a 32 bit
221 * int.
224 typedef union
226 float value;
227 /* FIXME: Assumes 32 bit int. */
228 unsigned int word;
229 } ieee_float_shape_type;
231 /* Get a 32 bit int from a float. */
233 #define GET_FLOAT_WORD(i,d) \
234 do { \
235 ieee_float_shape_type gf_u; \
236 gf_u.value = (d); \
237 (i) = gf_u.word; \
238 } while (0)
240 /* Set a float from a 32 bit int. */
242 #define SET_FLOAT_WORD(d,i) \
243 do { \
244 ieee_float_shape_type sf_u; \
245 sf_u.word = (i); \
246 (d) = sf_u.value; \
247 } while (0)
250 * Get expsign and mantissa as 16 bit and 64 bit ints from an 80 bit long
251 * double.
254 #define EXTRACT_LDBL80_WORDS(ix0,ix1,d) \
255 do { \
256 union IEEEl2bits ew_u; \
257 ew_u.e = (d); \
258 (ix0) = ew_u.xbits.expsign; \
259 (ix1) = ew_u.xbits.man; \
260 } while (0)
263 * Get expsign and mantissa as one 16 bit and two 64 bit ints from a 128 bit
264 * long double.
267 #define EXTRACT_LDBL128_WORDS(ix0,ix1,ix2,d) \
268 do { \
269 union IEEEl2bits ew_u; \
270 ew_u.e = (d); \
271 (ix0) = ew_u.xbits.expsign; \
272 (ix1) = ew_u.xbits.manh; \
273 (ix2) = ew_u.xbits.manl; \
274 } while (0)
276 /* Get expsign as a 16 bit int from a long double. */
278 #define GET_LDBL_EXPSIGN(i,d) \
279 do { \
280 union IEEEl2bits ge_u; \
281 ge_u.e = (d); \
282 (i) = ge_u.xbits.expsign; \
283 } while (0)
286 * Set an 80 bit long double from a 16 bit int expsign and a 64 bit int
287 * mantissa.
290 #define INSERT_LDBL80_WORDS(d,ix0,ix1) \
291 do { \
292 union IEEEl2bits iw_u; \
293 iw_u.xbits.expsign = (ix0); \
294 iw_u.xbits.man = (ix1); \
295 (d) = iw_u.e; \
296 } while (0)
299 * Set a 128 bit long double from a 16 bit int expsign and two 64 bit ints
300 * comprising the mantissa.
303 #define INSERT_LDBL128_WORDS(d,ix0,ix1,ix2) \
304 do { \
305 union IEEEl2bits iw_u; \
306 iw_u.xbits.expsign = (ix0); \
307 iw_u.xbits.manh = (ix1); \
308 iw_u.xbits.manl = (ix2); \
309 (d) = iw_u.e; \
310 } while (0)
312 /* Set expsign of a long double from a 16 bit int. */
314 #define SET_LDBL_EXPSIGN(d,v) \
315 do { \
316 union IEEEl2bits se_u; \
317 se_u.e = (d); \
318 se_u.xbits.expsign = (v); \
319 (d) = se_u.e; \
320 } while (0)
322 #ifdef __i386__
323 /* Long double constants are broken on i386. */
324 #define LD80C(m, ex, v) { \
325 .xbits.man = __CONCAT(m, ULL), \
326 .xbits.expsign = (0x3fff + (ex)) | ((v) < 0 ? 0x8000 : 0), \
328 #else
329 /* The above works on non-i386 too, but we use this to check v. */
330 #define LD80C(m, ex, v) { .e = (v), }
331 #endif
333 #ifdef FLT_EVAL_METHOD
335 * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
337 #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
338 #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
339 #else
340 #define STRICT_ASSIGN(type, lval, rval) do { \
341 volatile type __lval; \
343 if (sizeof(type) >= sizeof(long double)) \
344 (lval) = (rval); \
345 else { \
346 __lval = (rval); \
347 (lval) = __lval; \
349 } while (0)
350 #endif
351 #endif /* FLT_EVAL_METHOD */
353 /* Support switching the mode to FP_PE if necessary. */
354 #if defined(__i386__) && !defined(NO_FPSETPREC)
355 #define ENTERI() ENTERIT(long double)
356 #define ENTERIT(returntype) \
357 returntype __retval; \
358 fp_prec_t __oprec; \
360 if ((__oprec = fpgetprec()) != FP_PE) \
361 fpsetprec(FP_PE)
362 #define RETURNI(x) do { \
363 __retval = (x); \
364 if (__oprec != FP_PE) \
365 fpsetprec(__oprec); \
366 RETURNF(__retval); \
367 } while (0)
368 #define ENTERV() \
369 fp_prec_t __oprec; \
371 if ((__oprec = fpgetprec()) != FP_PE) \
372 fpsetprec(FP_PE)
373 #define RETURNV() do { \
374 if (__oprec != FP_PE) \
375 fpsetprec(__oprec); \
376 return; \
377 } while (0)
378 #else
379 #define ENTERI()
380 #define ENTERIT(x)
381 #define RETURNI(x) RETURNF(x)
382 #define ENTERV()
383 #define RETURNV() return
384 #endif
386 /* Default return statement if hack*_t() is not used. */
387 #define RETURNF(v) return (v)
390 * 2sum gives the same result as 2sumF without requiring |a| >= |b| or
391 * a == 0, but is slower.
393 #define _2sum(a, b) do { \
394 __typeof(a) __s, __w; \
396 __w = (a) + (b); \
397 __s = __w - (a); \
398 (b) = ((a) - (__w - __s)) + ((b) - __s); \
399 (a) = __w; \
400 } while (0)
403 * 2sumF algorithm.
405 * "Normalize" the terms in the infinite-precision expression a + b for
406 * the sum of 2 floating point values so that b is as small as possible
407 * relative to 'a'. (The resulting 'a' is the value of the expression in
408 * the same precision as 'a' and the resulting b is the rounding error.)
409 * |a| must be >= |b| or 0, b's type must be no larger than 'a's type, and
410 * exponent overflow or underflow must not occur. This uses a Theorem of
411 * Dekker (1971). See Knuth (1981) 4.2.2 Theorem C. The name "TwoSum"
412 * is apparently due to Skewchuk (1997).
414 * For this to always work, assignment of a + b to 'a' must not retain any
415 * extra precision in a + b. This is required by C standards but broken
416 * in many compilers. The brokenness cannot be worked around using
417 * STRICT_ASSIGN() like we do elsewhere, since the efficiency of this
418 * algorithm would be destroyed by non-null strict assignments. (The
419 * compilers are correct to be broken -- the efficiency of all floating
420 * point code calculations would be destroyed similarly if they forced the
421 * conversions.)
423 * Fortunately, a case that works well can usually be arranged by building
424 * any extra precision into the type of 'a' -- 'a' should have type float_t,
425 * double_t or long double. b's type should be no larger than 'a's type.
426 * Callers should use these types with scopes as large as possible, to
427 * reduce their own extra-precision and efficiciency problems. In
428 * particular, they shouldn't convert back and forth just to call here.
430 #ifdef DEBUG
431 #define _2sumF(a, b) do { \
432 __typeof(a) __w; \
433 volatile __typeof(a) __ia, __ib, __r, __vw; \
435 __ia = (a); \
436 __ib = (b); \
437 assert(__ia == 0 || fabsl(__ia) >= fabsl(__ib)); \
439 __w = (a) + (b); \
440 (b) = ((a) - __w) + (b); \
441 (a) = __w; \
443 /* The next 2 assertions are weak if (a) is already long double. */ \
444 assert((long double)__ia + __ib == (long double)(a) + (b)); \
445 __vw = __ia + __ib; \
446 __r = __ia - __vw; \
447 __r += __ib; \
448 assert(__vw == (a) && __r == (b)); \
449 } while (0)
450 #else /* !DEBUG */
451 #define _2sumF(a, b) do { \
452 __typeof(a) __w; \
454 __w = (a) + (b); \
455 (b) = ((a) - __w) + (b); \
456 (a) = __w; \
457 } while (0)
458 #endif /* DEBUG */
461 * Set x += c, where x is represented in extra precision as a + b.
462 * x must be sufficiently normalized and sufficiently larger than c,
463 * and the result is then sufficiently normalized.
465 * The details of ordering are that |a| must be >= |c| (so that (a, c)
466 * can be normalized without extra work to swap 'a' with c). The details of
467 * the normalization are that b must be small relative to the normalized 'a'.
468 * Normalization of (a, c) makes the normalized c tiny relative to the
469 * normalized a, so b remains small relative to 'a' in the result. However,
470 * b need not ever be tiny relative to 'a'. For example, b might be about
471 * 2**20 times smaller than 'a' to give about 20 extra bits of precision.
472 * That is usually enough, and adding c (which by normalization is about
473 * 2**53 times smaller than a) cannot change b significantly. However,
474 * cancellation of 'a' with c in normalization of (a, c) may reduce 'a'
475 * significantly relative to b. The caller must ensure that significant
476 * cancellation doesn't occur, either by having c of the same sign as 'a',
477 * or by having |c| a few percent smaller than |a|. Pre-normalization of
478 * (a, b) may help.
480 * This is a variant of an algorithm of Kahan (see Knuth (1981) 4.2.2
481 * exercise 19). We gain considerable efficiency by requiring the terms to
482 * be sufficiently normalized and sufficiently increasing.
484 #define _3sumF(a, b, c) do { \
485 __typeof(a) __tmp; \
487 __tmp = (c); \
488 _2sumF(__tmp, (a)); \
489 (b) += (a); \
490 (a) = __tmp; \
491 } while (0)
494 * Common routine to process the arguments to nan(), nanf(), and nanl().
496 void _scan_nan(uint32_t *__words, int __num_words, const char *__s);
499 * Mix 0, 1 or 2 NaNs. First add 0 to each arg. This normally just turns
500 * signaling NaNs into quiet NaNs by setting a quiet bit. We do this
501 * because we want to never return a signaling NaN, and also because we
502 * don't want the quiet bit to affect the result. Then mix the converted
503 * args using the specified operation.
505 * When one arg is NaN, the result is typically that arg quieted. When both
506 * args are NaNs, the result is typically the quietening of the arg whose
507 * mantissa is largest after quietening. When neither arg is NaN, the
508 * result may be NaN because it is indeterminate, or finite for subsequent
509 * construction of a NaN as the indeterminate 0.0L/0.0L.
511 * Technical complications: the result in bits after rounding to the final
512 * precision might depend on the runtime precision and/or on compiler
513 * optimizations, especially when different register sets are used for
514 * different precisions. Try to make the result not depend on at least the
515 * runtime precision by always doing the main mixing step in long double
516 * precision. Try to reduce dependencies on optimizations by adding the
517 * the 0's in different precisions (unless everything is in long double
518 * precision).
520 #define nan_mix(x, y) (nan_mix_op((x), (y), +))
521 #define nan_mix_op(x, y, op) (((x) + 0.0L) op ((y) + 0))
523 #ifdef _COMPLEX_H
526 * C99 specifies that complex numbers have the same representation as
527 * an array of two elements, where the first element is the real part
528 * and the second element is the imaginary part.
530 typedef union {
531 float complex f;
532 float a[2];
533 } float_complex;
534 typedef union {
535 double complex f;
536 double a[2];
537 } double_complex;
538 typedef union {
539 long double complex f;
540 long double a[2];
541 } long_double_complex;
542 #define REALPART(z) ((z).a[0])
543 #define IMAGPART(z) ((z).a[1])
546 * Inline functions that can be used to construct complex values.
548 * The C99 standard intends x+I*y to be used for this, but x+I*y is
549 * currently unusable in general since gcc introduces many overflow,
550 * underflow, sign and efficiency bugs by rewriting I*y as
551 * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
552 * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
553 * to -0.0+I*0.0.
555 * The C11 standard introduced the macros CMPLX(), CMPLXF() and CMPLXL()
556 * to construct complex values. Compilers that conform to the C99
557 * standard require the following functions to avoid the above issues.
560 #ifndef CMPLXF
561 static __inline float complex
562 CMPLXF(float x, float y)
564 float_complex z;
566 REALPART(z) = x;
567 IMAGPART(z) = y;
568 return (z.f);
570 #endif
572 #ifndef CMPLX
573 static __inline double complex
574 CMPLX(double x, double y)
576 double_complex z;
578 REALPART(z) = x;
579 IMAGPART(z) = y;
580 return (z.f);
582 #endif
584 #ifndef CMPLXL
585 static __inline long double complex
586 CMPLXL(long double x, long double y)
588 long_double_complex z;
590 REALPART(z) = x;
591 IMAGPART(z) = y;
592 return (z.f);
594 #endif
596 #endif /* _COMPLEX_H */
599 * The rnint() family rounds to the nearest integer for a restricted range
600 * range of args (up to about 2**MANT_DIG). We assume that the current
601 * rounding mode is FE_TONEAREST so that this can be done efficiently.
602 * Extra precision causes more problems in practice, and we only centralize
603 * this here to reduce those problems, and have not solved the efficiency
604 * problems. The exp2() family uses a more delicate version of this that
605 * requires extracting bits from the intermediate value, so it is not
606 * centralized here and should copy any solution of the efficiency problems.
609 static inline double
610 rnint(__double_t x)
613 * This casts to double to kill any extra precision. This depends
614 * on the cast being applied to a double_t to avoid compiler bugs
615 * (this is a cleaner version of STRICT_ASSIGN()). This is
616 * inefficient if there actually is extra precision, but is hard
617 * to improve on. We use double_t in the API to minimise conversions
618 * for just calling here. Note that we cannot easily change the
619 * magic number to the one that works directly with double_t, since
620 * the rounding precision is variable at runtime on x86 so the
621 * magic number would need to be variable. Assuming that the
622 * rounding precision is always the default is too fragile. This
623 * and many other complications will move when the default is
624 * changed to FP_PE.
626 return ((double)(x + 0x1.8p52) - 0x1.8p52);
629 static inline float
630 rnintf(__float_t x)
633 * As for rnint(), except we could just call that to handle the
634 * extra precision case, usually without losing efficiency.
636 return ((float)(x + 0x1.8p23F) - 0x1.8p23F);
639 #ifdef LDBL_MANT_DIG
641 * The complications for extra precision are smaller for rnintl() since it
642 * can safely assume that the rounding precision has been increased from
643 * its default to FP_PE on x86. We don't exploit that here to get small
644 * optimizations from limiting the rangle to double. We just need it for
645 * the magic number to work with long doubles. ld128 callers should use
646 * rnint() instead of this if possible. ld80 callers should prefer
647 * rnintl() since for amd64 this avoids swapping the register set, while
648 * for i386 it makes no difference (assuming FP_PE), and for other arches
649 * it makes little difference.
651 static inline long double
652 rnintl(long double x)
654 return (x + __CONCAT(0x1.8p, LDBL_MANT_DIG) / 2 -
655 __CONCAT(0x1.8p, LDBL_MANT_DIG) / 2);
657 #endif /* LDBL_MANT_DIG */
660 * irint() and i64rint() give the same result as casting to their integer
661 * return type provided their arg is a floating point integer. They can
662 * sometimes be more efficient because no rounding is required.
664 #if defined(amd64) || (defined(__i386__) && (!defined _SOFT_FLOAT))
665 #define irint(x) \
666 (sizeof(x) == sizeof(float) && \
667 sizeof(__float_t) == sizeof(long double) ? irintf(x) : \
668 sizeof(x) == sizeof(double) && \
669 sizeof(__double_t) == sizeof(long double) ? irintd(x) : \
670 sizeof(x) == sizeof(long double) ? irintl(x) : (int)(x))
671 #else
672 #define irint(x) ((int)(x))
673 #endif
675 #define i64rint(x) ((int64_t)(x)) /* only needed for ld128 so not opt. */
677 #if defined(__i386__)
678 static __inline int
679 irintf(float x)
681 int n;
683 __asm("fistl %0" : "=m" (n) : "t" (x));
684 return (n);
687 static __inline int
688 irintd(double x)
690 int n;
692 __asm("fistl %0" : "=m" (n) : "t" (x));
693 return (n);
695 #endif
697 #if defined(__amd64__) || (defined(__i386__) && (!defined _SOFT_FLOAT))
698 static __inline int
699 irintl(long double x)
701 int n;
703 __asm("fistl %0" : "=m" (n) : "t" (x));
704 return (n);
706 #endif
709 * The following are fast floor macros for 0 <= |x| < 0x1p(N-1), where
710 * N is the precision of the type of x. These macros are used in the
711 * half-cycle trignometric functions (e.g., sinpi(x)).
713 #define FFLOORF(x, j0, ix) do { \
714 (j0) = (((ix) >> 23) & 0xff) - 0x7f; \
715 (ix) &= ~(0x007fffff >> (j0)); \
716 SET_FLOAT_WORD((x), (ix)); \
717 } while (0)
719 #define FFLOOR(x, j0, ix, lx) do { \
720 (j0) = (((ix) >> 20) & 0x7ff) - 0x3ff; \
721 if ((j0) < 20) { \
722 (ix) &= ~(0x000fffff >> (j0)); \
723 (lx) = 0; \
724 } else { \
725 (lx) &= ~((uint32_t)0xffffffff >> ((j0) - 20)); \
727 INSERT_WORDS((x), (ix), (lx)); \
728 } while (0)
730 #define FFLOORL80(x, j0, ix, lx) do { \
731 j0 = ix - 0x3fff + 1; \
732 if ((j0) < 32) { \
733 (lx) = ((lx) >> 32) << 32; \
734 (lx) &= ~((((lx) << 32)-1) >> (j0)); \
735 } else { \
736 uint64_t _m; \
737 _m = (uint64_t)-1 >> (j0); \
738 if ((lx) & _m) (lx) &= ~_m; \
740 INSERT_LDBL80_WORDS((x), (ix), (lx)); \
741 } while (0)
743 #define FFLOORL128(x, ai, ar) do { \
744 union IEEEl2bits u; \
745 uint64_t m; \
746 int e; \
747 u.e = (x); \
748 e = u.bits.exp - 16383; \
749 if (e < 48) { \
750 m = ((1llu << 49) - 1) >> (e + 1); \
751 u.bits.manh &= ~m; \
752 u.bits.manl = 0; \
753 } else { \
754 m = (uint64_t)-1 >> (e - 48); \
755 u.bits.manl &= ~m; \
757 (ai) = u.e; \
758 (ar) = (x) - (ai); \
759 } while (0)
761 #ifdef DEBUG
762 #if defined(__amd64__) || defined(__i386__)
763 #define breakpoint() asm("int $3")
764 #else
765 #include <signal.h>
767 #define breakpoint() raise(SIGTRAP)
768 #endif
769 #endif
771 /* Write a pari script to test things externally. */
772 #ifdef DOPRINT
773 #include <stdio.h>
775 #ifndef DOPRINT_SWIZZLE
776 #define DOPRINT_SWIZZLE 0
777 #endif
779 #ifdef DOPRINT_LD80
781 #define DOPRINT_START(xp) do { \
782 uint64_t __lx; \
783 uint16_t __hx; \
785 /* Hack to give more-problematic args. */ \
786 EXTRACT_LDBL80_WORDS(__hx, __lx, *xp); \
787 __lx ^= DOPRINT_SWIZZLE; \
788 INSERT_LDBL80_WORDS(*xp, __hx, __lx); \
789 printf("x = %.21Lg; ", (long double)*xp); \
790 } while (0)
791 #define DOPRINT_END1(v) \
792 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
793 #define DOPRINT_END2(hi, lo) \
794 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
795 (long double)(hi), (long double)(lo))
797 #elif defined(DOPRINT_D64)
799 #define DOPRINT_START(xp) do { \
800 uint32_t __hx, __lx; \
802 EXTRACT_WORDS(__hx, __lx, *xp); \
803 __lx ^= DOPRINT_SWIZZLE; \
804 INSERT_WORDS(*xp, __hx, __lx); \
805 printf("x = %.21Lg; ", (long double)*xp); \
806 } while (0)
807 #define DOPRINT_END1(v) \
808 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
809 #define DOPRINT_END2(hi, lo) \
810 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
811 (long double)(hi), (long double)(lo))
813 #elif defined(DOPRINT_F32)
815 #define DOPRINT_START(xp) do { \
816 uint32_t __hx; \
818 GET_FLOAT_WORD(__hx, *xp); \
819 __hx ^= DOPRINT_SWIZZLE; \
820 SET_FLOAT_WORD(*xp, __hx); \
821 printf("x = %.21Lg; ", (long double)*xp); \
822 } while (0)
823 #define DOPRINT_END1(v) \
824 printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v))
825 #define DOPRINT_END2(hi, lo) \
826 printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \
827 (long double)(hi), (long double)(lo))
829 #else /* !DOPRINT_LD80 && !DOPRINT_D64 (LD128 only) */
831 #ifndef DOPRINT_SWIZZLE_HIGH
832 #define DOPRINT_SWIZZLE_HIGH 0
833 #endif
835 #define DOPRINT_START(xp) do { \
836 uint64_t __lx, __llx; \
837 uint16_t __hx; \
839 EXTRACT_LDBL128_WORDS(__hx, __lx, __llx, *xp); \
840 __llx ^= DOPRINT_SWIZZLE; \
841 __lx ^= DOPRINT_SWIZZLE_HIGH; \
842 INSERT_LDBL128_WORDS(*xp, __hx, __lx, __llx); \
843 printf("x = %.36Lg; ", (long double)*xp); \
844 } while (0)
845 #define DOPRINT_END1(v) \
846 printf("y = %.36Lg; z = 0; show(x, y, z);\n", (long double)(v))
847 #define DOPRINT_END2(hi, lo) \
848 printf("y = %.36Lg; z = %.36Lg; show(x, y, z);\n", \
849 (long double)(hi), (long double)(lo))
851 #endif /* DOPRINT_LD80 */
853 #else /* !DOPRINT */
854 #define DOPRINT_START(xp)
855 #define DOPRINT_END1(v)
856 #define DOPRINT_END2(hi, lo)
857 #endif /* DOPRINT */
859 #define RETURNP(x) do { \
860 DOPRINT_END1(x); \
861 RETURNF(x); \
862 } while (0)
863 #define RETURNPI(x) do { \
864 DOPRINT_END1(x); \
865 RETURNI(x); \
866 } while (0)
867 #define RETURN2P(x, y) do { \
868 DOPRINT_END2((x), (y)); \
869 RETURNF((x) + (y)); \
870 } while (0)
871 #define RETURN2PI(x, y) do { \
872 DOPRINT_END2((x), (y)); \
873 RETURNI((x) + (y)); \
874 } while (0)
875 #ifdef STRUCT_RETURN
876 #define RETURNSP(rp) do { \
877 if (!(rp)->lo_set) \
878 RETURNP((rp)->hi); \
879 RETURN2P((rp)->hi, (rp)->lo); \
880 } while (0)
881 #define RETURNSPI(rp) do { \
882 if (!(rp)->lo_set) \
883 RETURNPI((rp)->hi); \
884 RETURN2PI((rp)->hi, (rp)->lo); \
885 } while (0)
886 #endif
887 #define SUM2P(x, y) ({ \
888 const __typeof (x) __x = (x); \
889 const __typeof (y) __y = (y); \
891 DOPRINT_END2(__x, __y); \
892 __x + __y; \
896 * ieee style elementary functions
898 * We rename functions here to improve other sources' diffability
899 * against fdlibm.
901 #define __ieee754_sqrt sqrt
902 #define __ieee754_acos acos
903 #define __ieee754_acosh acosh
904 #define __ieee754_log log
905 #define __ieee754_log2 log2
906 #define __ieee754_atanh atanh
907 #define __ieee754_asin asin
908 #define __ieee754_atan2 atan2
909 #define __ieee754_exp exp
910 #define __ieee754_cosh cosh
911 #define __ieee754_fmod fmod
912 #define __ieee754_pow pow
913 #define __ieee754_lgamma lgamma
914 #define __ieee754_gamma gamma
915 #define __ieee754_lgamma_r lgamma_r
916 #define __ieee754_gamma_r gamma_r
917 #define __ieee754_log10 log10
918 #define __ieee754_sinh sinh
919 #define __ieee754_hypot hypot
920 #define __ieee754_j0 j0
921 #define __ieee754_j1 j1
922 #define __ieee754_y0 y0
923 #define __ieee754_y1 y1
924 #define __ieee754_jn jn
925 #define __ieee754_yn yn
926 #define __ieee754_remainder remainder
927 #define __ieee754_scalb scalb
928 #define __ieee754_sqrtf sqrtf
929 #define __ieee754_acosf acosf
930 #define __ieee754_acoshf acoshf
931 #define __ieee754_logf logf
932 #define __ieee754_atanhf atanhf
933 #define __ieee754_asinf asinf
934 #define __ieee754_atan2f atan2f
935 #define __ieee754_expf expf
936 #define __ieee754_coshf coshf
937 #define __ieee754_fmodf fmodf
938 #define __ieee754_powf powf
939 #define __ieee754_lgammaf lgammaf
940 #define __ieee754_gammaf gammaf
941 #define __ieee754_lgammaf_r lgammaf_r
942 #define __ieee754_gammaf_r gammaf_r
943 #define __ieee754_log10f log10f
944 #define __ieee754_log2f log2f
945 #define __ieee754_sinhf sinhf
946 #define __ieee754_hypotf hypotf
947 #define __ieee754_j0f j0f
948 #define __ieee754_j1f j1f
949 #define __ieee754_y0f y0f
950 #define __ieee754_y1f y1f
951 #define __ieee754_jnf jnf
952 #define __ieee754_ynf ynf
953 #define __ieee754_remainderf remainderf
954 #define __ieee754_scalbf scalbf
956 /* fdlibm kernel function */
957 int __kernel_rem_pio2(double*,double*,int,int,int);
959 /* double precision kernel functions */
960 #ifndef INLINE_REM_PIO2
961 int __ieee754_rem_pio2(double,double*);
962 #endif
963 double __kernel_sin(double,double,int);
964 double __kernel_cos(double,double);
965 double __kernel_tan(double,double,int);
966 double __ldexp_exp(double,int);
967 #ifdef _COMPLEX_H
968 double complex __ldexp_cexp(double complex,int);
969 #endif
971 /* float precision kernel functions */
972 #ifndef INLINE_REM_PIO2F
973 int __ieee754_rem_pio2f(float,double*);
974 #endif
975 #ifndef INLINE_KERNEL_SINDF
976 float __kernel_sindf(double);
977 #endif
978 #ifndef INLINE_KERNEL_COSDF
979 float __kernel_cosdf(double);
980 #endif
981 #ifndef INLINE_KERNEL_TANDF
982 float __kernel_tandf(double,int);
983 #endif
984 float __ldexp_expf(float,int);
985 #ifdef _COMPLEX_H
986 float complex __ldexp_cexpf(float complex,int);
987 #endif
989 /* long double precision kernel functions */
990 long double __kernel_sinl(long double, long double, int);
991 long double __kernel_cosl(long double, long double);
992 long double __kernel_tanl(long double, long double, int);
994 #endif /* !_MATH_PRIVATE_H_ */