From 46e1e5c0f98d06f377a01943fc0063cc8ef86aa1 Mon Sep 17 00:00:00 2001 From: numzero Date: Sun, 2 Nov 2025 00:38:17 +0300 Subject: [PATCH] swap storage order --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2e28ba5..29e6095 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,8 +8,8 @@ const M_STORAGE_MAX: u8 = (1 << M_BITS) - 1; const E_STORAGE_MAX: u8 = (1 << E_BITS) - 1; const M_BIAS: u8 = 1 << M_BITS; const E_BIAS: u8 = E_STORAGE_MAX - E_MAX; -const M_MASK: u8 = M_STORAGE_MAX << E_BITS; -const E_MASK: u8 = E_STORAGE_MAX; +const M_MASK: u8 = M_STORAGE_MAX; +const E_MASK: u8 = E_STORAGE_MAX << M_BITS; #[repr(transparent)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] @@ -18,7 +18,7 @@ pub struct F8(u8); impl F8 { /// Split self into the mantissa and exponent, as stored. 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)`. @@ -30,7 +30,7 @@ impl F8 { fn merge(m: u8, e: u8) -> Self { assert!(m <= M_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() { assert_eq!(u8::from(F8(0)), 0); 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}"); } } }