Skip to main content

nodejs/
ieee754.rs

1//! The `Math` transcendental functions, ported from V8's `ieee754.cc` (itself
2//! Sun's fdlibm 5.3 plus a few FreeBSD msun routines).
3//!
4//! The platform libm is not a substitute: Rust's `f64::sin` and friends call
5//! the OS `libm`, whose results differ from V8's in the last digit for a few
6//! percent of inputs, and they differ between macOS and Linux. V8 carries its
7//! own copy so `Math.cos(0.1)` is the same double everywhere; this module is
8//! that copy.
9//!
10//! **Contraction.** Node built by clang for AArch64 compiles each fdlibm
11//! expression with `-ffp-contract=on`: within ONE C expression, `a*b + c` and
12//! `c - a*b` become a single fused multiply-add (`llvm.fmuladd`, which the
13//! AArch64 backend lowers to `fmadd`/`fmsub`). The reference `node` on the
14//! primary development machine therefore returns different last digits from an
15//! unfused evaluation of the same source — measured: `Math.atan` differs on
16//! 4 of 3600 samples unfused and on 0 fused. Every expression below is written
17//! with `mul_add` exactly where clang fuses it (the multiply is the left
18//! operand if there is one, else the right; statements are never fused across a
19//! `;`), so the results are bit-identical to that node. `mul_add` is a true
20//! fused operation on every target, so the results are also identical across
21//! architectures.
22
23use std::f64::consts::PI;
24
25/// 64-bit view helpers for the fdlibm `GET_HIGH_WORD`/`SET_LOW_WORD` family.
26fn hi(x: f64) -> i32 {
27    (x.to_bits() >> 32) as i32
28}
29
30fn lo(x: f64) -> u32 {
31    x.to_bits() as u32
32}
33
34fn from_words(hi: i32, lo: u32) -> f64 {
35    f64::from_bits(((hi as u32 as u64) << 32) | lo as u64)
36}
37
38fn with_hi(x: f64, hi: i32) -> f64 {
39    from_words(hi, lo(x))
40}
41
42fn with_lo(x: f64, lo: u32) -> f64 {
43    from_words(self::hi(x), lo)
44}
45
46fn fma(a: f64, b: f64, c: f64) -> f64 {
47    a.mul_add(b, c)
48}
49
50const TWO24: f64 = 16777216.0;
51const TWON24: f64 = 5.960464477539063e-8;
52
53/// `2/pi` in 24-bit chunks (fdlibm `two_over_pi`).
54const TWO_OVER_PI: [i32; 66] = [
55    0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62, 0x95993C, 0x439041, 0xFE5163,
56    0xABDEBB, 0xC561B7, 0x246E3A, 0x424DD2, 0xE00649, 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129,
57    0xA73EE8, 0x8235F5, 0x2EBB44, 0x84E99C, 0x7026B4, 0x5F7E41, 0x3991D6, 0x398353, 0x39F49C,
58    0x845F8B, 0xBDF928, 0x3B1FF8, 0x97FFDE, 0x05980F, 0xEF2F11, 0x8B5A0A, 0x6D1F6D, 0x367ECF,
59    0x27CB09, 0xB74F46, 0x3F669E, 0x5FEA2D, 0x7527BA, 0xC7EBE5, 0xF17B3D, 0x0739F7, 0x8A5292,
60    0xEA6BFB, 0x5FB11F, 0x8D5D08, 0x560330, 0x46FC7B, 0x6BABF0, 0xCFBC20, 0x9AF436, 0x1DA9E3,
61    0x91615E, 0xE61B08, 0x659985, 0x5F14A0, 0x68408D, 0xFFD880, 0x4D7327, 0x310606, 0x1556CA,
62    0x73A8C9, 0x60E27B, 0xC08C6B,
63];
64
65/// `pi/2` in 24-bit chunks (fdlibm `PIo2`).
66const PIO2: [f64; 8] = [
67    1.570_796_251_296_997,
68    7.549_789_415_861_596e-8,
69    5.390_302_529_957_765e-15,
70    3.282_003_415_807_913e-22,
71    1.270_655_753_080_676e-29,
72    1.229_333_089_811_113_3e-36,
73    2.733_700_538_164_645_6e-44,
74    2.167_416_838_778_048_2e-51,
75];
76
77/// fdlibm `__kernel_rem_pio2` for double precision (`prec == 2`, `jk == 4`):
78/// `y = x - n*pi/2` for a huge `x` given as 24-bit chunks `tx` with scaled
79/// exponent `e0`. Returns `n & 7`.
80fn kernel_rem_pio2(tx: &[f64; 3], y: &mut [f64; 2], e0: i32, nx: usize) -> i32 {
81    const JK: usize = 4;
82    let jp = JK;
83    let mut iq = [0i32; 20];
84    let mut f = [0f64; 20];
85    let mut fq = [0f64; 20];
86    let mut q = [0f64; 20];
87
88    let jx = nx - 1;
89    let jv = ((e0 - 3) / 24).max(0) as usize;
90    let mut q0 = e0 - 24 * (jv as i32 + 1);
91
92    let first = jv as i32 - jx as i32;
93    for (k, fi) in f.iter_mut().take(jx + JK + 1).enumerate() {
94        let j = first + k as i32;
95        *fi = if j < 0 {
96            0.0
97        } else {
98            TWO_OVER_PI[j as usize] as f64
99        };
100    }
101    for i in 0..=JK {
102        let mut fw = 0.0;
103        for j in 0..=jx {
104            fw = fma(tx[j], f[jx + i - j], fw);
105        }
106        q[i] = fw;
107    }
108
109    let mut jz = JK;
110    let mut z;
111    let mut n;
112    let mut ih;
113    loop {
114        // Distill q[] into iq[] reversingly.
115        z = q[jz];
116        for (i, j) in (1..=jz).rev().enumerate() {
117            let fw = (TWON24 * z) as i32 as f64;
118            iq[i] = fma(-TWO24, fw, z) as i32;
119            z = q[j - 1] + fw;
120        }
121        // Compute n.
122        z = scalbn(z, q0);
123        z = fma(-8.0, (z * 0.125).floor(), z);
124        n = z as i32;
125        z -= n as f64;
126        ih = 0;
127        if q0 > 0 {
128            let i = iq[jz - 1] >> (24 - q0);
129            n += i;
130            iq[jz - 1] -= i << (24 - q0);
131            ih = iq[jz - 1] >> (23 - q0);
132        } else if q0 == 0 {
133            ih = iq[jz - 1] >> 23;
134        } else if z >= 0.5 {
135            ih = 2;
136        }
137        if ih > 0 {
138            n += 1;
139            let mut carry = 0;
140            for e in iq.iter_mut().take(jz) {
141                let j = *e;
142                if carry == 0 {
143                    if j != 0 {
144                        carry = 1;
145                        *e = 0x1000000 - j;
146                    }
147                } else {
148                    *e = 0xffffff - j;
149                }
150            }
151            if q0 > 0 {
152                match q0 {
153                    1 => iq[jz - 1] &= 0x7fffff,
154                    2 => iq[jz - 1] &= 0x3fffff,
155                    _ => {}
156                }
157            }
158            if ih == 2 {
159                z = 1.0 - z;
160                if carry != 0 {
161                    z -= scalbn(1.0, q0);
162                }
163            }
164        }
165        // Recompute when the leading terms cancelled completely.
166        if z == 0.0 {
167            let mut j = 0;
168            for e in iq.iter().take(jz).skip(JK) {
169                j |= *e;
170            }
171            if j == 0 {
172                let mut k = 1;
173                while iq[JK - k] == 0 {
174                    k += 1;
175                }
176                for i in jz + 1..=jz + k {
177                    f[jx + i] = TWO_OVER_PI[jv + i] as f64;
178                    let mut fw = 0.0;
179                    for j in 0..=jx {
180                        fw = fma(tx[j], f[jx + i - j], fw);
181                    }
182                    q[i] = fw;
183                }
184                jz += k;
185                continue;
186            }
187        }
188        break;
189    }
190
191    // Chop off zero terms.
192    if z == 0.0 {
193        jz -= 1;
194        q0 -= 24;
195        while iq[jz] == 0 {
196            jz -= 1;
197            q0 -= 24;
198        }
199    } else {
200        z = scalbn(z, -q0);
201        if z >= TWO24 {
202            let fw = (TWON24 * z) as i32 as f64;
203            iq[jz] = fma(-TWO24, fw, z) as i32;
204            jz += 1;
205            q0 += 24;
206            iq[jz] = fw as i32;
207        } else {
208            iq[jz] = z as i32;
209        }
210    }
211    // Convert the integer "bit" chunks to floating point.
212    let mut fw = scalbn(1.0, q0);
213    for i in (0..=jz).rev() {
214        q[i] = fw * iq[i] as f64;
215        fw *= TWON24;
216    }
217    // Compute PIo2[0..jp] * q[jz..0].
218    for i in (0..=jz).rev() {
219        let mut fw = 0.0;
220        let mut k = 0;
221        while k <= jp && k <= jz - i {
222            fw = fma(PIO2[k], q[i + k], fw);
223            k += 1;
224        }
225        fq[jz - i] = fw;
226    }
227    // Compress fq[] into y[].
228    let mut fw = 0.0;
229    for i in (0..=jz).rev() {
230        fw += fq[i];
231    }
232    y[0] = if ih == 0 { fw } else { -fw };
233    fw = fq[0] - fw;
234    for e in fq.iter().take(jz + 1).skip(1) {
235        fw += *e;
236    }
237    y[1] = if ih == 0 { fw } else { -fw };
238    n & 7
239}
240
241/// `x * 2^n` (fdlibm `scalbn`), exact for every `n` that keeps the result a
242/// normal number or rounds once into the subnormal range.
243fn scalbn(x: f64, n: i32) -> f64 {
244    let p1023 = f64::from_bits(0x7fe0_0000_0000_0000);
245    let m1022_p53 = f64::from_bits(0x0010_0000_0000_0000) * f64::from_bits(0x4340_0000_0000_0000);
246    let mut x = x;
247    let mut n = n;
248    if n > 1023 {
249        x *= p1023;
250        n -= 1023;
251        if n > 1023 {
252            x *= p1023;
253            n = (n - 1023).min(1023);
254        }
255    } else if n < -1022 {
256        x *= m1022_p53;
257        n += 1022 - 53;
258        if n < -1022 {
259            x *= m1022_p53;
260            n = (n + 1022 - 53).max(-1022);
261        }
262    }
263    x * f64::from_bits(((0x3ff + n) as u64) << 52)
264}
265
266/// High words of `n * pi/2`, `n = 1..=32` (fdlibm `npio2_hw`).
267const NPIO2_HW: [i32; 32] = [
268    0x3FF921FB, 0x400921FB, 0x4012D97C, 0x401921FB, 0x401F6A7A, 0x4022D97C, 0x4025FDBB, 0x402921FB,
269    0x402C463A, 0x402F6A7A, 0x4031475C, 0x4032D97C, 0x40346B9C, 0x4035FDBB, 0x40378FDB, 0x403921FB,
270    0x403AB41B, 0x403C463A, 0x403DD85A, 0x403F6A7A, 0x40407E4C, 0x404147AC, 0x4042106C, 0x4042D97C,
271    0x4043A28C, 0x40446B9C, 0x404534AC, 0x4045FDBB, 0x4046C6CB, 0x40478FDB, 0x404858EB, 0x404921FB,
272];
273
274/// fdlibm `__ieee754_rem_pio2`: reduce `x` to `y[0] + y[1] = x - n*pi/2`
275/// with `|y| <= pi/4`; returns `n`.
276fn rem_pio2(x: f64, y: &mut [f64; 2]) -> i32 {
277    const INVPIO2: f64 = std::f64::consts::FRAC_2_PI;
278    const PIO2_1: f64 = 1.570_796_326_734_125_6;
279    const PIO2_1T: f64 = 6.077_100_506_506_192e-11;
280    const PIO2_2: f64 = 6.077_100_506_303_966e-11;
281    const PIO2_2T: f64 = 2.022_266_248_795_950_6e-21;
282    const PIO2_3: f64 = 2.022_266_248_711_166_5e-21;
283    const PIO2_3T: f64 = 8.478_427_660_368_9e-32;
284
285    let hx = hi(x);
286    let ix = hx & 0x7fffffff;
287    if ix <= 0x3fe921fb {
288        y[0] = x;
289        y[1] = 0.0;
290        return 0;
291    }
292    if ix < 0x4002d97c {
293        // |x| < 3pi/4, special case with n = +-1.
294        if hx > 0 {
295            let mut z = x - PIO2_1;
296            if ix != 0x3ff921fb {
297                y[0] = z - PIO2_1T;
298                y[1] = (z - y[0]) - PIO2_1T;
299            } else {
300                z -= PIO2_2;
301                y[0] = z - PIO2_2T;
302                y[1] = (z - y[0]) - PIO2_2T;
303            }
304            return 1;
305        }
306        let mut z = x + PIO2_1;
307        if ix != 0x3ff921fb {
308            y[0] = z + PIO2_1T;
309            y[1] = (z - y[0]) + PIO2_1T;
310        } else {
311            z += PIO2_2;
312            y[0] = z + PIO2_2T;
313            y[1] = (z - y[0]) + PIO2_2T;
314        }
315        return -1;
316    }
317    if ix <= 0x413921fb {
318        // |x| <= 2^19 * (pi/2), medium size.
319        let t = x.abs();
320        let n = fma(t, INVPIO2, 0.5) as i32;
321        let fn_ = n as f64;
322        let mut r = fma(-fn_, PIO2_1, t);
323        let mut w = fn_ * PIO2_1T;
324        if n < 32 && ix != NPIO2_HW[(n - 1) as usize] {
325            y[0] = r - w;
326        } else {
327            let j = ix >> 20;
328            y[0] = r - w;
329            let mut i = j - ((hi(y[0]) >> 20) & 0x7ff);
330            if i > 16 {
331                let t = r;
332                w = fn_ * PIO2_2;
333                r = t - w;
334                w = fma(fn_, PIO2_2T, -((t - r) - w));
335                y[0] = r - w;
336                i = j - ((hi(y[0]) >> 20) & 0x7ff);
337                if i > 49 {
338                    let t = r;
339                    w = fn_ * PIO2_3;
340                    r = t - w;
341                    w = fma(fn_, PIO2_3T, -((t - r) - w));
342                    y[0] = r - w;
343                }
344            }
345        }
346        y[1] = (r - y[0]) - w;
347        if hx < 0 {
348            y[0] = -y[0];
349            y[1] = -y[1];
350            return -n;
351        }
352        return n;
353    }
354    if ix >= 0x7ff00000 {
355        y[0] = f64::NAN;
356        y[1] = y[0];
357        return 0;
358    }
359    // Large arguments: split |x| into three 24-bit chunks.
360    let e0 = (ix >> 20) - 1046;
361    let mut z = from_words(ix - (e0 << 20), lo(x));
362    let mut tx = [0f64; 3];
363    for t in tx.iter_mut().take(2) {
364        *t = z as i32 as f64;
365        z = (z - *t) * TWO24;
366    }
367    tx[2] = z;
368    let mut nx = 3;
369    while tx[nx - 1] == 0.0 {
370        nx -= 1;
371    }
372    let n = kernel_rem_pio2(&tx, y, e0, nx);
373    if hx < 0 {
374        y[0] = -y[0];
375        y[1] = -y[1];
376        return -n;
377    }
378    n
379}
380
381fn kernel_sin(x: f64, y: f64, iy: i32) -> f64 {
382    const S1: f64 = -1.666_666_666_666_663_2e-1;
383    const S2: f64 = 8.333_333_333_322_49e-3;
384    const S3: f64 = -1.984_126_982_985_795e-4;
385    const S4: f64 = 2.755_731_370_707_006_8e-6;
386    const S5: f64 = -2.505_076_025_340_686_3e-8;
387    const S6: f64 = 1.589_690_995_211_55e-10;
388    let ix = hi(x) & 0x7fffffff;
389    if ix < 0x3e400000 && x as i32 == 0 {
390        return x;
391    }
392    let z = x * x;
393    let v = z * x;
394    let r = fma(z, fma(z, fma(z, fma(z, S6, S5), S4), S3), S2);
395    if iy == 0 {
396        fma(v, fma(z, r, S1), x)
397    } else {
398        x - fma(-v, S1, fma(z, fma(0.5, y, -(v * r)), -y))
399    }
400}
401
402fn kernel_cos(x: f64, y: f64) -> f64 {
403    const C1: f64 = 4.166_666_666_666_66e-2;
404    const C2: f64 = -1.388_888_888_887_411e-3;
405    const C3: f64 = 2.480_158_728_947_673e-5;
406    const C4: f64 = -2.755_731_435_139_066_3e-7;
407    const C5: f64 = 2.087_572_321_298_175e-9;
408    const C6: f64 = -1.135_964_755_778_819_5e-11;
409    let ix = hi(x) & 0x7fffffff;
410    if ix < 0x3e400000 && x as i32 == 0 {
411        return 1.0;
412    }
413    let z = x * x;
414    let r = z * fma(z, fma(z, fma(z, fma(z, fma(z, C6, C5), C4), C3), C2), C1);
415    if ix < 0x3FD33333 {
416        1.0 - fma(0.5, z, -fma(z, r, -(x * y)))
417    } else {
418        let qx = if ix > 0x3fe90000 {
419            0.28125
420        } else {
421            from_words(ix - 0x00200000, 0)
422        };
423        let hz = fma(0.5, z, -qx);
424        let a = 1.0 - qx;
425        a - (hz - fma(z, r, -(x * y)))
426    }
427}
428
429fn kernel_tan(x: f64, y: f64, iy: i32) -> f64 {
430    const T: [f64; 13] = [
431        3.333_333_333_333_341e-1,
432        1.333_333_333_332_012_4e-1,
433        5.396_825_397_622_605e-2,
434        2.186_948_829_485_954_2e-2,
435        8.863_239_823_599_3e-3,
436        3.592_079_107_591_312_4e-3,
437        1.456_209_454_325_290_3e-3,
438        5.880_412_408_202_641e-4,
439        2.464_631_348_184_699e-4,
440        7.817_944_429_395_571e-5,
441        7.140_724_913_826_082e-5,
442        -1.855_863_748_552_754_6e-5,
443        2.590_730_518_636_337e-5,
444    ];
445    const PIO4: f64 = std::f64::consts::FRAC_PI_4;
446    const PIO4LO: f64 = 3.061_616_997_868_383e-17;
447    let mut x = x;
448    let mut y = y;
449    let hx = hi(x);
450    let ix = hx & 0x7fffffff;
451    if ix < 0x3e300000 && x as i32 == 0 {
452        if ((ix as u32 | lo(x)) | (iy + 1) as u32) == 0 {
453            return 1.0 / x.abs();
454        } else if iy == 1 {
455            return x;
456        }
457        // -1/(x+y) computed carefully.
458        let w = x + y;
459        let z = with_lo(w, 0);
460        let v = y - (z - x);
461        let a = -1.0 / w;
462        let t = with_lo(a, 0);
463        let s = fma(t, z, 1.0);
464        return fma(a, fma(t, v, s), t);
465    }
466    if ix >= 0x3FE59428 {
467        if hx < 0 {
468            x = -x;
469            y = -y;
470        }
471        let z = PIO4 - x;
472        let w = PIO4LO - y;
473        x = z + w;
474        y = 0.0;
475    }
476    let z = x * x;
477    let w = z * z;
478    let r = fma(
479        w,
480        fma(w, fma(w, fma(w, fma(w, T[11], T[9]), T[7]), T[5]), T[3]),
481        T[1],
482    );
483    let v = z * fma(
484        w,
485        fma(w, fma(w, fma(w, fma(w, T[12], T[10]), T[8]), T[6]), T[4]),
486        T[2],
487    );
488    let s = z * x;
489    let mut r = fma(z, fma(s, r + v, y), y);
490    r = fma(T[0], s, r);
491    let w = x + r;
492    if ix >= 0x3FE59428 {
493        let v = iy as f64;
494        let sign = (1 - ((hx >> 30) & 2)) as f64;
495        return sign * fma(-2.0, x - (w * w / (w + v) - r), v);
496    }
497    if iy == 1 {
498        return w;
499    }
500    let z = with_lo(w, 0);
501    let v = r - (z - x);
502    let a = -1.0 / w;
503    let t = with_lo(a, 0);
504    let s = fma(t, z, 1.0);
505    fma(a, fma(t, v, s), t)
506}
507
508pub fn sin(x: f64) -> f64 {
509    let ix = hi(x) & 0x7fffffff;
510    if ix <= 0x3fe921fb {
511        return kernel_sin(x, 0.0, 0);
512    }
513    if ix >= 0x7ff00000 {
514        return f64::NAN;
515    }
516    let mut y = [0.0; 2];
517    let n = rem_pio2(x, &mut y);
518    match n & 3 {
519        0 => kernel_sin(y[0], y[1], 1),
520        1 => kernel_cos(y[0], y[1]),
521        2 => -kernel_sin(y[0], y[1], 1),
522        _ => -kernel_cos(y[0], y[1]),
523    }
524}
525
526pub fn cos(x: f64) -> f64 {
527    let ix = hi(x) & 0x7fffffff;
528    if ix <= 0x3fe921fb {
529        return kernel_cos(x, 0.0);
530    }
531    if ix >= 0x7ff00000 {
532        return f64::NAN;
533    }
534    let mut y = [0.0; 2];
535    let n = rem_pio2(x, &mut y);
536    match n & 3 {
537        0 => kernel_cos(y[0], y[1]),
538        1 => -kernel_sin(y[0], y[1], 1),
539        2 => -kernel_cos(y[0], y[1]),
540        _ => kernel_sin(y[0], y[1], 1),
541    }
542}
543
544pub fn tan(x: f64) -> f64 {
545    let ix = hi(x) & 0x7fffffff;
546    if ix <= 0x3fe921fb {
547        return kernel_tan(x, 0.0, 1);
548    }
549    if ix >= 0x7ff00000 {
550        return f64::NAN;
551    }
552    let mut y = [0.0; 2];
553    let n = rem_pio2(x, &mut y);
554    kernel_tan(y[0], y[1], 1 - ((n & 1) << 1))
555}
556
557pub fn exp(x: f64) -> f64 {
558    const HALF: [f64; 2] = [0.5, -0.5];
559    const O_THRESHOLD: f64 = 7.097_827_128_933_84e2;
560    const U_THRESHOLD: f64 = -7.451_332_191_019_411e2;
561    const LN2_HI: [f64; 2] = [6.931_471_803_691_238e-1, -6.931_471_803_691_238e-1];
562    const LN2_LO: [f64; 2] = [1.908_214_929_270_587_7e-10, -1.908_214_929_270_587_7e-10];
563    const INVLN2: f64 = std::f64::consts::LOG2_E;
564    const P1: f64 = 1.666_666_666_666_660_2e-1;
565    const P2: f64 = -2.777_777_777_701_559_3e-3;
566    const P3: f64 = 6.613_756_321_437_934e-5;
567    const P4: f64 = -1.653_390_220_546_525_2e-6;
568    const P5: f64 = 4.138_136_797_057_238_5e-8;
569    const E: f64 = std::f64::consts::E;
570    const TWOM1000: f64 = 9.332_636_185_032_189e-302;
571
572    let mut x = x;
573    let hx = hi(x) as u32;
574    let xsb = ((hx >> 31) & 1) as usize;
575    let hx = hx & 0x7fffffff;
576    if hx >= 0x40862E42 {
577        if hx >= 0x7ff00000 {
578            if ((hx & 0xfffff) | lo(x)) != 0 {
579                return x + x;
580            }
581            return if xsb == 0 { x } else { 0.0 };
582        }
583        if x > O_THRESHOLD {
584            return f64::INFINITY;
585        }
586        if x < U_THRESHOLD {
587            return 0.0;
588        }
589    }
590    let (reduced_hi, reduced_lo, k);
591    if hx > 0x3fd62e42 {
592        if hx < 0x3FF0A2B2 {
593            // exp(1) is special-cased: the series below gets the last bit wrong.
594            if x == 1.0 {
595                return E;
596            }
597            reduced_hi = x - LN2_HI[xsb];
598            reduced_lo = LN2_LO[xsb];
599            k = 1 - xsb as i32 - xsb as i32;
600        } else {
601            k = fma(INVLN2, x, HALF[xsb]) as i32;
602            let t = k as f64;
603            reduced_hi = fma(-t, LN2_HI[0], x);
604            reduced_lo = t * LN2_LO[0];
605        }
606        x = reduced_hi - reduced_lo;
607    } else if hx < 0x3e300000 {
608        return 1.0 + x;
609    } else {
610        k = 0;
611        reduced_hi = 0.0;
612        reduced_lo = 0.0;
613    }
614    let t = x * x;
615    let c = fma(-t, fma(t, fma(t, fma(t, fma(t, P5, P4), P3), P2), P1), x);
616
617    let y = if k == 0 {
618        return 1.0 - ((x * c) / (c - 2.0) - x);
619    } else {
620        1.0 - ((reduced_lo - (x * c) / (2.0 - c)) - reduced_hi)
621    };
622    if k >= -1021 {
623        if k == 1024 {
624            return y * 2.0 * 8.98846567431158e307;
625        }
626        y * from_words(0x3ff00000 + (k << 20), 0)
627    } else {
628        y * from_words(0x3ff00000 + ((k + 1000) << 20), 0) * TWOM1000
629    }
630}
631
632pub fn expm1(x: f64) -> f64 {
633    const O_THRESHOLD: f64 = 7.097_827_128_933_84e2;
634    const LN2_HI: f64 = 6.931_471_803_691_238e-1;
635    const LN2_LO: f64 = 1.908_214_929_270_587_7e-10;
636    const INVLN2: f64 = std::f64::consts::LOG2_E;
637    const Q1: f64 = -3.333_333_333_333_313e-2;
638    const Q2: f64 = 1.587_301_587_254_814_6e-3;
639    const Q3: f64 = -7.936_507_578_674_88e-5;
640    const Q4: f64 = 4.008_217_827_329_362e-6;
641    const Q5: f64 = -2.010_992_181_836_243_7e-7;
642
643    let mut x = x;
644    let hx = hi(x) as u32;
645    let xsb = hx & 0x8000_0000;
646    let hx = hx & 0x7fff_ffff;
647    if hx >= 0x4043687A {
648        if hx >= 0x40862E42 {
649            if hx >= 0x7ff00000 {
650                if ((hx & 0xfffff) | lo(x)) != 0 {
651                    return x + x;
652                }
653                return if xsb == 0 { x } else { -1.0 };
654            }
655            if x > O_THRESHOLD {
656                return f64::INFINITY;
657            }
658        }
659        if xsb != 0 {
660            return -1.0;
661        }
662    }
663    let mut c = 0.0;
664    let k: i32;
665    if hx > 0x3fd62e42 {
666        let (reduced_hi, reduced_lo);
667        if hx < 0x3FF0A2B2 {
668            if xsb == 0 {
669                reduced_hi = x - LN2_HI;
670                reduced_lo = LN2_LO;
671                k = 1;
672            } else {
673                reduced_hi = x + LN2_HI;
674                reduced_lo = -LN2_LO;
675                k = -1;
676            }
677        } else {
678            k = fma(INVLN2, x, if xsb == 0 { 0.5 } else { -0.5 }) as i32;
679            let t = k as f64;
680            reduced_hi = fma(-t, LN2_HI, x);
681            reduced_lo = t * LN2_LO;
682        }
683        x = reduced_hi - reduced_lo;
684        c = (reduced_hi - x) - reduced_lo;
685    } else if hx < 0x3c900000 {
686        return x;
687    } else {
688        k = 0;
689    }
690    let hfx = 0.5 * x;
691    let hxs = x * hfx;
692    let r1 = fma(
693        hxs,
694        fma(hxs, fma(hxs, fma(hxs, fma(hxs, Q5, Q4), Q3), Q2), Q1),
695        1.0,
696    );
697    let t = fma(-r1, hfx, 3.0);
698    let mut e = hxs * ((r1 - t) / fma(-x, t, 6.0));
699    if k == 0 {
700        return x - fma(x, e, -hxs);
701    }
702    let twopk = from_words(0x3ff00000 + (k << 20), 0);
703    e = fma(x, e - c, -c);
704    e -= hxs;
705    if k == -1 {
706        return fma(0.5, x - e, -0.5);
707    }
708    if k == 1 {
709        if x < -0.25 {
710            return -2.0 * (e - (x + 0.5));
711        }
712        return fma(2.0, x - e, 1.0);
713    }
714    if k <= -2 || k > 56 {
715        let mut y = 1.0 - (e - x);
716        if k == 1024 {
717            y = y * 2.0 * 8.98846567431158e307;
718        } else {
719            y *= twopk;
720        }
721        return y - 1.0;
722    }
723
724    if k < 20 {
725        let t = from_words(0x3ff00000 - (0x200000 >> k), 0);
726        (t - (e - x)) * twopk
727    } else {
728        let t = from_words((0x3ff - k) << 20, 0);
729        let mut yy = x - (e + t);
730        yy += 1.0;
731        yy * twopk
732    }
733}
734
735pub fn log(x: f64) -> f64 {
736    const LN2_HI: f64 = 6.931_471_803_691_238e-1;
737    const LN2_LO: f64 = 1.908_214_929_270_587_7e-10;
738    const TWO54: f64 = 1.801_439_850_948_198_4e16;
739    const LG1: f64 = 6.666_666_666_666_735e-1;
740    const LG2: f64 = 3.999_999_999_940_942e-1;
741    const LG3: f64 = 2.857_142_874_366_239e-1;
742    const LG4: f64 = 2.222_219_843_214_978_4e-1;
743    const LG5: f64 = 1.818_357_216_161_805e-1;
744    const LG6: f64 = 1.531_383_769_920_937_3e-1;
745    const LG7: f64 = 1.479_819_860_511_658_6e-1;
746
747    let mut x = x;
748    let mut hx = hi(x);
749    let lx = lo(x);
750    let mut k = 0i32;
751    if hx < 0x00100000 {
752        if ((hx & 0x7fffffff) as u32 | lx) == 0 {
753            return f64::NEG_INFINITY;
754        }
755        if hx < 0 {
756            return f64::NAN;
757        }
758        k -= 54;
759        x *= TWO54;
760        hx = hi(x);
761    }
762    if hx >= 0x7ff00000 {
763        return x + x;
764    }
765    k += (hx >> 20) - 1023;
766    hx &= 0x000fffff;
767    let i = (hx + 0x95f64) & 0x100000;
768    x = with_hi(x, hx | (i ^ 0x3ff00000));
769    k += i >> 20;
770    let f = x - 1.0;
771    if (0x000fffff & (2 + hx)) < 3 {
772        if f == 0.0 {
773            if k == 0 {
774                return 0.0;
775            }
776            let dk = k as f64;
777            return fma(dk, LN2_HI, dk * LN2_LO);
778        }
779        let r = f * f * fma(-0.333_333_333_333_333_3, f, 0.5);
780        if k == 0 {
781            return f - r;
782        }
783        let dk = k as f64;
784        return fma(dk, LN2_HI, -(fma(-dk, LN2_LO, r) - f));
785    }
786    let s = f / (2.0 + f);
787    let dk = k as f64;
788    let z = s * s;
789    let i = hx - 0x6147a;
790    let w = z * z;
791    let j = 0x6b851 - hx;
792    let t1 = w * fma(w, fma(w, LG6, LG4), LG2);
793    let t2 = z * fma(w, fma(w, fma(w, LG7, LG5), LG3), LG1);
794    let i = i | j;
795    let r = t2 + t1;
796    if i > 0 {
797        let hfsq = 0.5 * f * f;
798        if k == 0 {
799            f - fma(-s, hfsq + r, hfsq)
800        } else {
801            fma(dk, LN2_HI, -((hfsq - fma(s, hfsq + r, dk * LN2_LO)) - f))
802        }
803    } else if k == 0 {
804        fma(-s, f - r, f)
805    } else {
806        fma(dk, LN2_HI, -(fma(s, f - r, -(dk * LN2_LO)) - f))
807    }
808}
809
810pub fn log1p(x: f64) -> f64 {
811    const LN2_HI: f64 = 6.931_471_803_691_238e-1;
812    const LN2_LO: f64 = 1.908_214_929_270_587_7e-10;
813    const TWO54: f64 = 1.801_439_850_948_198_4e16;
814    const LP1: f64 = 6.666_666_666_666_735e-1;
815    const LP2: f64 = 3.999_999_999_940_942e-1;
816    const LP3: f64 = 2.857_142_874_366_239e-1;
817    const LP4: f64 = 2.222_219_843_214_978_4e-1;
818    const LP5: f64 = 1.818_357_216_161_805e-1;
819    const LP6: f64 = 1.531_383_769_920_937_3e-1;
820    const LP7: f64 = 1.479_819_860_511_658_6e-1;
821
822    let hx = hi(x);
823    let ax = hx & 0x7fffffff;
824    let mut k = 1i32;
825    let mut f = 0.0;
826    let mut c = 0.0;
827    let mut hu = 0i32;
828    if hx < 0x3FDA827A {
829        if ax >= 0x3ff00000 {
830            if x == -1.0 {
831                return f64::NEG_INFINITY;
832            }
833            return f64::NAN;
834        }
835        if ax < 0x3e200000 {
836            if TWO54 + x > 0.0 && ax < 0x3c900000 {
837                return x;
838            }
839            return fma(-(x * x), 0.5, x);
840        }
841        if hx > 0 || hx <= 0xbfd2bec4u32 as i32 {
842            k = 0;
843            f = x;
844            hu = 1;
845        }
846    }
847    if hx >= 0x7ff00000 {
848        return x + x;
849    }
850    if k != 0 {
851        let u;
852        if hx < 0x43400000 {
853            u = 1.0 + x;
854            hu = hi(u);
855            k = (hu >> 20) - 1023;
856            c = if k > 0 { 1.0 - (u - x) } else { x - (u - 1.0) };
857            c /= u;
858        } else {
859            u = x;
860            hu = hi(u);
861            k = (hu >> 20) - 1023;
862            c = 0.0;
863        }
864        hu &= 0x000fffff;
865        let un;
866        if hu < 0x6a09e {
867            un = with_hi(u, hu | 0x3ff00000);
868        } else {
869            k += 1;
870            un = with_hi(u, hu | 0x3fe00000);
871            hu = (0x00100000 - hu) >> 2;
872        }
873        f = un - 1.0;
874    }
875    let hfsq = 0.5 * f * f;
876    if hu == 0 {
877        if f == 0.0 {
878            if k == 0 {
879                return 0.0;
880            }
881            c = fma(k as f64, LN2_LO, c);
882            return fma(k as f64, LN2_HI, c);
883        }
884        let r = hfsq * fma(-0.666_666_666_666_666_6, f, 1.0);
885        if k == 0 {
886            return f - r;
887        }
888        let dk = k as f64;
889        return fma(dk, LN2_HI, -((r - fma(dk, LN2_LO, c)) - f));
890    }
891    let s = f / (2.0 + f);
892    let z = s * s;
893    let r = z * fma(
894        z,
895        fma(
896            z,
897            fma(z, fma(z, fma(z, fma(z, LP7, LP6), LP5), LP4), LP3),
898            LP2,
899        ),
900        LP1,
901    );
902    if k == 0 {
903        f - fma(-s, hfsq + r, hfsq)
904    } else {
905        let dk = k as f64;
906        fma(
907            dk,
908            LN2_HI,
909            -((hfsq - fma(s, hfsq + r, fma(dk, LN2_LO, c))) - f),
910        )
911    }
912}
913
914/// FreeBSD `k_log1p`: `log(1+f) - f + f*f/2` for `f` in `~[sqrt(2)/2-1, sqrt(2)-1]`.
915fn k_log1p(f: f64) -> f64 {
916    const LG1: f64 = 6.666_666_666_666_735e-1;
917    const LG2: f64 = 3.999_999_999_940_942e-1;
918    const LG3: f64 = 2.857_142_874_366_239e-1;
919    const LG4: f64 = 2.222_219_843_214_978_4e-1;
920    const LG5: f64 = 1.818_357_216_161_805e-1;
921    const LG6: f64 = 1.531_383_769_920_937_3e-1;
922    const LG7: f64 = 1.479_819_860_511_658_6e-1;
923    let s = f / (2.0 + f);
924    let z = s * s;
925    let w = z * z;
926    let t1 = w * fma(w, fma(w, LG6, LG4), LG2);
927    let t2 = z * fma(w, fma(w, fma(w, LG7, LG5), LG3), LG1);
928    let r = t2 + t1;
929    let hfsq = 0.5 * f * f;
930    s * (hfsq + r)
931}
932
933/// Shared prologue of `log2`/`log10`: scale a subnormal, split off the
934/// exponent. Returns `(x_normalized_high_word_state, k, early_return)`.
935fn log_prologue(x: f64) -> Result<(f64, i32, i32), f64> {
936    const TWO54: f64 = 1.801_439_850_948_198_4e16;
937    let mut x = x;
938    let mut hx = hi(x);
939    let lx = lo(x);
940    let mut k = 0;
941    if hx < 0x00100000 {
942        if ((hx & 0x7fffffff) as u32 | lx) == 0 {
943            return Err(f64::NEG_INFINITY);
944        }
945        if hx < 0 {
946            return Err(f64::NAN);
947        }
948        k -= 54;
949        x *= TWO54;
950        hx = hi(x);
951    }
952    if hx >= 0x7ff00000 {
953        return Err(x + x);
954    }
955    if hx == 0x3ff00000 && lo(x) == 0 {
956        return Err(0.0);
957    }
958    Ok((x, k, hx))
959}
960
961pub fn log2(x: f64) -> f64 {
962    const IVLN2HI: f64 = 1.442_695_040_721_446_3;
963    const IVLN2LO: f64 = 1.675_171_316_488_651_2e-10;
964    let (mut x, mut k, mut hx) = match log_prologue(x) {
965        Ok(v) => v,
966        Err(r) => return r,
967    };
968    k += (hx >> 20) - 1023;
969    hx &= 0x000fffff;
970    let i = (hx + 0x95f64) & 0x100000;
971    x = with_hi(x, hx | (i ^ 0x3ff00000));
972    k += i >> 20;
973    let y = k as f64;
974    let f = x - 1.0;
975    let hfsq = 0.5 * f * f;
976    let r = k_log1p(f);
977    let hi_ = with_lo(f - hfsq, 0);
978    let lo_ = (f - hi_) - hfsq + r;
979    let mut val_hi = hi_ * IVLN2HI;
980    let mut val_lo = fma(lo_ + hi_, IVLN2LO, lo_ * IVLN2HI);
981    let w = y + val_hi;
982    val_lo += (y - w) + val_hi;
983    val_hi = w;
984    val_lo + val_hi
985}
986
987pub fn log10(x: f64) -> f64 {
988    const IVLN10: f64 = std::f64::consts::LOG10_E;
989    const LOG10_2HI: f64 = 3.010_299_956_636_117_7e-1;
990    const LOG10_2LO: f64 = 3.694_239_077_158_931e-13;
991    let (x, mut k, mut hx) = match log_prologue(x) {
992        Ok(v) => v,
993        Err(r) => return r,
994    };
995    let lx = lo(x);
996    k += (hx >> 20) - 1023;
997    let i = ((k as u32 & 0x8000_0000) >> 31) as i32;
998    hx = (hx & 0x000fffff) | ((0x3ff - i) << 20);
999    let y = (k + i) as f64;
1000    let x = from_words(hx, lx);
1001    let z = fma(y, LOG10_2LO, IVLN10 * log(x));
1002    fma(y, LOG10_2HI, z)
1003}
1004
1005pub fn atan(x: f64) -> f64 {
1006    const ATANHI: [f64; 4] = [
1007        4.636_476_090_008_061e-1,
1008        std::f64::consts::FRAC_PI_4,
1009        9.827_937_232_473_29e-1,
1010        std::f64::consts::FRAC_PI_2,
1011    ];
1012    const ATANLO: [f64; 4] = [
1013        2.269_877_745_296_168_7e-17,
1014        3.061_616_997_868_383e-17,
1015        1.390_331_103_123_099_8e-17,
1016        6.123_233_995_736_766e-17,
1017    ];
1018    const AT: [f64; 11] = [
1019        3.333_333_333_333_293e-1,
1020        -1.999_999_999_987_648_3e-1,
1021        1.428_571_427_250_346_6e-1,
1022        -1.111_111_040_546_235_6e-1,
1023        9.090_887_133_436_507e-2,
1024        -7.691_876_205_044_83e-2,
1025        6.661_073_137_387_531e-2,
1026        -5.833_570_133_790_573_5e-2,
1027        4.976_877_994_615_932_4e-2,
1028        -3.653_157_274_421_691_6e-2,
1029        1.628_582_011_536_578_2e-2,
1030    ];
1031    let mut x = x;
1032    let hx = hi(x);
1033    let ix = hx & 0x7fffffff;
1034    if ix >= 0x44100000 {
1035        if x.is_nan() {
1036            return x + x;
1037        }
1038        return if hx > 0 {
1039            ATANHI[3] + ATANLO[3]
1040        } else {
1041            -ATANHI[3] - ATANLO[3]
1042        };
1043    }
1044    let id: i32;
1045    if ix < 0x3fdc0000 {
1046        if ix < 0x3e200000 {
1047            return x;
1048        }
1049        id = -1;
1050    } else {
1051        x = x.abs();
1052        if ix < 0x3ff30000 {
1053            if ix < 0x3fe60000 {
1054                id = 0;
1055                x = fma(2.0, x, -1.0) / (2.0 + x);
1056            } else {
1057                id = 1;
1058                x = (x - 1.0) / (x + 1.0);
1059            }
1060        } else if ix < 0x40038000 {
1061            id = 2;
1062            x = (x - 1.5) / fma(1.5, x, 1.0);
1063        } else {
1064            id = 3;
1065            x = -1.0 / x;
1066        }
1067    }
1068    let z = x * x;
1069    let w = z * z;
1070    let s1 = z * fma(
1071        w,
1072        fma(
1073            w,
1074            fma(w, fma(w, fma(w, AT[10], AT[8]), AT[6]), AT[4]),
1075            AT[2],
1076        ),
1077        AT[0],
1078    );
1079    let s2 = w * fma(w, fma(w, fma(w, fma(w, AT[9], AT[7]), AT[5]), AT[3]), AT[1]);
1080    if id < 0 {
1081        return fma(-x, s1 + s2, x);
1082    }
1083    let id = id as usize;
1084    let z = ATANHI[id] - (fma(x, s1 + s2, -ATANLO[id]) - x);
1085    if hx < 0 {
1086        -z
1087    } else {
1088        z
1089    }
1090}
1091
1092pub fn atan2(y: f64, x: f64) -> f64 {
1093    const PI_O_4: f64 = std::f64::consts::FRAC_PI_4;
1094    const PI_O_2: f64 = std::f64::consts::FRAC_PI_2;
1095    const PI_LO: f64 = 1.224_646_799_147_353_2E-16;
1096    let hx = hi(x);
1097    let lx = lo(x);
1098    let ix = hx & 0x7fffffff;
1099    let hy = hi(y);
1100    let ly = lo(y);
1101    let iy = hy & 0x7fffffff;
1102    if x.is_nan() || y.is_nan() {
1103        return x + y;
1104    }
1105    if (hx.wrapping_sub(0x3ff00000) as u32 | lx) == 0 {
1106        return atan(y);
1107    }
1108    let mut m = ((hy >> 31) & 1) | ((hx >> 30) & 2);
1109    if (iy as u32 | ly) == 0 {
1110        return match m {
1111            0 | 1 => y,
1112            2 => PI,
1113            _ => -PI,
1114        };
1115    }
1116    if (ix as u32 | lx) == 0 {
1117        return if hy < 0 { -PI_O_2 } else { PI_O_2 };
1118    }
1119    if ix == 0x7ff00000 {
1120        if iy == 0x7ff00000 {
1121            return match m {
1122                0 => PI_O_4,
1123                1 => -PI_O_4,
1124                2 => 3.0 * PI_O_4,
1125                _ => -3.0 * PI_O_4,
1126            };
1127        }
1128        return match m {
1129            0 => 0.0,
1130            1 => -0.0,
1131            2 => PI,
1132            _ => -PI,
1133        };
1134    }
1135    if iy == 0x7ff00000 {
1136        return if hy < 0 { -PI_O_2 } else { PI_O_2 };
1137    }
1138    let k = (iy - ix) >> 20;
1139    let z;
1140    if k > 60 {
1141        z = fma(0.5, PI_LO, PI_O_2);
1142        m &= 1;
1143    } else if hx < 0 && k < -60 {
1144        z = 0.0;
1145    } else {
1146        z = atan((y / x).abs());
1147    }
1148    match m {
1149        0 => z,
1150        1 => -z,
1151        2 => PI - (z - PI_LO),
1152        _ => (z - PI_LO) - PI,
1153    }
1154}
1155
1156/// Rational approximation shared by `asin` and `acos`: returns `p(t)/q(t)`
1157/// written as the two numerator/denominator values.
1158fn asin_pq(t: f64) -> (f64, f64) {
1159    const PS0: f64 = 1.666_666_666_666_666_6e-1;
1160    const PS1: f64 = -3.255_658_186_224_009e-1;
1161    const PS2: f64 = 2.012_125_321_348_629_3e-1;
1162    const PS3: f64 = -4.005_553_450_067_941e-2;
1163    const PS4: f64 = 7.915_349_942_898_145e-4;
1164    const PS5: f64 = 3.479_331_075_960_212e-5;
1165    const QS1: f64 = -2.403_394_911_734_414;
1166    const QS2: f64 = 2.020_945_760_233_505_7;
1167    const QS3: f64 = -6.882_839_716_054_533e-1;
1168    const QS4: f64 = 7.703_815_055_590_194e-2;
1169    let p = t * fma(
1170        t,
1171        fma(t, fma(t, fma(t, fma(t, PS5, PS4), PS3), PS2), PS1),
1172        PS0,
1173    );
1174    let q = fma(t, fma(t, fma(t, fma(t, QS4, QS3), QS2), QS1), 1.0);
1175    (p, q)
1176}
1177
1178pub fn asin(x: f64) -> f64 {
1179    const PIO2_HI: f64 = std::f64::consts::FRAC_PI_2;
1180    const PIO2_LO: f64 = 6.123_233_995_736_766e-17;
1181    const PIO4_HI: f64 = std::f64::consts::FRAC_PI_4;
1182    let hx = hi(x);
1183    let ix = hx & 0x7fffffff;
1184    if ix >= 0x3ff00000 {
1185        if (ix.wrapping_sub(0x3ff00000) as u32 | lo(x)) == 0 {
1186            return fma(x, PIO2_HI, x * PIO2_LO);
1187        }
1188        return f64::NAN;
1189    } else if ix < 0x3fe00000 {
1190        if ix < 0x3e400000 {
1191            return x;
1192        }
1193        let t = x * x;
1194        let (p, q) = asin_pq(t);
1195        let w = p / q;
1196        return fma(x, w, x);
1197    }
1198    let w = 1.0 - x.abs();
1199    let t = w * 0.5;
1200    let (mut p, mut q) = asin_pq(t);
1201    let s = t.sqrt();
1202    let t = if ix >= 0x3FEF3333 {
1203        let w = p / q;
1204        PIO2_HI - fma(2.0, fma(s, w, s), -PIO2_LO)
1205    } else {
1206        let w = with_lo(s, 0);
1207        let c = fma(-w, w, t) / (s + w);
1208        let r = p / q;
1209        p = fma(2.0 * s, r, -fma(-2.0, c, PIO2_LO));
1210        q = fma(-2.0, w, PIO4_HI);
1211        PIO4_HI - (p - q)
1212    };
1213    if hx > 0 {
1214        t
1215    } else {
1216        -t
1217    }
1218}
1219
1220pub fn acos(x: f64) -> f64 {
1221    const PIO2_HI: f64 = std::f64::consts::FRAC_PI_2;
1222    const PIO2_LO: f64 = 6.123_233_995_736_766e-17;
1223    let hx = hi(x);
1224    let ix = hx & 0x7fffffff;
1225    if ix >= 0x3ff00000 {
1226        if (ix.wrapping_sub(0x3ff00000) as u32 | lo(x)) == 0 {
1227            if hx > 0 {
1228                return 0.0;
1229            }
1230            return fma(2.0, PIO2_LO, PI);
1231        }
1232        return f64::NAN;
1233    }
1234    if ix < 0x3fe00000 {
1235        if ix <= 0x3c600000 {
1236            return PIO2_HI + PIO2_LO;
1237        }
1238        let z = x * x;
1239        let (p, q) = asin_pq(z);
1240        let r = p / q;
1241        PIO2_HI - (x - fma(-x, r, PIO2_LO))
1242    } else if hx < 0 {
1243        let z = (1.0 + x) * 0.5;
1244        let (p, q) = asin_pq(z);
1245        let s = z.sqrt();
1246        let r = p / q;
1247        let w = fma(r, s, -PIO2_LO);
1248        fma(-2.0, s + w, PI)
1249    } else {
1250        let z = (1.0 - x) * 0.5;
1251        let s = z.sqrt();
1252        let df = with_lo(s, 0);
1253        let c = fma(-df, df, z) / (s + df);
1254        let (p, q) = asin_pq(z);
1255        let r = p / q;
1256        let w = fma(r, s, c);
1257        2.0 * (df + w)
1258    }
1259}
1260
1261pub fn sinh(x: f64) -> f64 {
1262    const KSINH_OVERFLOW: f64 = 710.4758600739439;
1263    const TWO_M28: f64 = 3.725290298461914e-9;
1264    const LOG_MAXD: f64 = 709.7822265625;
1265    const SHUGE: f64 = 1.0e307;
1266    let h = if x < 0.0 { -0.5 } else { 0.5 };
1267    let ax = x.abs();
1268    if ax < 22.0 {
1269        if ax < TWO_M28 {
1270            return x;
1271        }
1272        let t = expm1(ax);
1273        if ax < 1.0 {
1274            return h * fma(2.0, t, -(t * t / (t + 1.0)));
1275        }
1276        return h * (t + t / (t + 1.0));
1277    }
1278    if ax < LOG_MAXD {
1279        return h * exp(ax);
1280    }
1281    if ax <= KSINH_OVERFLOW {
1282        let w = exp(0.5 * ax);
1283        let t = h * w;
1284        return t * w;
1285    }
1286    x * SHUGE
1287}
1288
1289pub fn cosh(x: f64) -> f64 {
1290    const KCOSH_OVERFLOW: f64 = 710.4758600739439;
1291    let ix = hi(x) & 0x7fffffff;
1292    if ix < 0x3fd62e43 {
1293        let t = expm1(x.abs());
1294        let w = 1.0 + t;
1295        if ix < 0x3c800000 {
1296            return w;
1297        }
1298        return 1.0 + (t * t) / (w + w);
1299    }
1300    if ix < 0x40360000 {
1301        let t = exp(x.abs());
1302        return fma(0.5, t, 0.5 / t);
1303    }
1304    if ix < 0x40862e42 {
1305        return 0.5 * exp(x.abs());
1306    }
1307    if x.abs() <= KCOSH_OVERFLOW {
1308        let w = exp(0.5 * x.abs());
1309        let t = 0.5 * w;
1310        return t * w;
1311    }
1312    if ix >= 0x7ff00000 {
1313        return x * x;
1314    }
1315    f64::INFINITY
1316}
1317
1318pub fn tanh(x: f64) -> f64 {
1319    let jx = hi(x);
1320    let ix = jx & 0x7fffffff;
1321    if ix >= 0x7ff00000 {
1322        return if jx >= 0 {
1323            1.0 / x + 1.0
1324        } else {
1325            1.0 / x - 1.0
1326        };
1327    }
1328    let z;
1329    if ix < 0x40360000 {
1330        if ix < 0x3e300000 {
1331            return x;
1332        }
1333        if ix >= 0x3ff00000 {
1334            let t = expm1(2.0 * x.abs());
1335            z = 1.0 - 2.0 / (t + 2.0);
1336        } else {
1337            let t = expm1(-2.0 * x.abs());
1338            z = -t / (t + 2.0);
1339        }
1340    } else {
1341        z = 1.0;
1342    }
1343    if jx >= 0 {
1344        z
1345    } else {
1346        -z
1347    }
1348}
1349
1350pub fn asinh(x: f64) -> f64 {
1351    const LN2: f64 = std::f64::consts::LN_2;
1352    let hx = hi(x);
1353    let ix = hx & 0x7fffffff;
1354    if ix >= 0x7ff00000 {
1355        return x + x;
1356    }
1357    if ix < 0x3e300000 {
1358        return x;
1359    }
1360
1361    let w = if ix > 0x41b00000 {
1362        log(x.abs()) + LN2
1363    } else if ix > 0x40000000 {
1364        let t = x.abs();
1365        log(fma(2.0, t, 1.0 / (fma(x, x, 1.0).sqrt() + t)))
1366    } else {
1367        let t = x * x;
1368        log1p(x.abs() + t / (1.0 + (1.0 + t).sqrt()))
1369    };
1370    if hx > 0 {
1371        w
1372    } else {
1373        -w
1374    }
1375}
1376
1377pub fn acosh(x: f64) -> f64 {
1378    const LN2: f64 = std::f64::consts::LN_2;
1379    let hx = hi(x);
1380    let lx = lo(x);
1381    if hx < 0x3ff00000 {
1382        f64::NAN
1383    } else if hx >= 0x41b00000 {
1384        if hx >= 0x7ff00000 {
1385            x + x
1386        } else {
1387            log(x) + LN2
1388        }
1389    } else if (hx.wrapping_sub(0x3ff00000) as u32 | lx) == 0 {
1390        0.0
1391    } else if hx > 0x40000000 {
1392        let t = x * x;
1393        log(fma(2.0, x, -(1.0 / (x + (t - 1.0).sqrt()))))
1394    } else {
1395        let t = x - 1.0;
1396        log1p(t + fma(2.0, t, t * t).sqrt())
1397    }
1398}
1399
1400pub fn atanh(x: f64) -> f64 {
1401    let hx = hi(x);
1402    let lx = lo(x);
1403    let ix = hx & 0x7fffffff;
1404    if (ix as u32 | ((lx | lx.wrapping_neg()) >> 31)) > 0x3ff00000 {
1405        return f64::NAN;
1406    }
1407    if ix == 0x3ff00000 {
1408        return x / 0.0;
1409    }
1410    if ix < 0x3e300000 {
1411        return x;
1412    }
1413    let x = with_hi(x, ix);
1414
1415    let t = if ix < 0x3fe00000 {
1416        let t2 = x + x;
1417        0.5 * log1p(t2 + t2 * x / (1.0 - x))
1418    } else {
1419        0.5 * log1p((x + x) / (1.0 - x))
1420    };
1421    if hx >= 0 {
1422        t
1423    } else {
1424        -t
1425    }
1426}
1427
1428pub fn cbrt(x: f64) -> f64 {
1429    const B1: u32 = 715094163;
1430    const B2: u32 = 696219795;
1431    const P0: f64 = 1.875_951_824_271_77;
1432    const P1: f64 = -1.884_979_795_433_771_7;
1433    const P2: f64 = 1.621_429_720_105_354_5;
1434    const P3: f64 = -0.758_397_934_778_766;
1435    const P4: f64 = 0.145_996_192_886_612_45;
1436    let hx = hi(x) as u32;
1437    let low = lo(x);
1438    let sign = hx & 0x8000_0000;
1439    let hx = hx ^ sign;
1440    if hx >= 0x7ff00000 {
1441        return x + x;
1442    }
1443    let mut t;
1444    if hx < 0x00100000 {
1445        if (hx | low) == 0 {
1446            return x;
1447        }
1448        t = from_words(0x43500000, 0);
1449        t *= x;
1450        let high = hi(t) as u32;
1451        t = from_words((sign | ((high & 0x7fffffff) / 3 + B2)) as i32, 0);
1452    } else {
1453        t = from_words((sign | (hx / 3 + B1)) as i32, 0);
1454    }
1455    let r = (t * t) * (t / x);
1456    t *= fma((r * r) * r, fma(r, P4, P3), fma(r, fma(r, P2, P1), P0));
1457    t = f64::from_bits(t.to_bits().wrapping_add(0x8000_0000) & 0xffff_ffff_c000_0000);
1458    let s = t * t;
1459    let r = x / s;
1460    let w = t + t;
1461    let r = (r - t) / (w + r);
1462    fma(t, r, t)
1463}