Skip to content

Commit

Permalink
Added cast for signed integers. (#3623)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi authored Oct 10, 2023
2 parents 6a5261f + bcfa44c commit af284d4
Show file tree
Hide file tree
Showing 7 changed files with 1,275 additions and 108 deletions.
155 changes: 128 additions & 27 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1586,32 +1586,133 @@ extern fn downcast<FromType, ToType>(x: FromType) -> Option<ToType> implicits(Ra
// Marks `FromType` as upcastable to `ToType`.
// Do not add user code implementing this trait.
trait Upcastable<FromType, ToType>;
impl UpcastableU8U16 of Upcastable<u8, u16> {}
impl UpcastableU8U32 of Upcastable<u8, u32> {}
impl UpcastableU8U64 of Upcastable<u8, u64> {}
impl UpcastableU8U128 of Upcastable<u8, u128> {}
impl UpcastableU16U32 of Upcastable<u16, u32> {}
impl UpcastableU16U64 of Upcastable<u16, u64> {}
impl UpcastableU16U128 of Upcastable<u16, u128> {}
impl UpcastableU32U64 of Upcastable<u32, u64> {}
impl UpcastableU32U128 of Upcastable<u32, u128> {}
impl UpcastableU64U128 of Upcastable<u64, u128> {}
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 DowncastableU128U64 of Downcastable<u128, u64> {}
impl DowncastableU128U32 of Downcastable<u128, u32> {}
impl DowncastableU128U16 of Downcastable<u128, u16> {}
impl DowncastableU128U8 of Downcastable<u128, u8> {}

impl DowncastableU64U32 of Downcastable<u64, u32> {}
impl DowncastableU64U16 of Downcastable<u64, u16> {}
impl DowncastableU64U8 of Downcastable<u64, u8> {}

impl DowncastableU32U16 of Downcastable<u32, u16> {}
impl DowncastableU32U8 of Downcastable<u32, u8> {}

impl DowncastableU16U8 of Downcastable<u16, u8> {}
impl DowncastableU8I8 of Downcastable<u8, i8>;
impl DowncastableU8I16 of Downcastable<u8, i16>;
impl DowncastableU8U16 of Downcastable<u8, u16>;
impl DowncastableU8I32 of Downcastable<u8, i32>;
impl DowncastableU8U32 of Downcastable<u8, u32>;
impl DowncastableU8I64 of Downcastable<u8, i64>;
impl DowncastableU8U64 of Downcastable<u8, u64>;
impl DowncastableU8I128 of Downcastable<u8, i128>;
impl DowncastableU8U128 of Downcastable<u8, u128>;
impl DowncastableI8U8 of Downcastable<i8, u8>;
impl DowncastableI8I16 of Downcastable<i8, i16>;
impl DowncastableI8U16 of Downcastable<i8, u16>;
impl DowncastableI8I32 of Downcastable<i8, i32>;
impl DowncastableI8U32 of Downcastable<i8, u32>;
impl DowncastableI8I64 of Downcastable<i8, i64>;
impl DowncastableI8U64 of Downcastable<i8, u64>;
impl DowncastableI8I128 of Downcastable<i8, i128>;
impl DowncastableI8U128 of Downcastable<i8, u128>;

impl DowncastableU16I8 of Downcastable<u16, i8>;
impl DowncastableU16U8 of Downcastable<u16, u8>;
impl DowncastableU16I16 of Downcastable<u16, i16>;
impl DowncastableU16I32 of Downcastable<u16, i32>;
impl DowncastableU16U32 of Downcastable<u16, u32>;
impl DowncastableU16I64 of Downcastable<u16, i64>;
impl DowncastableU16U64 of Downcastable<u16, u64>;
impl DowncastableU16I128 of Downcastable<u16, i128>;
impl DowncastableU16U128 of Downcastable<u16, u128>;
impl DowncastableI16I8 of Downcastable<i16, i8>;
impl DowncastableI16U8 of Downcastable<i16, u8>;
impl DowncastableI16U16 of Downcastable<i16, u16>;
impl DowncastableI16I32 of Downcastable<i16, i32>;
impl DowncastableI16U32 of Downcastable<i16, u32>;
impl DowncastableI16I64 of Downcastable<i16, i64>;
impl DowncastableI16U64 of Downcastable<i16, u64>;
impl DowncastableI16I128 of Downcastable<i16, i128>;
impl DowncastableI16U128 of Downcastable<i16, u128>;

impl DowncastableU32I8 of Downcastable<u32, i8>;
impl DowncastableU32U8 of Downcastable<u32, u8>;
impl DowncastableU32I16 of Downcastable<u32, i16>;
impl DowncastableU32U16 of Downcastable<u32, u16>;
impl DowncastableU32I32 of Downcastable<u32, i32>;
impl DowncastableU32I64 of Downcastable<u32, i64>;
impl DowncastableU32U64 of Downcastable<u32, u64>;
impl DowncastableU32I128 of Downcastable<u32, i128>;
impl DowncastableU32U128 of Downcastable<u32, u128>;
impl DowncastableI32I8 of Downcastable<i32, i8>;
impl DowncastableI32U8 of Downcastable<i32, u8>;
impl DowncastableI32I16 of Downcastable<i32, i16>;
impl DowncastableI32U16 of Downcastable<i32, u16>;
impl DowncastableI32U32 of Downcastable<i32, u32>;
impl DowncastableI32I64 of Downcastable<i32, i64>;
impl DowncastableI32U64 of Downcastable<i32, u64>;
impl DowncastableI32I128 of Downcastable<i32, i128>;
impl DowncastableI32U128 of Downcastable<i32, u128>;

impl DowncastableU64I8 of Downcastable<u64, i8>;
impl DowncastableU64U8 of Downcastable<u64, u8>;
impl DowncastableU64I16 of Downcastable<u64, i16>;
impl DowncastableU64U16 of Downcastable<u64, u16>;
impl DowncastableU64I32 of Downcastable<u64, i32>;
impl DowncastableU64U32 of Downcastable<u64, u32>;
impl DowncastableU64I64 of Downcastable<u64, i64>;
impl DowncastableU64I128 of Downcastable<u64, i128>;
impl DowncastableU64U128 of Downcastable<u64, u128>;
impl DowncastableI64I8 of Downcastable<i64, i8>;
impl DowncastableI64U8 of Downcastable<i64, u8>;
impl DowncastableI64I16 of Downcastable<i64, i16>;
impl DowncastableI64U16 of Downcastable<i64, u16>;
impl DowncastableI64I32 of Downcastable<i64, i32>;
impl DowncastableI64U32 of Downcastable<i64, u32>;
impl DowncastableI64U64 of Downcastable<i64, u64>;
impl DowncastableI64I128 of Downcastable<i64, i128>;
impl DowncastableI64U128 of Downcastable<i64, u128>;

impl DowncastableU128I8 of Downcastable<u128, i8>;
impl DowncastableU128U8 of Downcastable<u128, u8>;
impl DowncastableU128I16 of Downcastable<u128, i16>;
impl DowncastableU128U16 of Downcastable<u128, u16>;
impl DowncastableU128I32 of Downcastable<u128, i32>;
impl DowncastableU128U32 of Downcastable<u128, u32>;
impl DowncastableU128I64 of Downcastable<u128, i64>;
impl DowncastableU128U64 of Downcastable<u128, u64>;
impl DowncastableU128I128 of Downcastable<u128, i128>;
impl DowncastableI128I8 of Downcastable<i128, i8>;
impl DowncastableI128U8 of Downcastable<i128, u8>;
impl DowncastableI128I16 of Downcastable<i128, i16>;
impl DowncastableI128U16 of Downcastable<i128, u16>;
impl DowncastableI128I32 of Downcastable<i128, i32>;
impl DowncastableI128U32 of Downcastable<i128, u32>;
impl DowncastableI128I64 of Downcastable<i128, i64>;
impl DowncastableI128U64 of Downcastable<i128, u64>;
impl DowncastableI128U128 of Downcastable<i128, u128>;

/// Default values
impl U8Default of Default<u8> {
Expand Down Expand Up @@ -1958,7 +2059,7 @@ impl I8Neg of Neg<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 @@ -2059,7 +2160,7 @@ impl I16Neg of Neg<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 @@ -2160,7 +2261,7 @@ impl I32Neg of Neg<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 @@ -2261,7 +2362,7 @@ impl I64Neg of Neg<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 af284d4

Please sign in to comment.