From da791a8bb72e22677c6d95a8958d5e6ee34d1592 Mon Sep 17 00:00:00 2001 From: Ori Ziv Date: Wed, 21 Jun 2023 20:05:06 +0300 Subject: [PATCH] Added i*_mul op. commit-id:7115f73c --- corelib/src/integer.cairo | 52 ++++++++++++++ corelib/src/test/integer_test.cairo | 104 ++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) diff --git a/corelib/src/integer.cairo b/corelib/src/integer.cairo index 5afb8360c24..c0a019c4045 100644 --- a/corelib/src/integer.cairo +++ b/corelib/src/integer.cairo @@ -1929,6 +1929,19 @@ impl I8SubEq of SubEq { } } +extern fn i8_wide_mul(lhs: i8, rhs: i8) -> i16 implicits() nopanic; +impl I8Mul of Mul { + 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 { + #[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; @@ -1987,6 +2000,19 @@ impl I16SubEq of SubEq { } } +extern fn i16_wide_mul(lhs: i16, rhs: i16) -> i32 implicits() nopanic; +impl I16Mul of Mul { + 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 { + #[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; @@ -2045,6 +2071,19 @@ impl I32SubEq of SubEq { } } +extern fn i32_wide_mul(lhs: i32, rhs: i32) -> i64 implicits() nopanic; +impl I32Mul of Mul { + 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 { + #[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; @@ -2103,6 +2142,19 @@ impl I64SubEq of SubEq { } } +extern fn i64_wide_mul(lhs: i64, rhs: i64) -> i128 implicits() nopanic; +impl I64Mul of Mul { + 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 { + #[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; diff --git a/corelib/src/test/integer_test.cairo b/corelib/src/test/integer_test.cairo index 2c29999351e..9fa1fd167a1 100644 --- a/corelib/src/test/integer_test.cairo +++ b/corelib/src/test/integer_test.cairo @@ -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] @@ -1188,6 +1196,24 @@ fn test_i8_add_underflow() { -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'); @@ -1206,6 +1232,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] @@ -1256,6 +1290,24 @@ fn test_i16_add_underflow() { -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'); @@ -1274,6 +1326,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] @@ -1324,6 +1384,24 @@ fn test_i32_add_underflow() { -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() { assert_eq(@1_i64, @1_i64, '1 == 1'); @@ -1350,6 +1428,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] @@ -1400,6 +1486,24 @@ fn test_i64_add_underflow() { -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() { assert_eq(@1_i128, @1_i128, '1 == 1');