Commit 7e1143b
authored
Rollup merge of rust-lang#128166 - ChaiTRex:isqrt, r=tgross35
Improved `checked_isqrt` and `isqrt` methods
### Improved tests of `isqrt` and `checked_isqrt` implementations
* Inputs chosen more thoroughly and systematically.
* Checks that `isqrt` and `checked_isqrt` have equivalent results for signed types, either equivalent numerically or equivalent as a panic and a `None`.
* Checks that `isqrt` has numerically-equivalent results for unsigned types and their `NonZero` counterparts.
### Added benchmarks for `isqrt` implementations
### Greatly sped up `checked_isqrt` and `isqrt` methods
* Uses a lookup table for 8-bit integers and then the Karatsuba square root algorithm for larger integers.
* Includes optimization hints that give the compiler the exact numeric range of results.
### Feature tracking issue
`isqrt` is an unstable feature tracked at rust-lang#116226.
<details><summary>Benchmarked improvements</summary>
### Command used to benchmark
./x bench library/core -- int_sqrt
### Before
benchmarks:
num::int_sqrt::i128::isqrt 439591.65/iter +/- 6652.70
num::int_sqrt::i16::isqrt 5302.97/iter +/- 160.93
num::int_sqrt::i32::isqrt 62999.11/iter +/- 2022.05
num::int_sqrt::i64::isqrt 125248.81/iter +/- 1674.43
num::int_sqrt::i8::isqrt 123.56/iter +/- 1.87
num::int_sqrt::isize::isqrt 125356.56/iter +/- 1017.03
num::int_sqrt::non_zero_u128::isqrt 437443.75/iter +/- 3535.43
num::int_sqrt::non_zero_u16::isqrt 8604.58/iter +/- 94.76
num::int_sqrt::non_zero_u32::isqrt 62933.33/iter +/- 517.30
num::int_sqrt::non_zero_u64::isqrt 125076.38/iter +/- 11340.61
num::int_sqrt::non_zero_u8::isqrt 221.51/iter +/- 1.58
num::int_sqrt::non_zero_usize::isqrt 136005.21/iter +/- 2020.35
num::int_sqrt::u128::isqrt 439014.55/iter +/- 3920.45
num::int_sqrt::u16::isqrt 8575.08/iter +/- 148.06
num::int_sqrt::u32::isqrt 63008.89/iter +/- 803.67
num::int_sqrt::u64::isqrt 125088.09/iter +/- 879.29
num::int_sqrt::u8::isqrt 230.18/iter +/- 2.04
num::int_sqrt::usize::isqrt 125237.51/iter +/- 4747.83
### After
benchmarks:
num::int_sqrt::i128::isqrt 105184.89/iter +/- 1171.38
num::int_sqrt::i16::isqrt 1910.26/iter +/- 78.50
num::int_sqrt::i32::isqrt 34260.34/iter +/- 960.84
num::int_sqrt::i64::isqrt 45939.19/iter +/- 2525.65
num::int_sqrt::i8::isqrt 22.87/iter +/- 0.45
num::int_sqrt::isize::isqrt 45884.17/iter +/- 595.49
num::int_sqrt::non_zero_u128::isqrt 106344.27/iter +/- 780.99
num::int_sqrt::non_zero_u16::isqrt 2790.19/iter +/- 53.43
num::int_sqrt::non_zero_u32::isqrt 33613.99/iter +/- 362.96
num::int_sqrt::non_zero_u64::isqrt 46235.42/iter +/- 429.69
num::int_sqrt::non_zero_u8::isqrt 31.78/iter +/- 0.75
num::int_sqrt::non_zero_usize::isqrt 46208.75/iter +/- 375.27
num::int_sqrt::u128::isqrt 106385.94/iter +/- 1649.95
num::int_sqrt::u16::isqrt 2747.69/iter +/- 28.72
num::int_sqrt::u32::isqrt 33627.09/iter +/- 475.68
num::int_sqrt::u64::isqrt 46182.29/iter +/- 311.16
num::int_sqrt::u8::isqrt 33.10/iter +/- 0.30
num::int_sqrt::usize::isqrt 46165.00/iter +/- 388.41
</details>File tree
11 files changed
+675
-67
lines changed- library/core
- benches
- num
- int_sqrt
- src/num
- tests/num
11 files changed
+675
-67
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1579 | 1579 | | |
1580 | 1580 | | |
1581 | 1581 | | |
1582 | | - | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 1590 | + | |
| 1591 | + | |
| 1592 | + | |
| 1593 | + | |
| 1594 | + | |
| 1595 | + | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
1583 | 1609 | | |
1584 | 1610 | | |
1585 | 1611 | | |
| |||
2800 | 2826 | | |
2801 | 2827 | | |
2802 | 2828 | | |
| 2829 | + | |
2803 | 2830 | | |
2804 | | - | |
2805 | | - | |
2806 | | - | |
2807 | | - | |
2808 | | - | |
2809 | 2831 | | |
2810 | 2832 | | |
2811 | | - | |
| 2833 | + | |
2812 | 2834 | | |
2813 | 2835 | | |
2814 | 2836 | | |
| |||
0 commit comments