Skip to content

Commit bb80eb0

Browse files
authored
Add #[inline] to mul, sub, inversion and xgcd ops (#1246)
Follow-up to #1229, continuing the inlining work from #981. This adds `#[inline]` to `mul`, `sub`, `inversion`, and `xgcd` where benchmarks showed clear wins. I also tried inlining `mul_mod_special`, but it ended up being significantly slower (~2×), so I left it as-is. ## Benchmarks Criterion, 100 samples. Showing deltas outside noise: | Benchmark | Change | | --- | --- | | `sub`, I512 | **-19.2%** | | `xgcd/256` | **-3.9%** | | `xgcd/3` | **-3.6%** | | `concatenating_mul`, I128 | **-3.4%** | | `sub`, I4096 | **-3.4%** | | `xgcd/64` | **-3.1%** | | `wrapping_square`, U256 | **-1.7%** | | `wrapping_mul`, U256 | **-1.5%** | | `wrapping_mul`, I4096 | **-1.5%** | | `xgcd/16` | **-1.5%** | No regressions outside noise.
1 parent 4cfae53 commit bb80eb0

File tree

4 files changed

+6
-0
lines changed

4 files changed

+6
-0
lines changed

src/int/mul.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
4949
}
5050

5151
/// Multiply `self` by `rhs`, returning a concatenated "wide" result.
52+
#[inline]
5253
#[must_use]
5354
pub const fn concatenating_mul<const RHS_LIMBS: usize, const WIDE_LIMBS: usize>(
5455
&self,

src/uint/gcd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ impl<const LIMBS: usize> OddUint<LIMBS> {
221221
/// Execute the Extended GCD algorithm.
222222
///
223223
/// Given `(self, rhs)`, computes `(g, x, y)` s.t. `self * x + rhs * y = g = gcd(self, rhs)`.
224+
#[inline]
224225
#[must_use]
225226
pub const fn xgcd(&self, rhs: &Self) -> OddUintXgcdOutput<LIMBS> {
226227
OddUintXgcdOutput::from_pattern_output(self.binxgcd_odd(rhs))

src/uint/invert_mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
105105
///
106106
/// If the inverse does not exist (`k > 0` and `self` is even, or `k > Self::BITS`),
107107
/// returns `CtOption::none`, otherwise returns `CtOption::some`.
108+
#[inline]
108109
#[must_use]
109110
pub const fn invert_mod2k_vartime(&self, k: u32) -> CtOption<Self> {
110111
if k == 0 {
@@ -132,6 +133,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
132133
///
133134
/// If the inverse does not exist (`k > 0` and `self` is even, or `k > Self::BITS`),
134135
/// returns `CtOption::none`, otherwise returns `CtOption::some`.
136+
#[inline]
135137
#[must_use]
136138
pub const fn invert_mod2k(&self, k: u32) -> CtOption<Self> {
137139
let (odd, is_odd) = self.to_odd_or_one();

src/uint/mul.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub(crate) mod schoolbook;
1010

1111
impl<const LIMBS: usize> Uint<LIMBS> {
1212
/// Multiply `self` by `rhs`, returning a concatenated "wide" result.
13+
#[inline]
1314
#[must_use]
1415
pub const fn concatenating_mul<const RHS_LIMBS: usize, const WIDE_LIMBS: usize>(
1516
&self,
@@ -121,6 +122,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
121122
}
122123

123124
/// Square self, returning a concatenated "wide" result.
125+
#[inline]
124126
#[must_use]
125127
pub const fn concatenating_square<const WIDE_LIMBS: usize>(&self) -> Uint<WIDE_LIMBS>
126128
where

0 commit comments

Comments
 (0)