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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::Uint;
use core::cmp::{min, Ordering};
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[must_use]
pub fn root(self, degree: usize) -> Self {
assert!(degree > 0, "degree must be greater than zero");
if self == Self::ZERO {
return Self::ZERO;
}
if degree >= Self::BITS {
return Self::from(1);
}
if degree == 1 {
return self;
}
#[allow(clippy::cast_precision_loss)]
#[allow(clippy::cast_sign_loss)]
let mut result = Self::approx_pow2(self.approx_log2() / degree as f64).unwrap();
let mut decreasing = false;
loop {
let division = result
.checked_pow(degree - 1)
.map_or(Self::ZERO, |power| self / power);
let iter = (division + Self::from(degree - 1) * result) / Self::from(degree);
match (decreasing, iter.cmp(&result)) {
(_, Ordering::Equal) | (true, Ordering::Greater) => break result,
(false, Ordering::Greater) => result = min(iter, result.saturating_shl(1)),
(_, Ordering::Less) => {
decreasing = true;
result = iter;
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{const_for, nlimbs};
use proptest::proptest;
#[test]
#[allow(clippy::absurd_extreme_comparisons)]
fn test_root() {
const_for!(BITS in SIZES if (BITS > 3) {
const LIMBS: usize = nlimbs(BITS);
type U = Uint<BITS, LIMBS>;
proptest!(|(value: U, degree in 1_usize..=5)| {
let root = value.root(degree);
let lower = root.pow(degree);
assert!(value >= lower);
let upper = root
.checked_add(U::from(1))
.and_then(|n| n.checked_pow(degree));
if let Some(upper) = upper {
assert!(value < upper);
}
});
});
}
#[test]
#[allow(clippy::absurd_extreme_comparisons)]
#[allow(clippy::reversed_empty_ranges)]
fn test_root_large() {
const_for!(BITS in SIZES if (BITS > 3) {
const LIMBS: usize = nlimbs(BITS);
type U = Uint<BITS, LIMBS>;
proptest!(|(value: U, degree in 1_usize..=BITS)| {
let root = value.root(degree);
let lower = root.pow(degree);
assert!(value >= lower);
let upper = root
.checked_add(U::from(1))
.and_then(|n| n.checked_pow(degree));
if let Some(upper) = upper {
assert!(value < upper);
}
});
});
}
}
#[cfg(feature = "bench")]
#[doc(hidden)]
pub mod bench {
use super::*;
use crate::{const_for, nlimbs};
use ::proptest::{
arbitrary::Arbitrary,
strategy::{Strategy, ValueTree},
test_runner::TestRunner,
};
use criterion::{black_box, BatchSize, Criterion};
pub fn group(criterion: &mut Criterion) {
const_for!(BITS in BENCH {
const LIMBS: usize = nlimbs(BITS);
bench_root::<BITS, LIMBS>(criterion, 2);
bench_root::<BITS, LIMBS>(criterion, 3);
bench_root::<BITS, LIMBS>(criterion, 5);
bench_root::<BITS, LIMBS>(criterion, 127);
});
}
fn bench_root<const BITS: usize, const LIMBS: usize>(criterion: &mut Criterion, degree: usize) {
let input = Uint::<BITS, LIMBS>::arbitrary();
let mut runner = TestRunner::deterministic();
criterion.bench_function(&format!("root/{}/{}", degree, BITS), move |bencher| {
bencher.iter_batched(
|| input.new_tree(&mut runner).unwrap().current(),
|value| black_box(black_box(value).root(black_box(degree))),
BatchSize::SmallInput,
);
});
}
}