-
Notifications
You must be signed in to change notification settings - Fork 1
/
LargeInt.cpp
289 lines (242 loc) · 8.12 KB
/
LargeInt.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef ASSERTS
#define NDEBUG // disable asserts; those asserts make sure that with PRECISION == [1 or 2], all is correct
#endif
// some 64-bit assert macros
#if defined(_LP64) && defined(largeintlib)
#define assert128(x) assert(precision != 2 || (x));
#else
#define assert128(x) ;
#endif
#include <assert.h>
#include <stdio.h>
#include "LargeInt.hpp"
using namespace std;
template<int precision>
LargeInt<precision>::LargeInt()
{
}
template<int precision>
LargeInt<precision>::LargeInt(const uint64_t &c)
{
array[0] = c;
for (int i = 1; i < precision; i++)
array[i] = 0;
}
template<int precision>
LargeInt<precision> LargeInt<precision>::operator+ (const LargeInt<precision>& other) const
{
LargeInt<precision> result;
int carry = 0;
for (int i = 0 ; i < precision ; i++)
{
result.array[i] = array[i] + other.array[i] + carry;
carry = (result.array[i] < array[i]) ? 1 : 0;
}
assert(precision != 1 || (result == other.array[0] + array[0]));
assert128(result.toInt128() == other.toInt128() + toInt128());
return result;
}
template<int precision>
LargeInt<precision> LargeInt<precision>::operator- (const LargeInt<precision>& other) const
{
LargeInt<precision> result;
int carry = 0;
for (int i = 0 ; i < precision ; i++)
{
result.array[i] = array[i] - other.array[i] - carry;
carry = (result.array[i] > array[i]) ? 1 : 0;
}
assert(precision != 1 || (result == array[0] - other.array[0]));
assert128(result.toInt128() == toInt128() - other.toInt128());
return result;
}
template<int precision>
LargeInt<precision> LargeInt<precision>::operator* (const int& coeff) const
{
LargeInt<precision> result (*this);
// minia doesn't have that many multiplications cases
if (coeff == 2 || coeff == 4)
{
result = result << (coeff / 2);
}
else
{
if (coeff == 21)
{
result = (result << 4) + (result << 2) + result;
}
else
{
printf("unsupported LargeInt multiplication: %d\n",coeff);
exit(1);
}
}
assert(precision != 1 || (result == array[0] * coeff));
assert128(result.toInt128() == toInt128() * coeff);
return result;
}
template<int precision>
LargeInt<precision> LargeInt<precision>::operator/ (const uint32_t& divisor) const
{
LargeInt<precision> result;
fill( result.array, result.array + precision, 0 );
// inspired by Divide32() from http://subversion.assembla.com/svn/pxcode/RakNet/Source/BigInt.cpp
uint64_t r = 0;
uint32_t mask32bits = ~0;
for (int i = precision-1; i >= 0; --i)
{
for (int j = 1; j >= 0; --j) // [j=1: high-32 bits, j=0: low-32 bits] of array[i]
{
uint64_t n = (r << 32) | ((array[i] >> (32*j)) & mask32bits );
result.array[i] = result.array[i] | (((n / divisor) & mask32bits) << (32*j));
r = n % divisor;
}
}
assert(precision != 1 || (result == array[0] / divisor));
assert128(result.toInt128() == toInt128() / divisor);
return result;
}
template<int precision>
uint32_t LargeInt<precision>::operator% (const uint32_t& divisor) const
{
uint64_t r = 0;
uint32_t mask32bits = ~0;
for (int i = precision-1; i >= 0; --i)
{
for (int j = 1; j >= 0; --j) // [j=1: high-32 bits, j=0: low-32 bits] of array[i]
{
uint64_t n = (r << 32) | ((array[i] >> (32*j)) & mask32bits );
r = n % divisor;
}
}
assert(precision != 1 || (r == array[0] % divisor));
assert128(r == toInt128() % divisor);
return (uint32_t)r;
}
template<int precision>
LargeInt<precision> LargeInt<precision>::operator^ (const LargeInt& other) const
{
LargeInt<precision> result;
for (int i=0 ; i < precision ; i++)
result.array[i] = array[i] ^ other.array[i];
assert(precision != 1 || (result == (array[0] ^ other.array[0])));
assert128(result.toInt128() == (toInt128() ^ other.toInt128()));
return result;
}
template<int precision>
LargeInt<precision> LargeInt<precision>::operator& (const LargeInt& other) const
{
LargeInt<precision> result;
for (int i=0 ; i < precision ; i++)
result.array[i] = array[i] & other.array[i];
assert(precision != 1 || (result == (array[0] & other.array[0])));
assert128(result.toInt128() == (toInt128() & other.toInt128()));
return result;
}
// Added by Sara
template<int precision>
LargeInt<precision> LargeInt<precision>::operator| (const LargeInt& other) const
{
LargeInt<precision> result;
for (int i=0 ; i < precision ; i++)
result.array[i] = array[i] | other.array[i];
assert(precision != 1 || (result == (array[0] | other.array[0])));
assert128(result.toInt128() == (toInt128() | other.toInt128()));
return result;
}
template<int precision>
LargeInt<precision> LargeInt<precision>::operator~ () const
{
LargeInt<precision> result;
for (int i=0 ; i < precision ; i++)
result.array[i] = ~array[i];
assert(precision != 1 || (result == ~array[0]));
assert128(result.toInt128() == ~toInt128());
return result;
}
template<int precision>
LargeInt<precision> LargeInt<precision>::operator<< (const int& coeff) const
{
LargeInt<precision> result (0);
int large_shift = coeff / 64;
int small_shift = coeff % 64;
for (int i = large_shift ; i < precision-1; i++)
{
result.array[i] = result.array[i] | (array[i-large_shift] << small_shift);
if (small_shift == 0) // gcc "bug".. uint64_t x; x>>64 == 1<<63, x<<64 == 1
result.array[i+1] = 0;
else
result.array[i+1] = array[i-large_shift] >> (64 - small_shift);
}
result.array[precision-1] = result.array[precision-1] | (array[precision-1-large_shift] << small_shift);
assert(precision != 1 || (result == (array[0] << coeff)));
assert128(result.toInt128() == (toInt128() << coeff));
return result;
}
template<int precision>
LargeInt<precision> LargeInt<precision>::operator>> (const int& coeff) const
{
LargeInt<precision> result (0);
int large_shift = coeff / 64;
int small_shift = coeff % 64;
result.array[0] = (array[large_shift] >> small_shift);
for (int i = 1 ; i < precision - large_shift ; i++)
{
result.array[i] = (array[i+large_shift] >> small_shift);
if (small_shift == 0 && large_shift > 0) // gcc "bug".. uint64_t x; x>>64 == 1<<63, x<<64 == 1
{
result.array[i-1] = result.array[i-1];
}
else
{
result.array[i-1] = result.array[i-1] | (array[i+large_shift] << (64 - small_shift));
}
}
assert(precision != 1 || ( small_shift == 0 || (result == array[0] >> coeff)));
assert128(small_shift == 0 || (result.toInt128() == (toInt128() >> coeff)));
return result;
}
template<int precision>
bool LargeInt<precision>::operator!= (const LargeInt& c) const
{
for (int i = 0 ; i < precision ; i++)
if( array[i] != c.array[i] )
return true;
return false;
}
template<int precision>
bool LargeInt<precision>::operator== (const LargeInt& c) const
{
for (int i = 0 ; i < precision ; i++)
if( array[i] != c.array[i] )
return false;
return true;
}
template<int precision>
bool LargeInt<precision>::operator< (const LargeInt& c) const
{
for (int i = precision-1 ; i>=0 ; --i)
if( array[i] != c.array[i] )
return array[i] < c.array[i];
return false;
}
template<int precision>
bool LargeInt<precision>::operator<=(const LargeInt& c) const
{
return operator==(c) || operator<(c);
}
template<int precision>
uint64_t LargeInt<precision>::toInt() const
{
return array[0];
}
#ifdef _LP64
template<int precision>
__uint128_t LargeInt<precision>::toInt128() const
{
return ((__uint128_t)array[0]) + (((__uint128_t)array[1]) << ((__uint128_t)64));
}
#endif
#ifdef kmer_precision
template class LargeInt<kmer_precision>; // since we didn't define the functions in a .h file, that trick removes linker errors, see http://www.parashift.com/c++-faq-lite/separate-template-class-defn-from-decl.html
#endif