#[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,
}
impl Single {
#[inline]
pub fn negative(self) -> bool {
self.0 >> 31 != 0
}
#[inline]
pub fn exponent(self) -> i32 {
((self.0 >> 23) & 0xFF) as i32
}
#[inline]
pub fn fraction(self) -> u32 {
self.0 & 0x7F_FFFF
}
#[inline]
pub fn is_nan(self) -> bool {
self.0 & 0x7FFF_FFFF > 0x7F80_0000
}
}
impl Double {
#[inline]
pub fn negative(self) -> bool {
self.0 >> 63 != 0
}
#[inline]
pub fn exponent(self) -> i32 {
((self.0 >> 52) & 0x7FF) as i32
}
#[inline]
pub fn fraction(self) -> u64 {
self.0 & 0xF_FFFF_FFFF_FFFF
}
#[inline]
pub fn is_nan(self) -> bool {
self.0 & 0x7FFF_FFFF_FFFF_FFFF > 0x7FF0_0000_0000_0000
}
}
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 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) -> (bool, Option<(i32, u64)>);
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;
}
#[cold]
#[inline(never)]
fn normalized(significand: u64, exponent: i32) -> (i32, u64) {
let shift = significand.leading_zeros();
(exponent - shift as i32, 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) -> (bool, Option<(i32, u64)>) {
let field = (self.0 >> 23) & 0xFF;
let fraction = u64::from(self.0 & 0x7F_FFFF);
let finite = if field.wrapping_sub(1) < 0xFE {
Some((field as i32 - 127, (fraction | 1 << 23) << 40))
} else if (field == 0) & (fraction != 0) {
Some(normalized(fraction, -86))
} else {
None
};
(self.0 >> 31 != 0, finite)
}
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) -> (bool, Option<(i32, u64)>) {
let field = (self.0 >> 52) & 0x7FF;
let fraction = self.0 & 0xF_FFFF_FFFF_FFFF;
let finite = if field.wrapping_sub(1) < 0x7FE {
Some((field as i32 - 1023, self.0 << 11 | 1 << 63))
} else if (field == 0) & (fraction != 0) {
Some(normalized(fraction, -1011))
} else {
None
};
(self.0 >> 63 != 0, finite)
}
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) -> (bool, Option<(i32, u64)>) {
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((field.max(1) - Extended::BIAS, self.significand))
} else if self.significand != 0 {
Some(normalized(self.significand, field.max(1) - Extended::BIAS))
} else {
None
};
(self.sign_exponent >> 15 != 0, finite)
}
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,
}
}
}