From 41e32cb99495bb10e9d6d9e9f7bd87ac8168c907 Mon Sep 17 00:00:00 2001 From: thanhnguyen-aws Date: Thu, 17 Apr 2025 11:43:42 -0700 Subject: [PATCH] add invariants --- library/core/src/num/int_macros.rs | 3 +++ library/core/src/num/uint_macros.rs | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index a95b1c4cf82de..2ae13a645d326 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -1589,6 +1589,7 @@ macro_rules! int_impl { let mut base = self; let mut acc: Self = 1; + #[safety::loop_invariant(true)] loop { if (exp & 1) == 1 { acc = try_opt!(acc.checked_mul(base)); @@ -2299,6 +2300,7 @@ macro_rules! int_impl { let mut acc: Self = 1; if intrinsics::is_val_statically_known(exp) { + #[safety::loop_invariant(exp>=1)] while exp > 1 { if (exp & 1) == 1 { acc = acc.wrapping_mul(base); @@ -2316,6 +2318,7 @@ macro_rules! int_impl { // at compile time. We can't use the same code for the constant // exponent case because LLVM is currently unable to unroll // this loop. + #[safety::loop_invariant(true)] loop { if (exp & 1) == 1 { acc = acc.wrapping_mul(base); diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 3d4dc7b3748b3..71052200acbb7 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -1780,6 +1780,7 @@ macro_rules! uint_impl { let mut base = self; let mut acc: Self = 1; + #[safety::loop_invariant(true)] loop { if (exp & 1) == 1 { acc = try_opt!(acc.checked_mul(base)); @@ -2349,6 +2350,7 @@ macro_rules! uint_impl { let mut acc: Self = 1; if intrinsics::is_val_statically_known(exp) { + #[safety::loop_invariant(exp>=1)] while exp > 1 { if (exp & 1) == 1 { acc = acc.wrapping_mul(base); @@ -2366,6 +2368,7 @@ macro_rules! uint_impl { // at compile time. We can't use the same code for the constant // exponent case because LLVM is currently unable to unroll // this loop. + #[safety::loop_invariant(true)] loop { if (exp & 1) == 1 { acc = acc.wrapping_mul(base); @@ -3044,6 +3047,7 @@ macro_rules! uint_impl { // Scratch space for storing results of overflowing_mul. let mut r; + #[safety::loop_invariant(true)] loop { if (exp & 1) == 1 { r = acc.overflowing_mul(base);