dashu_int/lib.rs
1// Copyright (c) 2022 Jacob Zhong
2//
3// Licensed under either of
4//
5// * Apache License, Version 2.0
6// (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
7// * MIT license
8// (LICENSE-MIT or https://opensource.org/licenses/MIT)
9//
10// at your option.
11//
12// Unless you explicitly state otherwise, any contribution intentionally submitted
13// for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
14// dual licensed as above, without any additional terms or conditions.
15//
16// This crate is a for of the `ibig` crate. The original LICENSE is included in the
17// [NOTICE.md](../NOTICE.md)
18
19//! A big integer library with good performance.
20//!
21//! The library implements efficient large integer arithmetic in pure Rust.
22//!
23//! The two main integer types are [UBig] (for unsigned integers) and [IBig] (for signed integers).
24//!
25//! Modular arithmetic is supported by the module [modular]. Some utilities for fast division is provided in the module [fast_div].
26//!
27//! To construct big integers from literals, please use the [`dashu-macro`](https://docs.rs/dashu-macros/latest/dashu_macros/)
28//! crate for your convenience.
29//!
30//! # Examples
31//!
32//! ```
33//! # use dashu_base::ParseError;
34//! use dashu_int::{IBig, fast_div::ConstDivisor, UBig};
35//!
36//! let a = UBig::from(12345678u32);
37//! let b = IBig::from(-0x10ff);
38//! let c = IBig::from_str_radix("-azz", 36).unwrap();
39//! let d: UBig = "15033211231241234523452345345787".parse()?;
40//! let e = 2 * &b - 1;
41//! let f = a * b.pow(10);
42//!
43//! assert_eq!(e, IBig::from(-0x21ff));
44//! assert_eq!(c.to_string(), "-14255");
45//! assert_eq!(
46//! f.in_radix(16).to_string(),
47//! "1589bda8effbfc495d8d73c83d8b27f94954e"
48//! );
49//! assert_eq!(
50//! format!("hello {:#x}", d % 0xabcd_1234_1341_3245_1345u128),
51//! "hello 0x1a7e7c487267d2658a93"
52//! );
53//!
54//! let ring = ConstDivisor::new(UBig::from(10000u32));
55//! let x = ring.reduce(12345);
56//! let y = ring.reduce(55443);
57//! assert_eq!(format!("{}", x - y), "6902 (mod 10000)");
58//! # Ok::<(), ParseError>(())
59//! ```
60//!
61//! # Optional dependencies
62//!
63//! * `std` (*default*): for `std::error::Error` and some internal usages of `std` functions.
64//! * `num-traits` (*default*): support traits from crate `num-traits`.
65//! * `num-integer` (*default*): support traits from crate `num-integer`.
66//! * `num-order` (*default*): support traits from crate `num-order`.
67//! * `rand` (*default*): support random number generation based on crate `rand`.
68//! * `serde`: support serialization and deserialization based on crate `serde`.
69//! * `zeroize`: support traits from crate `zeroize`
70
71#![cfg_attr(not(feature = "std"), no_std)]
72// TODO: apply these attributes to all crates
73// TODO: #![deny(missing_docs)]
74// TODO: #![deny(clippy::allow_attributes_without_reason)]
75#![deny(clippy::dbg_macro)]
76#![deny(clippy::undocumented_unsafe_blocks)]
77#![deny(clippy::let_underscore_must_use)]
78
79extern crate alloc;
80
81pub use crate::{ibig::IBig, ubig::UBig};
82pub use dashu_base::Sign;
83
84/// The primitive integer type used to construct the big integers, guaranteed to be
85/// a rust built-in unsigned integer type.
86///
87/// The big integers is interally represented as an array of [Word]s, so convert
88/// integers from and into [Word]s are efficient.
89///
90/// The size of a [Word] is usually the same as [usize], but it's not guaranteed.
91/// It's dependent on the target architecture.
92pub type Word = arch::word::Word;
93
94/// The primitive integer type that has exactly double the size of [Word].
95pub type DoubleWord = arch::word::DoubleWord;
96
97mod add;
98mod add_ops;
99mod arch;
100mod bits;
101mod buffer;
102mod cmp;
103mod convert;
104mod div;
105mod div_const;
106mod div_ops;
107mod error;
108pub mod fmt;
109mod gcd;
110mod gcd_ops;
111mod helper_macros;
112mod ibig;
113mod iter;
114mod log;
115mod math;
116mod memory;
117pub mod modular;
118mod mul;
119mod mul_ops;
120pub mod ops;
121mod parse;
122mod pow;
123mod primitive;
124mod radix;
125mod remove;
126mod repr;
127mod root;
128mod root_ops;
129mod shift;
130mod shift_ops;
131mod sign;
132mod sqr;
133mod third_party;
134mod ubig;
135
136// All the public items from third_party will be exposed
137#[allow(unused_imports)]
138pub use third_party::*;
139
140// Re-export types for fast division
141/// Prepared divisor types for fast division
142pub mod fast_div {
143 pub use super::div_const::ConstDivisor;
144}