Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yet another vector rewrite #53

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Implement add_truncate for FpVector
JoeyBF committed Jul 4, 2022
commit 5039b4cc225df6cc667fa5480a3bf0307e6f7f27
9 changes: 0 additions & 9 deletions ext/crates/fp/src/limb.rs
Original file line number Diff line number Diff line change
@@ -265,10 +265,6 @@ pub(crate) fn reduce<const P: u32>(limb: Limb) -> Limb {

/// Check whether or not a limb is reduced, i.e. whether every entry is a value in the range `0..P`.
/// This is currently **not** faster than calling [`reduce`] directly.
///
/// This function is currently only called in [`truncate`], which is itself never called. See there
/// for more details.
#[allow(dead_code)]
pub(crate) fn is_reduced<const P: u32>(limb: Limb) -> bool {
limb == reduce::<P>(limb)
}
@@ -339,11 +335,6 @@ pub(crate) const fn sign_rule(mut target: Limb, mut source: Limb) -> u32 {
}

/// Return either `Some(sum)` if no carries happen in the limb, or `None` if some carry does happen.
///
/// This function is currently never used. We will presumably use it when we implement
/// [`InternalBaseVectorMutP::_add_truncate`](crate::vector::internal::InternalBaseVectorMutP::_add_truncate).
/// See there for more details.
#[allow(dead_code)]
pub(crate) fn truncate<const P: u32>(sum: Limb) -> Option<Limb> {
if is_reduced::<P>(sum) {
Some(sum)
13 changes: 0 additions & 13 deletions ext/crates/fp/src/vector/generic.rs
Original file line number Diff line number Diff line change
@@ -84,19 +84,6 @@ impl<const P: u32> FpVectorP<P> {
self.limbs.resize(self.len.limbs(), 0);
}

/// This replaces the contents of the vector with the contents of the slice. The two must have
/// the same length.
pub fn copy_from_slice(&mut self, slice: &[u32]) {
assert_eq!(self.len(), slice.len());

self.limbs.clear();
self.limbs.extend(
slice
.chunks(limb::entries_per_limb_const::<P>())
.map(|x| limb::pack::<_, P>(x.iter().copied())),
);
}

/// Permanently remove the first `n` elements in the vector. `n` must be a multiple of
/// the number of entries per limb
pub(crate) fn trim_start(&mut self, n: usize) {
24 changes: 24 additions & 0 deletions ext/crates/fp/src/vector/internal/impl_internal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use itertools::Itertools;

use super::{InternalBaseVectorMutP, InternalBaseVectorP};
use crate::{
limb::{self, Limb, LimbLength},
@@ -138,6 +140,28 @@ impl<const P: u32> InternalBaseVectorMutP<P> for FpVectorP<P> {
fn _limbs_mut(&mut self) -> &mut [Limb] {
&mut self.limbs
}

fn _copy_from_slice(&mut self, slice: &[u32]) {
assert_eq!(self._len().logical(), slice.len());

self.limbs.clear();
self.limbs.extend(
slice
.chunks(limb::entries_per_limb_const::<P>())
.map(|x| limb::pack::<_, P>(x.iter().copied())),
);
}

fn _add_truncate<T: InternalBaseVectorP<P>>(&mut self, other: T, c: u32) -> Option<()> {
// We require `other` to start on a limb boundary. In practice we only ever call this
// function with `other: FpVectorP`, which satisfies this condition by definition.
debug_assert_eq!(other._len().start, 0);
for (left, right) in self.limbs.iter_mut().zip_eq(other._limbs()) {
*left = limb::add::<P>(*left, *right, c);
*left = limb::truncate::<P>(*left)?;
}
Some(())
}
}

impl<'a, const P: u32> InternalBaseVectorP<P> for SliceP<'a, P> {
10 changes: 6 additions & 4 deletions ext/crates/fp/src/vector/internal/mod.rs
Original file line number Diff line number Diff line change
@@ -671,15 +671,17 @@ pub trait InternalBaseVectorMutP<const P: u32>: InternalBaseVectorP<P> {
self._limbs_mut()[target_range.end - 1] |= result;
}

/// This replaces the contents of the vector with the contents of the slice. The two must have
/// the same length.
///
/// This method is only implemented on `FpVectorP` right now. This is the only use case so far,
/// so I don't feel too bad about marking them as unimplemented.
/// so I don't feel too bad about marking it as unimplemented in the general case.
fn _copy_from_slice(&mut self, _slice: &[u32]) {
unimplemented!();
}

/// This function is currently only used in [`algebra::PolynomialAlgebra::multiply_monomials`],
/// on [`FpVectorP`]. We mostly care about the Steenrod algebra, which doesn't use this code, so
/// I'm marking it as unimplemented for now.
/// This method is only implemented on `FpVectorP` right now. This is the only use case so far,
/// so I don't feel too bad about marking it as unimplemented in the general case.
fn _add_truncate<T: InternalBaseVectorP<P>>(&mut self, _other: T, _c: u32) -> Option<()> {
unimplemented!();
}