Skip to content

Commit

Permalink
Add SIMD to add_shift_none
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyBF committed Jul 4, 2022
1 parent 97fdd77 commit 84965b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
15 changes: 14 additions & 1 deletion ext/crates/fp/src/limb.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::Range;

pub(crate) use crate::constants::Limb;
use crate::{constants::BITS_PER_LIMB, prime::ValidPrime};
use crate::{constants::BITS_PER_LIMB, prime::ValidPrime, simd};

/// A struct containing the information required to access a specific entry in an array of `Limb`s.
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -231,6 +231,19 @@ pub(crate) const fn add<const P: u32>(limb_a: Limb, limb_b: Limb, coeff: u32) ->
}
}

/// Add (`c` times) all of the limbs in `rhs` to the limbs in `lhs`. This is optimized to use SIMD
/// when `P == 2`.
pub(crate) fn add_all<const P: u32>(lhs: &mut [Limb], rhs: &[Limb], c: u32) {
if P == 2 {
simd::add_simd(lhs, rhs, 0);
} else {
for (left, right) in lhs.iter_mut().zip(rhs) {
*left = add::<P>(*left, *right, c);
*left = reduce::<P>(*left);
}
}
}

/// Return the `Limb` whose entries are the entries of `limb` reduced modulo `P`.
///
/// Contributed by Robert Burklund.
Expand Down
12 changes: 5 additions & 7 deletions ext/crates/fp/src/vector/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,11 @@ pub trait InternalBaseVectorMutP<const P: u32>: InternalBaseVectorP<P> {
let target_inner_range = self._len().limb_range_inner();
let source_inner_range = other._len().limb_range_inner();
if !source_inner_range.is_empty() {
for (left, right) in self._limbs_mut()[target_inner_range]
.iter_mut()
.zip(&other_limbs[source_inner_range])
{
*left = limb::add::<P>(*left, *right, c);
*left = limb::reduce::<P>(*left);
}
limb::add_all::<P>(
&mut self._limbs_mut()[target_inner_range],
&other_limbs[source_inner_range],
c,
);
}
if source_range.len() > 1 {
// The first and last limbs are distinct, so we process the last.
Expand Down

0 comments on commit 84965b5

Please sign in to comment.