use super::math_data::{
EXP_POLY, EXP_TAB, LOG_POLY, LOG_POLY1, LOG_TAB, POW_LOG_POLY, POW_LOG_TAB,
};
const LN2HI: f64 = f64::from_bits(0x3fe6_2e42_fefa_3800);
const LN2LO: f64 = f64::from_bits(0x3d2e_f357_93c7_6730);
const INV_LN2_N: f64 = f64::from_bits(0x4067_1547_652b_82fe);
const NEG_LN2_HI_N: f64 = f64::from_bits(0xbf76_2e42_fefa_0000);
const NEG_LN2_LO_N: f64 = f64::from_bits(0xbd0c_f79a_bc9e_3b3a);
const SHIFT: f64 = f64::from_bits(0x4338_0000_0000_0000);
const TWO_27: f64 = 134_217_728.0;
const TWO_52: f64 = 4_503_599_627_370_496.0;
const TWO_1009: f64 = f64::from_bits((1023 + 1009) << 52);
const TWO_M1022: f64 = f64::MIN_POSITIVE;
const SIGN_MASK: u64 = 0x8000_0000_0000_0000;
const ONE_BITS: u64 = 0x3ff0_0000_0000_0000;
const INF_BITS: u64 = 0x7ff0_0000_0000_0000;
const N: u64 = 128;
const TABLE_SHIFT: u32 = 45;
const SIGN_BIAS: u32 = 0x800 << 7;
fn top12(x: f64) -> u32 {
(x.to_bits() >> 52) as u32
}
fn oflow(sign: u32) -> f64 {
let y = f64::from_bits((1023 + 769) << 52); (if sign != 0 { -y } else { y }) * y
}
fn uflow(sign: u32) -> f64 {
let y = f64::from_bits((1023 - 767) << 52); (if sign != 0 { -y } else { y }) * y
}
fn divzero(sign: u32) -> f64 {
(if sign != 0 { -1.0 } else { 1.0 }) / 0.0
}
fn invalid() -> f64 {
f64::NAN
}
fn issignaling(x: f64) -> bool {
let ix = x.to_bits();
(ix ^ 0x0008_0000_0000_0000).wrapping_mul(2) > 0x7ff8_0000_0000_0000_u64.wrapping_mul(2)
}
pub(crate) fn log(x: f64) -> f64 {
let a = |i: usize| f64::from_bits(LOG_POLY[i]);
let b = |i: usize| f64::from_bits(LOG_POLY1[i]);
let mut ix = x.to_bits();
let top = (ix >> 48) as u32;
const LO: u64 = 0x3fee_0000_0000_0000;
const HI: u64 = 0x3ff1_0900_0000_0000;
if ix.wrapping_sub(LO) < HI - LO {
if ix == ONE_BITS {
return 0.0;
}
let r = x - 1.0;
let r2 = r * r;
let r3 = r * r2;
let p0 = r2.mul_add(b(3), r.mul_add(b(2), b(1)));
let p1 = r2.mul_add(b(6), r.mul_add(b(5), b(4)));
let p2 = r3.mul_add(b(10), r2.mul_add(b(9), r.mul_add(b(8), b(7))));
let p = r3.mul_add(r3.mul_add(p2, p1), p0);
let rhi = (-r).mul_add(TWO_27, r.mul_add(TWO_27, r));
let rlo = r - rhi;
let rr = rhi * rhi;
let hi = rr.mul_add(b(0), r);
let lo = rr.mul_add(b(0), r - hi);
let lo = (b(0) * rlo).mul_add(rhi + r, lo);
let y = r3.mul_add(p, lo);
return y + hi;
}
if top.wrapping_sub(0x0010) >= 0x7ff0 - 0x0010 {
if ix.wrapping_mul(2) == 0 {
return divzero(1);
}
if ix == INF_BITS {
return x;
}
if (top & 0x8000) != 0 || (top & 0x7ff0) == 0x7ff0 {
return invalid();
}
ix = (x * TWO_52).to_bits().wrapping_sub(52 << 52);
}
const OFF: u64 = 0x3fe6_0000_0000_0000;
let tmp = ix.wrapping_sub(OFF);
let i = ((tmp >> TABLE_SHIFT) % N) as usize;
let k = ((tmp as i64) >> 52) as i32;
let iz = ix.wrapping_sub(tmp & (0xfff << 52));
let invc = f64::from_bits(LOG_TAB[i][0]);
let logc = f64::from_bits(LOG_TAB[i][1]);
let z = f64::from_bits(iz);
let r = z.mul_add(invc, -1.0);
let kd = f64::from(k);
let w = kd.mul_add(LN2HI, logc);
let hi = w + r;
let lo = kd.mul_add(LN2LO, w - hi + r);
let r2 = r * r;
let q = r2.mul_add(r.mul_add(a(4), a(3)), r.mul_add(a(2), a(1)));
(r * r2).mul_add(q, r2.mul_add(a(0), lo)) + hi
}
fn checkint(iy: u64) -> u32 {
let e = (iy >> 52 & 0x7ff) as i32;
if e < 0x3ff {
return 0;
}
if e > 0x3ff + 52 {
return 2;
}
let sh = (0x3ff + 52 - e) as u32;
if iy & ((1u64 << sh) - 1) != 0 {
return 0;
}
if iy & (1u64 << sh) != 0 {
return 1;
}
2
}
fn zeroinfnan(i: u64) -> bool {
i.wrapping_mul(2).wrapping_sub(1) >= INF_BITS.wrapping_mul(2) - 1
}
fn pow_log_inline(ix: u64) -> (f64, f64) {
let a = |i: usize| f64::from_bits(POW_LOG_POLY[i]);
const OFF: u64 = 0x3fe6_9555_0000_0000;
let tmp = ix.wrapping_sub(OFF);
let i = ((tmp >> TABLE_SHIFT) % N) as usize;
let k = ((tmp as i64) >> 52) as i32;
let iz = ix.wrapping_sub(tmp & (0xfff << 52));
let z = f64::from_bits(iz);
let kd = f64::from(k);
let [invc, logc, logctail] = POW_LOG_TAB[i].map(f64::from_bits);
let r = z.mul_add(invc, -1.0);
let t1 = kd.mul_add(LN2HI, logc);
let t2 = t1 + r;
let lo1 = kd.mul_add(LN2LO, logctail);
let lo2 = t1 - t2 + r;
let ar = a(0) * r;
let ar2 = r * ar;
let ar3 = r * ar2;
let hi = t2 + ar2;
let lo3 = ar.mul_add(r, -ar2);
let lo4 = t2 - hi + ar2;
let q = ar2.mul_add(r.mul_add(a(6), a(5)), r.mul_add(a(4), a(3)));
let q = ar2.mul_add(q, r.mul_add(a(2), a(1)));
let lo = ar3.mul_add(q, lo1 + lo2 + lo3 + lo4);
let y = hi + lo;
(y, hi - y + lo)
}
fn exp_specialcase(tmp: f64, mut sbits: u64, ki: u64) -> f64 {
if ki & 0x8000_0000 == 0 {
sbits = sbits.wrapping_sub(1009 << 52);
let scale = f64::from_bits(sbits);
return TWO_1009 * scale.mul_add(tmp, scale);
}
sbits = sbits.wrapping_add(1022 << 52);
let scale = f64::from_bits(sbits);
let st = scale * tmp;
let mut y = scale + st;
if y.abs() < 1.0 {
let one = if y < 0.0 { -1.0 } else { 1.0 };
let lo = scale - y + st;
let hi = one + y;
let lo = one - hi + y + lo;
y = (hi + lo) - one;
if y == 0.0 {
y = f64::from_bits(sbits & SIGN_MASK);
}
}
TWO_M1022 * y
}
fn exp_inline(x: f64, xtail: f64, sign_bias: u32) -> f64 {
let c = |i: usize| f64::from_bits(EXP_POLY[i]);
const TOP_TINY: u32 = 0x3c9;
const TOP_512: u32 = 0x408;
const TOP_1024: u32 = 0x409;
let mut abstop = top12(x) & 0x7ff;
if abstop.wrapping_sub(TOP_TINY) >= TOP_512 - TOP_TINY {
if abstop.wrapping_sub(TOP_TINY) >= 0x8000_0000 {
let one = 1.0 + x;
return if sign_bias != 0 { -one } else { one };
}
if abstop >= TOP_1024 {
return if x.to_bits() >> 63 != 0 {
uflow(sign_bias)
} else {
oflow(sign_bias)
};
}
abstop = 0;
}
let kd = x.mul_add(INV_LN2_N, SHIFT);
let ki = kd.to_bits();
let kd = kd - SHIFT;
let r = kd.mul_add(NEG_LN2_LO_N, kd.mul_add(NEG_LN2_HI_N, x));
let r = r + xtail;
let idx = (2 * (ki % N)) as usize;
let top = ki.wrapping_add(u64::from(sign_bias)) << (52 - 7);
let tail = f64::from_bits(EXP_TAB[idx]);
let sbits = EXP_TAB[idx + 1].wrapping_add(top);
let r2 = r * r;
let tmp = r2.mul_add(r.mul_add(c(1), c(0)), tail + r);
let tmp = (r2 * r2).mul_add(r.mul_add(c(3), c(2)), tmp);
if abstop == 0 {
return exp_specialcase(tmp, sbits, ki);
}
let scale = f64::from_bits(sbits);
scale.mul_add(tmp, scale)
}
pub(crate) fn pow(x: f64, y: f64) -> f64 {
let mut sign_bias = 0;
let mut ix = x.to_bits();
let iy = y.to_bits();
let mut topx = top12(x);
let topy = top12(y);
if topx.wrapping_sub(0x001) >= 0x7ff - 0x001
|| (topy & 0x7ff).wrapping_sub(0x3be) >= 0x43e - 0x3be
{
if zeroinfnan(iy) {
if iy.wrapping_mul(2) == 0 {
return if issignaling(x) { x + y } else { 1.0 };
}
if ix == ONE_BITS {
return if issignaling(y) { x + y } else { 1.0 };
}
if ix.wrapping_mul(2) > INF_BITS.wrapping_mul(2)
|| iy.wrapping_mul(2) > INF_BITS.wrapping_mul(2)
{
return x + y;
}
if ix.wrapping_mul(2) == ONE_BITS.wrapping_mul(2) {
return 1.0;
}
if (ix.wrapping_mul(2) < ONE_BITS.wrapping_mul(2)) == (iy >> 63 == 0) {
return 0.0;
}
return y * y;
}
if zeroinfnan(ix) {
let mut x2 = x * x;
if ix >> 63 != 0 && checkint(iy) == 1 {
x2 = -x2;
sign_bias = 1;
}
if ix.wrapping_mul(2) == 0 && iy >> 63 != 0 {
return divzero(sign_bias);
}
return if iy >> 63 != 0 { 1.0 / x2 } else { x2 };
}
if ix >> 63 != 0 {
let yint = checkint(iy);
if yint == 0 {
return invalid();
}
if yint == 1 {
sign_bias = SIGN_BIAS;
}
ix &= !SIGN_MASK;
topx &= 0x7ff;
}
if (topy & 0x7ff).wrapping_sub(0x3be) >= 0x43e - 0x3be {
if ix == ONE_BITS {
return 1.0;
}
if (topy & 0x7ff) < 0x3be {
return if ix > ONE_BITS { 1.0 + y } else { 1.0 - y };
}
return if (ix > ONE_BITS) == (topy < 0x800) {
oflow(0)
} else {
uflow(0)
};
}
if topx == 0 {
ix = (x * TWO_52).to_bits() & !SIGN_MASK;
ix = ix.wrapping_sub(52 << 52);
}
}
let (hi, lo) = pow_log_inline(ix);
let ehi = y * hi;
let elo = y.mul_add(lo, hi.mul_add(y, -ehi));
exp_inline(ehi, elo, sign_bias)
}