Add public API and docs #1
41
src/fmt.rs
Normal file
41
src/fmt.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
use crate::{E_BIAS, F8};
|
||||||
|
|
||||||
|
impl std::fmt::Binary for F8 {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
if f.alternate() {
|
||||||
|
f.write_str("0b")?;
|
||||||
|
}
|
||||||
|
if self.0 == 0 {
|
||||||
|
f.write_str("0.00000p0")?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let (m, e) = self.split();
|
||||||
|
write!(f, "1.{m:05b}p{e}", e = e as i8 - E_BIAS as i8)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for F8 {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{self:#b}f8")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_display() {
|
||||||
|
fn fmt_split(m: u8, e: u8) -> String {
|
||||||
|
let v = F8::merge(m, e);
|
||||||
|
format!("{v:b}")
|
||||||
|
}
|
||||||
|
assert_eq!("0.00000p0", fmt_split(0, 0));
|
||||||
|
assert_eq!("1.00000p0", fmt_split(0, E_BIAS));
|
||||||
|
assert_eq!("1.00000p1", fmt_split(0, E_BIAS + 1));
|
||||||
|
assert_eq!("1.00000p-1", fmt_split(0, E_BIAS - 1));
|
||||||
|
assert_eq!("1.00001p0", fmt_split(1, E_BIAS));
|
||||||
|
assert_eq!("1.11111p0", fmt_split(0b11111, E_BIAS));
|
||||||
|
}
|
||||||
|
}
|
||||||
41
src/lib.rs
41
src/lib.rs
|
|
@ -1,4 +1,5 @@
|
||||||
mod conv;
|
mod conv;
|
||||||
|
mod fmt;
|
||||||
mod ops;
|
mod ops;
|
||||||
|
|
||||||
pub const M_BITS: u8 = 5;
|
pub const M_BITS: u8 = 5;
|
||||||
|
|
@ -23,43 +24,3 @@ const E_MASK: u8 = E_STORAGE_MAX << M_BITS;
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct F8(u8);
|
pub struct F8(u8);
|
||||||
|
|
||||||
impl std::fmt::Binary for F8 {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
if f.alternate() {
|
|
||||||
f.write_str("0b")?;
|
|
||||||
}
|
|
||||||
if self.0 == 0 {
|
|
||||||
f.write_str("0.00000p0")?;
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
let (m, e) = self.split();
|
|
||||||
write!(f, "1.{m:05b}p{e}", e = e as i8 - E_BIAS as i8)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for F8 {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "{self:#b}f8")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_display() {
|
|
||||||
fn fmt_split(m: u8, e: u8) -> String {
|
|
||||||
let v = F8::merge(m, e);
|
|
||||||
format!("{v:b}")
|
|
||||||
}
|
|
||||||
assert_eq!("0.00000p0", fmt_split(0, 0));
|
|
||||||
assert_eq!("1.00000p0", fmt_split(0, E_BIAS));
|
|
||||||
assert_eq!("1.00000p1", fmt_split(0, E_BIAS + 1));
|
|
||||||
assert_eq!("1.00000p-1", fmt_split(0, E_BIAS - 1));
|
|
||||||
assert_eq!("1.00001p0", fmt_split(1, E_BIAS));
|
|
||||||
assert_eq!("1.11111p0", fmt_split(0b11111, E_BIAS));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user