use std::cmp::Ordering;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dint {
pub hi: u64,
pub lo: u64,
pub ex: i64,
pub sgn: u64,
}
impl Dint {
pub const ZERO: Dint = Dint {
hi: 0,
lo: 0,
ex: 0,
sgn: 0,
};
#[inline(always)]
pub fn from_table(table: &[u64], index: usize) -> Dint {
Dint {
hi: table[4 * index],
lo: table[4 * index + 1],
ex: table[4 * index + 2] as i64,
sgn: table[4 * index + 3],
}
}
#[inline(always)]
fn significand(self) -> u128 {
((self.hi as u128) << 64) | self.lo as u128
}
#[inline(always)]
fn is_zero(self) -> bool {
self.hi | self.lo == 0
}
#[inline(always)]
fn cmp_magnitude(self, other: Dint) -> Ordering {
self.ex
.cmp(&other.ex)
.then(self.hi.cmp(&other.hi))
.then(self.lo.cmp(&other.lo))
}
pub fn add(a: Dint, b: Dint) -> Dint {
if a.is_zero() {
return b;
}
if b.is_zero() {
return a;
}
match a.cmp_magnitude(b) {
Ordering::Equal => {
if a.sgn ^ b.sgn != 0 {
return Dint::ZERO;
}
return Dint { ex: a.ex + 1, ..a };
}
Ordering::Less => return Dint::add(b, a),
Ordering::Greater => {}
}
let big = a.significand();
let mut small = b.significand();
let mut m_ex = a.ex;
if a.ex > b.ex {
let sh = a.ex - b.ex;
if sh <= 128 {
small = small.wrapping_add(1 & (small >> (sh - 1)));
}
small = if sh < 128 { small >> sh } else { 0 };
}
let mut c;
if a.sgn ^ b.sgn != 0 {
c = big.wrapping_sub(small);
} else {
let (sum, overflow) = big.overflowing_add(small);
c = sum;
if overflow {
c = c.wrapping_add(c & 1);
c = (1u128 << 127) | (c >> 1);
m_ex += 1;
}
}
if c == 0 {
return Dint {
hi: 0,
lo: 0,
ex: m_ex.wrapping_sub(64i64.wrapping_add(a.ex)),
sgn: a.sgn,
};
}
let shift = c.leading_zeros();
c <<= shift;
Dint {
hi: (c >> 64) as u64,
lo: c as u64,
ex: m_ex - shift as i64,
sgn: a.sgn,
}
}
pub fn mul(a: Dint, b: Dint) -> Dint {
let mut t = (a.hi as u128) * (b.hi as u128);
let m1 = (a.hi as u128) * (b.lo as u128);
let m2 = (a.lo as u128) * (b.hi as u128);
let (m, carry) = m1.overflowing_add(m2);
if carry {
t = t.wrapping_add(1u128 << 64);
}
t = t.wrapping_add(m >> 64);
let ex = i64::from(t >> 127 == 0);
if ex == 1 {
t <<= 1;
}
t = t.wrapping_add(((m as u64) >> 63) as u128);
Dint {
hi: (t >> 64) as u64,
lo: t as u64,
ex: a.ex + b.ex - ex + 1,
sgn: a.sgn ^ b.sgn,
}
}
pub fn mul_int(b: i64, a: Dint) -> Dint {
if b == 0 {
return Dint::ZERO;
}
let c = b.unsigned_abs();
let sgn = if b < 0 { u64::from(a.sgn == 0) } else { a.sgn };
let mut t = (a.hi as u128) * (c as u128);
let high = (t >> 64) as u64;
let mut m = if high != 0 { high.leading_zeros() } else { 64 };
t <<= m;
let l = ((a.lo as u128) * (c as u128)) << (m - 1) >> 63;
let (sum, overflow) = l.overflowing_add(t);
t = sum;
if overflow {
t = t.wrapping_add(t & 1);
t = (1u128 << 127) | (t >> 1);
m -= 1;
}
Dint {
hi: (t >> 64) as u64,
lo: t as u64,
ex: a.ex + 64 - m as i64,
sgn,
}
}
pub fn from_f64(x: f64) -> Dint {
let bits = x.to_bits();
let biased = ((bits >> 52) & 0x7ff) as i64;
let m = (bits & (u64::MAX >> 12)) + if biased != 0 { 1u64 << 52 } else { 0 };
let t = m.leading_zeros();
Dint {
hi: m << t,
lo: 0,
ex: biased - 0x3ff - if t > 11 { t as i64 - 12 } else { 0 },
sgn: u64::from(x < 0.0),
}
}
pub fn to_f64(self) -> f64 {
let mut r = (self.hi >> 11) | (0x3ffu64 << 52);
let mut rd = f64::from_bits(0x3c90_0000_0000_0000);
if (self.hi >> 10) & 1 == 1 {
rd += f64::from_bits(0x3ca0_0000_0000_0000);
}
r |= self.sgn << 63;
let rounded = f64::from_bits(r) + if self.sgn == 0 { rd } else { -rd };
let scale = f64::from_bits(((self.ex + 1023) as u64 & 0x7ff) << 52);
rounded * scale
}
}
#[cfg(test)]
mod tests {
use super::*;
fn value(d: Dint) -> f64 {
let mut magnitude = (d.hi as f64) * 2f64.powi(-63);
let mut e = d.ex as i32;
while e.abs() > 500 {
let step = 500 * e.signum();
magnitude *= 2f64.powi(step);
e -= step;
}
magnitude *= 2f64.powi(e);
if d.sgn == 1 { -magnitude } else { magnitude }
}
#[test]
fn test_from_f64_holds_exact_value() {
for x in [1.0, 1.5, -3.25, f64::MIN_POSITIVE, f64::from_bits(1), 1e300] {
let d = Dint::from_f64(x);
assert_eq!(d.hi >> 63, 1);
assert_eq!(value(d), x, "{x}");
}
}
#[test]
fn test_add_and_mul_on_small_values_are_exact() {
let a = Dint::from_f64(1.5);
let b = Dint::from_f64(-0.25);
assert_eq!(value(Dint::add(a, b)), 1.25);
assert_eq!(value(Dint::add(b, a)), 1.25);
assert_eq!(value(Dint::add(a, a)), 3.0);
assert!(Dint::add(a, Dint { sgn: 1, ..a }).is_zero());
assert_eq!(value(Dint::mul(a, b)), -0.375);
assert_eq!(value(Dint::mul_int(-12, a)), -18.0);
assert_eq!(value(Dint::mul_int(1, b)), -0.25);
}
#[test]
fn test_to_f64_rounds_to_nearest() {
let one_and_half_ulp = Dint {
hi: (1u64 << 63) | (1 << 11) | (1 << 10) | 1,
lo: 0,
ex: 0,
sgn: 0,
};
assert_eq!(one_and_half_ulp.to_f64(), 1.0 + 2.0 * f64::EPSILON);
let below_half = Dint {
hi: (1u64 << 63) | (1 << 9),
lo: 0,
ex: 3,
sgn: 1,
};
assert_eq!(below_half.to_f64(), -8.0);
}
}