Skip to content

Commit

Permalink
Added i*_mul op.
Browse files Browse the repository at this point in the history
commit-id:7115f73c
  • Loading branch information
orizi committed Jul 24, 2023
1 parent ea5a9fb commit b2a9aef
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
52 changes: 52 additions & 0 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,19 @@ impl I8SubEq of SubEq<i8> {
}
}

extern fn i8_wide_mul(lhs: i8, rhs: i8) -> i16 implicits() nopanic;
impl I8Mul of Mul<i8> {
fn mul(lhs: i8, rhs: i8) -> i8 {
i8_try_from_felt252(i16_to_felt252(i8_wide_mul(lhs, rhs))).expect('i8_mul Overflow')
}
}
impl I8MulEq of MulEq<i8> {
#[inline(always)]
fn mul_eq(ref self: i8, other: i8) {
self = Mul::mul(self, other);
}
}

#[derive(Copy, Drop)]
extern type i16;
impl NumericLiterali16 of NumericLiteral<i16>;
Expand Down Expand Up @@ -1987,6 +2000,19 @@ impl I16SubEq of SubEq<i16> {
}
}

extern fn i16_wide_mul(lhs: i16, rhs: i16) -> i32 implicits() nopanic;
impl I16Mul of Mul<i16> {
fn mul(lhs: i16, rhs: i16) -> i16 {
i16_try_from_felt252(i32_to_felt252(i16_wide_mul(lhs, rhs))).expect('i16_mul Overflow')
}
}
impl I16MulEq of MulEq<i16> {
#[inline(always)]
fn mul_eq(ref self: i16, other: i16) {
self = Mul::mul(self, other);
}
}

#[derive(Copy, Drop)]
extern type i32;
impl NumericLiterali32 of NumericLiteral<i32>;
Expand Down Expand Up @@ -2045,6 +2071,19 @@ impl I32SubEq of SubEq<i32> {
}
}

extern fn i32_wide_mul(lhs: i32, rhs: i32) -> i64 implicits() nopanic;
impl I32Mul of Mul<i32> {
fn mul(lhs: i32, rhs: i32) -> i32 {
i32_try_from_felt252(i64_to_felt252(i32_wide_mul(lhs, rhs))).expect('i32_mul Overflow')
}
}
impl I32MulEq of MulEq<i32> {
#[inline(always)]
fn mul_eq(ref self: i32, other: i32) {
self = Mul::mul(self, other);
}
}

#[derive(Copy, Drop)]
extern type i64;
impl NumericLiterali64 of NumericLiteral<i64>;
Expand Down Expand Up @@ -2103,6 +2142,19 @@ impl I64SubEq of SubEq<i64> {
}
}

extern fn i64_wide_mul(lhs: i64, rhs: i64) -> i128 implicits() nopanic;
impl I64Mul of Mul<i64> {
fn mul(lhs: i64, rhs: i64) -> i64 {
i64_try_from_felt252(i128_to_felt252(i64_wide_mul(lhs, rhs))).expect('i64_mul Overflow')
}
}
impl I64MulEq of MulEq<i64> {
#[inline(always)]
fn mul_eq(ref self: i64, other: i64) {
self = Mul::mul(self, other);
}
}

#[derive(Copy, Drop)]
extern type i128;
impl NumericLiterali128 of NumericLiteral<i128>;
Expand Down
102 changes: 102 additions & 0 deletions corelib/src/test/integer_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,14 @@ fn test_i8_operators() {
assert_eq(@(-3_i8 + -6_i8), @-9_i8, '-3 + -6 == -9');
assert_eq(@(-3_i8 - -1_i8), @-2_i8, '-3 - -1 == -2');
assert_eq(@(-121_i8 - -21_i8), @-100_i8, '-121--21=-100');
assert_eq(@(1_i8 * 3_i8), @3_i8, '1 * 3 == 3');
assert_eq(@(2_i8 * 4_i8), @8_i8, '2 * 4 == 8');
assert_eq(@(-1_i8 * 3_i8), @-3_i8, '-1 * 3 == 3');
assert_eq(@(-2_i8 * 4_i8), @-8_i8, '-2 * 4 == 8');
assert_eq(@(1_i8 * -3_i8), @-3_i8, '1 * -3 == -3');
assert_eq(@(2_i8 * -4_i8), @-8_i8, '2 * -4 == -8');
assert_eq(@(-1_i8 * -3_i8), @3_i8, '-1 * -3 == 3');
assert_eq(@(-2_i8 * -4_i8), @8_i8, '-2 * -4 == 8');
}

#[test]
Expand Down Expand Up @@ -1176,6 +1184,24 @@ fn test_i8_add_overflow_2() {
0x64_i8 + 0x1e_i8;
}

#[test]
#[should_panic]
fn test_i8_mul_overflow_1() {
0x10_i8 * 0x10_i8;
}

#[test]
#[should_panic]
fn test_i8_mul_overflow_2() {
0x11_i8 * 0x10_i8;
}

#[test]
#[should_panic]
fn test_i8_mul_overflow_3() {
2_i8 * 0x40_i8;
}

#[test]
fn test_i16_operators() {
assert_eq(@1_i16, @1_i16, '1 == 1');
Expand All @@ -1194,6 +1220,14 @@ fn test_i16_operators() {
assert_eq(@(-3_i16 + -6_i16), @-9_i16, '-3 + -6 == -9');
assert_eq(@(-3_i16 - -1_i16), @-2_i16, '-3 - -1 == -2');
assert_eq(@(-231_i16 - -131_i16), @-100_i16, '-231--131=-100');
assert_eq(@(1_i16 * 3_i16), @3_i16, '1 * 3 == 3');
assert_eq(@(2_i16 * 4_i16), @8_i16, '2 * 4 == 8');
assert_eq(@(-1_i16 * 3_i16), @-3_i16, '-1 * 3 == 3');
assert_eq(@(-2_i16 * 4_i16), @-8_i16, '-2 * 4 == 8');
assert_eq(@(1_i16 * -3_i16), @-3_i16, '1 * -3 == -3');
assert_eq(@(2_i16 * -4_i16), @-8_i16, '2 * -4 == -8');
assert_eq(@(-1_i16 * -3_i16), @3_i16, '-1 * -3 == 3');
assert_eq(@(-2_i16 * -4_i16), @8_i16, '-2 * -4 == 8');
}

#[test]
Expand Down Expand Up @@ -1232,6 +1266,24 @@ fn test_i16_add_overflow_2() {
0x6400_i16 + 0x1e00_i16;
}

#[test]
#[should_panic]
fn test_i16_mul_overflow_1() {
0x1000_i16 * 0x1000_i16;
}

#[test]
#[should_panic]
fn test_i16_mul_overflow_2() {
0x1100_i16 * 0x1000_i16;
}

#[test]
#[should_panic]
fn test_i16_mul_overflow_3() {
2_i16 * 0x4000_i16;
}

#[test]
fn test_i32_operators() {
assert_eq(@1_i32, @1_i32, '1 == 1');
Expand All @@ -1250,6 +1302,14 @@ fn test_i32_operators() {
assert_eq(@(-3_i32 + -6_i32), @-9_i32, '-3 + -6 == -9');
assert_eq(@(-3_i32 - -1_i32), @-2_i32, '-3 - -1 == -2');
assert_eq(@(-231_i32 - -131_i32), @-100_i32, '-231--131=-100');
assert_eq(@(1_i32 * 3_i32), @3_i32, '1 * 3 == 3');
assert_eq(@(2_i32 * 4_i32), @8_i32, '2 * 4 == 8');
assert_eq(@(-1_i32 * 3_i32), @-3_i32, '-1 * 3 == 3');
assert_eq(@(-2_i32 * 4_i32), @-8_i32, '-2 * 4 == 8');
assert_eq(@(1_i32 * -3_i32), @-3_i32, '1 * -3 == -3');
assert_eq(@(2_i32 * -4_i32), @-8_i32, '2 * -4 == -8');
assert_eq(@(-1_i32 * -3_i32), @3_i32, '-1 * -3 == 3');
assert_eq(@(-2_i32 * -4_i32), @8_i32, '-2 * -4 == 8');
}

#[test]
Expand Down Expand Up @@ -1288,6 +1348,23 @@ fn test_i32_add_overflow_2() {
0x64000000_i32 + 0x1e000000_i32;
}

#[test]
#[should_panic]
fn test_i32_mul_overflow_1() {
0x10000000_i32 * 0x10000000_i32;
}

#[test]
#[should_panic]
fn test_i32_mul_overflow_2() {
0x11000000_i32 * 0x10000000_i32;
}

#[test]
#[should_panic]
fn test_i32_mul_overflow_3() {
2_i32 * 0x40000000_i32;
}

#[test]
fn test_i64_operators() {
Expand Down Expand Up @@ -1315,6 +1392,14 @@ fn test_i64_operators() {
assert_eq(@(-3_i64 + -6_i64), @-9_i64, '-3 + -6 == -9');
assert_eq(@(-3_i64 - -1_i64), @-2_i64, '-3 - -1 == -2');
assert_eq(@(-231_i64 - -131_i64), @-100_i64, '-231--131=-100');
assert_eq(@(1_i64 * 3_i64), @3_i64, '1 * 3 == 3');
assert_eq(@(2_i64 * 4_i64), @8_i64, '2 * 4 == 8');
assert_eq(@(-1_i64 * 3_i64), @-3_i64, '-1 * 3 == 3');
assert_eq(@(-2_i64 * 4_i64), @-8_i64, '-2 * 4 == 8');
assert_eq(@(1_i64 * -3_i64), @-3_i64, '1 * -3 == -3');
assert_eq(@(2_i64 * -4_i64), @-8_i64, '2 * -4 == -8');
assert_eq(@(-1_i64 * -3_i64), @3_i64, '-1 * -3 == 3');
assert_eq(@(-2_i64 * -4_i64), @8_i64, '-2 * -4 == 8');
}

#[test]
Expand Down Expand Up @@ -1353,6 +1438,23 @@ fn test_i64_add_overflow_2() {
0x6400000000000000_i64 + 0x1e00000000000000_i64;
}

#[test]
#[should_panic]
fn test_i64_mul_overflow_1() {
0x1000000000000000_i64 * 0x1000000000000000_i64;
}

#[test]
#[should_panic]
fn test_i64_mul_overflow_2() {
0x1100000000000000_i64 * 0x1000000000000000_i64;
}

#[test]
#[should_panic]
fn test_i64_mul_overflow_3() {
2_i64 * 0x4000000000000000_i64;
}

#[test]
fn test_i128_operators() {
Expand Down

0 comments on commit b2a9aef

Please sign in to comment.