use super::double_double::{
CANONICAL_NAN, add_exponent, fast_sum, fast_two_sum, fma_dw, mul_dd, round_ties_even,
};
use super::path::Path;
use super::tables::{EXP_ACCURATE_POLY, EXP_EXCEPTIONS, EXP_FAST_POLY, EXP_T0, EXP_T1, f};
const SCALE: f64 = f64::from_bits(0x40b7_1547_652b_82fe);
const L2H: f64 = f64::from_bits(0x3f26_2e42_ff00_0000);
const L2L: f64 = f64::from_bits(0x3d07_1843_2a1b_0e26);
const L2LL: f64 = f64::from_bits(0x3999_ff03_4254_2fc3);
const FAST_EPS: f64 = f64::from_bits(0x3c12_7000_0000_0000);
const TINY_ABS_BITS: u64 = 0x3c90_0000_0000_0000;
const LARGE_ABS_BITS: u64 = 0x4086_2e42_fefa_39f0;
const ZERO_ABS_BITS: u64 = 0x4087_4910_d52d_3052;
const SUBNORMAL_BITS: u64 = 0xc086_232b_dd7a_bcd2;
const INF_BITS: u64 = 0x7ff0_0000_0000_0000;
pub fn exp(x: f64) -> f64 {
exp_traced(x).0
}
#[doc(hidden)]
#[inline(always)]
pub fn exp_traced(x: f64) -> (f64, Path) {
let ix = x.to_bits();
let aix = ix & (u64::MAX >> 1);
if aix <= TINY_ABS_BITS {
return (1.0 + x, Path::Special);
}
if aix >= LARGE_ABS_BITS {
if aix > INF_BITS {
return (CANONICAL_NAN, Path::Special);
}
if aix == INF_BITS {
let value = if ix >> 63 == 1 { 0.0 } else { f64::INFINITY };
return (value, Path::Special);
}
if ix >> 63 == 0 {
return (f64::INFINITY, Path::Special);
}
if aix >= ZERO_ABS_BITS {
return (0.0, Path::Special);
}
}
let (t, ie, th, tl) = reduce(x);
let dx = (x - L2H * t) + L2L * t;
let dx2 = dx * dx;
let c = |i| f(&EXP_FAST_POLY, i);
let p = (c(0) + dx * c(1)) + dx2 * (c(2) + dx * c(3));
let mut fh = th;
let tx = th * dx;
let mut fl = tl + tx * p;
if ix > SUBNORMAL_BITS {
let (sh, e) = fast_two_sum(subnormal_offset(ie), fh);
fh = sh;
fl += e;
let ub = fh + (fl + FAST_EPS);
let lb = fh + (fl - FAST_EPS);
if ub != lb {
return exp_accurate(x);
}
(clear_exponent(lb), Path::Fast)
} else {
let ub = fh + (fl + FAST_EPS);
let lb = fh + (fl - FAST_EPS);
if ub != lb {
return exp_accurate(x);
}
(add_exponent(lb, ie), Path::Fast)
}
}
#[inline(always)]
fn reduce(x: f64) -> (f64, i64, f64, f64) {
let t = round_ties_even(x * SCALE);
let jt = t as i64;
let i0 = ((jt >> 6) & 0x3f) as usize;
let i1 = (jt & 0x3f) as usize;
let ie = jt >> 12;
let t0h = f(&EXP_T0, 2 * i0 + 1);
let t0l = f(&EXP_T0, 2 * i0);
let t1h = f(&EXP_T1, 2 * i1 + 1);
let t1l = f(&EXP_T1, 2 * i1);
let (th, tl) = mul_dd(t0h, t0l, t1h, t1l);
(t, ie, th, tl)
}
#[inline(always)]
fn subnormal_offset(ie: i64) -> f64 {
f64::from_bits(((1 - ie) as u64) << 52)
}
#[inline(always)]
fn clear_exponent(x: f64) -> f64 {
f64::from_bits(x.to_bits() & (u64::MAX >> 12))
}
#[inline(never)]
#[cold]
fn exp_accurate(x: f64) -> (f64, Path) {
let ix = x.to_bits();
let (t, ie, th, tl) = reduce(x);
let dx = x - L2H * t;
let dxl = L2L * t;
let dxll = L2LL * t + L2L.mul_add(t, -dxl);
let (dxh, dxl) = fast_two_sum(dx, dxl);
let dxl = dxl + dxll;
let (mut fh, mut fl) = poly_dd(dxh, dxl);
(fh, fl) = mul_dd(dxh, dxl, fh, fl);
(fh, fl) = mul_dd(fh, fl, th, tl);
(fh, fl) = fast_sum(th, tl, fh, fl);
if ix > SUBNORMAL_BITS {
let (sh, e) = fast_two_sum(subnormal_offset(ie), fh);
fl += e;
return (clear_exponent(sh + fl), Path::Accurate);
}
let (sh, sl) = fast_two_sum(fh, fl);
let d = sl.to_bits().wrapping_add(4) & (u64::MAX >> 12);
let (value, path) = if d <= 4 {
exception(x, sh)
} else {
(sh, Path::Accurate)
};
(add_exponent(value, ie), path)
}
#[inline(always)]
fn poly_dd(xh: f64, xl: f64) -> (f64, f64) {
let n = EXP_ACCURATE_POLY.len() / 2;
let mut ch = f(&EXP_ACCURATE_POLY, 2 * (n - 1));
let mut cl = f(&EXP_ACCURATE_POLY, 2 * (n - 1) + 1);
for i in (0..n - 1).rev() {
(ch, cl) = fma_dw(
xh,
xl,
ch,
cl,
f(&EXP_ACCURATE_POLY, 2 * i),
f(&EXP_ACCURATE_POLY, 2 * i + 1),
);
}
(ch, cl)
}
fn exception(x: f64, fallback: f64) -> (f64, Path) {
let entry = |i: usize| f(&EXP_EXCEPTIONS, 3 * i);
let (mut a, mut b) = (0, EXP_EXCEPTIONS.len() / 3);
while a + 1 < b {
let m = (a + b) / 2;
if x < entry(m) {
b = m;
} else {
a = m;
}
}
if x == entry(a) {
let value = f(&EXP_EXCEPTIONS, 3 * a + 1) + f(&EXP_EXCEPTIONS, 3 * a + 2);
return (value, Path::Exception);
}
(fallback, Path::Accurate)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exp_special_values() {
assert_eq!(exp(f64::NAN).to_bits(), CANONICAL_NAN.to_bits());
assert_eq!(
exp(f64::from_bits(0xfff0_0000_0000_0001)).to_bits(),
CANONICAL_NAN.to_bits()
);
assert_eq!(exp(f64::INFINITY), f64::INFINITY);
assert_eq!(exp(f64::NEG_INFINITY).to_bits(), 0);
assert_eq!(exp(0.0), 1.0);
assert_eq!(exp(-0.0), 1.0);
assert_eq!(exp(f64::from_bits(1)), 1.0);
assert_eq!(exp(f64::MAX), f64::INFINITY);
assert_eq!(exp(f64::MIN).to_bits(), 0);
assert_eq!(exp(1.0), std::f64::consts::E);
}
#[test]
fn test_clear_exponent_rounds_on_subnormal_grid() {
for ie in -1075..=-1022i64 {
let offset = subnormal_offset(ie);
for m in [0u64, 1, 2, 3, 0x000f_ffff_ffff_ffff] {
let sum = f64::from_bits(offset.to_bits() | m);
assert_eq!(clear_exponent(sum).to_bits(), m, "ie {ie} m {m}");
}
}
}
#[test]
fn test_listed_exception_takes_exception_path() {
let x = f(&EXP_EXCEPTIONS, 3 * 2);
assert_eq!(x.to_bits(), 0xbfd0_7e05_e6e0_3b83);
assert_eq!(exp_traced(x).1, Path::Exception);
}
#[test]
fn test_fast_path_serves_ordinary_inputs() {
assert_eq!(exp_traced(0.5).1, Path::Fast);
assert_eq!(exp_traced(-745.0).1, Path::Fast);
}
}