Skip to content

Commit bd389e2

Browse files
committed
Add binxgcd_; switches between the classic and optimized binxgcd algorithms.
1 parent 556b17b commit bd389e2

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/modular/bingcd/xgcd.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<const LIMBS: usize> OddUint<LIMBS> {
130130
let (abs_diff, rhs_gt_lhs) = lhs_.abs_diff(rhs_);
131131
let odd_rhs = Odd(Uint::select(rhs_, &abs_diff, rhs_is_even));
132132

133-
let mut output = self.classic_binxgcd(&odd_rhs).divide();
133+
let mut output = self.binxgcd_(&odd_rhs).divide();
134134
let matrix = &mut output.matrix;
135135

136136
// Modify the output to negate the transformation applied to the input.
@@ -144,6 +144,20 @@ impl<const LIMBS: usize> OddUint<LIMBS> {
144144
output
145145
}
146146

147+
/// Given `(self, rhs)`, computes `(g, x, y)` s.t. `self * x + rhs * y = g = gcd(self, rhs)`,
148+
/// leveraging the Binary Extended GCD algorithm.
149+
///
150+
/// This function switches between the "classic" and "optimized" algorithm at a best-effort
151+
/// threshold. When using [Uint]s with `LIMBS` close to the threshold, it may be useful to
152+
/// manually test whether the classic or optimized algorithm is faster for your machine.
153+
pub(crate) const fn binxgcd_(&self, rhs: &Self) -> DividedPatternXgcdOutput<LIMBS> {
154+
if LIMBS < 4 {
155+
self.classic_binxgcd(rhs)
156+
} else {
157+
self.optimized_binxgcd(rhs)
158+
}
159+
}
160+
147161
/// Execute the classic Binary Extended GCD algorithm.
148162
///
149163
/// Given `(self, rhs)`, computes `(g, x, y)` s.t. `self * x + rhs * y = g = gcd(self, rhs)`.

src/uint/gcd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<const LIMBS: usize> OddUint<LIMBS> {
188188
///
189189
/// Given `(self, rhs)`, computes `(g, x, y)` s.t. `self * x + rhs * y = g = gcd(self, rhs)`.
190190
pub const fn binxgcd(&self, rhs: &Self) -> OddUintXgcdOutput<LIMBS> {
191-
OddUintXgcdOutput::from_pattern_output(self.classic_binxgcd(rhs).divide())
191+
OddUintXgcdOutput::from_pattern_output(self.binxgcd_(rhs).divide())
192192
}
193193
}
194194

0 commit comments

Comments
 (0)