Skip to main content

Uint

Struct Uint 

Source
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>

Source

pub const LIMBS: usize = N

Number of 64-bit limbs.

Source

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.

Source

pub const ZERO: Self

Additive identity.

Source

pub const ONE: Self

Multiplicative identity.

Source

pub const MAX: Self

Largest representable value (all limbs set).

Source

pub const fn from_limbs(limbs: [u64; N]) -> Self

Constructs from raw little-endian limbs.

Source

pub const fn as_limbs(&self) -> &[u64; N]

Borrows the raw little-endian limbs.

Source

pub fn wrapping_add(self, rhs: Self) -> Self

Wrapping addition (modulo 2^BITS).

Source

pub fn wrapping_sub(self, rhs: Self) -> Self

Wrapping subtraction (modulo 2^BITS).

Source

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.

Source

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 of a general multiply.

Source

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.

Source

pub fn checked_add(self, rhs: Self) -> Option<Self>

Checked addition: None on overflow past 2^BITS.

Source

pub fn checked_sub(self, rhs: Self) -> Option<Self>

Checked subtraction: None if the result would be negative.

Source

pub fn checked_mul(self, rhs: Self) -> Option<Self>

Checked multiplication: None if the true product does not fit 2^BITS.

Source

pub fn wrapping_div(self, rhs: Self) -> Self

Wrapping division. Panics on a zero divisor, matching the behaviour of the primitive unsigned integer types.

Source

pub fn wrapping_rem(self, rhs: Self) -> Self

Wrapping remainder. Panics on a zero divisor, matching the behaviour of the primitive unsigned integer types.

Source

pub fn bitand(self, rhs: Self) -> Self

Bitwise AND.

Source

pub fn bitor(self, rhs: Self) -> Self

Bitwise OR.

Source

pub fn bitxor(self, rhs: Self) -> Self

Bitwise XOR.

Source

pub fn not(self) -> Self

Bitwise NOT (ones’ complement).

Source

pub fn shl(self, shift: u32) -> Self

Logical left shift by shift bits (modulo 2^BITS).

Source

pub fn shr(self, shift: u32) -> Self

Logical right shift by shift bits.

Source

pub fn is_zero(&self) -> bool

true when every limb is zero.

Source

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.

Source

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.

Source

pub fn is_one(&self) -> bool

true when the value equals one.

Source

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).

Source

pub fn checked_pow(self, exp: u32) -> Option<Self>

Exponentiation by squaring, returning None if the true power overflows 2^BITS. self^0 == 1.

Source

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.

Source

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.

Source

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.

Source

pub fn cube(self) -> Self

Integer cube: self³ modulo 2^BITS. Routes through the cube policy (cube_dispatch): sqr-then-multiply at every N.

Source

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.

Source

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.

Source

pub const fn cast_signed(self) -> Int<N>

Reinterprets the bit pattern as the signed sibling.

Source

pub fn as_f64(self) -> f64

Approximate f64 value (positive; truncated toward zero on overflow).

Source

pub const fn count_ones(self) -> u32

Set-bit count across all limbs, matching the primitive uN::count_ones contract.

Source

pub const fn is_power_of_two(self) -> bool

true when exactly one bit is set.

Source

pub fn next_power_of_two(self) -> Self

Smallest power of two >= self (1 for zero), wrapping on overflow.

Source

pub const fn from_str_radix(s: &str, radix: u32) -> Result<Self, ()>

Parses an unsigned decimal string. Only base 10 is supported.

Source§

impl<const N: usize> Uint<N>

Source

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.

Source

pub fn widen<const M: usize>(self) -> Uint<M>

Widens to a wider Uint<M> (M >= N), zero-extending the new high limbs. Lossless.

Source

pub fn narrow<const M: usize>(self) -> Option<Uint<M>>

Narrows to a narrower Uint<M> (M <= N). Returns None if any discarded high limb is non-zero (the value does not fit M).

Trait Implementations§

Source§

impl<const N: usize> Add for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl<const N: usize> BitAnd for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self

Performs the & operation. Read more
Source§

impl<const N: usize> BitOr for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self

Performs the | operation. Read more
Source§

impl<const N: usize> BitXor for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self

Performs the ^ operation. Read more
Source§

impl<const N: usize> Clone for Uint<N>

Source§

fn clone(&self) -> Uint<N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Copy for Uint<N>

Source§

impl<const N: usize> Debug for Uint<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Display for Uint<N>
where Limbs<N>: ComputeLimbs,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Div for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self

Performs the / operation. Read more
Source§

impl<const N: usize> Eq for Uint<N>

Source§

impl<const N: usize> From<u8> for Uint<N>

Source§

fn from(v: u8) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<u16> for Uint<N>

Source§

fn from(v: u16) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<u32> for Uint<N>

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<u64> for Uint<N>

Source§

fn from(v: u64) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> Hash for Uint<N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: usize> Mul for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
Source§

impl<const N: usize> Not for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self

Performs the unary ! operation. Read more
Source§

impl<const N: usize> Ord for Uint<N>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const N: usize> PartialEq for Uint<N>

Source§

fn eq(&self, other: &Uint<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> PartialOrd for Uint<N>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const N: usize> Rem for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Self) -> Self

Performs the % operation. Read more
Source§

impl<const N: usize> Shl<u32> for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the << operator.
Source§

fn shl(self, shift: u32) -> Self

Performs the << operation. Read more
Source§

impl<const N: usize> Shr<u32> for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the >> operator.
Source§

fn shr(self, shift: u32) -> Self

Performs the >> operation. Read more
Source§

impl<const N: usize> StructuralPartialEq for Uint<N>

Source§

impl<const N: usize> Sub for Uint<N>

Source§

type Output = Uint<N>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl<const N: usize> TryFrom<u128> for Uint<N>

Source§

type Error = ConvertError

The type returned in the event of a conversion error.
Source§

fn try_from(v: u128) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Uint<N>

§

impl<const N: usize> RefUnwindSafe for Uint<N>

§

impl<const N: usize> Send for Uint<N>

§

impl<const N: usize> Sync for Uint<N>

§

impl<const N: usize> Unpin for Uint<N>

§

impl<const N: usize> UnsafeUnpin for Uint<N>

§

impl<const N: usize> UnwindSafe for Uint<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.