Skip to content

Commit 8d98a4c

Browse files
committed
Added cast for signed integers.
commit-id:c56d4b74
1 parent 35333c6 commit 8d98a4c

File tree

6 files changed

+520
-78
lines changed

6 files changed

+520
-78
lines changed

corelib/src/integer.cairo

+133-7
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,61 @@ impl BoundedU256 of BoundedInt<u256> {
14321432
}
14331433
}
14341434

1435+
impl BoundedI8 of BoundedInt<i8> {
1436+
#[inline(always)]
1437+
fn min() -> i8 nopanic {
1438+
-0x80
1439+
}
1440+
#[inline(always)]
1441+
fn max() -> i8 nopanic {
1442+
0x7f
1443+
}
1444+
}
1445+
1446+
impl BoundedI16 of BoundedInt<i16> {
1447+
#[inline(always)]
1448+
fn min() -> i16 nopanic {
1449+
-0x8000
1450+
}
1451+
#[inline(always)]
1452+
fn max() -> i16 nopanic {
1453+
0x7fff
1454+
}
1455+
}
1456+
1457+
impl BoundedI32 of BoundedInt<i32> {
1458+
#[inline(always)]
1459+
fn min() -> i32 nopanic {
1460+
-0x80000000
1461+
}
1462+
#[inline(always)]
1463+
fn max() -> i32 nopanic {
1464+
0x7fffffff
1465+
}
1466+
}
1467+
1468+
impl BoundedI64 of BoundedInt<i64> {
1469+
#[inline(always)]
1470+
fn min() -> i64 nopanic {
1471+
-0x8000000000000000
1472+
}
1473+
#[inline(always)]
1474+
fn max() -> i64 nopanic {
1475+
0x7fffffffffffffff
1476+
}
1477+
}
1478+
1479+
impl BoundedI128 of BoundedInt<i128> {
1480+
#[inline(always)]
1481+
fn min() -> i128 nopanic {
1482+
-0x80000000000000000000000000000000
1483+
}
1484+
#[inline(always)]
1485+
fn max() -> i128 nopanic {
1486+
0x7fffffffffffffffffffffffffffffff
1487+
}
1488+
}
1489+
14351490
/// Conversions.
14361491
impl Felt252TryIntoU8 of TryInto<felt252, u8> {
14371492
fn try_into(self: felt252) -> Option<u8> {
@@ -1568,31 +1623,102 @@ extern fn downcast<FromType, ToType>(x: FromType) -> Option<ToType> implicits(Ra
15681623
// Do not add user code implementing this trait.
15691624
trait Upcastable<FromType, ToType>;
15701625
impl UpcastableU8U16 of Upcastable<u8, u16> {}
1626+
impl UpcastableU8I16 of Upcastable<u8, i16> {}
15711627
impl UpcastableU8U32 of Upcastable<u8, u32> {}
1628+
impl UpcastableU8I32 of Upcastable<u8, i32> {}
15721629
impl UpcastableU8U64 of Upcastable<u8, u64> {}
1630+
impl UpcastableU8I64 of Upcastable<u8, i64> {}
15731631
impl UpcastableU8U128 of Upcastable<u8, u128> {}
1632+
impl UpcastableU8I128 of Upcastable<u8, i128> {}
1633+
impl UpcastableI8I16 of Upcastable<i8, i16> {}
1634+
impl UpcastableI8I32 of Upcastable<i8, i32> {}
1635+
impl UpcastableI8I64 of Upcastable<i8, i64> {}
1636+
impl UpcastableI8I128 of Upcastable<i8, i128> {}
15741637
impl UpcastableU16U32 of Upcastable<u16, u32> {}
1638+
impl UpcastableU16I32 of Upcastable<u16, i32> {}
15751639
impl UpcastableU16U64 of Upcastable<u16, u64> {}
1640+
impl UpcastableU16I64 of Upcastable<u16, i64> {}
15761641
impl UpcastableU16U128 of Upcastable<u16, u128> {}
1642+
impl UpcastableU16I128 of Upcastable<u16, i128> {}
1643+
impl UpcastableI16I32 of Upcastable<i16, i32> {}
1644+
impl UpcastableI16I64 of Upcastable<i16, i64> {}
1645+
impl UpcastableI16I128 of Upcastable<i16, i128> {}
15771646
impl UpcastableU32U64 of Upcastable<u32, u64> {}
1647+
impl UpcastableU32I64 of Upcastable<u32, i64> {}
15781648
impl UpcastableU32U128 of Upcastable<u32, u128> {}
1649+
impl UpcastableU32I128 of Upcastable<u32, i128> {}
1650+
impl UpcastableI32I64 of Upcastable<i32, i64> {}
1651+
impl UpcastableI32I128 of Upcastable<i32, i128> {}
15791652
impl UpcastableU64U128 of Upcastable<u64, u128> {}
1653+
impl UpcastableU64I128 of Upcastable<u64, i128> {}
1654+
impl UpcastableI64I128 of Upcastable<i64, i128> {}
15801655
// Marks `FromType` as downcastable to `ToType`.
15811656
// Do not add user code implementing this trait.
15821657
trait Downcastable<FromType, ToType>;
1658+
impl DowncastableU128I128 of Downcastable<u128, i128> {}
15831659
impl DowncastableU128U64 of Downcastable<u128, u64> {}
1660+
impl DowncastableU128I64 of Downcastable<u128, i64> {}
15841661
impl DowncastableU128U32 of Downcastable<u128, u32> {}
1662+
impl DowncastableU128I32 of Downcastable<u128, i32> {}
15851663
impl DowncastableU128U16 of Downcastable<u128, u16> {}
1664+
impl DowncastableU128I16 of Downcastable<u128, i16> {}
15861665
impl DowncastableU128U8 of Downcastable<u128, u8> {}
1587-
1666+
impl DowncastableU128I8 of Downcastable<u128, i8> {}
1667+
impl DowncastableI128U128 of Downcastable<i128, u128> {}
1668+
impl DowncastableI128U64 of Downcastable<i128, u64> {}
1669+
impl DowncastableI128I64 of Downcastable<i128, i64> {}
1670+
impl DowncastableI128U32 of Downcastable<i128, u32> {}
1671+
impl DowncastableI128I32 of Downcastable<i128, i32> {}
1672+
impl DowncastableI128U16 of Downcastable<i128, u16> {}
1673+
impl DowncastableI128I16 of Downcastable<i128, i16> {}
1674+
impl DowncastableI128U8 of Downcastable<i128, u8> {}
1675+
impl DowncastableI128I8 of Downcastable<i128, i8> {}
1676+
1677+
impl DowncastableU64I64 of Downcastable<u64, i64> {}
15881678
impl DowncastableU64U32 of Downcastable<u64, u32> {}
1679+
impl DowncastableU64I32 of Downcastable<u64, i32> {}
15891680
impl DowncastableU64U16 of Downcastable<u64, u16> {}
1681+
impl DowncastableU64I16 of Downcastable<u64, i16> {}
15901682
impl DowncastableU64U8 of Downcastable<u64, u8> {}
1591-
1683+
impl DowncastableU64I8 of Downcastable<u64, i8> {}
1684+
impl DowncastableI64U128 of Downcastable<i64, u128> {}
1685+
impl DowncastableI64U64 of Downcastable<i64, u64> {}
1686+
impl DowncastableI64U32 of Downcastable<i64, u32> {}
1687+
impl DowncastableI64I32 of Downcastable<i64, i32> {}
1688+
impl DowncastableI64U16 of Downcastable<i64, u16> {}
1689+
impl DowncastableI64I16 of Downcastable<i64, i16> {}
1690+
impl DowncastableI64U8 of Downcastable<i64, u8> {}
1691+
impl DowncastableI64I8 of Downcastable<i64, i8> {}
1692+
1693+
impl DowncastableU32I32 of Downcastable<u32, i32> {}
15921694
impl DowncastableU32U16 of Downcastable<u32, u16> {}
1695+
impl DowncastableU32I16 of Downcastable<u32, i16> {}
15931696
impl DowncastableU32U8 of Downcastable<u32, u8> {}
1594-
1697+
impl DowncastableU32I8 of Downcastable<u32, i8> {}
1698+
impl DowncastableI32U128 of Downcastable<i32, u128> {}
1699+
impl DowncastableI32U64 of Downcastable<i32, u64> {}
1700+
impl DowncastableI32U32 of Downcastable<i32, u32> {}
1701+
impl DowncastableI32U16 of Downcastable<i32, u16> {}
1702+
impl DowncastableI32I16 of Downcastable<i32, i16> {}
1703+
impl DowncastableI32U8 of Downcastable<i32, u8> {}
1704+
impl DowncastableI32I8 of Downcastable<i32, i8> {}
1705+
1706+
impl DowncastableU16I16 of Downcastable<u16, i16> {}
15951707
impl DowncastableU16U8 of Downcastable<u16, u8> {}
1708+
impl DowncastableU16I8 of Downcastable<u16, i8> {}
1709+
impl DowncastableI16U128 of Downcastable<i16, u128> {}
1710+
impl DowncastableI16U64 of Downcastable<i16, u64> {}
1711+
impl DowncastableI16U32 of Downcastable<i16, u32> {}
1712+
impl DowncastableI16U16 of Downcastable<i16, u16> {}
1713+
impl DowncastableI16U8 of Downcastable<i16, u8> {}
1714+
impl DowncastableI16I8 of Downcastable<i16, i8> {}
1715+
1716+
impl DowncastableU8I8 of Downcastable<u8, i8> {}
1717+
impl DowncastableI8U128 of Downcastable<i8, u128> {}
1718+
impl DowncastableI8U64 of Downcastable<i8, u64> {}
1719+
impl DowncastableI8U32 of Downcastable<i8, u32> {}
1720+
impl DowncastableI8U16 of Downcastable<i8, u16> {}
1721+
impl DowncastableI8U8 of Downcastable<i8, u8> {}
15961722

15971723
/// Default values
15981724
impl U8Default of Default<u8> {
@@ -1932,7 +2058,7 @@ impl I8SubEq of SubEq<i8> {
19322058
extern fn i8_wide_mul(lhs: i8, rhs: i8) -> i16 implicits() nopanic;
19332059
impl I8Mul of Mul<i8> {
19342060
fn mul(lhs: i8, rhs: i8) -> i8 {
1935-
i8_try_from_felt252(i16_to_felt252(i8_wide_mul(lhs, rhs))).expect('i8_mul Overflow')
2061+
i8_wide_mul(lhs, rhs).try_into().expect('i8_mul Overflow')
19362062
}
19372063
}
19382064
impl I8MulEq of MulEq<i8> {
@@ -2023,7 +2149,7 @@ impl I16SubEq of SubEq<i16> {
20232149
extern fn i16_wide_mul(lhs: i16, rhs: i16) -> i32 implicits() nopanic;
20242150
impl I16Mul of Mul<i16> {
20252151
fn mul(lhs: i16, rhs: i16) -> i16 {
2026-
i16_try_from_felt252(i32_to_felt252(i16_wide_mul(lhs, rhs))).expect('i16_mul Overflow')
2152+
i16_wide_mul(lhs, rhs).try_into().expect('i16_mul Overflow')
20272153
}
20282154
}
20292155
impl I16MulEq of MulEq<i16> {
@@ -2114,7 +2240,7 @@ impl I32SubEq of SubEq<i32> {
21142240
extern fn i32_wide_mul(lhs: i32, rhs: i32) -> i64 implicits() nopanic;
21152241
impl I32Mul of Mul<i32> {
21162242
fn mul(lhs: i32, rhs: i32) -> i32 {
2117-
i32_try_from_felt252(i64_to_felt252(i32_wide_mul(lhs, rhs))).expect('i32_mul Overflow')
2243+
i32_wide_mul(lhs, rhs).try_into().expect('i32_mul Overflow')
21182244
}
21192245
}
21202246
impl I32MulEq of MulEq<i32> {
@@ -2205,7 +2331,7 @@ impl I64SubEq of SubEq<i64> {
22052331
extern fn i64_wide_mul(lhs: i64, rhs: i64) -> i128 implicits() nopanic;
22062332
impl I64Mul of Mul<i64> {
22072333
fn mul(lhs: i64, rhs: i64) -> i64 {
2208-
i64_try_from_felt252(i128_to_felt252(i64_wide_mul(lhs, rhs))).expect('i64_mul Overflow')
2334+
i64_wide_mul(lhs, rhs).try_into().expect('i64_mul Overflow')
22092335
}
22102336
}
22112337
impl I64MulEq of MulEq<i64> {

0 commit comments

Comments
 (0)