#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Single(pub u32);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Double(pub u64);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Extended {
pub sign_exponent: u16,
pub significand: u64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Category {
Zero,
Denormal,
Normal,
Infinity,
Nan,
Unsupported,
}
macro_rules! binary_methods {
($word:ty, fraction_bits: $fraction_bits:literal, exponent_mask: $exponent_mask:literal) => {
#[inline]
pub fn next_up(self) -> Self {
self.adjacent(false)
}
#[inline]
pub fn next_down(self) -> Self {
self.adjacent(true)
}
fn adjacent(self, toward_negative: bool) -> Self {
if self.is_nan() || self == Self::infinity(toward_negative) {
self
} else if self.0 << 1 == 0 {
Self(Self::zero(toward_negative).0 | 1)
} else if self.negative() == toward_negative {
Self(self.0 + 1)
} else {
Self(self.0 - 1)
}
}
#[inline]
pub fn negative(self) -> bool {
self.0 >> (<$word>::BITS - 1) != 0
}
#[inline]
pub fn exponent(self) -> i32 {
((self.0 >> $fraction_bits) & $exponent_mask) as i32
}
#[inline]
pub fn fraction(self) -> $word {
self.0 & ((1 << $fraction_bits) - 1)
}
#[inline]
pub fn is_nan(self) -> bool {
let magnitude = self.0 & (<$word>::MAX >> 1);
magnitude > $exponent_mask << $fraction_bits
}
#[inline]
pub fn is_denormal(self) -> bool {
self.exponent() == 0 && self.fraction() != 0
}
#[inline]
pub fn zero(negative: bool) -> Self {
<Self as Format>::zero(negative)
}
};
}
impl Single {
binary_methods!(u32, fraction_bits: 23, exponent_mask: 0xFF);
}
impl Double {
binary_methods!(u64, fraction_bits: 52, exponent_mask: 0x7FF);
}
impl Extended {
#[inline]
pub fn from_bytes(bytes: [u8; 10]) -> Extended {
let [b0, b1, b2, b3, b4, b5, b6, b7, b8, b9] = bytes;
Extended {
sign_exponent: u16::from_le_bytes([b8, b9]),
significand: u64::from_le_bytes([b0, b1, b2, b3, b4, b5, b6, b7]),
}
}
#[inline]
pub fn to_bytes(self) -> [u8; 10] {
let [b0, b1, b2, b3, b4, b5, b6, b7] = self.significand.to_le_bytes();
let [b8, b9] = self.sign_exponent.to_le_bytes();
[b0, b1, b2, b3, b4, b5, b6, b7, b8, b9]
}
pub(crate) const BIAS: i32 = 16383;
pub(crate) const EXPONENT_MASK: u16 = 0x7FFF;
pub(crate) const MIN_EXPONENT: i32 = -16382;
pub(crate) const MAX_EXPONENT: i32 = 16383;
#[inline]
pub fn negative(self) -> bool {
self.sign_exponent >> 15 != 0
}
#[inline]
pub fn exponent(self) -> i32 {
i32::from(self.sign_exponent & Extended::EXPONENT_MASK)
}
#[inline]
pub fn is_zero(self) -> bool {
self.sign_exponent & Extended::EXPONENT_MASK == 0 && self.significand == 0
}
#[inline]
pub fn is_nan(self) -> bool {
self.sign_exponent & Extended::EXPONENT_MASK == Extended::EXPONENT_MASK
&& self.significand > 1 << 63
}
#[inline]
pub fn is_infinity(self) -> bool {
self.sign_exponent & Extended::EXPONENT_MASK == Extended::EXPONENT_MASK
&& self.significand == 1 << 63
}
#[inline]
pub fn is_denormal(self) -> bool {
self.sign_exponent & Extended::EXPONENT_MASK == 0 && self.significand != 0
}
#[inline]
pub fn classify(self) -> Category {
let integer = self.significand >> 63 != 0;
match self.sign_exponent & Extended::EXPONENT_MASK {
0 if self.significand == 0 => Category::Zero,
0 => Category::Denormal,
_ if !integer => Category::Unsupported,
Extended::EXPONENT_MASK if self.significand == 1 << 63 => Category::Infinity,
Extended::EXPONENT_MASK => Category::Nan,
_ => Category::Normal,
}
}
#[inline]
pub fn canonical(self) -> Option<Extended> {
match self.classify() {
Category::Unsupported => None,
Category::Denormal if self.significand >> 63 != 0 => Some(Extended {
sign_exponent: self.sign_exponent | 1,
..self
}),
_ => Some(self),
}
}
#[inline]
pub fn zero(negative: bool) -> Extended {
<Extended as Format>::zero(negative)
}
#[inline]
pub fn infinity(negative: bool) -> Extended {
<Extended as Format>::infinity(negative)
}
#[inline]
pub fn normalized(self) -> Option<(i32, u64)> {
if self.significand == 0 {
return None;
}
let shift = self.significand.leading_zeros();
Some((
self.exponent().max(1) - shift as i32,
self.significand << shift,
))
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Class {
Zero,
Finite,
Infinity,
Nan,
}
#[derive(Clone, Copy)]
pub(crate) struct Decoded {
pub(crate) negative: bool,
pub(crate) class: Class,
pub(crate) exponent: i32,
pub(crate) significand: u64,
}
#[derive(Clone, Copy)]
pub(crate) struct Magnitude {
pub(crate) exponent: i32,
pub(crate) significand: u64,
}
pub(crate) trait Format: Copy {
fn decode(self) -> Decoded;
fn finite(self) -> Option<(bool, Magnitude)>;
fn is_normal(self) -> bool;
fn normal(self) -> (bool, i32, u64);
fn encode(negative: bool, exponent: i32, significand: u64) -> Self;
fn zero(negative: bool) -> Self;
fn infinity(negative: bool) -> Self;
fn words(self) -> (u64, u16);
fn from_words(low: u64, top: u16) -> Self;
}
fn normalized(significand: u64, exponent: i32) -> Magnitude {
let shift = significand.leading_zeros();
Magnitude {
exponent: exponent - shift as i32,
significand: significand << shift,
}
}
impl Format for Single {
fn decode(self) -> Decoded {
let negative = self.0 >> 31 != 0;
let field = (self.0 >> 23) & 0xFF;
let fraction = u64::from(self.0 & 0x7F_FFFF);
let (class, exponent, significand) = if field == 0xFF {
(
if fraction == 0 {
Class::Infinity
} else {
Class::Nan
},
0,
0,
)
} else if field != 0 {
(
Class::Finite,
field as i32 - 127,
(fraction | 1 << 23) << 40,
)
} else if fraction != 0 {
let shift = fraction.leading_zeros();
(Class::Finite, -86 - shift as i32, fraction << shift)
} else {
(Class::Zero, 0, 0)
};
Decoded {
negative,
class,
exponent,
significand,
}
}
#[inline]
fn finite(self) -> Option<(bool, Magnitude)> {
let field = (self.0 >> 23) & 0xFF;
let fraction = u64::from(self.0 & 0x7F_FFFF);
let finite = if field.wrapping_sub(1) < 0xFE {
Some(Magnitude {
exponent: field as i32 - 127,
significand: (fraction | 1 << 23) << 40,
})
} else if (field == 0) & (fraction != 0) {
Some(normalized(fraction, -86))
} else {
None
};
finite.map(|magnitude| (self.0 >> 31 != 0, magnitude))
}
fn words(self) -> (u64, u16) {
(u64::from(self.0), 0)
}
fn from_words(low: u64, _: u16) -> Single {
Single(low as u32)
}
fn is_normal(self) -> bool {
((self.0 >> 23) & 0xFF).wrapping_sub(1) < 0xFE
}
fn normal(self) -> (bool, i32, u64) {
(
self.0 >> 31 != 0,
((self.0 >> 23) & 0xFF) as i32 - 127,
u64::from(self.0) << 40 | 1 << 63,
)
}
fn encode(negative: bool, exponent: i32, significand: u64) -> Single {
Single(
u32::from(negative) << 31
| (exponent.wrapping_add(127) as u32 & 0xFF) << 23
| ((significand << 1) >> 41) as u32,
)
}
fn zero(negative: bool) -> Single {
Single(u32::from(negative) << 31)
}
fn infinity(negative: bool) -> Single {
Single(u32::from(negative) << 31 | 0x7F80_0000)
}
}
impl Format for Double {
fn decode(self) -> Decoded {
let negative = self.0 >> 63 != 0;
let field = (self.0 >> 52) & 0x7FF;
let fraction = self.0 & 0xF_FFFF_FFFF_FFFF;
let (class, exponent, significand) = if field == 0x7FF {
(
if fraction == 0 {
Class::Infinity
} else {
Class::Nan
},
0,
0,
)
} else if field != 0 {
(
Class::Finite,
field as i32 - 1023,
(fraction | 1 << 52) << 11,
)
} else if fraction != 0 {
let shift = fraction.leading_zeros();
(Class::Finite, -1011 - shift as i32, fraction << shift)
} else {
(Class::Zero, 0, 0)
};
Decoded {
negative,
class,
exponent,
significand,
}
}
#[inline]
fn finite(self) -> Option<(bool, Magnitude)> {
let field = (self.0 >> 52) & 0x7FF;
let fraction = self.0 & 0xF_FFFF_FFFF_FFFF;
let finite = if field.wrapping_sub(1) < 0x7FE {
Some(Magnitude {
exponent: field as i32 - 1023,
significand: self.0 << 11 | 1 << 63,
})
} else if (field == 0) & (fraction != 0) {
Some(normalized(fraction, -1011))
} else {
None
};
finite.map(|magnitude| (self.0 >> 63 != 0, magnitude))
}
fn words(self) -> (u64, u16) {
(self.0, 0)
}
fn from_words(low: u64, _: u16) -> Double {
Double(low)
}
fn is_normal(self) -> bool {
((self.0 >> 52) & 0x7FF).wrapping_sub(1) < 0x7FE
}
fn normal(self) -> (bool, i32, u64) {
(
self.0 >> 63 != 0,
((self.0 >> 52) & 0x7FF) as i32 - 1023,
self.0 << 11 | 1 << 63,
)
}
fn encode(negative: bool, exponent: i32, significand: u64) -> Double {
Double(
u64::from(negative) << 63
| (exponent.wrapping_add(1023) as u64 & 0x7FF) << 52
| (significand << 1) >> 12,
)
}
fn zero(negative: bool) -> Double {
Double(u64::from(negative) << 63)
}
fn infinity(negative: bool) -> Double {
Double(u64::from(negative) << 63 | 0x7FF0_0000_0000_0000)
}
}
impl Format for Extended {
fn decode(self) -> Decoded {
let negative = self.sign_exponent >> 15 != 0;
let field = i32::from(self.sign_exponent & Extended::EXPONENT_MASK);
let (class, exponent, significand) = if field == i32::from(Extended::EXPONENT_MASK) {
(
if self.significand << 1 == 0 {
Class::Infinity
} else {
Class::Nan
},
0,
0,
)
} else if self.significand >> 63 != 0 {
(
Class::Finite,
field.max(1) - Extended::BIAS,
self.significand,
)
} else if self.significand != 0 {
let shift = self.significand.leading_zeros();
(
Class::Finite,
field.max(1) - Extended::BIAS - shift as i32,
self.significand << shift,
)
} else {
(Class::Zero, 0, 0)
};
Decoded {
negative,
class,
exponent,
significand,
}
}
#[inline]
fn finite(self) -> Option<(bool, Magnitude)> {
let field = i32::from(self.sign_exponent & Extended::EXPONENT_MASK);
let finite = if field == i32::from(Extended::EXPONENT_MASK) {
None
} else if self.significand >> 63 != 0 {
Some(Magnitude {
exponent: field.max(1) - Extended::BIAS,
significand: self.significand,
})
} else if self.significand != 0 {
Some(normalized(self.significand, field.max(1) - Extended::BIAS))
} else {
None
};
finite.map(|magnitude| (self.sign_exponent >> 15 != 0, magnitude))
}
fn words(self) -> (u64, u16) {
(self.significand, self.sign_exponent)
}
fn from_words(low: u64, top: u16) -> Extended {
Extended {
sign_exponent: top,
significand: low,
}
}
fn is_normal(self) -> bool {
((self.sign_exponent & Extended::EXPONENT_MASK).wrapping_sub(1)
< Extended::EXPONENT_MASK - 1)
& (self.significand >> 63 != 0)
}
fn normal(self) -> (bool, i32, u64) {
(
self.sign_exponent >> 15 != 0,
i32::from(self.sign_exponent & Extended::EXPONENT_MASK) - Extended::BIAS,
self.significand,
)
}
fn encode(negative: bool, exponent: i32, significand: u64) -> Extended {
Extended {
sign_exponent: u16::from(negative) << 15
| (exponent.wrapping_add(Extended::BIAS) as u16 & Extended::EXPONENT_MASK),
significand,
}
}
fn zero(negative: bool) -> Extended {
Extended {
sign_exponent: u16::from(negative) << 15,
significand: 0,
}
}
fn infinity(negative: bool) -> Extended {
Extended {
sign_exponent: u16::from(negative) << 15 | Extended::EXPONENT_MASK,
significand: 1 << 63,
}
}
}