1#![cfg_attr(feature = "std", doc = " ```")]
54#![cfg_attr(not(feature = "std"), doc = " ```ignore")]
55#![doc(html_root_url = "https://docs.rs/num-bigint/0.2")]
103#![no_std]
104
105extern crate alloc;
106
107#[cfg(feature = "std")]
108extern crate std;
109
110#[macro_use]
111extern crate smallvec;
112
113#[cfg(feature = "prime")]
114#[macro_use]
115extern crate lazy_static;
116
117extern crate num_integer as integer;
118
119use core::fmt;
120#[cfg(feature = "std")]
121use std::error::Error;
122
123#[macro_use]
124mod macros;
125
126mod bigint;
127mod biguint;
128
129#[cfg(feature = "prime")]
130pub mod prime;
131
132pub mod algorithms;
133pub mod traits;
134
135pub use crate::traits::*;
136
137#[cfg(feature = "rand")]
138mod bigrand;
139
140#[cfg(target_pointer_width = "32")]
141type UsizePromotion = u32;
142#[cfg(target_pointer_width = "64")]
143type UsizePromotion = u64;
144
145#[cfg(target_pointer_width = "32")]
146type IsizePromotion = i32;
147#[cfg(target_pointer_width = "64")]
148type IsizePromotion = i64;
149
150#[derive(Debug, Clone, PartialEq, Eq)]
151pub struct ParseBigIntError {
152 kind: BigIntErrorKind,
153}
154
155#[derive(Debug, Clone, PartialEq, Eq)]
156enum BigIntErrorKind {
157 Empty,
158 InvalidDigit,
159}
160
161impl ParseBigIntError {
162 fn __description(&self) -> &str {
163 use crate::BigIntErrorKind::*;
164 match self.kind {
165 Empty => "cannot parse integer from empty string",
166 InvalidDigit => "invalid digit found in string",
167 }
168 }
169
170 fn empty() -> Self {
171 ParseBigIntError {
172 kind: BigIntErrorKind::Empty,
173 }
174 }
175
176 fn invalid() -> Self {
177 ParseBigIntError {
178 kind: BigIntErrorKind::InvalidDigit,
179 }
180 }
181}
182
183impl fmt::Display for ParseBigIntError {
184 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
185 self.__description().fmt(f)
186 }
187}
188
189#[cfg(feature = "std")]
190impl Error for ParseBigIntError {
191 fn description(&self) -> &str {
192 self.__description()
193 }
194}
195
196pub use crate::biguint::BigUint;
197pub use crate::biguint::IntoBigUint;
198pub use crate::biguint::ToBigUint;
199
200pub use crate::bigint::negate_sign;
201pub use crate::bigint::BigInt;
202pub use crate::bigint::IntoBigInt;
203pub use crate::bigint::Sign;
204pub use crate::bigint::ToBigInt;
205
206#[cfg(feature = "rand")]
207pub use crate::bigrand::{RandBigInt, RandomBits, UniformBigInt, UniformBigUint};
208
209#[cfg(feature = "prime")]
210pub use bigrand::RandPrime;
211
212#[cfg(not(feature = "u64_digit"))]
213pub const VEC_SIZE: usize = 8;
214
215#[cfg(feature = "u64_digit")]
216pub const VEC_SIZE: usize = 4;
217
218mod big_digit {
219 #[cfg(not(feature = "u64_digit"))]
221 pub type BigDigit = u32;
222 #[cfg(feature = "u64_digit")]
223 pub type BigDigit = u64;
224
225 #[cfg(not(feature = "u64_digit"))]
228 pub type DoubleBigDigit = u64;
229 #[cfg(feature = "u64_digit")]
230 pub type DoubleBigDigit = u128;
231
232 #[cfg(not(feature = "u64_digit"))]
234 pub type SignedDoubleBigDigit = i64;
235 #[cfg(feature = "u64_digit")]
236 pub type SignedDoubleBigDigit = i128;
237
238 #[cfg(not(feature = "u64_digit"))]
240 pub const BITS: usize = 32;
241 #[cfg(feature = "u64_digit")]
242 pub const BITS: usize = 64;
243
244 #[cfg(not(feature = "u64_digit"))]
245 const LO_MASK: DoubleBigDigit = (-1i32 as DoubleBigDigit) >> BITS;
246 #[cfg(feature = "u64_digit")]
247 const LO_MASK: DoubleBigDigit = (-1i64 as DoubleBigDigit) >> BITS;
248
249 #[inline]
250 fn get_hi(n: DoubleBigDigit) -> BigDigit {
251 (n >> BITS) as BigDigit
252 }
253 #[inline]
254 fn get_lo(n: DoubleBigDigit) -> BigDigit {
255 (n & LO_MASK) as BigDigit
256 }
257
258 #[inline]
260 pub fn from_doublebigdigit(n: DoubleBigDigit) -> (BigDigit, BigDigit) {
261 (get_hi(n), get_lo(n))
262 }
263
264 #[inline]
266 pub fn to_doublebigdigit(hi: BigDigit, lo: BigDigit) -> DoubleBigDigit {
267 (DoubleBigDigit::from(lo)) | ((DoubleBigDigit::from(hi)) << BITS)
268 }
269}