-
Notifications
You must be signed in to change notification settings - Fork 63
/
integer_common.h
461 lines (363 loc) · 9.97 KB
/
integer_common.h
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#ifndef INTEGER_COMMON_H
#define INTEGER_COMMON_H
#include "primetest.h"
//note: gmp already has c++ bindings so could have just used those. oh well
//const bool output_stats=false;
/*struct generic_stats {
vector<int> entries;
void add(int i) {
if (!output_stats) {
return;
}
entries.push_back(i);
}
void output(string name) {
if (!output_stats) {
return;
}
sort(entries.begin(), entries.end());
vector<double> percentiles={0, 0.01, 0.1, 1, 10, 25, 50, 75, 90, 99, 99.9, 99.99, 100};
print( "::", name );
print( " num =", entries.size() );
if (entries.empty()) {
return;
}
for (double c : percentiles) {
int i=(c/100)*entries.size();
if (i<0) {
i=0;
}
if (i>=entries.size()) {
i=entries.size()-1;
}
print( " ", c, " -> ", entries.at(i) );
}
double total=0;
for (int c : entries) {
total+=c;
}
print( " ", "avg", " -> ", total/double(entries.size()) );
}
};*/
/*struct track_cycles {
generic_stats& stats;
uint64 start_time;
bool is_aborted=false;
track_cycles(generic_stats& t_stats) : stats(t_stats) {
if (!enable_track_cycles) {
return;
}
start_time=__rdtsc();
}
void abort() {
if (!enable_track_cycles) {
return;
}
is_aborted=true;
}
~track_cycles() {
if (!enable_track_cycles) {
return;
}
if (is_aborted) {
return;
}
uint64 end_time=__rdtsc();
uint64 delta=end_time-start_time;
int delta_int=delta;
if (delta_int==delta) {
stats.add(delta_int);
} else {
stats.add(INT_MAX);
}
}
};*/
struct track_max_type {
map<pair<int, string>, pair<int, bool>> data;
void add(int line, string name, int value, bool negative) {
auto& v=data[make_pair(line, name)];
v.first=max(v.first, value);
v.second|=negative;
}
void output(int basis_bits) {
print( "== track max ==" );
for (auto c : data) {
print(c.first.second, double(c.second.first)/basis_bits, c.second.second);
}
}
};
track_max_type track_max;
//#define TRACK_MAX(data) track_max.add(#data " {" __func__ ":" "__LINE__" ")", (data).num_bits())
#define TRACK_MAX(data) track_max.add(__LINE__, #data, (data).num_bits(), (data)<0)
//typedef __mpz_struct mpz_t[1];
typedef __mpz_struct mpz_struct;
int mpz_num_bits_upper_bound(mpz_struct* v) {
return mpz_size(v)*sizeof(mp_limb_t)*8;
}
static bool allow_integer_constructor=false; //don't want static integers because they use the wrong allocator
//16 bytes
struct integer {
mpz_struct impl[1];
~integer() {
mpz_clear(impl);
}
integer() {
assert(allow_integer_constructor);
mpz_init(impl);
}
integer(const integer& t) {
mpz_init(impl);
mpz_set(impl, t.impl);
}
integer(integer&& t) {
mpz_init(impl);
mpz_swap(impl, t.impl);
}
explicit integer(int64 i) {
mpz_init(impl);
mpz_set_si(impl, i);
}
explicit integer(const string& s) {
mpz_init(impl);
int res=mpz_set_str(impl, s.c_str(), 0);
assert(res==0);
}
explicit integer(const std::vector<uint8_t> v) {
mpz_init(impl);
mpz_import(impl, v.size(), 1, sizeof(v[0]), 1, 0, &v[0]);
}
//lsb first
explicit integer(const vector<uint64>& data) {
mpz_init(impl);
mpz_import(impl, data.size(), -1, 8, 0, 0, &data[0]);
}
integer(const uint8_t *bytes, size_t size) {
mpz_init(impl);
mpz_import(impl, size, 1, 1, 1, 0, bytes);
}
//lsb first
vector<uint64> to_vector() const {
vector<uint64> res;
res.resize(mpz_sizeinbase(impl, 2)/64 + 1, 0);
size_t count;
mpz_export(res.data(), &count, -1, 8, 0, 0, impl);
res.resize(count);
return res;
}
vector<uint8_t> to_bytes() const {
vector<uint8_t> res((mpz_sizeinbase(impl, 2) + 7) / 8);
mpz_export(res.data(), NULL, 1, 1, 0, 0, impl);
return res;
}
integer& operator=(const integer& t) {
mpz_set(impl, t.impl);
return *this;
}
integer& operator=(integer&& t) {
mpz_swap(impl, t.impl);
return *this;
}
integer& operator=(int64 i) {
mpz_set_si(impl, i);
return *this;
}
integer& operator=(const string& s) {
int res=mpz_set_str(impl, s.c_str(), 0);
assert(res==0);
return *this;
}
void set_bit(int index, bool value) {
if (value) {
mpz_setbit(impl, index);
} else {
mpz_clrbit(impl, index);
}
}
bool get_bit(int index) {
return mpz_tstbit(impl, index);
}
USED string to_string() const {
string res_string="0x";
res_string.resize(res_string.size() + mpz_sizeinbase(impl, 16) + 2);
mpz_get_str(&(res_string[2]), 16, impl);
if (res_string.substr(0, 3)=="0x-") {
res_string.at(0)='-';
res_string.at(1)='0';
res_string.at(2)='x';
}
//get rid of the null terminator and everything after it
return res_string.c_str();
}
string to_string_dec() const {
string res_string;
res_string.resize(mpz_sizeinbase(impl, 10));
mpz_get_str(&(res_string[0]), 10, impl);
return res_string.c_str();
}
integer& operator+=(const integer& t) {
mpz_add(impl, impl, t.impl);
return *this;
}
integer operator+(const integer& t) const {
integer res;
mpz_add(res.impl, impl, t.impl);
return res;
}
integer& operator-=(const integer& t) {
mpz_sub(impl, impl, t.impl);
return *this;
}
integer operator-(const integer& t) const {
integer res;
mpz_sub(res.impl, impl, t.impl);
return res;
}
integer& operator*=(const integer& t) {
mpz_mul(impl, impl, t.impl);
return *this;
}
integer operator*(const integer& t) const {
integer res;
mpz_mul(res.impl, impl, t.impl);
return res;
}
integer& operator<<=(int i) {
assert(i>=0);
mpz_mul_2exp(impl, impl, i);
return *this;
}
integer operator<<(int i) const {
assert(i>=0);
integer res;
mpz_mul_2exp(res.impl, impl, i);
return res;
}
integer operator-() const {
integer res;
mpz_neg(res.impl, impl);
return res;
}
integer& operator/=(const integer& t) {
mpz_fdiv_q(impl, impl, t.impl);
return *this;
}
integer operator/(const integer& t) const {
integer res;
mpz_fdiv_q(res.impl, impl, t.impl);
return res;
}
integer& operator>>=(int i) {
assert(i>=0);
mpz_fdiv_q_2exp(impl, impl, i);
return *this;
}
integer operator>>(int i) const {
assert(i>=0);
integer res;
mpz_fdiv_q_2exp(res.impl, impl, i);
return res;
}
//this is different from mpz_fdiv_r because it ignores the sign of t
integer& operator%=(const integer& t) {
mpz_mod(impl, impl, t.impl);
return *this;
}
integer operator%(const integer& t) const {
integer res;
mpz_mod(res.impl, impl, t.impl);
return res;
}
integer fdiv_r(const integer& t) const {
integer res;
mpz_fdiv_r(res.impl, impl, t.impl);
return res;
}
bool prime() const {
return is_prime_bpsw(impl) != 0;
}
bool operator<(const integer& t) const {
return mpz_cmp(impl, t.impl)<0;
}
bool operator<=(const integer& t) const {
return mpz_cmp(impl, t.impl)<=0;
}
bool operator==(const integer& t) const {
return mpz_cmp(impl, t.impl)==0;
}
bool operator>=(const integer& t) const {
return mpz_cmp(impl, t.impl)>=0;
}
bool operator>(const integer& t) const {
return mpz_cmp(impl, t.impl)>0;
}
bool operator!=(const integer& t) const {
return mpz_cmp(impl, t.impl)!=0;
}
bool operator<(int i) const {
return mpz_cmp_si(impl, i)<0;
}
bool operator<=(int i) const {
return mpz_cmp_si(impl, i)<=0;
}
bool operator==(int i) const {
return mpz_cmp_si(impl, i)==0;
}
bool operator>=(int i) const {
return mpz_cmp_si(impl, i)>=0;
}
bool operator>(int i) const {
return mpz_cmp_si(impl, i)>0;
}
bool operator!=(int i) const {
return mpz_cmp_si(impl, i)!=0;
}
int num_bits() const {
return mpz_sizeinbase(impl, 2);
}
};
integer abs(const integer& t) {
integer res;
mpz_abs(res.impl, t.impl);
return res;
}
integer root(const integer& t, int n) {
integer res;
mpz_root(res.impl, t.impl, n);
return res;
}
struct gcd_res {
integer gcd;
integer s;
integer t;
};
//a*s + b*t = gcd ; gcd>=0
// abs(s) < abs(b) / (2 gcd)
// abs(t) < abs(a) / (2 gcd)
//(except if |s|<=1 or |t|<=1)
gcd_res gcd(const integer& a, const integer& b) {
gcd_res res;
mpz_gcdext(res.gcd.impl, res.s.impl, res.t.impl, a.impl, b.impl);
return res;
}
integer rand_integer(int num_bits, int seed=-1) {
thread_local gmp_randstate_t state;
thread_local bool is_init=false;
if (!is_init) {
gmp_randinit_mt(state);
gmp_randseed_ui(state, 0);
is_init=true;
}
if (seed!=-1) {
gmp_randseed_ui(state, seed);
}
integer res;
assert(num_bits>=0);
mpz_urandomb(res.impl, state, num_bits);
return res;
}
USED string to_string(mpz_struct* t) {
integer t_int;
mpz_set(t_int.impl, t);
return t_int.to_string();
}
#endif // INTEGER_COMMON_H