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
#[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 = WORD_BIT_SIZE * 2;
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
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 as_int(&self) -> i8 {
*self as i8
}
}
use smallvec::CollectionAllocErr;
#[derive(Debug)]
pub enum Error {
ExponentOverflow(Sign),
DivisionByZero,
InvalidArgument,
MemoryAllocation(CollectionAllocErr),
}
impl Clone for Error {
fn clone(&self) -> Self {
match self {
Self::ExponentOverflow(arg) => Self::ExponentOverflow(arg.clone()),
Self::DivisionByZero => Self::DivisionByZero,
Self::InvalidArgument => Self::InvalidArgument,
Self::MemoryAllocation(arg) => Self::MemoryAllocation(match arg {
CollectionAllocErr::CapacityOverflow => CollectionAllocErr::CapacityOverflow,
CollectionAllocErr::AllocErr { layout } => {
CollectionAllocErr::AllocErr { layout: *layout }
}
}),
}
}
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::ExponentOverflow(l0), Self::ExponentOverflow(r0)) => l0 == r0,
(Self::MemoryAllocation(l0), Self::MemoryAllocation(r0)) => {
core::mem::discriminant(l0) == core::mem::discriminant(r0)
}
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}
#[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,
}