support division
This commit is contained in:
parent
a423f553df
commit
59809a3830
31
src/ops.rs
31
src/ops.rs
|
|
@ -51,6 +51,22 @@ impl std::ops::Mul for F8 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::ops::Div for F8 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn div(self, rhs: Self) -> Self::Output {
|
||||||
|
if rhs.0 == 0 {
|
||||||
|
return Self(0xff);
|
||||||
|
}
|
||||||
|
if self.0 == 0 {
|
||||||
|
return Self(0);
|
||||||
|
}
|
||||||
|
let (m1, e1) = self.split_unbias();
|
||||||
|
let (m2, e2) = rhs.split_unbias();
|
||||||
|
Self::merge_unbias(((m1 as u32) << 24) / m2 as u32, e1 as i32 - e2 as i32 - 24)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -88,4 +104,19 @@ mod tests {
|
||||||
assert_eq!(F8::from(12) * F8::from(1), F8::from(12));
|
assert_eq!(F8::from(12) * F8::from(1), F8::from(12));
|
||||||
assert_eq!(F8::from(12) * F8::from(2), F8::from(24));
|
assert_eq!(F8::from(12) * F8::from(2), F8::from(24));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_div() {
|
||||||
|
assert_eq!(F8::from(3) / F8::from(0), F8::from(0xff));
|
||||||
|
assert_eq!(F8::from(0) / F8::from(7), F8::from(0));
|
||||||
|
assert_eq!(F8::from(3) / F8::from(5), F8::merge_unbias(0b100110, -6));
|
||||||
|
assert_eq!(F8::from(5) / F8::from(3), F8::merge_unbias(0b110101, -5));
|
||||||
|
assert_eq!(F8::from(6) / F8::from(3), F8::from(2));
|
||||||
|
assert_eq!(F8::from(7) / F8::from(3), F8::merge_unbias(0b100101, -4));
|
||||||
|
assert_eq!(F8::from(12) / F8::from(1), F8::from(12));
|
||||||
|
assert_eq!(F8::from(12) / F8::from(2), F8::from(6));
|
||||||
|
assert_eq!(F8::from(12) / F8::from(3), F8::from(4));
|
||||||
|
assert_eq!(F8::from(12) / F8::from(4), F8::from(3));
|
||||||
|
assert_eq!(F8::from(12) / F8::from(5), F8::merge_unbias(0b100110, -4));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user