Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Bounded numeric trait. #6063

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions corelib/src/num/traits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub use one::One;
pub mod bit_size;
pub use bit_size::BitSize;

mod bounded;
pub use bounded::Bounded;

#[feature("corelib-internal-use")]
pub mod ops;
pub use ops::overflowing::{OverflowingAdd, OverflowingSub, OverflowingMul};
Expand Down
69 changes: 69 additions & 0 deletions corelib/src/num/traits/bounded.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/// A trait defining minimum and maximum bounds for numeric types.
/// Only supports types that can have a constant value.
///
/// Example:
/// ```
/// Bounded::<MyType>::MAX;
/// ```
pub trait Bounded<T> {
/// The minimum allowable value.
const MIN: T;
/// The maximum allowable value.
const MAX: T;
}

impl BoundedU8 of Bounded<u8> {
const MIN: u8 = 0x0;
const MAX: u8 = 0xff;
}

impl BoundedU16 of Bounded<u16> {
const MIN: u16 = 0x0;
const MAX: u16 = 0xffff;
}

impl BoundedU32 of Bounded<u32> {
const MIN: u32 = 0x0;
const MAX: u32 = 0xffffffff;
}

impl BoundedU64 of Bounded<u64> {
const MIN: u64 = 0x0;
const MAX: u64 = 0xffffffffffffffff;
}

impl BoundedU128 of Bounded<u128> {
const MIN: u128 = 0x0;
const MAX: u128 = 0xffffffffffffffffffffffffffffffff;
}

impl BoundedU256 of Bounded<u256> {
const MIN: u256 = 0x0;
const MAX: u256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
}

// Implementations for signed integer types
impl BoundedI8 of Bounded<i8> {
const MIN: i8 = -0x80;
const MAX: i8 = 0x7f;
}

impl BoundedI16 of Bounded<i16> {
const MIN: i16 = -0x8000;
const MAX: i16 = 0x7fff;
}

impl BoundedI32 of Bounded<i32> {
const MIN: i32 = -0x80000000;
const MAX: i32 = 0x7fffffff;
}

impl BoundedI64 of Bounded<i64> {
const MIN: i64 = -0x8000000000000000;
const MAX: i64 = 0x7fffffffffffffff;
}

impl BoundedI128 of Bounded<i128> {
const MIN: i128 = -0x80000000000000000000000000000000;
const MAX: i128 = 0x7fffffffffffffffffffffffffffffff;
}
12 changes: 6 additions & 6 deletions corelib/src/num/traits/ops/saturating.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,36 @@ pub trait SaturatingMul<T> {

pub(crate) mod overflow_based {
pub(crate) impl TSaturatingAdd<
T, +Drop<T>, +core::num::traits::OverflowingAdd<T>, +core::integer::BoundedInt<T>
T, +Drop<T>, +core::num::traits::OverflowingAdd<T>, +core::num::traits::Bounded<T>
> of core::num::traits::SaturatingAdd<T> {
fn saturating_add(self: T, other: T) -> T {
let (result, overflow) = self.overflowing_add(other);
match overflow {
true => core::integer::BoundedInt::max(),
true => core::num::traits::Bounded::MAX,
false => result,
}
}
}

pub(crate) impl TSaturatingSub<
T, +Drop<T>, +core::num::traits::OverflowingSub<T>, +core::integer::BoundedInt<T>
T, +Drop<T>, +core::num::traits::OverflowingSub<T>, +core::num::traits::Bounded<T>
> of core::num::traits::SaturatingSub<T> {
fn saturating_sub(self: T, other: T) -> T {
let (result, overflow) = self.overflowing_sub(other);
match overflow {
true => core::integer::BoundedInt::min(),
true => core::num::traits::Bounded::MIN,
false => result,
}
}
}

pub(crate) impl TSaturatingMul<
T, +Drop<T>, +core::num::traits::OverflowingMul<T>, +core::integer::BoundedInt<T>
T, +Drop<T>, +core::num::traits::OverflowingMul<T>, +core::num::traits::Bounded<T>
> of core::num::traits::SaturatingMul<T> {
fn saturating_mul(self: T, other: T) -> T {
let (result, overflow) = self.overflowing_mul(other);
match overflow {
true => core::integer::BoundedInt::max(),
true => core::num::traits::Bounded::MAX,
false => result,
}
}
Expand Down
Loading