mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-09-15 03:26:18 +00:00
factor: Get rid of the custom OverflowingAdd
trait (#1697)
* factor: Confine knowledge of num_traits to numeric::traits This should make it easier to deal with API changes of num_traits, or eventually switching to an abstraction provided in the stdlib.
This commit is contained in:
parent
4eb6edaf52
commit
42b048d316
4 changed files with 10 additions and 25 deletions
|
@ -12,11 +12,11 @@ categories = ["command-line-utilities"]
|
|||
edition = "2018"
|
||||
|
||||
[build-dependencies]
|
||||
num-traits = "0.2" # used in src/numerics.rs, which is included by build.rs
|
||||
num-traits = "0.2.13" # used in src/numerics.rs, which is included by build.rs
|
||||
|
||||
|
||||
[dependencies]
|
||||
num-traits = "0.2"
|
||||
num-traits = "0.2.13" # Needs at least version 0.2.13 for "OverflowingAdd"
|
||||
rand = { version="0.7", features=["small_rng"] }
|
||||
smallvec = { version="0.6.13, < 1.0" }
|
||||
uucore = { version=">=0.0.5", package="uucore", path="../../uucore" }
|
||||
|
|
|
@ -29,6 +29,7 @@ use miller_rabin::is_prime;
|
|||
|
||||
#[path = "src/numeric/modular_inverse.rs"]
|
||||
mod modular_inverse;
|
||||
#[allow(unused_imports)] // imports there are used, but invisible from build.rs
|
||||
#[path = "src/numeric/traits.rs"]
|
||||
mod traits;
|
||||
use modular_inverse::modular_inverse;
|
||||
|
|
|
@ -8,8 +8,7 @@
|
|||
|
||||
use super::*;
|
||||
|
||||
use num_traits::identities::{One, Zero};
|
||||
use traits::{DoubleInt, Int, OverflowingAdd};
|
||||
use traits::{DoubleInt, Int, One, OverflowingAdd, Zero};
|
||||
|
||||
pub(crate) trait Arithmetic: Copy + Sized {
|
||||
// The type of integers mod m, in some opaque representation
|
||||
|
@ -74,7 +73,7 @@ impl<T: DoubleInt> Montgomery<T> {
|
|||
let Montgomery { a, n } = self;
|
||||
let m = T::from_double_width(x).wrapping_mul(a);
|
||||
let nm = (n.as_double_width()) * (m.as_double_width());
|
||||
let (xnm, overflow) = x.overflowing_add_(nm); // x + n*m
|
||||
let (xnm, overflow) = x.overflowing_add(&nm); // x + n*m
|
||||
debug_assert_eq!(
|
||||
xnm % (T::DoubleWidth::one() << T::zero().count_zeros() as usize),
|
||||
T::DoubleWidth::zero()
|
||||
|
@ -130,7 +129,7 @@ impl<T: DoubleInt> Arithmetic for Montgomery<T> {
|
|||
}
|
||||
|
||||
fn add(&self, a: Self::ModInt, b: Self::ModInt) -> Self::ModInt {
|
||||
let (r, overflow) = a.overflowing_add_(b);
|
||||
let (r, overflow) = a.overflowing_add(&b);
|
||||
|
||||
// In case of overflow, a+b = 2⁶⁴ + r = (2⁶⁴ - n) + r (working mod n)
|
||||
let r = if !overflow {
|
||||
|
|
|
@ -6,31 +6,16 @@
|
|||
// * For the full copyright and license information, please view the LICENSE file
|
||||
// * that was distributed with this source code.
|
||||
|
||||
pub(crate) use num_traits::{
|
||||
identities::{One, Zero},
|
||||
ops::overflowing::OverflowingAdd,
|
||||
};
|
||||
use num_traits::{
|
||||
int::PrimInt,
|
||||
ops::wrapping::{WrappingMul, WrappingNeg, WrappingSub},
|
||||
};
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
// NOTE: Trait can be removed once num-traits adds a similar one;
|
||||
// see https://github.com/rust-num/num-traits/issues/168
|
||||
pub(crate) trait OverflowingAdd: Sized {
|
||||
fn overflowing_add_(self, n: Self) -> (Self, bool);
|
||||
}
|
||||
|
||||
macro_rules! overflowing {
|
||||
($x:ty) => {
|
||||
impl OverflowingAdd for $x {
|
||||
fn overflowing_add_(self, n: Self) -> (Self, bool) {
|
||||
self.overflowing_add(n)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
overflowing!(u32);
|
||||
overflowing!(u64);
|
||||
overflowing!(u128);
|
||||
|
||||
pub(crate) trait Int:
|
||||
Display + Debug + PrimInt + OverflowingAdd + WrappingNeg + WrappingSub + WrappingMul
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue