pub const CANONICAL_NAN: f64 = f64::from_bits(0x7ff8_0000_0000_0000);
const ROUND_MAGIC: f64 = 6_755_399_441_055_744.0;
#[inline(always)]
pub fn fast_two_sum(a: f64, b: f64) -> (f64, f64) {
let s = a + b;
let z = s - a;
(s, b - z)
}
#[inline(always)]
pub fn two_sum(a: f64, b: f64) -> (f64, f64) {
let s = a + b;
let d = s - a;
(s, (b - d) + (a + (d - s)))
}
#[inline(always)]
pub fn two_prod(a: f64, b: f64) -> (f64, f64) {
let p = a * b;
(p, a.mul_add(b, -p))
}
#[inline(always)]
pub fn fast_sum(xh: f64, xl: f64, yh: f64, yl: f64) -> (f64, f64) {
let (sh, sl) = fast_two_sum(xh, yh);
(sh, (xl + yl) + sl)
}
#[inline(always)]
pub fn sum(xh: f64, xl: f64, ch: f64, cl: f64) -> (f64, f64) {
let (sh, sl) = two_sum(xh, ch);
(sh, (xl + cl) + sl)
}
#[inline(always)]
pub fn mul_dd(xh: f64, xl: f64, ch: f64, cl: f64) -> (f64, f64) {
let (ahhh, err) = two_prod(ch, xh);
(ahhh, (cl * xh + ch * xl) + err)
}
#[inline(always)]
pub fn mul_d_dd(x: f64, ch: f64, cl: f64) -> (f64, f64) {
let (ahhh, err) = two_prod(ch, x);
(ahhh, cl * x + err)
}
#[inline(always)]
pub fn fma_dw(ah: f64, al: f64, bh: f64, bl: f64, ch: f64, cl: f64) -> (f64, f64) {
let dh = ah.mul_add(bh, ch);
let t = ch - dh;
let e = ah.mul_add(bh, t);
let f = e + cl;
let g = ah * bl + f;
(dh, al * bh + g)
}
#[inline(always)]
pub fn horner_dd(xh: f64, xl: f64, c: &[u64], low: f64) -> (f64, f64) {
let n = c.len() / 2;
let (mut ch, l) = fast_two_sum(f64::from_bits(c[2 * (n - 1)]), low);
let mut cl = f64::from_bits(c[2 * (n - 1) + 1]) + l;
for i in (0..n - 1).rev() {
(ch, cl) = mul_dd(xh, xl, ch, cl);
(ch, cl) = fast_sum(
f64::from_bits(c[2 * i]),
f64::from_bits(c[2 * i + 1]),
ch,
cl,
);
}
(ch, cl)
}
#[inline(always)]
pub fn round_ties_even(y: f64) -> f64 {
(y + ROUND_MAGIC) - ROUND_MAGIC
}
#[inline(always)]
pub fn add_exponent(x: f64, e: i64) -> f64 {
f64::from_bits(x.to_bits().wrapping_add((e as u64) << 52))
}
#[cfg(test)]
mod tests {
use super::*;
fn dyadic(x: f64) -> (i128, i32) {
if x == 0.0 {
return (0, 0);
}
let bits = x.to_bits();
let biased = ((bits >> 52) & 0x7ff) as i32;
let frac = (bits & ((1u64 << 52) - 1)) as i128;
let (mut m, mut e) = if biased == 0 {
(frac, -1074)
} else {
(frac | (1i128 << 52), biased - 1075)
};
while m & 1 == 0 {
m >>= 1;
e += 1;
}
if bits >> 63 == 1 { (-m, e) } else { (m, e) }
}
fn exact_sum(values: &[f64]) -> (i128, i32) {
let parts: Vec<(i128, i32)> = values.iter().map(|&v| dyadic(v)).collect();
let base = parts
.iter()
.filter(|p| p.0 != 0)
.map(|p| p.1)
.min()
.unwrap_or(0);
let total = parts
.iter()
.map(|&(m, e)| if m == 0 { 0 } else { m << (e - base) })
.sum();
(total, base)
}
fn same_value(a: (i128, i32), b: (i128, i32)) -> bool {
if a.0 == 0 || b.0 == 0 {
return a.0 == b.0;
}
let base = a.1.min(b.1);
(a.0 << (a.1 - base)) == (b.0 << (b.1 - base))
}
const SAMPLES: [f64; 10] = [
1.0,
-3.5,
0.1,
1e-10,
-7.25e3,
1.0 + f64::EPSILON,
0.333_333_333_333_333_3,
-2.0e-5,
6.02e2,
1.25e-3,
];
#[test]
fn test_two_sum_and_fast_two_sum_are_exact() {
for &a in &SAMPLES {
for &b in &SAMPLES {
let (s, e) = two_sum(a, b);
assert!(
same_value(exact_sum(&[s, e]), exact_sum(&[a, b])),
"{a} {b}"
);
if a.abs() >= b.abs() {
let (s, e) = fast_two_sum(a, b);
assert!(
same_value(exact_sum(&[s, e]), exact_sum(&[a, b])),
"{a} {b}"
);
}
}
}
}
#[test]
fn test_two_prod_is_exact() {
for &a in &SAMPLES {
for &b in &SAMPLES {
let (p, e) = two_prod(a, b);
let (ma, ea) = dyadic(a);
let (mb, eb) = dyadic(b);
assert!(
same_value(exact_sum(&[p, e]), (ma * mb, ea + eb)),
"{a} {b}"
);
}
}
}
#[test]
fn test_round_ties_even_matches_integer_rounding() {
let cases = [
(0.5, 0.0),
(1.5, 2.0),
(2.5, 2.0),
(-0.5, 0.0),
(-1.5, -2.0),
(-2.5, -2.0),
(3.499_999_999_999_999_6, 3.0),
(4_194_303.5, 4_194_304.0),
(-4_398_046_511_103.5, -4_398_046_511_104.0),
];
for (y, expected) in cases {
assert_eq!(round_ties_even(y), expected, "{y}");
}
let mut y = -1.0e6;
while y < 1.0e6 {
assert_eq!(round_ties_even(y), y.round_ties_even(), "{y}");
y += 0.123_456_789;
}
}
#[test]
fn test_add_exponent_scales_exactly() {
assert_eq!(add_exponent(1.5, 10), 1536.0);
assert_eq!(add_exponent(1.5, -1022), 1.5 * f64::MIN_POSITIVE);
assert_eq!(add_exponent(1.0, 1023), 2f64.powi(1023));
}
}