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