use std::cmp::Ordering;
use crate::compress::varint::{Reader, put_count};
const WORDS: usize = 35;
const POINT: u32 = 1074;
pub(super) const BLOCK_BITS: u32 = 2106;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) struct Exact([u64; WORDS]);
impl Default for Exact {
fn default() -> Self {
Exact::ZERO
}
}
impl std::fmt::Debug for Exact {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Exact({})", self.round())
}
}
impl Exact {
pub(super) const ZERO: Exact = Exact([0; WORDS]);
#[cfg(test)]
pub(super) fn of(value: f64) -> Option<Exact> {
let mut exact = Exact::ZERO;
exact.add_float(value).then_some(exact)
}
pub(super) fn add_float(&mut self, value: f64) -> bool {
let Some((mantissa, shift)) = units(value) else { return false };
if value.is_sign_negative() {
self.sub_shifted(mantissa, shift)
} else {
self.add_shifted(mantissa, shift)
}
true
}
pub(super) fn sub_float(&mut self, value: f64) -> bool {
self.add_float(-value)
}
pub(super) fn of_whole(count: u64) -> Exact {
let mut exact = Exact::ZERO;
exact.add_shifted(count, POINT);
exact
}
pub(super) fn whole(&self) -> Option<u64> {
if self.is_negative() || low_bits_set(&self.0, POINT) {
return None;
}
let whole = shift_right(&self.0, POINT);
whole[1..].iter().all(|word| *word == 0).then_some(whole[0])
}
pub(super) fn times(&self, factor: u64) -> Exact {
let mut words = self.magnitude();
multiply(&mut words, factor);
let product = Exact(words);
debug_assert!(!product.is_negative(), "a product past the accumulator");
if self.is_negative() {
let mut negative = Exact::ZERO;
negative.sub(&product);
return negative;
}
product
}
pub(super) fn add(&mut self, other: &Exact) {
let mut carry = false;
for (word, more) in self.0.iter_mut().zip(other.0) {
let (sum, over) = word.overflowing_add(more);
let (sum, again) = sum.overflowing_add(u64::from(carry));
(*word, carry) = (sum, over || again);
}
}
pub(super) fn sub(&mut self, other: &Exact) {
let mut borrow = false;
for (word, less) in self.0.iter_mut().zip(other.0) {
let (difference, under) = word.overflowing_sub(less);
let (difference, again) = difference.overflowing_sub(u64::from(borrow));
(*word, borrow) = (difference, under || again);
}
}
fn add_shifted(&mut self, mantissa: u64, shift: u32) {
let (word, bits) = ((shift / 64) as usize, shift % 64);
let wide = u128::from(mantissa) << bits;
let (low, over) = self.0[word].overflowing_add(wide as u64);
self.0[word] = low;
let (high, mut carry) = self.0[word + 1].overflowing_add((wide >> 64) as u64);
let (high, again) = high.overflowing_add(u64::from(over));
self.0[word + 1] = high;
carry |= again;
for next in &mut self.0[word + 2..] {
if !carry {
break;
}
(*next, carry) = next.overflowing_add(1);
}
}
fn sub_shifted(&mut self, mantissa: u64, shift: u32) {
let (word, bits) = ((shift / 64) as usize, shift % 64);
let wide = u128::from(mantissa) << bits;
let (low, under) = self.0[word].overflowing_sub(wide as u64);
self.0[word] = low;
let (high, mut borrow) = self.0[word + 1].overflowing_sub((wide >> 64) as u64);
let (high, again) = high.overflowing_sub(u64::from(under));
self.0[word + 1] = high;
borrow |= again;
for next in &mut self.0[word + 2..] {
if !borrow {
break;
}
(*next, borrow) = next.overflowing_sub(1);
}
}
pub(super) fn is_zero(&self) -> bool {
self.0.iter().all(|word| *word == 0)
}
pub(super) fn is_negative(&self) -> bool {
self.0[WORDS - 1] >> 63 == 1
}
fn magnitude(&self) -> [u64; WORDS] {
let mut magnitude = *self;
if self.is_negative() {
magnitude = Exact::ZERO;
magnitude.sub(self);
}
magnitude.0
}
pub(super) fn round(&self) -> f64 {
signed(self.is_negative(), to_float(&self.magnitude(), Remainder::None))
}
pub(super) fn divided(&self, divisor: u64) -> f64 {
self.scaled(1, divisor)
}
pub(super) fn scaled(&self, factor: u64, divisor: u64) -> f64 {
if divisor == 0 {
return f64::NAN;
}
let mut words = self.magnitude();
multiply(&mut words, factor);
let remainder = divide(&mut words, divisor);
let rest = match (2 * u128::from(remainder)).cmp(&u128::from(divisor)) {
_ if remainder == 0 => Remainder::None,
Ordering::Less => Remainder::BelowHalf,
Ordering::Equal => Remainder::Half,
Ordering::Greater => Remainder::AboveHalf,
};
signed(self.is_negative(), to_float(&words, rest))
}
pub(super) fn put(&self, out: &mut Vec<u8>) {
let magnitude = self.magnitude();
let Some(low) = magnitude.iter().position(|word| *word != 0) else { return out.push(0) };
let zeros = low as u32 * 64 + magnitude[low].trailing_zeros();
let shifted = shift_right(&magnitude, zeros);
let bits = bit_length(&shifted);
let bytes = bits.div_ceil(8) as usize;
put_count(out, bytes as u64);
put_count(out, (u64::from(zeros) << 1) | u64::from(self.is_negative()));
out.extend((0..bytes).rev().map(|place| (shifted[place / 8] >> (place % 8 * 8)) as u8));
}
pub(super) fn read(reader: &mut Reader<'_>, most_bits: u32) -> Exact {
let bytes = reader.count(most_bits.div_ceil(8) as usize);
if bytes == 0 {
return Exact::ZERO;
}
let token = reader.uvarint();
let zeros = token >> 1;
let magnitude = reader.take(bytes);
if magnitude.len() != bytes || zeros >= u64::from(most_bits) {
reader.fail("an exact sum past its bound");
return Exact::ZERO;
}
if magnitude[0] == 0 || magnitude[bytes - 1] & 1 == 0 {
reader.fail("an exact sum not written as its one spelling");
return Exact::ZERO;
}
let mut words = [0_u64; WORDS];
for (place, byte) in magnitude.iter().rev().enumerate() {
words[place / 8] |= u64::from(*byte) << (place % 8 * 8);
}
let bits = bit_length(&words) + zeros as u32;
if bits > most_bits {
reader.fail("an exact sum past its bound");
return Exact::ZERO;
}
let mut exact = Exact(shift_left(&words, zeros as u32));
if token & 1 == 1 {
let positive = exact;
exact = Exact::ZERO;
exact.sub(&positive);
}
exact
}
}
impl PartialOrd for Exact {
fn partial_cmp(&self, other: &Exact) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Exact {
fn cmp(&self, other: &Exact) -> Ordering {
match (self.is_negative(), other.is_negative()) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => self.0.iter().rev().cmp(other.0.iter().rev()),
}
}
}
fn units(value: f64) -> Option<(u64, u32)> {
let bits = value.to_bits();
let exponent = ((bits >> 52) & 0x7ff) as u32;
let fraction = bits & ((1 << 52) - 1);
match exponent {
0x7ff => None,
0 => Some((fraction, 0)),
_ => Some((fraction | (1 << 52), exponent - 1)),
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Remainder {
None,
BelowHalf,
Half,
AboveHalf,
}
fn to_float(words: &[u64; WORDS], rest: Remainder) -> f64 {
let length = bit_length(words);
let shift = length.saturating_sub(53);
let mantissa = shift_right(words, shift)[0];
let up = if shift == 0 {
match rest {
Remainder::AboveHalf => true,
Remainder::Half => mantissa & 1 == 1,
Remainder::None | Remainder::BelowHalf => false,
}
} else {
let dropped_high = bit(words, shift - 1);
let dropped_low = low_bits_set(words, shift - 1) || rest != Remainder::None;
dropped_high && (dropped_low || mantissa & 1 == 1)
};
let bits = (u64::from(shift) << 52) + mantissa + u64::from(up);
if bits >= f64::INFINITY.to_bits() { f64::INFINITY } else { f64::from_bits(bits) }
}
fn signed(negative: bool, magnitude: f64) -> f64 {
if negative { -magnitude } else { magnitude }
}
fn bit_length(words: &[u64; WORDS]) -> u32 {
words.iter().rposition(|word| *word != 0).map_or(0, |top| top as u32 * 64 + 64 - words[top].leading_zeros())
}
fn bit(words: &[u64; WORDS], at: u32) -> bool {
words[(at / 64) as usize] >> (at % 64) & 1 == 1
}
fn low_bits_set(words: &[u64; WORDS], below: u32) -> bool {
let (whole, part) = ((below / 64) as usize, below % 64);
words[..whole].iter().any(|word| *word != 0) || (part > 0 && words[whole] & ((1 << part) - 1) != 0)
}
fn shift_right(words: &[u64; WORDS], by: u32) -> [u64; WORDS] {
let (whole, part) = ((by / 64) as usize, by % 64);
let mut out = [0_u64; WORDS];
for place in 0..WORDS.saturating_sub(whole) {
let low = words[place + whole] >> part;
let high = if part > 0 && place + whole + 1 < WORDS { words[place + whole + 1] << (64 - part) } else { 0 };
out[place] = low | high;
}
out
}
fn shift_left(words: &[u64; WORDS], by: u32) -> [u64; WORDS] {
let (whole, part) = ((by / 64) as usize, by % 64);
let mut out = [0_u64; WORDS];
for place in whole..WORDS {
let high = words[place - whole] << part;
let low = if part > 0 && place > whole { words[place - whole - 1] >> (64 - part) } else { 0 };
out[place] = high | low;
}
out
}
fn multiply(words: &mut [u64; WORDS], factor: u64) {
let mut carry = 0_u128;
for word in words.iter_mut() {
let product = u128::from(*word) * u128::from(factor) + carry;
*word = product as u64;
carry = product >> 64;
}
}
fn divide(words: &mut [u64; WORDS], divisor: u64) -> u64 {
let mut remainder = 0_u128;
for word in words.iter_mut().rev() {
let dividend = (remainder << 64) | u128::from(*word);
*word = (dividend / u128::from(divisor)) as u64;
remainder = dividend % u128::from(divisor);
}
remainder as u64
}
#[cfg(test)]
#[path = "exact_tests.rs"]
mod tests;