#![doc = include_str!("../Readme.md")]
#![doc(issue_tracker_base_url = "https://github.com/recmo/uint/issues/")]
#![warn(clippy::all, clippy::pedantic, clippy::cargo, clippy::nursery)]
#![allow(
clippy::module_name_repetitions,
clippy::inline_always,
clippy::unreadable_literal,
clippy::doc_markdown // Unfortunately many false positives on Latex.
)]
#![cfg_attr(
any(test, feature = "bench"),
allow(clippy::wildcard_imports, clippy::cognitive_complexity)
)]
#![cfg_attr(
all(has_generic_const_exprs, feature = "generic_const_exprs"),
allow(incomplete_features)
)]
#![cfg_attr(
all(has_generic_const_exprs, feature = "generic_const_exprs"),
feature(generic_const_exprs)
)]
#![cfg_attr(coverage_nightly, feature(no_coverage))]
#![cfg_attr(has_doc_cfg, feature(doc_cfg))]
#![cfg_attr(has_core_intrinsics, feature(core_intrinsics))]
extern crate self as ruint;
mod add;
pub mod algorithms;
pub mod aliases;
mod base_convert;
mod bit_arr;
mod bits;
mod bytes;
mod cmp;
mod const_for;
mod div;
mod from;
mod gcd;
mod log;
mod modular;
mod mul;
mod pow;
mod root;
mod special;
mod string;
mod support;
mod uint_dyn;
mod utils;
#[cfg(all(feature = "dyn", feature = "unstable"))]
#[doc(inline)]
pub use uint_dyn::UintDyn;
#[doc(inline)]
pub use bit_arr::Bits;
#[doc(inline)]
pub use self::{
base_convert::BaseConvertError,
bytes::nbytes,
from::{FromUintError, ToFieldError, ToUintError, UintTryFrom, UintTryTo},
string::ParseError,
};
#[doc(inline)]
pub use ruint_macro::uint;
#[cfg(all(has_generic_const_exprs, feature = "generic_const_exprs"))]
pub mod nightly {
pub type Uint<const BITS: usize> = crate::Uint<BITS, { crate::nlimbs(BITS) }>;
pub type Bits<const BITS: usize> = crate::Bits<BITS, { crate::nlimbs(BITS) }>;
}
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct Uint<const BITS: usize, const LIMBS: usize> {
limbs: [u64; LIMBS],
}
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
pub const LIMBS: usize = nlimbs(BITS);
const MASK: u64 = mask(BITS);
pub const BITS: usize = BITS;
pub const MIN: Self = Self::ZERO;
pub const ZERO: Self = Self { limbs: [0; LIMBS] };
pub const MAX: Self = {
let mut limbs = [u64::MAX; LIMBS];
if BITS > 0 {
limbs[LIMBS - 1] &= Self::MASK;
}
Self { limbs }
};
#[must_use]
#[inline(always)]
pub const fn as_limbs(&self) -> &[u64; LIMBS] {
&self.limbs
}
#[must_use]
#[inline(always)]
pub unsafe fn as_limbs_mut(&mut self) -> &mut [u64; LIMBS] {
&mut self.limbs
}
#[must_use]
#[inline(always)]
pub const fn into_limbs(self) -> [u64; LIMBS] {
self.limbs
}
#[must_use]
#[track_caller]
#[inline(always)]
pub const fn from_limbs(limbs: [u64; LIMBS]) -> Self {
Self::assert_valid();
if BITS > 0 && Self::MASK < u64::MAX {
assert!(
limbs[Self::LIMBS - 1] <= Self::MASK,
"Value too large for this Uint"
);
}
Self { limbs }
}
#[must_use]
#[track_caller]
pub fn from_limbs_slice(slice: &[u64]) -> Self {
match Self::overflowing_from_limbs_slice(slice) {
(n, false) => n,
(_, true) => panic!("Value too large for this Uint"),
}
}
#[must_use]
pub fn checked_from_limbs_slice(slice: &[u64]) -> Option<Self> {
match Self::overflowing_from_limbs_slice(slice) {
(n, false) => Some(n),
(_, true) => None,
}
}
#[must_use]
pub fn wrapping_from_limbs_slice(slice: &[u64]) -> Self {
Self::overflowing_from_limbs_slice(slice).0
}
#[must_use]
pub fn overflowing_from_limbs_slice(slice: &[u64]) -> (Self, bool) {
Self::assert_valid();
if slice.len() < LIMBS {
let mut limbs = [0; LIMBS];
limbs[..slice.len()].copy_from_slice(slice);
(Self::from_limbs(limbs), false)
} else {
let (head, tail) = slice.split_at(LIMBS);
let mut limbs = [0; LIMBS];
limbs.copy_from_slice(head);
let mut overflow = tail.iter().any(|&limb| limb != 0);
if LIMBS > 0 {
overflow |= limbs[LIMBS - 1] > Self::MASK;
limbs[LIMBS - 1] &= Self::MASK;
}
(Self::from_limbs(limbs), overflow)
}
}
#[must_use]
pub fn saturating_from_limbs_slice(slice: &[u64]) -> Self {
match Self::overflowing_from_limbs_slice(slice) {
(n, false) => n,
(_, true) => Self::MAX,
}
}
#[inline(always)]
const fn assert_valid() {
#[allow(clippy::manual_assert)]
if LIMBS != Self::LIMBS {
panic!("Can not construct Uint<BITS, LIMBS> with incorrect LIMBS");
}
}
}
impl<const BITS: usize, const LIMBS: usize> Default for Uint<BITS, LIMBS> {
fn default() -> Self {
Self::ZERO
}
}
#[must_use]
pub const fn nlimbs(bits: usize) -> usize {
(bits + 63) / 64
}
#[must_use]
const fn mask(bits: usize) -> u64 {
if bits == 0 {
return 0;
}
let bits = bits % 64;
if bits == 0 {
u64::MAX
} else {
(1 << bits) - 1
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_mask() {
assert_eq!(mask(0), 0);
assert_eq!(mask(1), 1);
assert_eq!(mask(5), 0x1f);
assert_eq!(mask(63), u64::max_value() >> 1);
assert_eq!(mask(64), u64::max_value());
}
#[test]
fn test_max() {
assert_eq!(Uint::<0, 0>::MAX, Uint::ZERO);
assert_eq!(Uint::<1, 1>::MAX, Uint::from_limbs([1]));
assert_eq!(Uint::<7, 1>::MAX, Uint::from_limbs([127]));
assert_eq!(Uint::<64, 1>::MAX, Uint::from_limbs([u64::MAX]));
assert_eq!(
Uint::<100, 2>::MAX,
Uint::from_limbs([u64::MAX, u64::MAX >> 28])
);
}
#[test]
fn test_constants() {
const_for!(BITS in SIZES {
const LIMBS: usize = nlimbs(BITS);
assert_eq!(Uint::<BITS, LIMBS>::MIN, Uint::<BITS, LIMBS>::ZERO);
let _ = Uint::<BITS, LIMBS>::MAX;
});
}
}
#[cfg(feature = "bench")]
#[doc(hidden)]
pub mod bench {
use super::*;
use criterion::Criterion;
pub fn group(criterion: &mut Criterion) {
add::bench::group(criterion);
mul::bench::group(criterion);
div::bench::group(criterion);
pow::bench::group(criterion);
log::bench::group(criterion);
root::bench::group(criterion);
modular::bench::group(criterion);
algorithms::bench::group(criterion);
}
}