From 79ab26f1189615fae69d22c816d458fe4a17bca0 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 27 Aug 2024 14:58:32 -0700 Subject: [PATCH] fixed: implement shift operators --- fixed_wide/src/fixed.rs | 28 ++++++++++++++++++++++++++++ fixed_wide_vectors/src/tests/mod.rs | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index 1efe378..5f72a47 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -91,3 +91,31 @@ impl_assign_operator!( Fixed, BitOrAssign, bitor_assign ); impl_operator!( Fixed, BitOr, bitor, Self ); impl_assign_operator!( Fixed, BitXorAssign, bitxor_assign ); impl_operator!( Fixed, BitXor, bitxor, Self ); + +macro_rules! impl_shift_operator { + ( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { + impl core::ops::$trait for $struct{ + type Output = $output; + + fn $method(self, other: u32) -> Self::Output { + Self { + bits:self.bits.$method(other), + frac:PhantomData, + } + } + } + }; +} +macro_rules! impl_shift_assign_operator { + ( $struct: ident, $trait: ident, $method: ident ) => { + impl core::ops::$trait for $struct{ + fn $method(&mut self, other: u32) { + self.bits.$method(other); + } + } + }; +} +impl_shift_assign_operator!( Fixed, ShlAssign, shl_assign ); +impl_shift_operator!( Fixed, Shl, shl, Self ); +impl_shift_assign_operator!( Fixed, ShrAssign, shr_assign ); +impl_shift_operator!( Fixed, Shr, shr, Self ); diff --git a/fixed_wide_vectors/src/tests/mod.rs b/fixed_wide_vectors/src/tests/mod.rs index 24bf782..426d018 100644 --- a/fixed_wide_vectors/src/tests/mod.rs +++ b/fixed_wide_vectors/src/tests/mod.rs @@ -25,6 +25,12 @@ fn you_can_add_numbers(){ assert_eq!(a+a,Planar64Wide3::from((3i128*2).pow(4)*2)) } +#[test] +fn you_can_shr_numbers(){ + let a=Planar64::from(4); + assert_eq!(a>>1,Planar64::from(2)) +} + #[test] fn wide_vec3(){ let v=Vector3::from_value(Planar64::from(3));