Optimize integer pow by removing exit branch · patricklam/verify-rust-std@4cfe24a (original) (raw)

`@@ -1495,18 +1495,17 @@ macro_rules! int_impl {

`

1495

1495

`let mut base = self;

`

1496

1496

`let mut acc: Self = 1;

`

1497

1497

``

1498

``

`-

while exp > 1 {

`

``

1498

`+

loop {

`

1499

1499

`if (exp & 1) == 1 {

`

1500

1500

` acc = try_opt!(acc.checked_mul(base));

`

``

1501

`+

// since exp!=0, finally the exp must be 1.

`

``

1502

`+

if exp == 1 {

`

``

1503

`+

return Some(acc);

`

``

1504

`+

}

`

1501

1505

`}

`

1502

1506

` exp /= 2;

`

1503

1507

` base = try_opt!(base.checked_mul(base));

`

1504

1508

`}

`

1505

``

`-

// since exp!=0, finally the exp must be 1.

`

1506

``

`-

// Deal with the final bit of the exponent separately, since

`

1507

``

`-

// squaring the base afterwards is not necessary and may cause a

`

1508

``

`-

// needless overflow.

`

1509

``

`-

acc.checked_mul(base)

`

1510

1509

`}

`

1511

1510

``

1512

1511

`` /// Strict exponentiation. Computes self.pow(exp), panicking if

``

`@@ -1546,18 +1545,17 @@ macro_rules! int_impl {

`

1546

1545

`let mut base = self;

`

1547

1546

`let mut acc: Self = 1;

`

1548

1547

``

1549

``

`-

while exp > 1 {

`

``

1548

`+

loop {

`

1550

1549

`if (exp & 1) == 1 {

`

1551

1550

` acc = acc.strict_mul(base);

`

``

1551

`+

// since exp!=0, finally the exp must be 1.

`

``

1552

`+

if exp == 1 {

`

``

1553

`+

return acc;

`

``

1554

`+

}

`

1552

1555

`}

`

1553

1556

` exp /= 2;

`

1554

1557

` base = base.strict_mul(base);

`

1555

1558

`}

`

1556

``

`-

// since exp!=0, finally the exp must be 1.

`

1557

``

`-

// Deal with the final bit of the exponent separately, since

`

1558

``

`-

// squaring the base afterwards is not necessary and may cause a

`

1559

``

`-

// needless overflow.

`

1560

``

`-

acc.strict_mul(base)

`

1561

1559

`}

`

1562

1560

``

1563

1561

`/// Returns the square root of the number, rounded down.

`

`@@ -2181,19 +2179,17 @@ macro_rules! int_impl {

`

2181

2179

`let mut base = self;

`

2182

2180

`let mut acc: Self = 1;

`

2183

2181

``

2184

``

`-

while exp > 1 {

`

``

2182

`+

loop {

`

2185

2183

`if (exp & 1) == 1 {

`

2186

2184

` acc = acc.wrapping_mul(base);

`

``

2185

`+

// since exp!=0, finally the exp must be 1.

`

``

2186

`+

if exp == 1 {

`

``

2187

`+

return acc;

`

``

2188

`+

}

`

2187

2189

`}

`

2188

2190

` exp /= 2;

`

2189

2191

` base = base.wrapping_mul(base);

`

2190

2192

`}

`

2191

``

-

2192

``

`-

// since exp!=0, finally the exp must be 1.

`

2193

``

`-

// Deal with the final bit of the exponent separately, since

`

2194

``

`-

// squaring the base afterwards is not necessary and may cause a

`

2195

``

`-

// needless overflow.

`

2196

``

`-

acc.wrapping_mul(base)

`

2197

2193

`}

`

2198

2194

``

2199

2195

`` /// Calculates self + rhs

``

`@@ -2687,9 +2683,14 @@ macro_rules! int_impl {

`

2687

2683

`// Scratch space for storing results of overflowing_mul.

`

2688

2684

`let mut r;

`

2689

2685

``

2690

``

`-

while exp > 1 {

`

``

2686

`+

loop {

`

2691

2687

`if (exp & 1) == 1 {

`

2692

2688

` r = acc.overflowing_mul(base);

`

``

2689

`+

// since exp!=0, finally the exp must be 1.

`

``

2690

`+

if exp == 1 {

`

``

2691

`+

r.1 |= overflown;

`

``

2692

`+

return r;

`

``

2693

`+

}

`

2693

2694

` acc = r.0;

`

2694

2695

` overflown |= r.1;

`

2695

2696

`}

`

`@@ -2698,14 +2699,6 @@ macro_rules! int_impl {

`

2698

2699

` base = r.0;

`

2699

2700

` overflown |= r.1;

`

2700

2701

`}

`

2701

``

-

2702

``

`-

// since exp!=0, finally the exp must be 1.

`

2703

``

`-

// Deal with the final bit of the exponent separately, since

`

2704

``

`-

// squaring the base afterwards is not necessary and may cause a

`

2705

``

`-

// needless overflow.

`

2706

``

`-

r = acc.overflowing_mul(base);

`

2707

``

`-

r.1 |= overflown;

`

2708

``

`-

r

`

2709

2702

`}

`

2710

2703

``

2711

2704

`` /// Raises self to the power of exp, using exponentiation by squaring.

``

`@@ -2732,19 +2725,17 @@ macro_rules! int_impl {

`

2732

2725

`let mut base = self;

`

2733

2726

`let mut acc = 1;

`

2734

2727

``

2735

``

`-

while exp > 1 {

`

``

2728

`+

loop {

`

2736

2729

`if (exp & 1) == 1 {

`

2737

2730

` acc = acc * base;

`

``

2731

`+

// since exp!=0, finally the exp must be 1.

`

``

2732

`+

if exp == 1 {

`

``

2733

`+

return acc;

`

``

2734

`+

}

`

2738

2735

`}

`

2739

2736

` exp /= 2;

`

2740

2737

` base = base * base;

`

2741

2738

`}

`

2742

``

-

2743

``

`-

// since exp!=0, finally the exp must be 1.

`

2744

``

`-

// Deal with the final bit of the exponent separately, since

`

2745

``

`-

// squaring the base afterwards is not necessary and may cause a

`

2746

``

`-

// needless overflow.

`

2747

``

`-

acc * base

`

2748

2739

`}

`

2749

2740

``

2750

2741

`/// Returns the square root of the number, rounded down.

`