assert range on truncation

This commit is contained in:
Quaternions 2024-09-27 12:27:31 -07:00
parent 438d0ec6ec
commit 1e83366386

View File

@ -585,6 +585,9 @@ macro_rules! impl_fix_rhs_lt_lhs_not_const_generic{
paste::item!{ paste::item!{
#[inline] #[inline]
pub fn [<fix_ $rhs>](self)->Fixed<$rhs,{$rhs*32}>{ pub fn [<fix_ $rhs>](self)->Fixed<$rhs,{$rhs*32}>{
let max=bnum::cast::As::as_::<BInt::<$lhs>>(BInt::<$rhs>::MAX).shl(($lhs-$rhs)*32);
let min=bnum::cast::As::as_::<BInt::<$lhs>>(BInt::<$rhs>::MIN).shl(($lhs-$rhs)*32);
assert!(min<self.bits&&self.bits<max,"Truncation detected");
Fixed::from_bits(bnum::cast::As::as_::<BInt::<$rhs>>(self.bits.shr(($lhs-$rhs)*32))) Fixed::from_bits(bnum::cast::As::as_::<BInt::<$rhs>>(self.bits.shr(($lhs-$rhs)*32)))
} }
} }