1use core::fmt::Display;
4
5#[cfg(feature = "std")]
6use std::collections::TryReserveError;
7
8#[cfg(not(feature = "std"))]
9use alloc::collections::TryReserveError;
10
11#[cfg(not(target_arch = "x86"))]
13pub type Word = u64;
14
15#[cfg(not(target_arch = "x86"))]
17pub type DoubleWord = u128;
18
19#[cfg(not(target_arch = "x86"))]
21pub type SignedWord = i128;
22
23#[cfg(target_arch = "x86")]
25pub type Word = u32;
26
27#[cfg(target_arch = "x86")]
29pub type DoubleWord = u64;
30
31#[cfg(target_arch = "x86")]
33pub type SignedWord = i64;
34
35pub type Exponent = i32;
37
38#[cfg(not(target_arch = "x86"))]
40pub const EXPONENT_MAX: Exponent = Exponent::MAX;
41
42#[cfg(target_arch = "x86")]
44pub const EXPONENT_MAX: Exponent = Exponent::MAX / 4;
45
46#[cfg(not(target_arch = "x86"))]
48pub const EXPONENT_MIN: Exponent = Exponent::MIN;
49
50#[cfg(target_arch = "x86")]
52pub const EXPONENT_MIN: Exponent = Exponent::MIN / 4;
53
54pub const WORD_MAX: Word = Word::MAX;
56
57pub const WORD_BASE: DoubleWord = WORD_MAX as DoubleWord + 1;
59
60pub const WORD_BIT_SIZE: usize = core::mem::size_of::<Word>() * 8;
62
63pub const WORD_SIGNIFICANT_BIT: Word = WORD_MAX << (WORD_BIT_SIZE - 1);
65
66pub const DEFAULT_P: usize = 128;
68
69pub const EXPONENT_BIT_SIZE: usize = core::mem::size_of::<Exponent>() * 8;
71
72#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
74pub enum Sign {
75 Neg = -1,
77
78 Pos = 1,
80}
81
82impl Sign {
83 pub fn invert(&self) -> Self {
85 match *self {
86 Sign::Pos => Sign::Neg,
87 Sign::Neg => Sign::Pos,
88 }
89 }
90
91 pub fn is_positive(&self) -> bool {
93 *self == Sign::Pos
94 }
95
96 pub fn is_negative(&self) -> bool {
98 *self == Sign::Neg
99 }
100
101 pub fn to_int(&self) -> i8 {
103 *self as i8
104 }
105}
106
107#[derive(Debug, Clone, Copy)]
109pub enum Error {
110 ExponentOverflow(Sign),
112
113 DivisionByZero,
115
116 InvalidArgument,
118
119 MemoryAllocation,
121}
122
123#[cfg(feature = "std")]
124impl std::error::Error for Error {
125 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
126 None
127 }
128}
129
130impl Display for Error {
131 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
132 let repr = match self {
133 Error::ExponentOverflow(s) => {
134 if s.is_positive() {
135 "positive overflow"
136 } else {
137 "negative overflow"
138 }
139 }
140 Error::DivisionByZero => "division by zero",
141 Error::InvalidArgument => "invalid argument",
142 Error::MemoryAllocation => "memory allocation failure",
143 };
144 f.write_str(repr)
145 }
146}
147
148impl PartialEq for Error {
149 fn eq(&self, other: &Self) -> bool {
150 match (self, other) {
151 (Self::ExponentOverflow(l0), Self::ExponentOverflow(r0)) => l0 == r0,
152 _ => core::mem::discriminant(self) == core::mem::discriminant(other),
153 }
154 }
155}
156
157impl From<TryReserveError> for Error {
158 fn from(_: TryReserveError) -> Self {
159 Error::MemoryAllocation
160 }
161}
162
163#[derive(PartialEq, Eq, Copy, Clone, Debug)]
165pub enum Radix {
166 Bin = 2,
168
169 Oct = 8,
171
172 Dec = 10,
174
175 Hex = 16,
177}
178
179#[derive(Eq, PartialEq, Debug, Copy, Clone)]
181pub enum RoundingMode {
182 None = 1,
184
185 Up = 2,
187
188 Down = 4,
190
191 ToZero = 8,
193
194 FromZero = 16,
196
197 ToEven = 32,
199
200 ToOdd = 64,
202}