Skip to content

Commit

Permalink
Added cast for signed integers.
Browse files Browse the repository at this point in the history
commit-id:c56d4b74
  • Loading branch information
orizi committed Jul 9, 2023
1 parent 5601cda commit 59681b0
Show file tree
Hide file tree
Showing 6 changed files with 520 additions and 78 deletions.
140 changes: 133 additions & 7 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,61 @@ impl BoundedU256 of BoundedInt<u256> {
}
}

impl BoundedI8 of BoundedInt<i8> {
#[inline(always)]
fn min() -> i8 nopanic {
-0x80
}
#[inline(always)]
fn max() -> i8 nopanic {
0x7f
}
}

impl BoundedI16 of BoundedInt<i16> {
#[inline(always)]
fn min() -> i16 nopanic {
-0x8000
}
#[inline(always)]
fn max() -> i16 nopanic {
0x7fff
}
}

impl BoundedI32 of BoundedInt<i32> {
#[inline(always)]
fn min() -> i32 nopanic {
-0x80000000
}
#[inline(always)]
fn max() -> i32 nopanic {
0x7fffffff
}
}

impl BoundedI64 of BoundedInt<i64> {
#[inline(always)]
fn min() -> i64 nopanic {
-0x8000000000000000
}
#[inline(always)]
fn max() -> i64 nopanic {
0x7fffffffffffffff
}
}

impl BoundedI128 of BoundedInt<i128> {
#[inline(always)]
fn min() -> i128 nopanic {
-0x80000000000000000000000000000000
}
#[inline(always)]
fn max() -> i128 nopanic {
0x7fffffffffffffffffffffffffffffff
}
}

/// Conversions.
impl Felt252TryIntoU8 of TryInto<felt252, u8> {
fn try_into(self: felt252) -> Option<u8> {
Expand Down Expand Up @@ -1568,31 +1623,102 @@ extern fn downcast<FromType, ToType>(x: FromType) -> Option<ToType> implicits(Ra
// Do not add user code implementing this trait.
trait Upcastable<FromType, ToType>;
impl UpcastableU8U16 of Upcastable<u8, u16> {}
impl UpcastableU8I16 of Upcastable<u8, i16> {}
impl UpcastableU8U32 of Upcastable<u8, u32> {}
impl UpcastableU8I32 of Upcastable<u8, i32> {}
impl UpcastableU8U64 of Upcastable<u8, u64> {}
impl UpcastableU8I64 of Upcastable<u8, i64> {}
impl UpcastableU8U128 of Upcastable<u8, u128> {}
impl UpcastableU8I128 of Upcastable<u8, i128> {}
impl UpcastableI8I16 of Upcastable<i8, i16> {}
impl UpcastableI8I32 of Upcastable<i8, i32> {}
impl UpcastableI8I64 of Upcastable<i8, i64> {}
impl UpcastableI8I128 of Upcastable<i8, i128> {}
impl UpcastableU16U32 of Upcastable<u16, u32> {}
impl UpcastableU16I32 of Upcastable<u16, i32> {}
impl UpcastableU16U64 of Upcastable<u16, u64> {}
impl UpcastableU16I64 of Upcastable<u16, i64> {}
impl UpcastableU16U128 of Upcastable<u16, u128> {}
impl UpcastableU16I128 of Upcastable<u16, i128> {}
impl UpcastableI16I32 of Upcastable<i16, i32> {}
impl UpcastableI16I64 of Upcastable<i16, i64> {}
impl UpcastableI16I128 of Upcastable<i16, i128> {}
impl UpcastableU32U64 of Upcastable<u32, u64> {}
impl UpcastableU32I64 of Upcastable<u32, i64> {}
impl UpcastableU32U128 of Upcastable<u32, u128> {}
impl UpcastableU32I128 of Upcastable<u32, i128> {}
impl UpcastableI32I64 of Upcastable<i32, i64> {}
impl UpcastableI32I128 of Upcastable<i32, i128> {}
impl UpcastableU64U128 of Upcastable<u64, u128> {}
impl UpcastableU64I128 of Upcastable<u64, i128> {}
impl UpcastableI64I128 of Upcastable<i64, i128> {}
// Marks `FromType` as downcastable to `ToType`.
// Do not add user code implementing this trait.
trait Downcastable<FromType, ToType>;
impl DowncastableU128I128 of Downcastable<u128, i128> {}
impl DowncastableU128U64 of Downcastable<u128, u64> {}
impl DowncastableU128I64 of Downcastable<u128, i64> {}
impl DowncastableU128U32 of Downcastable<u128, u32> {}
impl DowncastableU128I32 of Downcastable<u128, i32> {}
impl DowncastableU128U16 of Downcastable<u128, u16> {}
impl DowncastableU128I16 of Downcastable<u128, i16> {}
impl DowncastableU128U8 of Downcastable<u128, u8> {}

impl DowncastableU128I8 of Downcastable<u128, i8> {}
impl DowncastableI128U128 of Downcastable<i128, u128> {}
impl DowncastableI128U64 of Downcastable<i128, u64> {}
impl DowncastableI128I64 of Downcastable<i128, i64> {}
impl DowncastableI128U32 of Downcastable<i128, u32> {}
impl DowncastableI128I32 of Downcastable<i128, i32> {}
impl DowncastableI128U16 of Downcastable<i128, u16> {}
impl DowncastableI128I16 of Downcastable<i128, i16> {}
impl DowncastableI128U8 of Downcastable<i128, u8> {}
impl DowncastableI128I8 of Downcastable<i128, i8> {}

impl DowncastableU64I64 of Downcastable<u64, i64> {}
impl DowncastableU64U32 of Downcastable<u64, u32> {}
impl DowncastableU64I32 of Downcastable<u64, i32> {}
impl DowncastableU64U16 of Downcastable<u64, u16> {}
impl DowncastableU64I16 of Downcastable<u64, i16> {}
impl DowncastableU64U8 of Downcastable<u64, u8> {}

impl DowncastableU64I8 of Downcastable<u64, i8> {}
impl DowncastableI64U128 of Downcastable<i64, u128> {}
impl DowncastableI64U64 of Downcastable<i64, u64> {}
impl DowncastableI64U32 of Downcastable<i64, u32> {}
impl DowncastableI64I32 of Downcastable<i64, i32> {}
impl DowncastableI64U16 of Downcastable<i64, u16> {}
impl DowncastableI64I16 of Downcastable<i64, i16> {}
impl DowncastableI64U8 of Downcastable<i64, u8> {}
impl DowncastableI64I8 of Downcastable<i64, i8> {}

impl DowncastableU32I32 of Downcastable<u32, i32> {}
impl DowncastableU32U16 of Downcastable<u32, u16> {}
impl DowncastableU32I16 of Downcastable<u32, i16> {}
impl DowncastableU32U8 of Downcastable<u32, u8> {}

impl DowncastableU32I8 of Downcastable<u32, i8> {}
impl DowncastableI32U128 of Downcastable<i32, u128> {}
impl DowncastableI32U64 of Downcastable<i32, u64> {}
impl DowncastableI32U32 of Downcastable<i32, u32> {}
impl DowncastableI32U16 of Downcastable<i32, u16> {}
impl DowncastableI32I16 of Downcastable<i32, i16> {}
impl DowncastableI32U8 of Downcastable<i32, u8> {}
impl DowncastableI32I8 of Downcastable<i32, i8> {}

impl DowncastableU16I16 of Downcastable<u16, i16> {}
impl DowncastableU16U8 of Downcastable<u16, u8> {}
impl DowncastableU16I8 of Downcastable<u16, i8> {}
impl DowncastableI16U128 of Downcastable<i16, u128> {}
impl DowncastableI16U64 of Downcastable<i16, u64> {}
impl DowncastableI16U32 of Downcastable<i16, u32> {}
impl DowncastableI16U16 of Downcastable<i16, u16> {}
impl DowncastableI16U8 of Downcastable<i16, u8> {}
impl DowncastableI16I8 of Downcastable<i16, i8> {}

impl DowncastableU8I8 of Downcastable<u8, i8> {}
impl DowncastableI8U128 of Downcastable<i8, u128> {}
impl DowncastableI8U64 of Downcastable<i8, u64> {}
impl DowncastableI8U32 of Downcastable<i8, u32> {}
impl DowncastableI8U16 of Downcastable<i8, u16> {}
impl DowncastableI8U8 of Downcastable<i8, u8> {}

/// Default values
impl U8Default of Default<u8> {
Expand Down Expand Up @@ -1932,7 +2058,7 @@ 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')
i8_wide_mul(lhs, rhs).try_into().expect('i8_mul Overflow')
}
}
impl I8MulEq of MulEq<i8> {
Expand Down Expand Up @@ -2023,7 +2149,7 @@ 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')
i16_wide_mul(lhs, rhs).try_into().expect('i16_mul Overflow')
}
}
impl I16MulEq of MulEq<i16> {
Expand Down Expand Up @@ -2114,7 +2240,7 @@ 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')
i32_wide_mul(lhs, rhs).try_into().expect('i32_mul Overflow')
}
}
impl I32MulEq of MulEq<i32> {
Expand Down Expand Up @@ -2205,7 +2331,7 @@ 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')
i64_wide_mul(lhs, rhs).try_into().expect('i64_mul Overflow')
}
}
impl I64MulEq of MulEq<i64> {
Expand Down
Loading

0 comments on commit 59681b0

Please sign in to comment.