use crate::*;
#[derive(derivative::Derivative, Clone)]
#[derivative(Debug, PartialEq, Eq)]
pub struct Format {
pub signed: bool,
pub exp: u8,
pub mant: usize,
pub excess: u32,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
pub interpret: Interpret,
}
impl Format {
pub fn new(exp: u8, mant: usize, excess: u32) -> Format {
if exp > 31 {
panic!("exponent bits must be less than 32");
}
Format {
signed: true,
exp,
mant,
excess,
interpret: |_| None,
}
}
pub fn new_unsigned(exp: u8, mant: usize, excess: u32) -> Format {
if exp > 31 {
panic!("exponent bits must be less than 32");
}
Format {
signed: false,
exp,
mant,
excess,
interpret: |_| None,
}
}
pub fn new_with_sign(signed: bool, exp: u8, mant: usize, excess: u32) -> Format {
if exp > 31 {
panic!("exponent bits must be less than 32");
}
Format {
signed,
exp,
mant,
excess,
interpret: |_| None,
}
}
pub fn new_ieee_excess(exp: u8, mant: usize) -> Format {
if exp > 31 {
panic!("exponent bits must be less than 32");
}
Format {
signed: true,
exp,
mant,
excess: (1 << (exp - 1)) - 1,
interpret: |_| None,
}
}
pub fn new_ieee_excess_with_sign(signed: bool, exp: u8, mant: usize) -> Format {
if exp > 31 {
panic!("exponent bits must be less than 32");
}
Format {
signed,
exp,
mant,
excess: (1 << (exp - 1)) - 1,
interpret: |_| None,
}
}
pub fn len(&self) -> usize {
self.signed as usize + self.exp as usize + self.mant
}
}
impl IeeeBinary for Format {
fn ieee_binary32() -> Self {
Self {
interpret: Interpret::ieee_binary32(),
..Self::new(8, 23, 127)
}
}
fn ieee_binary64() -> Self {
Self {
interpret: Interpret::ieee_binary64(),
..Self::new(11, 52, 1023)
}
}
}