Skip to content

Add loop_invariants for some Int power functions #327

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading