Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NonZero() for const value fmt. #6424

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions corelib/src/math.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::zeroable::{IsZeroResult, NonZeroIntoImpl, Zeroable};
#[allow(unused_imports)]
use crate::traits::{Into, TryInto};
#[allow(unused_imports)]
use crate::option::OptionTrait;
use crate::integer::{u256_wide_mul, u512_safe_div_rem_by_u256, U128MulGuarantee};
use crate::RangeCheck;
Expand All @@ -22,21 +23,23 @@ pub fn egcd<
+Add<T>,
+Mul<T>,
+DivRem<T>,
+Zeroable<T>,
+Oneable<T>,
+core::num::traits::Zero<T>,
+core::num::traits::One<T>,
+TryInto<T, NonZero<T>>,
>(
a: NonZero<T>, b: NonZero<T>
) -> (T, T, T, bool) {
let (q, r) = DivRem::<T>::div_rem(a.into(), b);

if r.is_zero() {
return (b.into(), Zeroable::zero(), Oneable::one(), false);
}
let r = if let Option::Some(r) = r.try_into() {
r
} else {
return (b.into(), core::num::traits::Zero::zero(), core::num::traits::One::one(), false);
};

// `sign` (1 for true, -1 for false) is the sign of `g` in the current iteration.
// 0 is considered negative for this purpose.
let (g, s, t, sign) = egcd(b, r.try_into().unwrap());
let (g, s, t, sign) = egcd(b, r);
// We know that `a = q*b + r` and that `s*b - t*r = sign*g`.
// So `t*a - (s + q*t)*b = t*r - s*b = sign*g`.
// Thus we pick `new_s = t`, `new_t = s + q*t`, `new_sign = !sign`.
Expand All @@ -55,14 +58,14 @@ pub fn inv_mod<
+Sub<T>,
+Mul<T>,
+DivRem<T>,
+Zeroable<T>,
+Oneable<T>,
+core::num::traits::Zero<T>,
+core::num::traits::One<T>,
+TryInto<T, NonZero<T>>,
>(
a: NonZero<T>, n: NonZero<T>
) -> Option<T> {
if Oneable::<T>::is_one(n.into()) {
return Option::Some(Zeroable::zero());
if core::num::traits::One::<T>::is_one(@n.into()) {
return Option::Some(core::num::traits::Zero::zero());
}
let (g, s, _, sub_direction) = egcd(a, n);
if g.is_one() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Parameters: v0: core::integer::u8
blk0 (root):
Statements:
(v1: core::integer::u8) <- 4
(v2: core::zeroable::NonZero::<core::integer::u8>) <- 4
(v2: core::zeroable::NonZero::<core::integer::u8>) <- NonZero(4)
End:
Goto(blk2, {})

Expand Down Expand Up @@ -477,7 +477,7 @@ Parameters: v0: core::integer::u256
blk0 (root):
Statements:
(v1: core::integer::u256) <- { 4: core::integer::u128, 0: core::integer::u128 }
(v2: core::zeroable::NonZero::<core::integer::u256>) <- { 4: core::integer::u128, 0: core::integer::u128 }
(v2: core::zeroable::NonZero::<core::integer::u256>) <- NonZero({ 4: core::integer::u128, 0: core::integer::u128 })
End:
Goto(blk2, {})

Expand Down Expand Up @@ -719,7 +719,7 @@ blk0 (root):
Statements:
(v1: core::integer::u64) <- 1
(v2: core::integer::u128) <- 1
(v3: core::zeroable::NonZero::<core::integer::u128>) <- 1
(v3: core::zeroable::NonZero::<core::integer::u128>) <- NonZero(1)
End:
Goto(blk2, {})

Expand Down Expand Up @@ -3285,7 +3285,7 @@ Parameters: v0: core::integer::i8
blk0 (root):
Statements:
(v1: core::integer::i8) <- 4
(v2: core::zeroable::NonZero::<core::integer::i8>) <- 4
(v2: core::zeroable::NonZero::<core::integer::i8>) <- NonZero(4)
End:
Goto(blk2, {})

Expand Down
6 changes: 5 additions & 1 deletion crates/cairo-lang-semantic/src/items/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ impl<Db: ?Sized + Upcast<dyn SemanticGroup + 'static>> DebugWithDb<Db> for Const
inner.fmt(f, db)?;
write!(f, ")")
}
ConstValue::NonZero(value) => value.fmt(f, db),
ConstValue::NonZero(value) => {
write!(f, "NonZero(")?;
value.fmt(f, db)?;
write!(f, ")")
}
ConstValue::Boxed(value) => {
value.fmt(f, db)?;
write!(f, ".into_box()")
Expand Down