Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Specialize `Ord::cmp`, `const_eq`, `const_is_zero` for small sizes ([#561])

[#561]: https://github.com/recmo/uint/pull/561

## [1.17.2] - 2025-12-28

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
pub const fn leading_zeros(&self) -> usize {
let fixed = Self::MASK.leading_zeros() as usize;

as_primitives!(self, {
as_primitives!(self; {
u64(x) => return x.leading_zeros() as usize - fixed,
u128(x) => return x.leading_zeros() as usize - fixed,
u256(lo, hi) => return (select_unpredictable_u32(hi != 0,
u256((lo, hi)) => return (select_unpredictable_u32(hi != 0,
hi.leading_zeros(),
lo.leading_zeros() + 128
)) as usize - fixed,
Expand Down
12 changes: 12 additions & 0 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ impl<const BITS: usize, const LIMBS: usize> PartialOrd for Uint<BITS, LIMBS> {
impl<const BITS: usize, const LIMBS: usize> Ord for Uint<BITS, LIMBS> {
#[inline]
fn cmp(&self, rhs: &Self) -> Ordering {
as_primitives!(self, rhs; {
u64(x, y) => return x.cmp(&y),
u128(x, y) => return x.cmp(&y),
});
crate::algorithms::cmp(self.as_limbs(), rhs.as_limbs())
}
}
Expand Down Expand Up @@ -82,6 +86,10 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[inline]
#[must_use]
pub const fn const_is_zero(&self) -> bool {
as_primitives!(self; {
u64(x) => return x == 0,
u128(x) => return x == 0,
});
self.const_eq(&Self::ZERO)
}

Expand All @@ -92,6 +100,10 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
#[inline]
#[must_use]
pub const fn const_eq(&self, other: &Self) -> bool {
as_primitives!(self, other; {
u64(x, y) => return x == y,
u128(x, y) => return x == y,
});
// TODO: Replace with `self == other` and deprecate once `PartialEq` is const.
let a = self.as_limbs();
let b = other.as_limbs();
Expand Down
2 changes: 1 addition & 1 deletion src/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
/// Convert to IEEE 754 double precision float bit representation.
#[inline]
fn as_f64_bits(&self) -> u64 {
as_primitives!(self, {
as_primitives!(self; {
u64(x) => return f64::to_bits(x as f64),
});

Expand Down
30 changes: 20 additions & 10 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,39 @@ macro_rules! let_double_bits {

/// Specialize an operation for u64, u128, u256 ([u128; 2])...
macro_rules! as_primitives {
($uint:expr, { $($arm:ident $t:tt => $e:expr),* $(,)? }) => {
($($uints:expr),* $(,)?; $($rest:tt)*) => {
as_primitives!(@inner ($($uints),*); $($rest)*);
};

(@inner $uints:tt; { $($arm:ident $t:tt => $e:expr),* $(,)? }) => {
$(
as_primitives!(@arm $uint; $arm $t => $e);
as_primitives!(@arm $uints; $arm $t => $e);
)*
};

(@arm $uint:expr; u64($n:ident) => $e:expr) => {
(@arm ($($uint:expr),*); u64($($n:pat),*) => $e:expr) => {
if LIMBS == 1 {
let $n = $uint.limbs[0];
$(
let $n = $uint.limbs[0];
)*
$e
}
};
(@arm $uint:expr; u128($n:ident) => $e:expr) => {
(@arm ($($uint:expr),*); u128($($n:pat),*) => $e:expr) => {
if LIMBS == 2 {
let $n = $uint.as_double_words()[0].get();
$(
let $n = $uint.as_double_words()[0].get();
)*
$e
}
};
(@arm $uint:expr; u256($lo:ident, $hi:ident) => $e:expr) => {
(@arm ($($uint:expr),*); u256($(($lo:pat, $hi:pat)),*) => $e:expr) => {
if LIMBS == 4 {
let &[lo, hi] = $uint.as_double_words() else { unreachable!() };
let $lo = lo.get();
let $hi = hi.get();
$(
let &[lo, hi] = $uint.as_double_words() else { unreachable!() };
let $lo = lo.get();
let $hi = hi.get();
)*
$e
}
};
Expand Down
Loading