Sitelet https://github.com/starkware-libs/cairo/commit/219825c373f3603f57500c641a4d297d52d27a97
Skip to content

Commit 219825c

Browse files
authored
refactor(corelib): Refactored integer.cairo by additional uses. (#10041)
1 parent 05c5df7 commit 219825c

1 file changed

Lines changed: 35 additions & 46 deletions

File tree

‎corelib/src/integer.cairo‎

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
use crate::RangeCheck;
6969
#[allow(unused_imports)]
7070
use crate::array::{ArrayTrait, SpanTrait};
71+
use crate::internal::OptionRev;
7172
#[feature("bounded-int-utils")]
72-
use crate::internal::bounded_int::{downcast, upcast};
73+
use crate::internal::bounded_int::{self, downcast, upcast};
7374
use crate::option::OptionTrait;
7475
use crate::result::ResultTrait;
7576
use crate::traits::{BitAnd, BitNot, BitOr, BitXor, Default, Felt252DictValue, Into, TryInto};
@@ -1210,10 +1211,9 @@ pub fn u256_wide_mul(a: u256, b: u256) -> u512 nopanic {
12101211

12111212
/// Helper function for implementation of `u256_wide_mul`.
12121213
/// Used for adding two u128s and receiving a BoundedInt for the carry result.
1213-
#[feature("bounded-int-utils")]
12141214
pub(crate) fn u128_add_with_bounded_int_carry(
12151215
a: u128, b: u128,
1216-
) -> (u128, crate::internal::bounded_int::BoundedInt<0, 1>) nopanic {
1216+
) -> (u128, bounded_int::BoundedInt<0, 1>) nopanic {
12171217
match u128_overflowing_add(a, b) {
12181218
Ok(v) => (v, 0),
12191219
Err(v) => (v, 1),
@@ -1981,12 +1981,11 @@ impl I8Sub of Sub<i8> {
19811981

19821982
impl I8Neg of Neg<i8> {
19831983
#[inline]
1984-
#[feature("bounded-int-utils")]
19851984
fn neg(a: i8) -> i8 {
1986-
let core::internal::OptionRev::Some(a) = core::internal::bounded_int::trim_min(a) else {
1987-
crate::panic_with_felt252('i8_neg Underflow');
1988-
};
1989-
upcast(core::internal::bounded_int::NegateHelper::negate(a))
1985+
match bounded_int::trim_min(a) {
1986+
OptionRev::Some(a) => upcast(bounded_int::NegateHelper::negate(a)),
1987+
OptionRev::None => crate::panic_with_felt252('i8_neg Underflow'),
1988+
}
19901989
}
19911990
}
19921991

@@ -2071,12 +2070,11 @@ impl I16Sub of Sub<i16> {
20712070

20722071
impl I16Neg of Neg<i16> {
20732072
#[inline]
2074-
#[feature("bounded-int-utils")]
20752073
fn neg(a: i16) -> i16 {
2076-
let core::internal::OptionRev::Some(a) = core::internal::bounded_int::trim_min(a) else {
2077-
crate::panic_with_felt252('i16_neg Underflow');
2078-
};
2079-
upcast(core::internal::bounded_int::NegateHelper::negate(a))
2074+
match bounded_int::trim_min(a) {
2075+
OptionRev::Some(a) => upcast(bounded_int::NegateHelper::negate(a)),
2076+
OptionRev::None => crate::panic_with_felt252('i16_neg Underflow'),
2077+
}
20802078
}
20812079
}
20822080

@@ -2162,12 +2160,11 @@ impl I32Sub of Sub<i32> {
21622160

21632161
impl I32Neg of Neg<i32> {
21642162
#[inline]
2165-
#[feature("bounded-int-utils")]
21662163
fn neg(a: i32) -> i32 {
2167-
let core::internal::OptionRev::Some(a) = core::internal::bounded_int::trim_min(a) else {
2168-
crate::panic_with_felt252('i32_neg Underflow');
2169-
};
2170-
upcast(core::internal::bounded_int::NegateHelper::negate(a))
2164+
match bounded_int::trim_min(a) {
2165+
OptionRev::Some(a) => upcast(bounded_int::NegateHelper::negate(a)),
2166+
OptionRev::None => crate::panic_with_felt252('i32_neg Underflow'),
2167+
}
21712168
}
21722169
}
21732170

@@ -2253,12 +2250,11 @@ impl I64Sub of Sub<i64> {
22532250

22542251
impl I64Neg of Neg<i64> {
22552252
#[inline]
2256-
#[feature("bounded-int-utils")]
22572253
fn neg(a: i64) -> i64 {
2258-
let core::internal::OptionRev::Some(a) = core::internal::bounded_int::trim_min(a) else {
2259-
crate::panic_with_felt252('i64_neg Underflow');
2260-
};
2261-
upcast(core::internal::bounded_int::NegateHelper::negate(a))
2254+
match bounded_int::trim_min(a) {
2255+
OptionRev::Some(a) => upcast(bounded_int::NegateHelper::negate(a)),
2256+
OptionRev::None => crate::panic_with_felt252('i64_neg Underflow'),
2257+
}
22622258
}
22632259
}
22642260

@@ -2349,21 +2345,19 @@ impl I128Sub of Sub<i128> {
23492345

23502346
impl I128Neg of Neg<i128> {
23512347
#[inline]
2352-
#[feature("bounded-int-utils")]
23532348
fn neg(a: i128) -> i128 {
2354-
let core::internal::OptionRev::Some(a) = core::internal::bounded_int::trim_min(a) else {
2355-
crate::panic_with_felt252('i128_neg Underflow');
2356-
};
2357-
upcast(core::internal::bounded_int::NegateHelper::negate(a))
2349+
match bounded_int::trim_min(a) {
2350+
OptionRev::Some(a) => upcast(bounded_int::NegateHelper::negate(a)),
2351+
OptionRev::None => crate::panic_with_felt252('i128_neg Underflow'),
2352+
}
23582353
}
23592354
}
23602355

23612356
impl I128Mul of Mul<i128> {
23622357
fn mul(lhs: i128, rhs: i128) -> i128 {
23632358
let (lhs_u127, lhs_neg) = lhs.abs_and_sign();
2364-
#[feature("bounded-int-utils")]
2365-
let (rhs_u127, res_neg) = match core::internal::bounded_int::constrain::<i128, 0>(rhs) {
2366-
Ok(lt0) => (upcast(core::internal::bounded_int::NegateHelper::negate(lt0)), !lhs_neg),
2359+
let (rhs_u127, res_neg) = match bounded_int::constrain::<i128, 0>(rhs) {
2360+
Ok(lt0) => (upcast(bounded_int::NegateHelper::negate(lt0)), !lhs_neg),
23672361
Err(ge0) => (upcast(ge0), lhs_neg),
23682362
};
23692363
let res_as_u128 = lhs_u127 * rhs_u127;
@@ -3384,50 +3378,45 @@ pub(crate) trait AbsAndSign<Signed, Unsigned> {
33843378
}
33853379

33863380
impl I8ToU8 of AbsAndSign<i8, u8> {
3387-
#[feature("bounded-int-utils")]
33883381
fn abs_and_sign(self: i8) -> (u8, bool) {
3389-
match core::internal::bounded_int::constrain::<i8, 0>(self) {
3390-
Ok(lt0) => (upcast(core::internal::bounded_int::NegateHelper::negate(lt0)), true),
3382+
match bounded_int::constrain::<i8, 0>(self) {
3383+
Ok(lt0) => (upcast(bounded_int::NegateHelper::negate(lt0)), true),
33913384
Err(ge0) => (upcast(ge0), false),
33923385
}
33933386
}
33943387
}
33953388

33963389
impl I16ToU16 of AbsAndSign<i16, u16> {
3397-
#[feature("bounded-int-utils")]
33983390
fn abs_and_sign(self: i16) -> (u16, bool) {
3399-
match core::internal::bounded_int::constrain::<i16, 0>(self) {
3400-
Ok(lt0) => (upcast(core::internal::bounded_int::NegateHelper::negate(lt0)), true),
3391+
match bounded_int::constrain::<i16, 0>(self) {
3392+
Ok(lt0) => (upcast(bounded_int::NegateHelper::negate(lt0)), true),
34013393
Err(ge0) => (upcast(ge0), false),
34023394
}
34033395
}
34043396
}
34053397

34063398
impl I32ToU32 of AbsAndSign<i32, u32> {
3407-
#[feature("bounded-int-utils")]
34083399
fn abs_and_sign(self: i32) -> (u32, bool) {
3409-
match core::internal::bounded_int::constrain::<i32, 0>(self) {
3410-
Ok(lt0) => (upcast(core::internal::bounded_int::NegateHelper::negate(lt0)), true),
3400+
match bounded_int::constrain::<i32, 0>(self) {
3401+
Ok(lt0) => (upcast(bounded_int::NegateHelper::negate(lt0)), true),
34113402
Err(ge0) => (upcast(ge0), false),
34123403
}
34133404
}
34143405
}
34153406

34163407
impl I64ToU64 of AbsAndSign<i64, u64> {
3417-
#[feature("bounded-int-utils")]
34183408
fn abs_and_sign(self: i64) -> (u64, bool) {
3419-
match core::internal::bounded_int::constrain::<i64, 0>(self) {
3420-
Ok(lt0) => (upcast(core::internal::bounded_int::NegateHelper::negate(lt0)), true),
3409+
match bounded_int::constrain::<i64, 0>(self) {
3410+
Ok(lt0) => (upcast(bounded_int::NegateHelper::negate(lt0)), true),
34213411
Err(ge0) => (upcast(ge0), false),
34223412
}
34233413
}
34243414
}
34253415

34263416
impl I128ToU128 of AbsAndSign<i128, u128> {
3427-
#[feature("bounded-int-utils")]
34283417
fn abs_and_sign(self: i128) -> (u128, bool) {
3429-
match core::internal::bounded_int::constrain::<i128, 0>(self) {
3430-
Ok(lt0) => (upcast(core::internal::bounded_int::NegateHelper::negate(lt0)), true),
3418+
match bounded_int::constrain::<i128, 0>(self) {
3419+
Ok(lt0) => (upcast(bounded_int::NegateHelper::negate(lt0)), true),
34313420
Err(ge0) => (upcast(ge0), false),
34323421
}
34333422
}

0 commit comments

Comments
 (0)