| 1 |
/* |
| 2 |
* ==================================================== |
| 3 |
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 4 |
* |
| 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 |
* ==================================================== |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
* from: @(#)fdlibm.h 5.1 93/09/24 |
| 14 |
* $FreeBSD$ |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifndef _MATH_PRIVATE_H_ |
| 18 |
#define _MATH_PRIVATE_H_ |
| 19 |
|
| 20 |
#include <cfloat> |
| 21 |
#include <stdint.h> |
| 22 |
#include <sys/types.h> |
| 23 |
|
| 24 |
#include "mozilla/EndianUtils.h" |
| 25 |
|
| 26 |
#include "fdlibm.h" |
| 27 |
|
| 28 |
/* |
| 29 |
* Emulate FreeBSD internal double types. |
| 30 |
* Adapted from https://github.com/freebsd/freebsd-src/search?q=__double_t |
| 31 |
*/ |
| 32 |
|
| •33 |
#if defined __FLT_EVAL_METHOD__ && (__FLT_EVAL_METHOD__ == 2) |
| 34 |
typedef long double __double_t; |
| 35 |
#else |
| 36 |
typedef double __double_t; |
| •37 |
#endif |
| 38 |
typedef __double_t double_t; |
| 39 |
typedef float __float_t; |
| 40 |
|
| 41 |
/* |
| 42 |
* The original fdlibm code used statements like: |
| 43 |
* n0 = ((*(int*)&one)>>29)^1; * index of high word * |
| 44 |
* ix0 = *(n0+(int*)&x); * high word of x * |
| 45 |
* ix1 = *((1-n0)+(int*)&x); * low word of x * |
| 46 |
* to dig two 32 bit words out of the 64 bit IEEE floating point |
| 47 |
* value. That is non-ANSI, and, moreover, the gcc instruction |
| 48 |
* scheduler gets it wrong. We instead use the following macros. |
| 49 |
* Unlike the original code, we determine the endianness at compile |
| 50 |
* time, not at run time; I don't see much benefit to selecting |
| 51 |
* endianness at run time. |
| 52 |
*/ |
| 53 |
|
| 54 |
#ifndef u_int32_t |
| 55 |
#define u_int32_t uint32_t |
| 56 |
#endif |
| 57 |
#ifndef u_int64_t |
| 58 |
#define u_int64_t uint64_t |
| 59 |
#endif |
| 60 |
|
| 61 |
/* A union which permits us to convert between a long double and |
| 62 |
four 32 bit ints. */ |
| 63 |
|
| 64 |
#if MOZ_BIG_ENDIAN() |
| 65 |
|
| 66 |
typedef union |
| 67 |
{ |
| 68 |
long double value; |
| 69 |
struct { |
| 70 |
u_int32_t mswhi; |
| 71 |
u_int32_t mswlo; |
| 72 |
u_int32_t lswhi; |
| 73 |
u_int32_t lswlo; |
| 74 |
} parts32; |
| 75 |
struct { |
| 76 |
u_int64_t msw; |
| 77 |
u_int64_t lsw; |
| 78 |
} parts64; |
| 79 |
} ieee_quad_shape_type; |
| 80 |
|
| 81 |
#endif |
| 82 |
|
| 83 |
#if MOZ_LITTLE_ENDIAN() |
| 84 |
|
| 85 |
typedef union |
| 86 |
{ |
| 87 |
long double value; |
| 88 |
struct { |
| 89 |
u_int32_t lswlo; |
| 90 |
u_int32_t lswhi; |
| 91 |
u_int32_t mswlo; |
| 92 |
u_int32_t mswhi; |
| 93 |
} parts32; |
| 94 |
struct { |
| 95 |
u_int64_t lsw; |
| 96 |
u_int64_t msw; |
| 97 |
} parts64; |
| 98 |
} ieee_quad_shape_type; |
| 99 |
|
| 100 |
#endif |
| 101 |
|
| 102 |
#if MOZ_BIG_ENDIAN() |
| 103 |
|
| 104 |
typedef union |
| 105 |
{ |
| 106 |
double value; |
| 107 |
struct |
| 108 |
{ |
| 109 |
u_int32_t msw; |
| 110 |
u_int32_t lsw; |
| 111 |
} parts; |
| 112 |
struct |
| 113 |
{ |
| 114 |
u_int64_t w; |
| 115 |
} xparts; |
| 116 |
} ieee_double_shape_type; |
| 117 |
|
| 118 |
#endif |
| 119 |
|
| 120 |
#if MOZ_LITTLE_ENDIAN() |
| 121 |
|
| 122 |
typedef union |
| 123 |
{ |
| 124 |
double value; |
| 125 |
struct |
| 126 |
{ |
| 127 |
u_int32_t lsw; |
| 128 |
u_int32_t msw; |
| 129 |
} parts; |
| 130 |
struct |
| 131 |
{ |
| 132 |
u_int64_t w; |
| 133 |
} xparts; |
| 134 |
} ieee_double_shape_type; |
| 135 |
|
| 136 |
#endif |
| 137 |
|
| 138 |
/* Get two 32 bit ints from a double. */ |
| 139 |
|
| 140 |
#define EXTRACT_WORDS(ix0,ix1,d) \ |
| 141 |
do { \ |
| 142 |
ieee_double_shape_type ew_u; \ |
| 143 |
ew_u.value = (d); \ |
| 144 |
(ix0) = ew_u.parts.msw; \ |
| 145 |
(ix1) = ew_u.parts.lsw; \ |
| 146 |
} while (0) |
| 147 |
|
| 148 |
/* Get a 64-bit int from a double. */ |
| 149 |
#define EXTRACT_WORD64(ix,d) \ |
| 150 |
do { \ |
| 151 |
ieee_double_shape_type ew_u; \ |
| 152 |
ew_u.value = (d); \ |
| 153 |
(ix) = ew_u.xparts.w; \ |
| 154 |
} while (0) |
| 155 |
|
| 156 |
/* Get the more significant 32 bit int from a double. */ |
| 157 |
|
| 158 |
#define GET_HIGH_WORD(i,d) \ |
| 159 |
do { \ |
| 160 |
ieee_double_shape_type gh_u; \ |
| 161 |
gh_u.value = (d); \ |
| 162 |
(i) = gh_u.parts.msw; \ |
| 163 |
} while (0) |
| 164 |
|
| 165 |
/* Get the less significant 32 bit int from a double. */ |
| 166 |
|
| 167 |
#define GET_LOW_WORD(i,d) \ |
| 168 |
do { \ |
| 169 |
ieee_double_shape_type gl_u; \ |
| 170 |
gl_u.value = (d); \ |
| 171 |
(i) = gl_u.parts.lsw; \ |
| 172 |
} while (0) |
| 173 |
|
| 174 |
/* Set a double from two 32 bit ints. */ |
| 175 |
|
| 176 |
#define INSERT_WORDS(d,ix0,ix1) \ |
| 177 |
do { \ |
| 178 |
ieee_double_shape_type iw_u; \ |
| 179 |
iw_u.parts.msw = (ix0); \ |
| 180 |
iw_u.parts.lsw = (ix1); \ |
| 181 |
(d) = iw_u.value; \ |
| 182 |
} while (0) |
| 183 |
|
| 184 |
/* Set a double from a 64-bit int. */ |
| 185 |
#define INSERT_WORD64(d,ix) \ |
| 186 |
do { \ |
| 187 |
ieee_double_shape_type iw_u; \ |
| 188 |
iw_u.xparts.w = (ix); \ |
| 189 |
(d) = iw_u.value; \ |
| 190 |
} while (0) |
| 191 |
|
| 192 |
/* Set the more significant 32 bits of a double from an int. */ |
| 193 |
|
| 194 |
#define SET_HIGH_WORD(d,v) \ |
| 195 |
do { \ |
| 196 |
ieee_double_shape_type sh_u; \ |
| 197 |
sh_u.value = (d); \ |
| 198 |
sh_u.parts.msw = (v); \ |
| 199 |
(d) = sh_u.value; \ |
| 200 |
} while (0) |
| 201 |
|
| 202 |
/* Set the less significant 32 bits of a double from an int. */ |
| 203 |
|
| 204 |
#define SET_LOW_WORD(d,v) \ |
| 205 |
do { \ |
| 206 |
ieee_double_shape_type sl_u; \ |
| 207 |
sl_u.value = (d); \ |
| 208 |
sl_u.parts.lsw = (v); \ |
| 209 |
(d) = sl_u.value; \ |
| 210 |
} while (0) |
| 211 |
|
| 212 |
/* |
| 213 |
* A union which permits us to convert between a float and a 32 bit |
| 214 |
* int. |
| 215 |
*/ |
| 216 |
|
| 217 |
typedef union |
| 218 |
{ |
| 219 |
float value; |
| 220 |
/* FIXME: Assumes 32 bit int. */ |
| 221 |
unsigned int word; |
| 222 |
} ieee_float_shape_type; |
| 223 |
|
| 224 |
/* Get a 32 bit int from a float. */ |
| 225 |
|
| 226 |
#define GET_FLOAT_WORD(i,d) \ |
| 227 |
do { \ |
| 228 |
ieee_float_shape_type gf_u; \ |
| 229 |
gf_u.value = (d); \ |
| 230 |
(i) = gf_u.word; \ |
| 231 |
} while (0) |
| 232 |
|
| 233 |
/* Set a float from a 32 bit int. */ |
| 234 |
|
| 235 |
#define SET_FLOAT_WORD(d,i) \ |
| 236 |
do { \ |
| 237 |
ieee_float_shape_type sf_u; \ |
| 238 |
sf_u.word = (i); \ |
| 239 |
(d) = sf_u.value; \ |
| 240 |
} while (0) |
| 241 |
|
| 242 |
/* |
| 243 |
* Get expsign and mantissa as 16 bit and 64 bit ints from an 80 bit long |
| 244 |
* double. |
| 245 |
*/ |
| 246 |
|
| 247 |
#define EXTRACT_LDBL80_WORDS(ix0,ix1,d) \ |
| 248 |
do { \ |
| 249 |
union IEEEl2bits ew_u; \ |
| 250 |
ew_u.e = (d); \ |
| 251 |
(ix0) = ew_u.xbits.expsign; \ |
| 252 |
(ix1) = ew_u.xbits.man; \ |
| 253 |
} while (0) |
| 254 |
|
| 255 |
/* |
| 256 |
* Get expsign and mantissa as one 16 bit and two 64 bit ints from a 128 bit |
| 257 |
* long double. |
| 258 |
*/ |
| 259 |
|
| 260 |
#define EXTRACT_LDBL128_WORDS(ix0,ix1,ix2,d) \ |
| 261 |
do { \ |
| 262 |
union IEEEl2bits ew_u; \ |
| 263 |
ew_u.e = (d); \ |
| 264 |
(ix0) = ew_u.xbits.expsign; \ |
| 265 |
(ix1) = ew_u.xbits.manh; \ |
| 266 |
(ix2) = ew_u.xbits.manl; \ |
| 267 |
} while (0) |
| 268 |
|
| 269 |
/* Get expsign as a 16 bit int from a long double. */ |
| 270 |
|
| 271 |
#define GET_LDBL_EXPSIGN(i,d) \ |
| 272 |
do { \ |
| 273 |
union IEEEl2bits ge_u; \ |
| 274 |
ge_u.e = (d); \ |
| 275 |
(i) = ge_u.xbits.expsign; \ |
| 276 |
} while (0) |
| 277 |
|
| 278 |
/* |
| 279 |
* Set an 80 bit long double from a 16 bit int expsign and a 64 bit int |
| 280 |
* mantissa. |
| 281 |
*/ |
| 282 |
|
| 283 |
#define INSERT_LDBL80_WORDS(d,ix0,ix1) \ |
| 284 |
do { \ |
| 285 |
union IEEEl2bits iw_u; \ |
| 286 |
iw_u.xbits.expsign = (ix0); \ |
| 287 |
iw_u.xbits.man = (ix1); \ |
| 288 |
(d) = iw_u.e; \ |
| 289 |
} while (0) |
| 290 |
|
| 291 |
/* |
| 292 |
* Set a 128 bit long double from a 16 bit int expsign and two 64 bit ints |
| 293 |
* comprising the mantissa. |
| 294 |
*/ |
| 295 |
|
| 296 |
#define INSERT_LDBL128_WORDS(d,ix0,ix1,ix2) \ |
| 297 |
do { \ |
| 298 |
union IEEEl2bits iw_u; \ |
| 299 |
iw_u.xbits.expsign = (ix0); \ |
| 300 |
iw_u.xbits.manh = (ix1); \ |
| 301 |
iw_u.xbits.manl = (ix2); \ |
| 302 |
(d) = iw_u.e; \ |
| 303 |
} while (0) |
| 304 |
|
| 305 |
/* Set expsign of a long double from a 16 bit int. */ |
| 306 |
|
| 307 |
#define SET_LDBL_EXPSIGN(d,v) \ |
| 308 |
do { \ |
| 309 |
union IEEEl2bits se_u; \ |
| 310 |
se_u.e = (d); \ |
| 311 |
se_u.xbits.expsign = (v); \ |
| 312 |
(d) = se_u.e; \ |
| 313 |
} while (0) |
| 314 |
|
| 315 |
#ifdef __i386__ |
| 316 |
/* Long double constants are broken on i386. */ |
| 317 |
#define LD80C(m, ex, v) { \ |
| 318 |
.xbits.man = __CONCAT(m, ULL), \ |
| 319 |
.xbits.expsign = (0x3fff + (ex)) | ((v) < 0 ? 0x8000 : 0), \ |
| 320 |
} |
| 321 |
#else |
| 322 |
/* The above works on non-i386 too, but we use this to check v. */ |
| 323 |
#define LD80C(m, ex, v) { .e = (v), } |
| 324 |
#endif |
| 325 |
|
| 326 |
#ifdef FLT_EVAL_METHOD |
| 327 |
/* |
| 328 |
* Attempt to get strict C99 semantics for assignment with non-C99 compilers. |
| 329 |
*/ |
| 330 |
#if !defined(_MSC_VER) && (FLT_EVAL_METHOD == 0 || __GNUC__ == 0) |
| 331 |
#define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval)) |
| 332 |
#else |
| 333 |
#define STRICT_ASSIGN(type, lval, rval) do { \ |
| 334 |
volatile type __lval; \ |
| 335 |
\ |
| 336 |
if (sizeof(type) >= sizeof(long double)) \ |
| 337 |
(lval) = (rval); \ |
| 338 |
else { \ |
| 339 |
__lval = (rval); \ |
| 340 |
(lval) = __lval; \ |
| 341 |
} \ |
| 342 |
} while (0) |
| 343 |
#endif |
| 344 |
#else |
| 345 |
#define STRICT_ASSIGN(type, lval, rval) do { \ |
| 346 |
volatile type __lval; \ |
| 347 |
\ |
| 348 |
if (sizeof(type) >= sizeof(long double)) \ |
| 349 |
(lval) = (rval); \ |
| 350 |
else { \ |
| 351 |
__lval = (rval); \ |
| 352 |
(lval) = __lval; \ |
| 353 |
} \ |
| 354 |
} while (0) |
| 355 |
#endif /* FLT_EVAL_METHOD */ |
| 356 |
|
| 357 |
/* Support switching the mode to FP_PE if necessary. */ |
| 358 |
#if defined(__i386__) && !defined(NO_FPSETPREC) |
| 359 |
#define ENTERI() ENTERIT(long double) |
| 360 |
#define ENTERIT(returntype) \ |
| 361 |
returntype __retval; \ |
| 362 |
fp_prec_t __oprec; \ |
| 363 |
\ |
| 364 |
if ((__oprec = fpgetprec()) != FP_PE) \ |
| 365 |
fpsetprec(FP_PE) |
| 366 |
#define RETURNI(x) do { \ |
| 367 |
__retval = (x); \ |
| 368 |
if (__oprec != FP_PE) \ |
| 369 |
fpsetprec(__oprec); \ |
| 370 |
RETURNF(__retval); \ |
| 371 |
} while (0) |
| 372 |
#define ENTERV() \ |
| 373 |
fp_prec_t __oprec; \ |
| 374 |
\ |
| 375 |
if ((__oprec = fpgetprec()) != FP_PE) \ |
| 376 |
fpsetprec(FP_PE) |
| 377 |
#define RETURNV() do { \ |
| 378 |
if (__oprec != FP_PE) \ |
| 379 |
fpsetprec(__oprec); \ |
| 380 |
return; \ |
| 381 |
} while (0) |
| 382 |
#else |
| 383 |
#define ENTERI() |
| 384 |
#define ENTERIT(x) |
| 385 |
#define RETURNI(x) RETURNF(x) |
| 386 |
#define ENTERV() |
| 387 |
#define RETURNV() return |
| 388 |
#endif |
| 389 |
|
| 390 |
/* Default return statement if hack*_t() is not used. */ |
| 391 |
#define RETURNF(v) return (v) |
| 392 |
|
| 393 |
/* |
| 394 |
* 2sum gives the same result as 2sumF without requiring |a| >= |b| or |
| 395 |
* a == 0, but is slower. |
| 396 |
*/ |
| 397 |
#define _2sum(a, b) do { \ |
| 398 |
__typeof(a) __s, __w; \ |
| 399 |
\ |
| 400 |
__w = (a) + (b); \ |
| 401 |
__s = __w - (a); \ |
| 402 |
(b) = ((a) - (__w - __s)) + ((b) - __s); \ |
| 403 |
(a) = __w; \ |
| 404 |
} while (0) |
| 405 |
|
| 406 |
/* |
| 407 |
* 2sumF algorithm. |
| 408 |
* |
| 409 |
* "Normalize" the terms in the infinite-precision expression a + b for |
| 410 |
* the sum of 2 floating point values so that b is as small as possible |
| 411 |
* relative to 'a'. (The resulting 'a' is the value of the expression in |
| 412 |
* the same precision as 'a' and the resulting b is the rounding error.) |
| 413 |
* |a| must be >= |b| or 0, b's type must be no larger than 'a's type, and |
| 414 |
* exponent overflow or underflow must not occur. This uses a Theorem of |
| 415 |
* Dekker (1971). See Knuth (1981) 4.2.2 Theorem C. The name "TwoSum" |
| 416 |
* is apparently due to Skewchuk (1997). |
| 417 |
* |
| 418 |
* For this to always work, assignment of a + b to 'a' must not retain any |
| 419 |
* extra precision in a + b. This is required by C standards but broken |
| 420 |
* in many compilers. The brokenness cannot be worked around using |
| 421 |
* STRICT_ASSIGN() like we do elsewhere, since the efficiency of this |
| 422 |
* algorithm would be destroyed by non-null strict assignments. (The |
| 423 |
* compilers are correct to be broken -- the efficiency of all floating |
| 424 |
* point code calculations would be destroyed similarly if they forced the |
| 425 |
* conversions.) |
| 426 |
* |
| 427 |
* Fortunately, a case that works well can usually be arranged by building |
| 428 |
* any extra precision into the type of 'a' -- 'a' should have type float_t, |
| 429 |
* double_t or long double. b's type should be no larger than 'a's type. |
| 430 |
* Callers should use these types with scopes as large as possible, to |
| 431 |
* reduce their own extra-precision and efficiciency problems. In |
| 432 |
* particular, they shouldn't convert back and forth just to call here. |
| 433 |
*/ |
| 434 |
#ifdef DEBUG |
| 435 |
#define _2sumF(a, b) do { \ |
| 436 |
__typeof(a) __w; \ |
| 437 |
volatile __typeof(a) __ia, __ib, __r, __vw; \ |
| 438 |
\ |
| 439 |
__ia = (a); \ |
| 440 |
__ib = (b); \ |
| 441 |
assert(__ia == 0 || fabsl(__ia) >= fabsl(__ib)); \ |
| 442 |
\ |
| 443 |
__w = (a) + (b); \ |
| 444 |
(b) = ((a) - __w) + (b); \ |
| 445 |
(a) = __w; \ |
| 446 |
\ |
| 447 |
/* The next 2 assertions are weak if (a) is already long double. */ \ |
| 448 |
assert((long double)__ia + __ib == (long double)(a) + (b)); \ |
| 449 |
__vw = __ia + __ib; \ |
| 450 |
__r = __ia - __vw; \ |
| 451 |
__r += __ib; \ |
| 452 |
assert(__vw == (a) && __r == (b)); \ |
| 453 |
} while (0) |
| 454 |
#else /* !DEBUG */ |
| 455 |
#define _2sumF(a, b) do { \ |
| 456 |
__typeof(a) __w; \ |
| 457 |
\ |
| 458 |
__w = (a) + (b); \ |
| 459 |
(b) = ((a) - __w) + (b); \ |
| 460 |
(a) = __w; \ |
| 461 |
} while (0) |
| 462 |
#endif /* DEBUG */ |
| 463 |
|
| 464 |
/* |
| 465 |
* Set x += c, where x is represented in extra precision as a + b. |
| 466 |
* x must be sufficiently normalized and sufficiently larger than c, |
| 467 |
* and the result is then sufficiently normalized. |
| 468 |
* |
| 469 |
* The details of ordering are that |a| must be >= |c| (so that (a, c) |
| 470 |
* can be normalized without extra work to swap 'a' with c). The details of |
| 471 |
* the normalization are that b must be small relative to the normalized 'a'. |
| 472 |
* Normalization of (a, c) makes the normalized c tiny relative to the |
| 473 |
* normalized a, so b remains small relative to 'a' in the result. However, |
| 474 |
* b need not ever be tiny relative to 'a'. For example, b might be about |
| 475 |
* 2**20 times smaller than 'a' to give about 20 extra bits of precision. |
| 476 |
* That is usually enough, and adding c (which by normalization is about |
| 477 |
* 2**53 times smaller than a) cannot change b significantly. However, |
| 478 |
* cancellation of 'a' with c in normalization of (a, c) may reduce 'a' |
| 479 |
* significantly relative to b. The caller must ensure that significant |
| 480 |
* cancellation doesn't occur, either by having c of the same sign as 'a', |
| 481 |
* or by having |c| a few percent smaller than |a|. Pre-normalization of |
| 482 |
* (a, b) may help. |
| 483 |
* |
| 484 |
* This is a variant of an algorithm of Kahan (see Knuth (1981) 4.2.2 |
| 485 |
* exercise 19). We gain considerable efficiency by requiring the terms to |
| 486 |
* be sufficiently normalized and sufficiently increasing. |
| 487 |
*/ |
| 488 |
#define _3sumF(a, b, c) do { \ |
| 489 |
__typeof(a) __tmp; \ |
| 490 |
\ |
| 491 |
__tmp = (c); \ |
| 492 |
_2sumF(__tmp, (a)); \ |
| 493 |
(b) += (a); \ |
| 494 |
(a) = __tmp; \ |
| 495 |
} while (0) |
| 496 |
|
| 497 |
/* |
| 498 |
* Common routine to process the arguments to nan(), nanf(), and nanl(). |
| 499 |
*/ |
| 500 |
void _scan_nan(uint32_t *__words, int __num_words, const char *__s); |
| 501 |
|
| 502 |
/* |
| 503 |
* Mix 0, 1 or 2 NaNs. First add 0 to each arg. This normally just turns |
| 504 |
* signaling NaNs into quiet NaNs by setting a quiet bit. We do this |
| 505 |
* because we want to never return a signaling NaN, and also because we |
| 506 |
* don't want the quiet bit to affect the result. Then mix the converted |
| 507 |
* args using the specified operation. |
| 508 |
* |
| 509 |
* When one arg is NaN, the result is typically that arg quieted. When both |
| 510 |
* args are NaNs, the result is typically the quietening of the arg whose |
| 511 |
* mantissa is largest after quietening. When neither arg is NaN, the |
| 512 |
* result may be NaN because it is indeterminate, or finite for subsequent |
| 513 |
* construction of a NaN as the indeterminate 0.0L/0.0L. |
| 514 |
* |
| 515 |
* Technical complications: the result in bits after rounding to the final |
| 516 |
* precision might depend on the runtime precision and/or on compiler |
| 517 |
* optimizations, especially when different register sets are used for |
| 518 |
* different precisions. Try to make the result not depend on at least the |
| 519 |
* runtime precision by always doing the main mixing step in long double |
| 520 |
* precision. Try to reduce dependencies on optimizations by adding the |
| 521 |
* the 0's in different precisions (unless everything is in long double |
| 522 |
* precision). |
| 523 |
*/ |
| 524 |
#define nan_mix(x, y) (nan_mix_op((x), (y), +)) |
| 525 |
#define nan_mix_op(x, y, op) (((x) + 0.0L) op ((y) + 0)) |
| 526 |
|
| 527 |
#ifdef _COMPLEX_H |
| 528 |
|
| 529 |
/* |
| 530 |
* C99 specifies that complex numbers have the same representation as |
| 531 |
* an array of two elements, where the first element is the real part |
| 532 |
* and the second element is the imaginary part. |
| 533 |
*/ |
| 534 |
typedef union { |
| 535 |
float complex f; |
| 536 |
float a[2]; |
| 537 |
} float_complex; |
| 538 |
typedef union { |
| 539 |
double complex f; |
| 540 |
double a[2]; |
| 541 |
} double_complex; |
| 542 |
typedef union { |
| 543 |
long double complex f; |
| 544 |
long double a[2]; |
| 545 |
} long_double_complex; |
| 546 |
#define REALPART(z) ((z).a[0]) |
| 547 |
#define IMAGPART(z) ((z).a[1]) |
| 548 |
|
| 549 |
/* |
| 550 |
* Inline functions that can be used to construct complex values. |
| 551 |
* |
| 552 |
* The C99 standard intends x+I*y to be used for this, but x+I*y is |
| 553 |
* currently unusable in general since gcc introduces many overflow, |
| 554 |
* underflow, sign and efficiency bugs by rewriting I*y as |
| 555 |
* (0.0+I)*(y+0.0*I) and laboriously computing the full complex product. |
| 556 |
* In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted |
| 557 |
* to -0.0+I*0.0. |
| 558 |
* |
| 559 |
* The C11 standard introduced the macros CMPLX(), CMPLXF() and CMPLXL() |
| 560 |
* to construct complex values. Compilers that conform to the C99 |
| 561 |
* standard require the following functions to avoid the above issues. |
| 562 |
*/ |
| 563 |
|
| 564 |
#ifndef CMPLXF |
| 565 |
static __inline float complex |
| 566 |
CMPLXF(float x, float y) |
| 567 |
{ |
| 568 |
float_complex z; |
| 569 |
|
| 570 |
REALPART(z) = x; |
| 571 |
IMAGPART(z) = y; |
| 572 |
return (z.f); |
| 573 |
} |
| 574 |
#endif |
| 575 |
|
| 576 |
#ifndef CMPLX |
| 577 |
static __inline double complex |
| 578 |
CMPLX(double x, double y) |
| 579 |
{ |
| 580 |
double_complex z; |
| 581 |
|
| 582 |
REALPART(z) = x; |
| 583 |
IMAGPART(z) = y; |
| 584 |
return (z.f); |
| 585 |
} |
| 586 |
#endif |
| 587 |
|
| 588 |
#ifndef CMPLXL |
| 589 |
static __inline long double complex |
| 590 |
CMPLXL(long double x, long double y) |
| 591 |
{ |
| 592 |
long_double_complex z; |
| 593 |
|
| 594 |
REALPART(z) = x; |
| 595 |
IMAGPART(z) = y; |
| 596 |
return (z.f); |
| 597 |
} |
| 598 |
#endif |
| 599 |
|
| 600 |
#endif /* _COMPLEX_H */ |
| 601 |
|
| 602 |
/* |
| 603 |
* The rnint() family rounds to the nearest integer for a restricted range |
| 604 |
* range of args (up to about 2**MANT_DIG). We assume that the current |
| 605 |
* rounding mode is FE_TONEAREST so that this can be done efficiently. |
| 606 |
* Extra precision causes more problems in practice, and we only centralize |
| 607 |
* this here to reduce those problems, and have not solved the efficiency |
| 608 |
* problems. The exp2() family uses a more delicate version of this that |
| 609 |
* requires extracting bits from the intermediate value, so it is not |
| 610 |
* centralized here and should copy any solution of the efficiency problems. |
| 611 |
*/ |
| 612 |
|
| 613 |
static inline double |
| 614 |
rnint(__double_t x) |
| 615 |
{ |
| 616 |
/* |
| 617 |
* This casts to double to kill any extra precision. This depends |
| 618 |
* on the cast being applied to a double_t to avoid compiler bugs |
| 619 |
* (this is a cleaner version of STRICT_ASSIGN()). This is |
| 620 |
* inefficient if there actually is extra precision, but is hard |
| 621 |
* to improve on. We use double_t in the API to minimise conversions |
| 622 |
* for just calling here. Note that we cannot easily change the |
| 623 |
* magic number to the one that works directly with double_t, since |
| 624 |
* the rounding precision is variable at runtime on x86 so the |
| 625 |
* magic number would need to be variable. Assuming that the |
| 626 |
* rounding precision is always the default is too fragile. This |
| 627 |
* and many other complications will move when the default is |
| 628 |
* changed to FP_PE. |
| 629 |
*/ |
| 630 |
return ((double)(x + 0x1.8p52) - 0x1.8p52); |
| 631 |
} |
| 632 |
|
| 633 |
/* |
| 634 |
* irint() and i64rint() give the same result as casting to their integer |
| 635 |
* return type provided their arg is a floating point integer. They can |
| 636 |
* sometimes be more efficient because no rounding is required. |
| 637 |
*/ |
| 638 |
#if defined(amd64) || defined(__i386__) |
| 639 |
#define irint(x) \ |
| 640 |
(sizeof(x) == sizeof(float) && \ |
| 641 |
sizeof(__float_t) == sizeof(long double) ? irintf(x) : \ |
| 642 |
sizeof(x) == sizeof(double) && \ |
| 643 |
sizeof(__double_t) == sizeof(long double) ? irintd(x) : \ |
| 644 |
sizeof(x) == sizeof(long double) ? irintl(x) : (int)(x)) |
| 645 |
#else |
| 646 |
#define irint(x) ((int)(x)) |
| 647 |
#endif |
| 648 |
|
| 649 |
#define i64rint(x) ((int64_t)(x)) /* only needed for ld128 so not opt. */ |
| 650 |
|
| 651 |
#if defined(__i386__) |
| 652 |
static __inline int |
| 653 |
irintf(float x) |
| 654 |
{ |
| 655 |
int n; |
| 656 |
|
| 657 |
__asm("fistl %0" : "=m" (n) : "t" (x)); |
| 658 |
return (n); |
| 659 |
} |
| 660 |
|
| 661 |
static __inline int |
| 662 |
irintd(double x) |
| 663 |
{ |
| 664 |
int n; |
| 665 |
|
| 666 |
__asm("fistl %0" : "=m" (n) : "t" (x)); |
| 667 |
return (n); |
| 668 |
} |
| 669 |
#endif |
| 670 |
|
| 671 |
#if defined(__amd64__) || defined(__i386__) |
| 672 |
static __inline int |
| 673 |
irintl(long double x) |
| 674 |
{ |
| 675 |
int n; |
| 676 |
|
| 677 |
__asm("fistl %0" : "=m" (n) : "t" (x)); |
| 678 |
return (n); |
| 679 |
} |
| 680 |
#endif |
| 681 |
|
| 682 |
#ifdef DEBUG |
| 683 |
#if defined(__amd64__) || defined(__i386__) |
| 684 |
#define breakpoint() asm("int $3") |
| 685 |
#elif defined(__wasm__) |
| 686 |
#define breakpoint() __builtin_trap() |
| 687 |
#else |
| 688 |
#include <signal.h> |
| 689 |
|
| 690 |
#define breakpoint() raise(SIGTRAP) |
| 691 |
#endif |
| 692 |
#endif |
| 693 |
|
| 694 |
/* Write a pari script to test things externally. */ |
| 695 |
#ifdef DOPRINT |
| 696 |
#include <stdio.h> |
| 697 |
|
| 698 |
#ifndef DOPRINT_SWIZZLE |
| 699 |
#define DOPRINT_SWIZZLE 0 |
| 700 |
#endif |
| 701 |
|
| 702 |
#ifdef DOPRINT_LD80 |
| 703 |
|
| 704 |
#define DOPRINT_START(xp) do { \ |
| 705 |
uint64_t __lx; \ |
| 706 |
uint16_t __hx; \ |
| 707 |
\ |
| 708 |
/* Hack to give more-problematic args. */ \ |
| 709 |
EXTRACT_LDBL80_WORDS(__hx, __lx, *xp); \ |
| 710 |
__lx ^= DOPRINT_SWIZZLE; \ |
| 711 |
INSERT_LDBL80_WORDS(*xp, __hx, __lx); \ |
| 712 |
printf("x = %.21Lg; ", (long double)*xp); \ |
| 713 |
} while (0) |
| 714 |
#define DOPRINT_END1(v) \ |
| 715 |
printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v)) |
| 716 |
#define DOPRINT_END2(hi, lo) \ |
| 717 |
printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \ |
| 718 |
(long double)(hi), (long double)(lo)) |
| 719 |
|
| 720 |
#elif defined(DOPRINT_D64) |
| 721 |
|
| 722 |
#define DOPRINT_START(xp) do { \ |
| 723 |
uint32_t __hx, __lx; \ |
| 724 |
\ |
| 725 |
EXTRACT_WORDS(__hx, __lx, *xp); \ |
| 726 |
__lx ^= DOPRINT_SWIZZLE; \ |
| 727 |
INSERT_WORDS(*xp, __hx, __lx); \ |
| 728 |
printf("x = %.21Lg; ", (long double)*xp); \ |
| 729 |
} while (0) |
| 730 |
#define DOPRINT_END1(v) \ |
| 731 |
printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v)) |
| 732 |
#define DOPRINT_END2(hi, lo) \ |
| 733 |
printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \ |
| 734 |
(long double)(hi), (long double)(lo)) |
| 735 |
|
| 736 |
#elif defined(DOPRINT_F32) |
| 737 |
|
| 738 |
#define DOPRINT_START(xp) do { \ |
| 739 |
uint32_t __hx; \ |
| 740 |
\ |
| 741 |
GET_FLOAT_WORD(__hx, *xp); \ |
| 742 |
__hx ^= DOPRINT_SWIZZLE; \ |
| 743 |
SET_FLOAT_WORD(*xp, __hx); \ |
| 744 |
printf("x = %.21Lg; ", (long double)*xp); \ |
| 745 |
} while (0) |
| 746 |
#define DOPRINT_END1(v) \ |
| 747 |
printf("y = %.21Lg; z = 0; show(x, y, z);\n", (long double)(v)) |
| 748 |
#define DOPRINT_END2(hi, lo) \ |
| 749 |
printf("y = %.21Lg; z = %.21Lg; show(x, y, z);\n", \ |
| 750 |
(long double)(hi), (long double)(lo)) |
| 751 |
|
| 752 |
#else /* !DOPRINT_LD80 && !DOPRINT_D64 (LD128 only) */ |
| 753 |
|
| 754 |
#ifndef DOPRINT_SWIZZLE_HIGH |
| 755 |
#define DOPRINT_SWIZZLE_HIGH 0 |
| 756 |
#endif |
| 757 |
|
| 758 |
#define DOPRINT_START(xp) do { \ |
| 759 |
uint64_t __lx, __llx; \ |
| 760 |
uint16_t __hx; \ |
| 761 |
\ |
| 762 |
EXTRACT_LDBL128_WORDS(__hx, __lx, __llx, *xp); \ |
| 763 |
__llx ^= DOPRINT_SWIZZLE; \ |
| 764 |
__lx ^= DOPRINT_SWIZZLE_HIGH; \ |
| 765 |
INSERT_LDBL128_WORDS(*xp, __hx, __lx, __llx); \ |
| 766 |
printf("x = %.36Lg; ", (long double)*xp); \ |
| 767 |
} while (0) |
| 768 |
#define DOPRINT_END1(v) \ |
| 769 |
printf("y = %.36Lg; z = 0; show(x, y, z);\n", (long double)(v)) |
| 770 |
#define DOPRINT_END2(hi, lo) \ |
| 771 |
printf("y = %.36Lg; z = %.36Lg; show(x, y, z);\n", \ |
| 772 |
(long double)(hi), (long double)(lo)) |
| 773 |
|
| 774 |
#endif /* DOPRINT_LD80 */ |
| 775 |
|
| 776 |
#else /* !DOPRINT */ |
| 777 |
#define DOPRINT_START(xp) |
| 778 |
#define DOPRINT_END1(v) |
| 779 |
#define DOPRINT_END2(hi, lo) |
| 780 |
#endif /* DOPRINT */ |
| 781 |
|
| 782 |
#define RETURNP(x) do { \ |
| 783 |
DOPRINT_END1(x); \ |
| 784 |
RETURNF(x); \ |
| 785 |
} while (0) |
| 786 |
#define RETURNPI(x) do { \ |
| 787 |
DOPRINT_END1(x); \ |
| 788 |
RETURNI(x); \ |
| 789 |
} while (0) |
| 790 |
#define RETURN2P(x, y) do { \ |
| 791 |
DOPRINT_END2((x), (y)); \ |
| 792 |
RETURNF((x) + (y)); \ |
| 793 |
} while (0) |
| 794 |
#define RETURN2PI(x, y) do { \ |
| 795 |
DOPRINT_END2((x), (y)); \ |
| 796 |
RETURNI((x) + (y)); \ |
| 797 |
} while (0) |
| 798 |
#ifdef STRUCT_RETURN |
| 799 |
#define RETURNSP(rp) do { \ |
| 800 |
if (!(rp)->lo_set) \ |
| 801 |
RETURNP((rp)->hi); \ |
| 802 |
RETURN2P((rp)->hi, (rp)->lo); \ |
| 803 |
} while (0) |
| 804 |
#define RETURNSPI(rp) do { \ |
| 805 |
if (!(rp)->lo_set) \ |
| 806 |
RETURNPI((rp)->hi); \ |
| 807 |
RETURN2PI((rp)->hi, (rp)->lo); \ |
| 808 |
} while (0) |
| 809 |
#endif |
| 810 |
#define SUM2P(x, y) ({ \ |
| 811 |
const __typeof (x) __x = (x); \ |
| 812 |
const __typeof (y) __y = (y); \ |
| 813 |
\ |
| 814 |
DOPRINT_END2(__x, __y); \ |
| 815 |
__x + __y; \ |
| 816 |
}) |
| 817 |
|
| 818 |
/* |
| 819 |
* ieee style elementary functions |
| 820 |
* |
| 821 |
* We rename functions here to improve other sources' diffability |
| 822 |
* against fdlibm. |
| 823 |
*/ |
| 824 |
#define __ieee754_sqrt sqrt |
| 825 |
#define __ieee754_acos acos |
| 826 |
#define __ieee754_acosh acosh |
| 827 |
#define __ieee754_log log |
| 828 |
#define __ieee754_log2 log2 |
| 829 |
#define __ieee754_atanh atanh |
| 830 |
#define __ieee754_asin asin |
| 831 |
#define __ieee754_atan2 atan2 |
| 832 |
#define __ieee754_exp exp |
| 833 |
#define __ieee754_cosh cosh |
| 834 |
#define __ieee754_fmod fmod |
| 835 |
#define __ieee754_pow pow |
| 836 |
#define __ieee754_lgamma lgamma |
| 837 |
#define __ieee754_gamma gamma |
| 838 |
#define __ieee754_lgamma_r lgamma_r |
| 839 |
#define __ieee754_gamma_r gamma_r |
| 840 |
#define __ieee754_log10 log10 |
| 841 |
#define __ieee754_sinh sinh |
| 842 |
#define __ieee754_hypot hypot |
| 843 |
#define __ieee754_j0 j0 |
| 844 |
#define __ieee754_j1 j1 |
| 845 |
#define __ieee754_y0 y0 |
| 846 |
#define __ieee754_y1 y1 |
| 847 |
#define __ieee754_jn jn |
| 848 |
#define __ieee754_yn yn |
| 849 |
#define __ieee754_remainder remainder |
| 850 |
#define __ieee754_scalb scalb |
| 851 |
#define __ieee754_sqrtf sqrtf |
| 852 |
#define __ieee754_acosf acosf |
| 853 |
#define __ieee754_acoshf acoshf |
| 854 |
#define __ieee754_logf logf |
| 855 |
#define __ieee754_atanhf atanhf |
| 856 |
#define __ieee754_asinf asinf |
| 857 |
#define __ieee754_atan2f atan2f |
| 858 |
#define __ieee754_expf expf |
| 859 |
#define __ieee754_coshf coshf |
| 860 |
#define __ieee754_fmodf fmodf |
| 861 |
#define __ieee754_powf powf |
| 862 |
#define __ieee754_lgammaf lgammaf |
| 863 |
#define __ieee754_gammaf gammaf |
| 864 |
#define __ieee754_lgammaf_r lgammaf_r |
| 865 |
#define __ieee754_gammaf_r gammaf_r |
| 866 |
#define __ieee754_log10f log10f |
| 867 |
#define __ieee754_log2f log2f |
| 868 |
#define __ieee754_sinhf sinhf |
| 869 |
#define __ieee754_hypotf hypotf |
| 870 |
#define __ieee754_j0f j0f |
| 871 |
#define __ieee754_j1f j1f |
| 872 |
#define __ieee754_y0f y0f |
| 873 |
#define __ieee754_y1f y1f |
| 874 |
#define __ieee754_jnf jnf |
| 875 |
#define __ieee754_ynf ynf |
| 876 |
#define __ieee754_remainderf remainderf |
| 877 |
#define __ieee754_scalbf scalbf |
| 878 |
|
| 879 |
#define acos fdlibm_acos |
| 880 |
#define acosf fdlibm_acosf |
| 881 |
#define asin fdlibm_asin |
| 882 |
#define asinf fdlibm_asinf |
| 883 |
#define atan fdlibm_atan |
| 884 |
#define atanf fdlibm_atanf |
| 885 |
#define atan2 fdlibm_atan2 |
| 886 |
#define cos fdlibm_cos |
| 887 |
#define cosf fdlibm_cosf |
| 888 |
#define sin fdlibm_sin |
| 889 |
#define sinf fdlibm_sinf |
| 890 |
#define tan fdlibm_tan |
| 891 |
#define tanf fdlibm_tanf |
| 892 |
#define cosh fdlibm_cosh |
| 893 |
#define sinh fdlibm_sinh |
| 894 |
#define tanh fdlibm_tanh |
| 895 |
#define exp fdlibm_exp |
| 896 |
#define expf fdlibm_expf |
| 897 |
#define exp2 fdlibm_exp2 |
| 898 |
#define exp2f fdlibm_exp2f |
| 899 |
#define log fdlibm_log |
| 900 |
#define logf fdlibm_logf |
| 901 |
#define log10 fdlibm_log10 |
| 902 |
#define log10f fdlibm_log10f |
| 903 |
#define pow fdlibm_pow |
| 904 |
#define powf fdlibm_powf |
| 905 |
#define ceil fdlibm_ceil |
| 906 |
#define ceilf fdlibm_ceilf |
| 907 |
#define fabs fdlibm_fabs |
| 908 |
#define fabsf fdlibm_fabsf |
| 909 |
#define floor fdlibm_floor |
| 910 |
#define acosh fdlibm_acosh |
| 911 |
#define asinh fdlibm_asinh |
| 912 |
#define atanh fdlibm_atanh |
| 913 |
#define cbrt fdlibm_cbrt |
| 914 |
#define expm1 fdlibm_expm1 |
| 915 |
#define hypot fdlibm_hypot |
| 916 |
#define hypotf fdlibm_hypotf |
| 917 |
#define log1p fdlibm_log1p |
| 918 |
#define log2 fdlibm_log2 |
| 919 |
#define scalb fdlibm_scalb |
| 920 |
#define copysign fdlibm_copysign |
| 921 |
#define scalbn fdlibm_scalbn |
| 922 |
#define scalbnf fdlibm_scalbnf |
| 923 |
#define trunc fdlibm_trunc |
| 924 |
#define truncf fdlibm_truncf |
| 925 |
#define floorf fdlibm_floorf |
| 926 |
#define nearbyint fdlibm_nearbyint |
| 927 |
#define nearbyintf fdlibm_nearbyintf |
| 928 |
#define rint fdlibm_rint |
| 929 |
#define rintf fdlibm_rintf |
| 930 |
#define sqrtf fdlibm_sqrtf |
| 931 |
|
| 932 |
/* fdlibm kernel function */ |
| 933 |
int __kernel_rem_pio2(double*,double*,int,int,int); |
| 934 |
|
| 935 |
/* double precision kernel functions */ |
| 936 |
#ifndef INLINE_REM_PIO2 |
| 937 |
int __ieee754_rem_pio2(double,double*); |
| 938 |
#endif |
| 939 |
double __kernel_sin(double,double,int); |
| 940 |
double __kernel_cos(double,double); |
| 941 |
double __kernel_tan(double,double,int); |
| 942 |
double __ldexp_exp(double,int); |
| 943 |
#ifdef _COMPLEX_H |
| 944 |
double complex __ldexp_cexp(double complex,int); |
| 945 |
#endif |
| 946 |
|
| 947 |
/* float precision kernel functions */ |
| 948 |
#ifndef INLINE_REM_PIO2F |
| 949 |
int __ieee754_rem_pio2f(float,double*); |
| 950 |
#endif |
| 951 |
#ifndef INLINE_KERNEL_SINDF |
| 952 |
float __kernel_sindf(double); |
| 953 |
#endif |
| 954 |
#ifndef INLINE_KERNEL_COSDF |
| 955 |
float __kernel_cosdf(double); |
| 956 |
#endif |
| 957 |
#ifndef INLINE_KERNEL_TANDF |
| 958 |
float __kernel_tandf(double,int); |
| 959 |
#endif |
| 960 |
float __ldexp_expf(float,int); |
| 961 |
#ifdef _COMPLEX_H |
| 962 |
float complex __ldexp_cexpf(float complex,int); |
| 963 |
#endif |
| 964 |
|
| 965 |
/* long double precision kernel functions */ |
| 966 |
long double __kernel_sinl(long double, long double, int); |
| 967 |
long double __kernel_cosl(long double, long double); |
| 968 |
long double __kernel_tanl(long double, long double, int); |
| 969 |
|
| 970 |
#endif /* !_MATH_PRIVATE_H_ */ |