Skip to content

Commit

Permalink
Added add and sub for i*.
Browse files Browse the repository at this point in the history
commit-id:a8262aa8
  • Loading branch information
orizi committed Jul 24, 2023
1 parent 7cd8370 commit c30ee11
Show file tree
Hide file tree
Showing 16 changed files with 1,490 additions and 28 deletions.
192 changes: 192 additions & 0 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,13 @@ impl U256Zeroable of Zeroable<u256> {
}
}

enum SignedIntegerResult<T> {
InRange: T,
Underflow: T,
Overflow: T,
}
impl SignedIntegerResultDrop<T, impl TDrop: Drop<T>> of Drop<SignedIntegerResult<T>>;

#[derive(Copy, Drop)]
extern type i8;
impl NumericLiterali8 of NumericLiteral<i8>;
Expand All @@ -1885,6 +1892,43 @@ impl I8PartialEq of PartialEq<i8> {
}
}

extern fn i8_overflowing_add_impl(
lhs: i8, rhs: i8
) -> SignedIntegerResult<i8> implicits(RangeCheck) nopanic;
extern fn i8_overflowing_sub_impl(
lhs: i8, rhs: i8
) -> SignedIntegerResult<i8> implicits(RangeCheck) nopanic;
impl I8Add of Add<i8> {
fn add(lhs: i8, rhs: i8) -> i8 {
match i8_overflowing_add_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i8_add Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i8_add Overflow'),
}
}
}
impl I8AddEq of AddEq<i8> {
#[inline(always)]
fn add_eq(ref self: i8, other: i8) {
self = Add::add(self, other);
}
}
impl I8Sub of Sub<i8> {
fn sub(lhs: i8, rhs: i8) -> i8 {
match i8_overflowing_sub_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i8_sub Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i8_sub Overflow'),
}
}
}
impl I8SubEq of SubEq<i8> {
#[inline(always)]
fn sub_eq(ref self: i8, other: i8) {
self = Sub::sub(self, other);
}
}

#[derive(Copy, Drop)]
extern type i16;
impl NumericLiterali16 of NumericLiteral<i16>;
Expand All @@ -1906,6 +1950,43 @@ impl I16PartialEq of PartialEq<i16> {
}
}

extern fn i16_overflowing_add_impl(
lhs: i16, rhs: i16
) -> SignedIntegerResult<i16> implicits(RangeCheck) nopanic;
extern fn i16_overflowing_sub_impl(
lhs: i16, rhs: i16
) -> SignedIntegerResult<i16> implicits(RangeCheck) nopanic;
impl I16Add of Add<i16> {
fn add(lhs: i16, rhs: i16) -> i16 {
match i16_overflowing_add_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i16_add Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i16_add Overflow'),
}
}
}
impl I16AddEq of AddEq<i16> {
#[inline(always)]
fn add_eq(ref self: i16, other: i16) {
self = Add::add(self, other);
}
}
impl I16Sub of Sub<i16> {
fn sub(lhs: i16, rhs: i16) -> i16 {
match i16_overflowing_sub_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i16_sub Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i16_sub Overflow'),
}
}
}
impl I16SubEq of SubEq<i16> {
#[inline(always)]
fn sub_eq(ref self: i16, other: i16) {
self = Sub::sub(self, other);
}
}

#[derive(Copy, Drop)]
extern type i32;
impl NumericLiterali32 of NumericLiteral<i32>;
Expand All @@ -1927,6 +2008,43 @@ impl I32PartialEq of PartialEq<i32> {
}
}

extern fn i32_overflowing_add_impl(
lhs: i32, rhs: i32
) -> SignedIntegerResult<i32> implicits(RangeCheck) nopanic;
extern fn i32_overflowing_sub_impl(
lhs: i32, rhs: i32
) -> SignedIntegerResult<i32> implicits(RangeCheck) nopanic;
impl I32Add of Add<i32> {
fn add(lhs: i32, rhs: i32) -> i32 {
match i32_overflowing_add_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i32_add Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i32_add Overflow'),
}
}
}
impl I32AddEq of AddEq<i32> {
#[inline(always)]
fn add_eq(ref self: i32, other: i32) {
self = Add::add(self, other);
}
}
impl I32Sub of Sub<i32> {
fn sub(lhs: i32, rhs: i32) -> i32 {
match i32_overflowing_sub_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i32_sub Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i32_sub Overflow'),
}
}
}
impl I32SubEq of SubEq<i32> {
#[inline(always)]
fn sub_eq(ref self: i32, other: i32) {
self = Sub::sub(self, other);
}
}

#[derive(Copy, Drop)]
extern type i64;
impl NumericLiterali64 of NumericLiteral<i64>;
Expand All @@ -1948,6 +2066,43 @@ impl I64PartialEq of PartialEq<i64> {
}
}

extern fn i64_overflowing_add_impl(
lhs: i64, rhs: i64
) -> SignedIntegerResult<i64> implicits(RangeCheck) nopanic;
extern fn i64_overflowing_sub_impl(
lhs: i64, rhs: i64
) -> SignedIntegerResult<i64> implicits(RangeCheck) nopanic;
impl I64Add of Add<i64> {
fn add(lhs: i64, rhs: i64) -> i64 {
match i64_overflowing_add_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i64_add Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i64_add Overflow'),
}
}
}
impl I64AddEq of AddEq<i64> {
#[inline(always)]
fn add_eq(ref self: i64, other: i64) {
self = Add::add(self, other);
}
}
impl I64Sub of Sub<i64> {
fn sub(lhs: i64, rhs: i64) -> i64 {
match i64_overflowing_sub_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i64_sub Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i64_sub Overflow'),
}
}
}
impl I64SubEq of SubEq<i64> {
#[inline(always)]
fn sub_eq(ref self: i64, other: i64) {
self = Sub::sub(self, other);
}
}

#[derive(Copy, Drop)]
extern type i128;
impl NumericLiterali128 of NumericLiteral<i128>;
Expand All @@ -1968,3 +2123,40 @@ impl I128PartialEq of PartialEq<i128> {
!(*lhs == *rhs)
}
}

extern fn i128_overflowing_add_impl(
lhs: i128, rhs: i128
) -> SignedIntegerResult<i128> implicits(RangeCheck) nopanic;
extern fn i128_overflowing_sub_impl(
lhs: i128, rhs: i128
) -> SignedIntegerResult<i128> implicits(RangeCheck) nopanic;
impl I128Add of Add<i128> {
fn add(lhs: i128, rhs: i128) -> i128 {
match i128_overflowing_add_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i128_add Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i128_add Overflow'),
}
}
}
impl I128AddEq of AddEq<i128> {
#[inline(always)]
fn add_eq(ref self: i128, other: i128) {
self = Add::add(self, other);
}
}
impl I128Sub of Sub<i128> {
fn sub(lhs: i128, rhs: i128) -> i128 {
match i128_overflowing_sub_impl(lhs, rhs) {
SignedIntegerResult::InRange(result) => result,
SignedIntegerResult::Underflow(_) => panic_with_felt252('i128_sub Underflow'),
SignedIntegerResult::Overflow(_) => panic_with_felt252('i128_sub Overflow'),
}
}
}
impl I128SubEq of SubEq<i128> {
#[inline(always)]
fn sub_eq(ref self: i128, other: i128) {
self = Sub::sub(self, other);
}
}
Loading

0 comments on commit c30ee11

Please sign in to comment.