pub struct Uint<const N: usize> { /* private fields */ }Expand description
Unsigned fixed-width integer of N little-endian 64-bit limbs.
Implementations§
Source§impl<const N: usize> Uint<N>
impl<const N: usize> Uint<N>
Sourcepub const BITS: u32
pub const BITS: u32
Bit width (LIMBS * 64). u32 so it composes directly with the
leading_zeros / count_ones u32 surface and matches the
historic named-type BITS constant.
Sourcepub const fn from_limbs(limbs: [u64; N]) -> Self
pub const fn from_limbs(limbs: [u64; N]) -> Self
Constructs from raw little-endian limbs.
Sourcepub fn wrapping_add(self, rhs: Self) -> Self
pub fn wrapping_add(self, rhs: Self) -> Self
Wrapping addition (modulo 2^BITS).
Sourcepub fn wrapping_sub(self, rhs: Self) -> Self
pub fn wrapping_sub(self, rhs: Self) -> Self
Wrapping subtraction (modulo 2^BITS).
Sourcepub fn wrapping_mul(self, rhs: Self) -> Self
pub fn wrapping_mul(self, rhs: Self) -> Self
Wrapping multiplication (modulo 2^BITS).
Schoolbook multiply truncated to the low N limbs. Only the
product limbs that land below 2^BITS are kept, so no
[u64; 2*N] output buffer is needed — the higher partial
products are simply never written. Carries that would land at or
above limb N are discarded, which is exactly the modular
reduction.
Sourcepub const fn wrapping_sqr(self) -> Self
pub const fn wrapping_sqr(self) -> Self
Wrapping square (self² modulo 2^BITS). Named entry point for
the open-coded x * x pattern. Thin delegator DOWN to the sqr
policy (sqr_dispatch): half-product squaring via the
sqr_low_fixed kernel — each cross term is formed once and doubled,
so the limb-multiply count is N(N+1)/2 rather than the N² of a
general multiply.
Sourcepub const fn wrapping_cube(self) -> Self
pub const fn wrapping_cube(self) -> Self
Wrapping cube (self³ modulo 2^BITS). Named entry point for the
open-coded x * x * x pattern. Thin delegator DOWN to the cube
policy (cube_dispatch): sqr then one multiply via the const
kernels — no cheaper form exists below two multiplies.
Sourcepub fn checked_add(self, rhs: Self) -> Option<Self>
pub fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition: None on overflow past 2^BITS.
Sourcepub fn checked_sub(self, rhs: Self) -> Option<Self>
pub fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction: None if the result would be negative.
Sourcepub fn checked_mul(self, rhs: Self) -> Option<Self>
pub fn checked_mul(self, rhs: Self) -> Option<Self>
Checked multiplication: None if the true product does not fit
2^BITS.
Sourcepub fn wrapping_div(self, rhs: Self) -> Self
pub fn wrapping_div(self, rhs: Self) -> Self
Wrapping division. Panics on a zero divisor, matching the behaviour of the primitive unsigned integer types.
Sourcepub fn wrapping_rem(self, rhs: Self) -> Self
pub fn wrapping_rem(self, rhs: Self) -> Self
Wrapping remainder. Panics on a zero divisor, matching the behaviour of the primitive unsigned integer types.
Sourcepub const fn bit_length(&self) -> u32
pub const fn bit_length(&self) -> u32
Bit length (significant bits): 0 for zero, else
floor(log2(self)) + 1, equivalently BITS - leading_zeros.
For an unsigned value there is no sign, so magnitude and stored
representation coincide — bit_length and the uN-contract
bit-count methods below agree by construction.
Sourcepub const fn leading_zeros(&self) -> u32
pub const fn leading_zeros(&self) -> u32
Number of leading zero bits in the BITS-wide representation,
matching the primitive uN::leading_zeros contract:
BITS - bit_length, and BITS for zero.
Sourcepub const fn wrapping_pow(self, exp: u32) -> Self
pub const fn wrapping_pow(self, exp: u32) -> Self
Wrapping exponentiation by squaring (self^exp modulo 2^BITS).
self^0 == 1. Thin delegator DOWN to the pow policy
(pow_dispatch): binary square-and-multiply over the const
kernels; optimal for the small fixed exponents the root iteration
needs (k-1, k).
Sourcepub fn checked_pow(self, exp: u32) -> Option<Self>
pub fn checked_pow(self, exp: u32) -> Option<Self>
Exponentiation by squaring, returning None if the true power
overflows 2^BITS. self^0 == 1.
Sourcepub fn pow(self, exp: u32) -> Self
pub fn pow(self, exp: u32) -> Self
Exponentiation by squaring. Routes through the pow policy
(pow_dispatch): binary square-and-multiply at every N.
Result is self^exp modulo 2^BITS; self^0 == 1.
Sourcepub fn isqrt(self) -> Self
pub fn isqrt(self) -> Self
Integer square root: the largest r with r² <= self.
Routes through the isqrt policy (isqrt_dispatch):
N ∈ {1, 2} takes the hardware native path; N >= 3 takes
the Newton limb kernel.
Sourcepub fn sqr(self) -> Self
pub fn sqr(self) -> Self
Integer square: self² modulo 2^BITS. Routes through the sqr
policy (sqr_dispatch): half-product squaring kernel at every N.
Sourcepub fn cube(self) -> Self
pub fn cube(self) -> Self
Integer cube: self³ modulo 2^BITS. Routes through the cube
policy (cube_dispatch): sqr-then-multiply at every N.
Sourcepub fn icbrt(self) -> Self
pub fn icbrt(self) -> Self
Integer cube root: floor(self^(1/3)). Routes through the icbrt
policy (icbrt_dispatch):
N ∈ {1, 2} takes the narrow path; N >= 3 takes the Newton
limb kernel with an f64::cbrt seed.
Sourcepub fn root_int(self, k: u32) -> (Self, bool)
pub fn root_int(self, k: u32) -> (Self, bool)
Integer kth root: returns (root, exact) where
root = floor(self^(1/k)) and exact is true iff
root^k == self. k must be >= 1.
Brent–Zimmermann RootInt (Modern Computer Arithmetic §1.5.2): the
integer projection of Newton’s iteration
u = ((k-1)·s + m / s^(k-1)) / k, started from an upper bound on
the root and run until the monotone-decreasing sequence first
fails to decrease (u >= s), at which point s is the floor
root. The seed is the no_std-safe bit-length bound
2^ceil(bit_length / k) — a clean upper bound since
(2^ceil(L/k))^k >= 2^L > m. k == 2 reuses the dedicated
Self::isqrt; k == 3 is the cube root.
Sourcepub const fn cast_signed(self) -> Int<N>
pub const fn cast_signed(self) -> Int<N>
Reinterprets the bit pattern as the signed sibling.
Sourcepub fn as_f64(self) -> f64
pub fn as_f64(self) -> f64
Approximate f64 value (positive; truncated toward zero on
overflow).
Sourcepub const fn count_ones(self) -> u32
pub const fn count_ones(self) -> u32
Set-bit count across all limbs, matching the primitive
uN::count_ones contract.
Sourcepub const fn is_power_of_two(self) -> bool
pub const fn is_power_of_two(self) -> bool
true when exactly one bit is set.
Sourcepub fn next_power_of_two(self) -> Self
pub fn next_power_of_two(self) -> Self
Smallest power of two >= self (1 for zero), wrapping on
overflow.
Source§impl<const N: usize> Uint<N>
impl<const N: usize> Uint<N>
Sourcepub const fn resize_n<const M: usize>(self) -> Uint<M>
pub const fn resize_n<const M: usize>(self) -> Uint<M>
Resizes to M limbs: zero-extends when widening, drops the high
limbs when narrowing. Direction-agnostic and infallible.
Named resize_n (not resize) so the const-generic width bridge
does not collide with the type-generic Int::resize the named-
type API expects.