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

feat: wrapping traits #5175

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3008,3 +3008,27 @@ impl U256OverflowingMul of core::num::traits::OverflowingMul<u256> {
u256_overflow_mul(self, v)
}
}

/// WrappingAdd implementations
impl TWrappingAdd<T, +core::num::traits::OverflowingAdd<T>> of core::num::traits::WrappingAdd<T> {
fn wrapping_add(self: T, v: T) -> T {
let (result, _) = self.overflowing_add(v);
result
}
}

/// WrappingSub implementations
impl TWrappingSub<T, +core::num::traits::OverflowingSub<T>> of core::num::traits::WrappingSub<T> {
fn wrapping_sub(self: T, v: T) -> T {
let (result, _) = self.overflowing_sub(v);
result
}
}

/// WrappingMul implementations
impl TWrappingMul<T, +core::num::traits::OverflowingMul<T>> of core::num::traits::WrappingMul<T> {
fn wrapping_mul(self: T, v: T) -> T {
let (result, _) = self.overflowing_mul(v);
result
}
}
3 changes: 2 additions & 1 deletion corelib/src/num/traits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pub mod bit_size;
pub use bit_size::BitSize;

pub mod ops;
pub use ops::{OverflowingAdd, OverflowingSub, OverflowingMul};
pub use ops::overflowing::{OverflowingAdd, OverflowingSub, OverflowingMul};
pub use ops::wrapping::{WrappingAdd, WrappingSub, WrappingMul};
2 changes: 1 addition & 1 deletion corelib/src/num/traits/ops.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod overflowing;
pub use overflowing::{OverflowingAdd, OverflowingSub, OverflowingMul};
pub mod wrapping;
20 changes: 20 additions & 0 deletions corelib/src/num/traits/ops/wrapping.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// Performs addition that wraps around on overflow.
pub trait WrappingAdd<T> {
/// Wrapping (modular) addition. Computes `self + other`, wrapping around at the boundary of the
/// type.
fn wrapping_add(self: T, v: T) -> T;
}

/// Performs subtraction that wraps around on overflow.
pub trait WrappingSub<T> {
/// Wrapping (modular) subtraction. Computes `self - other`, wrapping around at the boundary of
/// the type.
fn wrapping_sub(self: T, v: T) -> T;
}

/// Performs multiplication that wraps around on overflow.
pub trait WrappingMul<T> {
/// Wrapping (modular) multiplication. Computes `self * other`, wrapping around at the boundary
/// of the type.
fn wrapping_mul(self: T, v: T) -> T;
}
112 changes: 111 additions & 1 deletion corelib/src/test/num_test.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::num::traits::BitSize;
use core::num::traits::{OverflowingAdd, OverflowingSub, OverflowingMul};
use core::num::traits::{
OverflowingAdd, OverflowingSub, OverflowingMul, WrappingAdd, WrappingSub, WrappingMul
};
use core::integer::BoundedInt;


Expand All @@ -19,6 +21,8 @@ fn test_bit_size() {
assert!(BitSize::<bytes31>::bits() == 248);
}

// Overflowing tests

#[test]
fn tests_overflowing_add_unsigned_integers() {
assert_eq!(1_u8.overflowing_add(2), (3, false));
Expand Down Expand Up @@ -125,3 +129,109 @@ fn test_overflowing_mul_unsigned_integers() {
assert_eq!(2_u256.overflowing_mul(3), (6, false));
assert_eq!(BoundedInt::<u256>::max().overflowing_mul(2), (BoundedInt::<u256>::max() - 1, true));
}

// Wrapping tests

#[test]
fn tests_wrapping_add_unsigned_integers() {
assert_eq!(1_u8.wrapping_add(2), 3);
assert_eq!(BoundedInt::<u8>::max().wrapping_add(1), 0);
assert_eq!(1_u16.wrapping_add(2), 3);
assert_eq!(BoundedInt::<u16>::max().wrapping_add(1), 0);
assert_eq!(1_u32.wrapping_add(2), 3);
assert_eq!(BoundedInt::<u32>::max().wrapping_add(1), 0);
assert_eq!(1_u64.wrapping_add(2), 3);
assert_eq!(BoundedInt::<u64>::max().wrapping_add(1), 0);
assert_eq!(1_u128.wrapping_add(2), 3);
assert_eq!(BoundedInt::<u128>::max().wrapping_add(1), 0);
assert_eq!(1_u256.wrapping_add(2), 3);
assert_eq!(BoundedInt::<u256>::max().wrapping_add(1), 0);
}

#[test]
fn test_wrapping_add_positive_signed_integers() {
assert!(1_i8.wrapping_add(2) == 3);
assert!(BoundedInt::<i8>::max().wrapping_add(1) == -0x80);
assert!(1_i16.wrapping_add(2) == 3);
assert!(BoundedInt::<i16>::max().wrapping_add(1) == -0x8000);
assert!(1_i32.wrapping_add(2) == 3);
assert!(BoundedInt::<i32>::max().wrapping_add(1) == -0x80000000);
assert!(1_i64.wrapping_add(2) == 3);
assert!(BoundedInt::<i64>::max().wrapping_add(1) == -0x8000000000000000);
assert!(1_i128.wrapping_add(2) == 3);
assert!(BoundedInt::<i128>::max().wrapping_add(1) == -0x80000000000000000000000000000000);
}

#[test]
fn test_wrapping_add_negative_signed_integers() {
assert!((-1_i8).wrapping_add(-2) == -3);
assert!(BoundedInt::<i8>::min().wrapping_add(-1) == 0x7f);
assert!((-1_i16).wrapping_add(-2) == -3);
assert!(BoundedInt::<i16>::min().wrapping_add(-1) == 0x7fff);
assert!((-1_i32).wrapping_add(-2) == -3);
assert!(BoundedInt::<i32>::min().wrapping_add(-1) == 0x7fffffff);
assert!((-1_i64).wrapping_add(-2) == -3);
assert!(BoundedInt::<i64>::min().wrapping_add(-1) == 0x7fffffffffffffff);
assert!((-1_i128).wrapping_add(-2) == -3);
assert!(BoundedInt::<i128>::min().wrapping_add(-1) == 0x7fffffffffffffffffffffffffffffff);
}

#[test]
fn test_wrapping_sub_unsigned_integers() {
assert_eq!(3_u8.wrapping_sub(2), 1);
assert_eq!(0_u8.wrapping_sub(1), BoundedInt::<u8>::max());
assert_eq!(3_u16.wrapping_sub(2), 1);
assert_eq!(0_u16.wrapping_sub(1), BoundedInt::<u16>::max());
assert_eq!(3_u32.wrapping_sub(2), 1);
assert_eq!(0_u32.wrapping_sub(1), BoundedInt::<u32>::max());
assert_eq!(3_u64.wrapping_sub(2), 1);
assert_eq!(0_u64.wrapping_sub(1), BoundedInt::<u64>::max());
assert_eq!(3_u128.wrapping_sub(2), 1);
assert_eq!(0_u128.wrapping_sub(1), BoundedInt::<u128>::max());
assert_eq!(3_u256.wrapping_sub(2), 1);
assert_eq!(0_u256.wrapping_sub(1), BoundedInt::<u256>::max());
}

#[test]
fn test_wrapping_sub_positive_signed_integers() {
assert!(3_i8.wrapping_sub(2) == 1);
assert!(BoundedInt::<i8>::min().wrapping_sub(1) == BoundedInt::<i8>::max());
assert!(3_i16.wrapping_sub(2) == 1);
assert!(BoundedInt::<i16>::min().wrapping_sub(1) == BoundedInt::<i16>::max());
assert!(3_i32.wrapping_sub(2) == 1);
assert!(BoundedInt::<i32>::min().wrapping_sub(1) == BoundedInt::<i32>::max());
assert!(3_i64.wrapping_sub(2) == 1);
assert!(BoundedInt::<i64>::min().wrapping_sub(1) == BoundedInt::<i64>::max());
assert!(3_i128.wrapping_sub(2) == 1);
assert!(BoundedInt::<i128>::min().wrapping_sub(1) == BoundedInt::<i128>::max());
}

#[test]
fn test_wrapping_sub_negative_signed_integers() {
assert!((-3_i8).wrapping_sub(-2) == -1);
assert!(BoundedInt::<i8>::max().wrapping_sub(-1) == BoundedInt::<i8>::min());
assert!((-3_i16).wrapping_sub(-2) == -1);
assert!(BoundedInt::<i16>::max().wrapping_sub(-1) == BoundedInt::<i16>::min());
assert!((-3_i32).wrapping_sub(-2) == -1);
assert!(BoundedInt::<i32>::max().wrapping_sub(-1) == BoundedInt::<i32>::min());
assert!((-3_i64).wrapping_sub(-2) == -1);
assert!(BoundedInt::<i64>::max().wrapping_sub(-1) == BoundedInt::<i64>::min());
assert!((-3_i128).wrapping_sub(-2) == -1);
assert!(BoundedInt::<i128>::max().wrapping_sub(-1) == BoundedInt::<i128>::min());
}

#[test]
fn test_wrapping_mul_unsigned_integers() {
assert_eq!(2_u8.wrapping_mul(3), 6);
assert_eq!(BoundedInt::<u8>::max().wrapping_mul(2), BoundedInt::<u8>::max() - 1);
assert_eq!(2_u16.wrapping_mul(3), 6);
assert_eq!(BoundedInt::<u16>::max().wrapping_mul(2), BoundedInt::<u16>::max() - 1);
assert_eq!(2_u32.wrapping_mul(3), 6);
assert_eq!(BoundedInt::<u32>::max().wrapping_mul(2), BoundedInt::<u32>::max() - 1);
assert_eq!(2_u64.wrapping_mul(3), 6);
assert_eq!(BoundedInt::<u64>::max().wrapping_mul(2), BoundedInt::<u64>::max() - 1);
assert_eq!(2_u128.wrapping_mul(3), 6);
assert_eq!(BoundedInt::<u128>::max().wrapping_mul(2), BoundedInt::<u128>::max() - 1);
assert_eq!(2_u256.wrapping_mul(3), 6);
assert_eq!(BoundedInt::<u256>::max().wrapping_mul(2), BoundedInt::<u256>::max() - 1);
}
Loading