-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bignum.cpp
244 lines (189 loc) · 5.91 KB
/
Bignum.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include "Bignum.h"
const uint64_t Bignum::BASE = UINT32_MAX + 1ULL;
uint64_t digit(const std::vector<uint32_t>& digits, std::vector<uint32_t>::size_type index) {
return index >= digits.size() ? 0U : digits[index];
}
void strip_leading_zeros(std::vector<uint32_t>& digits) {
for (std::vector<uint32_t>::reverse_iterator i = digits.rbegin(); *i == 0U && digits.size() > 1; ++i)
digits.erase((++i).base());
}
std::vector<uint32_t> add(const std::vector<uint32_t>& first, const std::vector<uint32_t>& second) {
std::vector<uint32_t>::size_type max_length = std::max(first.size(), second.size());
uint64_t carry(0);
std::vector<uint32_t> sum_digits;
for (std::vector<uint32_t>::size_type i = 0; i < max_length; ++i) {
uint64_t sum = digit(first, i) + digit(second, i) + carry;
sum_digits.push_back(sum % Bignum::BASE);
carry = sum / Bignum::BASE;
}
if (carry != 0)
sum_digits.push_back(carry);
strip_leading_zeros(sum_digits);
return sum_digits;
}
std::vector<uint32_t> subtract(const std::vector<uint32_t>& first, const std::vector<uint32_t>& second) {
std::vector<uint32_t>::size_type max_length = std::max(first.size(), second.size());
int64_t borrow(0);
std::vector<uint32_t> difference_digits;
for (std::vector<uint32_t>::size_type i = 0; i < max_length; ++i) {
int64_t difference = digit(first, i) - digit(second, i) - borrow;
if (difference < 0) {
difference += Bignum::BASE;
borrow = 1;
} else
borrow = 0;
difference_digits.push_back(difference % Bignum::BASE);
}
strip_leading_zeros(difference_digits);
return difference_digits;
}
Bignum::Bignum(int64_t value) : store() {
sign = value < 0 ? -1 : (value == 0 ? 0 : 1);
uint64_t no_sign = ::abs(value);
store.push_back((uint32_t) (no_sign & 0x00000000FFFFFFFFULL));
store.push_back((uint32_t) (no_sign >> 32));
strip_leading_zeros(store);
}
Bignum::Bignum(const std::vector<uint32_t>& digits, int sign)
: store(digits), sign(sign) {
}
Bignum::Bignum(const Bignum& other)
: store(other.store), sign(other.sign) {
}
Bignum& Bignum::operator=(const Bignum& other) {
if (this != &other) {
store = other.store;
sign = other.sign;
}
return *this;
}
bool Bignum::equal(const Bignum& other) const {
return store == other.store && sign == other.sign;
}
bool Bignum::less(const Bignum& other) const {
if (sign < other.sign)
return true;
if (store.size() < other.store.size())
return true;
for (int i = store.size() - 1; i >= 0; --i) {
if (sign < 0 ? store[i] > other.store[i] : store[i] < other.store[i])
return true;
}
return false;
}
int Bignum::signum() const {
return sign;
}
Bignum Bignum::abs() const {
return sign >= 0 ? Bignum(*this) : Bignum(store, 1);
}
bool operator==(const Bignum& left, const Bignum& right) {
return left.equal(right);
}
bool operator!=(const Bignum& left, const Bignum& right) {
return !(left == right);
}
bool operator<(const Bignum& left, const Bignum& right) {
return left.less(right);
}
bool operator<=(const Bignum& left, const Bignum& right) {
return !(left > right);
}
bool operator>(const Bignum& left, const Bignum& right) {
return right < left;
}
bool operator>=(const Bignum& left, const Bignum& right) {
return right <= left;
}
Bignum operator+(const Bignum& left, const Bignum& right) {
return Bignum(left) += right;
}
const Bignum& Bignum::operator+=(const Bignum& other) {
if (other.sign == 0)
return *this;
Bignum this_abs = abs();
Bignum other_abs = other.abs();
if (sign == other.sign)
store = add(this_abs.store, other_abs.store);
else {
if (this_abs > other_abs)
store = subtract(this_abs.store, other_abs.store);
else {
store = subtract(other_abs.store, this_abs.store);
sign = other.sign;
}
}
reconcile_sign_of_zero();
return *this;
}
Bignum operator-(const Bignum& left, const Bignum& right) {
return Bignum(left) -= right;
}
const Bignum& Bignum::operator-=(const Bignum& other) {
if (other.sign == 0)
return *this;
Bignum this_abs = abs();
Bignum other_abs = other.abs();
if (sign != other.sign)
store = add(this_abs.store, other_abs.store);
else {
if (this_abs > other_abs)
store = subtract(this_abs.store, other_abs.store);
else {
store = subtract(other_abs.store, this_abs.store);
sign = -sign;
}
}
reconcile_sign_of_zero();
return *this;
}
Bignum operator>>(const Bignum& b, unsigned int n) {
return Bignum(b) >>= n;
}
const Bignum& Bignum::operator>>=(unsigned int n) {
uint32_t leading(0);
uint32_t trailing(0);
for (std::vector<uint32_t>::reverse_iterator i = store.rbegin(); i != store.rend(); ++i) {
trailing = *i & ((1 << n) - 1);
*i >>= n;
*i |= leading;
leading = trailing << (sizeof(uint32_t) * CHAR_BIT - n);
}
reconcile_sign_of_zero();
return *this;
}
Bignum Bignum::operator-() const {
return Bignum(store, -sign);
}
Bignum& operator++(Bignum& n) {
n += 1;
return n;
}
Bignum operator++(Bignum& n, int) {
Bignum old(n);
++n;
return old;
}
Bignum& operator--(Bignum& n) {
n -= 1;
return n;
}
Bignum operator--(Bignum& n, int) {
Bignum old(n);
--n;
return old;
}
void Bignum::reconcile_sign_of_zero() {
if (store.size() == 1 && store[0] == 0U)
sign = 0;
}
std::ostream& operator<<(std::ostream& out, const Bignum& n) {
out << "sign: " << n.sign << std::endl;
out << "digits: " << std::endl;
for (std::vector<uint32_t>::size_type i = 0; i != n.store.size(); ++i)
out << i << ": " << std::hex << n.store[i] << std::endl;
return out;
}