support addition!

This commit is contained in:
numzero 2025-11-02 02:41:59 +03:00
parent 4c2f02cfeb
commit 25fba68cd5
2 changed files with 37 additions and 0 deletions

View File

@ -1,3 +1,5 @@
mod ops;
pub const M_BITS: u8 = 5; pub const M_BITS: u8 = 5;
pub const E_BITS: u8 = 3; pub const E_BITS: u8 = 3;
const E_CAP: u8 = 4; const E_CAP: u8 = 4;

35
src/ops.rs Normal file
View File

@ -0,0 +1,35 @@
use super::*;
impl std::ops::Add for F8 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let (m1, e1) = self.split_unbias();
let (m2, e2) = rhs.split_unbias();
if e1 < e2 {
return rhs + self;
}
if rhs.0 == 0 {
return self;
}
let e_diff = e1 - e2;
let m = m1 + (m2 >> e_diff);
Self::merge_unbias(m, e1)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(F8::from(0b0011) + F8::from(0b0000), F8::from(0b0011));
assert_eq!(F8::from(0b0000) + F8::from(0b0111), F8::from(0b0111));
assert_eq!(F8::from(0b0011) + F8::from(0b0101), F8::from(0b1000));
assert_eq!(F8::from(0b0101) + F8::from(0b0011), F8::from(0b1000));
assert_eq!(F8::from(0b0011) + F8::from(0b0110), F8::from(0b1001));
assert_eq!(F8::from(0b0011) + F8::from(0b0111), F8::from(0b1010));
assert_eq!(F8::from(0b1100) + F8::from(0b0001), F8::from(0b1101));
}
}