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

chore: loops clean up #326

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions src/algorithms/add.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
#![allow(dead_code)] // TODO

use super::ops::{adc, sbb};
use core::cmp::Ordering;

#[inline(always)]
#[must_use]
pub fn cmp(lhs: &[u64], rhs: &[u64]) -> Ordering {
debug_assert_eq!(lhs.len(), rhs.len());
for (l, r) in lhs.iter().rev().zip(rhs.iter().rev()) {
match l.cmp(r) {
Ordering::Equal => continue,
other => return other,
}
}
Ordering::Equal
}

/// `lhs += rhs + carry`
#[inline(always)]
pub fn adc_n(lhs: &mut [u64], rhs: &[u64], mut carry: u64) -> u64 {
for (l, r) in lhs.iter_mut().zip(rhs.iter()) {
let (result, new_carry) = adc(*l, *r, carry);
*l = result;
carry = new_carry;
for i in 0..lhs.len() {
(lhs[i], carry) = adc(lhs[i], rhs[i], carry);
}
carry
}

/// `lhs -= rhs + carry`
/// `lhs -= rhs - borrow`
#[inline(always)]
pub fn sbb_n(lhs: &mut [u64], rhs: &[u64], mut carry: u64) -> u64 {
for (l, r) in lhs.iter_mut().zip(rhs.iter()) {
let (result, new_carry) = sbb(*l, *r, carry);
*l = result;
carry = new_carry;
pub fn sbb_n(lhs: &mut [u64], rhs: &[u64], mut borrow: u64) -> u64 {
for i in 0..lhs.len() {
(lhs[i], borrow) = sbb(lhs[i], rhs[i], borrow);

Check warning on line 16 in src/algorithms/add.rs

View check run for this annotation

Codecov / codecov/patch

src/algorithms/add.rs#L14-L16

Added lines #L14 - L16 were not covered by tests
}
carry
borrow

Check warning on line 18 in src/algorithms/add.rs

View check run for this annotation

Codecov / codecov/patch

src/algorithms/add.rs#L18

Added line #L18 was not covered by tests
}
5 changes: 1 addition & 4 deletions src/algorithms/div/knuth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,7 @@ pub fn div_nxm(numerator: &mut [u64], divisor: &mut [u64]) {
#[cfg(test)]
mod tests {
use super::*;
use crate::algorithms::{
add::{cmp, sbb_n},
addmul,
};
use crate::algorithms::{addmul, cmp, sbb_n};
use alloc::vec::Vec;
use core::cmp::Ordering;
use proptest::{
Expand Down
1 change: 0 additions & 1 deletion src/algorithms/div/small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#![allow(clippy::many_single_char_names, clippy::similar_names)]
// Truncation is intentional
#![allow(clippy::cast_possible_truncation)]
#![allow(dead_code)] // TODO

use super::reciprocal::{reciprocal, reciprocal_2};
use crate::{algorithms::DoubleWord, utils::unlikely};
Expand Down
33 changes: 32 additions & 1 deletion src/algorithms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#![allow(missing_docs)] // TODO: document algorithms

use core::cmp::Ordering;

mod add;
pub mod div;
mod gcd;
Expand All @@ -15,7 +17,7 @@
mod shift;

pub use self::{
add::{adc_n, cmp, sbb_n},
add::{adc_n, sbb_n},
div::div,
gcd::{gcd, gcd_extended, inv_mod, LehmerMatrix},
mul::{add_nx1, addmul, addmul_n, addmul_nx1, addmul_ref, mul_nx1, submul_nx1},
Expand Down Expand Up @@ -85,3 +87,32 @@
(self.low(), self.high())
}
}

/// Compare two `u64` slices in reverse order.
#[inline(always)]
#[must_use]
pub fn cmp(left: &[u64], right: &[u64]) -> Ordering {
let l = core::cmp::min(left.len(), right.len());

// Slice to the loop iteration range to enable bound check
// elimination in the compiler
let lhs = &left[..l];
let rhs = &right[..l];

for i in (0..l).rev() {
match i8::from(lhs[i] > rhs[i]) - i8::from(lhs[i] < rhs[i]) {
-1 => return Ordering::Less,
0 => {}
1 => return Ordering::Greater,
_ => unsafe { core::hint::unreachable_unchecked() },

Check warning on line 107 in src/algorithms/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/algorithms/mod.rs#L107

Added line #L107 was not covered by tests
}

// Equivalent to:
// match lhs[i].cmp(&rhs[i]) {
// Ordering::Equal => {}
// non_eq => return non_eq,
// }
}

left.len().cmp(&right.len())
}
9 changes: 7 additions & 2 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,18 @@ macro_rules! impl_bit_op {
self.$fn_assign(&rhs);
}
}

impl<const BITS: usize, const LIMBS: usize> $trait_assign<&Uint<BITS, LIMBS>>
for Uint<BITS, LIMBS>
{
#[inline]
fn $fn_assign(&mut self, rhs: &Uint<BITS, LIMBS>) {
for (limb, &rhs) in self.limbs.iter_mut().zip(rhs.as_limbs()) {
u64::$fn_assign(limb, rhs);
for i in 0..LIMBS {
u64::$fn_assign(&mut self.limbs[i], rhs.limbs[i]);
}
}
}

impl<const BITS: usize, const LIMBS: usize> $trait<Uint<BITS, LIMBS>>
for Uint<BITS, LIMBS>
{
Expand All @@ -504,6 +506,7 @@ macro_rules! impl_bit_op {
self
}
}

impl<const BITS: usize, const LIMBS: usize> $trait<&Uint<BITS, LIMBS>>
for Uint<BITS, LIMBS>
{
Expand All @@ -515,6 +518,7 @@ macro_rules! impl_bit_op {
self
}
}

impl<const BITS: usize, const LIMBS: usize> $trait<Uint<BITS, LIMBS>>
for &Uint<BITS, LIMBS>
{
Expand All @@ -526,6 +530,7 @@ macro_rules! impl_bit_op {
rhs
}
}

impl<const BITS: usize, const LIMBS: usize> $trait<&Uint<BITS, LIMBS>>
for &Uint<BITS, LIMBS>
{
Expand Down
9 changes: 6 additions & 3 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[cfg(feature = "alloc")]
#[must_use]
#[inline]
pub const fn as_le_bytes(&self) -> Cow<'_, [u8]> {
#[allow(clippy::missing_const_for_fn)]
pub fn as_le_bytes(&self) -> Cow<'_, [u8]> {
// On little endian platforms this is a no-op.
#[cfg(target_endian = "little")]
return Cow::Borrowed(self.as_le_slice());
Expand All @@ -57,8 +58,10 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[cfg(target_endian = "big")]
return Cow::Owned({
let mut cpy = *self;
cpy.limbs.iter_mut().for_each(|limb| limb.reverse_bits());
slice::from_raw_parts(cpy.limbs.as_ptr().cast(), Self::BYTES).to_vec()
Comment on lines -60 to -61
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't compile, also can't make the function const.

for limb in &mut cpy.limbs {
*limb = limb.reverse_bits();
}
unsafe { slice::from_raw_parts(cpy.limbs.as_ptr().cast(), Self::BYTES).to_vec() }
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ pub(crate) const fn rem_up(a: usize, b: usize) -> usize {
}
}

#[allow(dead_code)]
#[allow(dead_code)] // This is used by some support features.
#[inline]
fn last_idx<T: PartialEq>(x: &[T], value: &T) -> usize {
x.iter().rposition(|b| b != value).map_or(0, |idx| idx + 1)
}

#[allow(dead_code)]
#[allow(dead_code)] // This is used by some support features.
#[inline]
#[must_use]
pub(crate) fn trim_end_slice<'a, T: PartialEq>(slice: &'a [T], value: &T) -> &'a [T] {
Expand Down