Skip to content

Commit 228bb41

Browse files
add some loop invariants
1 parent b152f68 commit 228bb41

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

library/core/src/num/int_macros.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,8 @@ macro_rules! int_impl {
15881588
}
15891589
let mut base = self;
15901590
let mut acc: Self = 1;
1591-
1591+
1592+
#[safety::loop_invariant(true)]
15921593
loop {
15931594
if (exp & 1) == 1 {
15941595
acc = try_opt!(acc.checked_mul(base));
@@ -2299,6 +2300,7 @@ macro_rules! int_impl {
22992300
let mut acc: Self = 1;
23002301

23012302
if intrinsics::is_val_statically_known(exp) {
2303+
#[safety::loop_invariant(exp>=1)]
23022304
while exp > 1 {
23032305
if (exp & 1) == 1 {
23042306
acc = acc.wrapping_mul(base);
@@ -2316,6 +2318,7 @@ macro_rules! int_impl {
23162318
// at compile time. We can't use the same code for the constant
23172319
// exponent case because LLVM is currently unable to unroll
23182320
// this loop.
2321+
#[safety::loop_invariant(true)]
23192322
loop {
23202323
if (exp & 1) == 1 {
23212324
acc = acc.wrapping_mul(base);
@@ -2928,7 +2931,8 @@ macro_rules! int_impl {
29282931
let mut overflown = false;
29292932
// Scratch space for storing results of overflowing_mul.
29302933
let mut r;
2931-
2934+
2935+
#[safety::loop_invariant(true)]
29322936
loop {
29332937
if (exp & 1) == 1 {
29342938
r = acc.overflowing_mul(base);

library/core/src/num/uint_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ macro_rules! uint_impl {
14021402
n = self.ilog2() / (base.ilog2() + 1);
14031403
r = base.pow(n);
14041404
}
1405-
1405+
#[safety::loop_invariant(base > 1 && r > 1 && r <= s && n <= r)]
14061406
while r <= self / base {
14071407
n += 1;
14081408
r *= base;

0 commit comments

Comments
 (0)