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