From f10a0e57bca5099ae1693a82a37a27b31112db4e Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 26 Mar 2024 19:45:38 +0000 Subject: [PATCH] Added zeroable and oneable traits --- src/f128/types/fixed.cairo | 35 +++++++++++++++++++++++++++++++++-- src/f64/types/fixed.cairo | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/f128/types/fixed.cairo b/src/f128/types/fixed.cairo index b574f1c..e3f2bd7 100644 --- a/src/f128/types/fixed.cairo +++ b/src/f128/types/fixed.cairo @@ -80,11 +80,11 @@ trait FixedTrait { impl FixedImpl of FixedTrait { fn ZERO() -> Fixed { - return Fixed { mag: 0, sign: false }; + return core::num::traits::Zero::zero(); } fn ONE() -> Fixed { - return Fixed { mag: ONE_u128, sign: false }; + return core::num::traits::One::one(); } fn new(mag: u128, sign: bool) -> Fixed { @@ -529,6 +529,37 @@ impl PackFixed of StorePacking { } } + +impl FixedZero of core::num::traits::Zero { + fn zero() -> Fixed { + Fixed { mag: 0, sign: false } + } + #[inline(always)] + fn is_zero(self: @Fixed) -> bool { + *self.mag == 0 + } + #[inline(always)] + fn is_non_zero(self: @Fixed) -> bool { + !self.is_zero() + } +} + +// One trait implementations +impl FixedOne of core::num::traits::One { + fn one() -> Fixed { + Fixed { mag: ONE_u128, sign: false } + } + #[inline(always)] + fn is_one(self: @Fixed) -> bool { + *self == FixedOne::one() + } + #[inline(always)] + fn is_non_one(self: @Fixed) -> bool { + !self.is_one() + } +} + + // Tests -------------------------------------------------------------------------------------------------------------- #[cfg(test)] diff --git a/src/f64/types/fixed.cairo b/src/f64/types/fixed.cairo index 14c7c8b..5e4ef81 100644 --- a/src/f64/types/fixed.cairo +++ b/src/f64/types/fixed.cairo @@ -76,11 +76,11 @@ trait FixedTrait { impl FixedImpl of FixedTrait { fn ZERO() -> Fixed { - return Fixed { mag: 0, sign: false }; + return core::num::traits::Zero::zero(); } fn ONE() -> Fixed { - return Fixed { mag: ONE, sign: false }; + return core::num::traits::One::one(); } fn new(mag: u64, sign: bool) -> Fixed { @@ -527,6 +527,34 @@ impl PackFixed of StorePacking { } } +impl FixedZero of core::num::traits::Zero { + fn zero() -> Fixed { + Fixed { mag: 0, sign: false } + } + #[inline(always)] + fn is_zero(self: @Fixed) -> bool { + *self.mag == 0 + } + #[inline(always)] + fn is_non_zero(self: @Fixed) -> bool { + !self.is_zero() + } +} + +// One trait implementations +impl FixedOne of core::num::traits::One { + fn one() -> Fixed { + Fixed { mag: ONE, sign: false } + } + #[inline(always)] + fn is_one(self: @Fixed) -> bool { + *self == FixedOne::one() + } + #[inline(always)] + fn is_non_one(self: @Fixed) -> bool { + !self.is_one() + } +} #[cfg(test)] mod tests {