swap storage order

This commit is contained in:
numzero 2025-11-02 00:38:17 +03:00
parent 1a8542091c
commit 46e1e5c0f9

View File

@ -8,8 +8,8 @@ const M_STORAGE_MAX: u8 = (1 << M_BITS) - 1;
const E_STORAGE_MAX: u8 = (1 << E_BITS) - 1; const E_STORAGE_MAX: u8 = (1 << E_BITS) - 1;
const M_BIAS: u8 = 1 << M_BITS; const M_BIAS: u8 = 1 << M_BITS;
const E_BIAS: u8 = E_STORAGE_MAX - E_MAX; const E_BIAS: u8 = E_STORAGE_MAX - E_MAX;
const M_MASK: u8 = M_STORAGE_MAX << E_BITS; const M_MASK: u8 = M_STORAGE_MAX;
const E_MASK: u8 = E_STORAGE_MAX; const E_MASK: u8 = E_STORAGE_MAX << M_BITS;
#[repr(transparent)] #[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
@ -18,7 +18,7 @@ pub struct F8(u8);
impl F8 { impl F8 {
/// Split self into the mantissa and exponent, as stored. /// Split self into the mantissa and exponent, as stored.
fn split(self) -> (u8, u8) { fn split(self) -> (u8, u8) {
(self.0 >> E_BITS, self.0 & E_MASK) (self.0 & M_MASK, self.0 >> M_BITS)
} }
/// Split self into integers (m, e) such that `self == m * 2.pow(e)`. /// Split self into integers (m, e) such that `self == m * 2.pow(e)`.
@ -30,7 +30,7 @@ impl F8 {
fn merge(m: u8, e: u8) -> Self { fn merge(m: u8, e: u8) -> Self {
assert!(m <= M_STORAGE_MAX); assert!(m <= M_STORAGE_MAX);
assert!(e <= E_STORAGE_MAX); assert!(e <= E_STORAGE_MAX);
Self((m << E_BITS) | e) Self((e << M_BITS) | m)
} }
} }
@ -58,7 +58,7 @@ mod tests {
fn test_int_conv() { fn test_int_conv() {
assert_eq!(u8::from(F8(0)), 0); assert_eq!(u8::from(F8(0)), 0);
for e in 0..E_MAX { for e in 0..E_MAX {
assert_eq!(u8::from(F8(e + E_BIAS)), 1 << e, "e={e}"); assert_eq!(u8::from(F8::merge(0, e + E_BIAS)), 1 << e, "e={e}");
} }
} }
} }