-
Notifications
You must be signed in to change notification settings - Fork 0
/
uint2048.cpp
658 lines (539 loc) · 15.5 KB
/
uint2048.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#include "uint2048.hpp"
// --- constructors ---
// - standard -
uint2048::uint2048(){
for (auto i = 0u; i < 32u; ++i) parts_[i] = 0ull;
}
uint2048::uint2048(uint64_t num){
for (auto i = 1u; i < 32u; ++i) parts_[i] = 0ull;
parts_[0] = num;
}
// - copy -
uint2048::uint2048(const uint2048& num){
for (auto i = 0u; i < 32u; ++i) parts_[i] = num.parts_[i];
}
// --- functions ---
bool uint2048::highest_bit(uint16_t* index) const{
auto res = 0ul;
auto part = 0ull;
/*
loop down from the most significant ull and find the first one that is non-zero.
use the intrinsic function _BitScanReverse64 to find the index of that ull's
most significant bit.
set the index pointer's value to:
64 * num of ulls below + the index resulting from _BitScanReverse64
return true
if all ulls are found to be zero, set the index to 0 and return false.
*/
for (auto i = 0u; i < 32u; ++i){
part = parts_[31u - i];
if (part > 0u){
// intrinsic function
// bsr instruction
// find index of most significant bit
_BitScanReverse64(&res, part);
// could use FMA here?
*index = ((31u - i) * 64u) + static_cast<uint16_t>(res);
return true;
}
}
*index = 0u;
return false;
}
uint16_t uint2048::num_bits() const{
uint16_t res = 0u;
if (highest_bit(&res)) return ++res;
else return res;
}
/*
returns a bitset representation of the parts array
*/
std::bitset<2048> uint2048::to_bitset(){
return *reinterpret_cast<std::bitset<2048>*>(parts_);
}
// --- operators ---
// - assignment -
uint2048& operator+=(uint2048& operand_a, const uint2048& operand_b){
uint64_t a, b;
uint64_t* c;
uint8_t carry_flag = 0u;
for (auto i = 0u; i < 32u; ++i){
a = operand_a.parts_[i];
b = operand_b.parts_[i];
c = &(operand_a.parts_[i]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
// carry_flag is set to 1 if there is a carry bit
carry_flag = _addcarry_u64(carry_flag, a, b, c);
}
return operand_a;
}
uint2048& operator+=(uint2048& operand_a, uint64_t operand_b){
uint64_t a;
uint64_t* b;
uint8_t carry_flag = 0u;
a = operand_a.parts_[0u];
b = &(operand_a.parts_[0u]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
// carry_flag is set to 1 if there is a carry bit
carry_flag = _addcarry_u64(carry_flag, a, operand_b, b);
if (carry_flag){
for (auto i = 1u; i < 32u; ++i){
a = operand_a.parts_[i];
b = &(operand_a.parts_[i]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
carry_flag = _addcarry_u64(carry_flag, a, 0, b);
if (!carry_flag) break;
}
}
return operand_a;
}
uint2048& operator-=(uint2048& operand_a, const uint2048& operand_b){
uint64_t a, b;
uint64_t* c;
uint8_t borrow_flag = 0u;
for (auto i = 0u; i < 32u; ++i){
a = operand_a.parts_[i];
b = operand_b.parts_[i];
c = &(operand_a.parts_[i]);
// intrinsic function
// sbb instruction
// *c = a - (b + borrow)
// borrow_flag is set to 1 if (a < (b + borrow))
borrow_flag = _subborrow_u64(borrow_flag, a, b, c);
}
return operand_a;
}
uint2048& operator%=(uint2048& operand_dividend, const uint2048& operand_divisor){
uint2048 quotient;
if (operand_dividend < operand_divisor) return operand_dividend;
quotient = operand_dividend / operand_divisor;
operand_dividend -= (operand_divisor * quotient);
return operand_dividend;
}
uint2048& operator<<=(uint2048& operand_a, uint16_t operand_b){
if (operand_b >= 2048u){
for (auto i = 0u; i < 32u; ++i) operand_a.parts_[i] = 0ull;
return operand_a;
}
uint64_t a, b, c;
auto overflow = 0ull;
auto shift = operand_b / 64u;
if (shift){
for (auto i = 0u; i < (32u - shift); ++i) operand_a.parts_[31u - i] = operand_a.parts_[31u - i - shift];
for (auto i = 0u; i < shift; ++i) operand_a.parts_[i] = 0ull;
}
if (operand_b %= 64u){
for (auto i = 0u; i < 32u; ++i){
a = operand_a.parts_[i];
b = a << operand_b;
c = b + overflow;
overflow = a >> (64u - operand_b);
operand_a.parts_[i] = c;
}
}
return operand_a;
}
uint2048& operator>>=(uint2048& operand_a, uint16_t operand_b){
// if the input is greater than or equal to 2048
// set all unsigned long longs in parts_ to zero
// return reference to *this
if (operand_b >= 2048u){
for (auto i = 0u; i < 32u; ++i) operand_a.parts_[i] = 0ull;
return operand_a;
}
uint64_t a, b, c;
auto overflow = 0ull;
auto shift = operand_b / 64u;
if (shift){
for (auto i = 0u; i < (32u - shift); ++i)
operand_a.parts_[i] = operand_a.parts_[i + shift];
for (auto i = 0u; i < shift; ++i)
operand_a.parts_[31u - i] = 0ull;
}
if (operand_b %= 64u)
for (auto i = 0u; i < 32u; ++i){
a = operand_a.parts_[31u - i];
b = a >> operand_b;
c = b + overflow;
overflow = a << (64u - operand_b);
operand_a.parts_[31u - i] = c;
}
return operand_a;
}
// - increment / decrement -
uint2048& uint2048::operator++(){
uint64_t a;
uint64_t* b;
uint8_t carry_flag = 0u;
a = parts_[0];
b = &(parts_[0]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
// carry_flag is set to 1 if there is a carry bit
carry_flag = _addcarry_u64(carry_flag, a, 1ull, b);
if (carry_flag)
for (auto i = 1u; i < 32u; ++i){
a = parts_[i];
b = &(parts_[i]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
// carry_flag is set to 1 if there is a carry bit
carry_flag = _addcarry_u64(carry_flag, a, 0, b);
if (!carry_flag) break;
}
return *this;
}
uint2048 uint2048::operator++(int){
uint2048 ret;
uint64_t a;
uint64_t* b;
uint8_t carry_flag = 0u;
ret = *this;
a = parts_[0];
b = &(parts_[0]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
// carry_flag is set to 1 if there is a carry bit
carry_flag = _addcarry_u64(carry_flag, a, 1ull, b);
if (carry_flag)
for (auto i = 1u; i < 32u; ++i){
a = parts_[i];
b = &(parts_[i]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
// carry_flag is set to 1 if there is a carry bit
carry_flag = _addcarry_u64(carry_flag, a, 0, b);
if (!carry_flag) break;
}
return ret;
}
// - arithmetic -
uint2048 operator+(const uint2048& operand_a, const uint2048& operand_b){
uint2048 ret;
uint64_t a, b;
uint64_t* c;
uint8_t carry_flag = 0u;
for (auto i = 0u; i < 32u; ++i){
a = operand_a.parts_[i];
b = operand_b.parts_[i];
c = &(ret.parts_[i]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
// carry_flag is set to 1 if there is a carry bit
carry_flag = _addcarry_u64(carry_flag, a, b, c);
}
return ret;
}
uint2048 operator+(const uint2048& operand_a, uint64_t operand_b){
uint2048 ret;
uint64_t a;
uint64_t* b;
uint8_t carry_flag = 0u;
a = operand_a.parts_[0u];
b = &(ret.parts_[0u]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
// carry_flag is set to 1 if there is a carry bit
carry_flag = _addcarry_u64(carry_flag, a, operand_b, b);
if (carry_flag){
for (auto i = 1u; i < 32u; ++i){
a = operand_a.parts_[i];
b = &(ret.parts_[i]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
carry_flag = _addcarry_u64(carry_flag, a, 0, b);
if (!carry_flag) break;
}
}
return ret;
}
uint2048 operator+(uint64_t operand_a, const uint2048& operand_b){
uint2048 ret;
uint64_t a;
uint64_t* b;
uint8_t carry_flag = 0u;
a = operand_b.parts_[0u];
b = &(ret.parts_[0u]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
// carry_flag is set to 1 if there is a carry bit
carry_flag = _addcarry_u64(carry_flag, a, operand_a, b);
if (carry_flag){
for (auto i = 1u; i < 32u; ++i){
a = operand_b.parts_[i];
b = &(ret.parts_[i]);
// intrinsic function
// adcx instruction
// *c = a + b + carry
carry_flag = _addcarry_u64(carry_flag, a, 0, b);
if (!carry_flag) break;
}
}
return ret;
}
uint2048 operator-(const uint2048& operand_a, const uint2048& operand_b){
uint2048 ret;
uint64_t a, b;
uint64_t* c;
uint8_t borrow_flag = 0u;
for (auto i = 0u; i < 32u; ++i){
a = operand_a.parts_[i];
b = operand_b.parts_[i];
c = &(ret.parts_[i]);
// intrinsic function
// sbb instruction
// *c = a - (b + borrow)
// borrow_flag is set to 1 if (a < (b + borrow))
borrow_flag = _subborrow_u64(borrow_flag, a, b, c);
}
return ret;
}
uint2048 operator-(const uint2048& operand_a, uint64_t operand_b){
uint2048 ret;
uint64_t a;
uint64_t* b;
uint8_t borrow_flag = 0u;
a = operand_a.parts_[0];
b = &(ret.parts_[0]);
// intrinsic function
// sbb instruction
// *c = a - (num + borrow)
// borrow_flag is set to 1 if (a < (b + borrow))
borrow_flag = _subborrow_u64(borrow_flag, a, operand_b, b);
if (borrow_flag){
for (auto i = 1u; i < 32u; ++i){
a = operand_a.parts_[i];
b = &(ret.parts_[i]);
// intrinsic function
// sbb instruction
// *b = a - borrow
// borrow_flag is set to 1 if (a < (b + borrow))
borrow_flag = _subborrow_u64(borrow_flag, a, 0, b);
if (!borrow_flag) break;
}
}
else{
for (auto i = 1u; i < 32u; ++i) ret.parts_[i] = operand_a.parts_[i];
}
return ret;
}
uint2048 operator*(const uint2048& operand_a, const uint2048& operand_b){
uint2048 ret;
if (operand_b == 0ull || operand_a == 0ull) return ret;
if (operand_b == 1ull) return operand_a;
if (operand_a == 1ull) return operand_b;
uint2048 temp_a, temp_b;
uint16_t num_loops;
num_loops = operand_b.num_bits();
temp_a = operand_b;
temp_b = operand_a;
for (auto i = 0u; i < num_loops; ++i){
if (temp_a & 1ull) ret += temp_b;
temp_a >>= 1u;
temp_b <<= 1u;
}
return ret;
}
uint2048 operator/(const uint2048& operand_dividend, const uint2048& operand_divisor){
uint2048 quotient;
// ! need better way to handle division by 0, expections are gross
// perhaps an error flag in the object?
if (operand_divisor > operand_dividend || operand_divisor == 0ull) return quotient;
uint2048 dividend, remainder;
uint2048 offering;
uint16_t dividend_bits, divisor_bits, remainder_bits;
uint16_t bits_from_dividend;
dividend = operand_dividend;
dividend_bits = dividend.num_bits();
divisor_bits = operand_divisor.num_bits();
remainder_bits = 0u;
// loop while there are more bits to divide into
while ((dividend_bits + remainder_bits) >= divisor_bits){
// determine how many bits are needed from the dividend
bits_from_dividend = divisor_bits - remainder_bits;
// if there are remainder bits
if (remainder_bits){
// push the remainder into the offering with trailing zeros
// equal to the number of bits from the dividend
offering = remainder << bits_from_dividend;
// take the the most significant bits from the dividend
// and add them to the offering.
// the number of bits to take from the dividend are equal to
// bits_from_dividend
offering += (dividend >> (dividend_bits - bits_from_dividend));
}
else{
// there are no remainder bits to add to the offering.
// only take bits from the dividend
offering = dividend >> (dividend_bits - bits_from_dividend);
}
// chop off the most significant bits from the dividend.
// takes a number of bits equal to bits_from_dividend
dividend <<= (2048u - (dividend_bits - bits_from_dividend));
dividend >>= (2048u - (dividend_bits - bits_from_dividend));
// push the bits in quotient to the left by bits_from_dividend
quotient <<= bits_from_dividend;
// calculate the number of bits left in the dividend
dividend_bits = dividend_bits - bits_from_dividend;
// loop until the offering is greater than or equal to
// the divisor
while (offering < operand_divisor){
// if there are no more bits in the dividend return the quotient
if (!dividend_bits) return quotient;
// shift the bits in the offering 1 to the left
offering <<= 1u;
// add the most significant bit in the dividend to the offering
offering += (dividend >> (dividend_bits - 1u));
// chop off the most significant bit from the dividend
dividend <<= (2049u - dividend_bits);
dividend >>= (2049u - dividend_bits);
// shift the bits in the quotient 1 to the left
quotient <<= 1u;
// decrement dividend_bits
--dividend_bits;
}
// increment quotient
++quotient;
// calculate the remainder
remainder = offering - operand_divisor;
// get the number of bits in the remainder
remainder_bits = remainder.num_bits();
}
// shift the bits in the quotient to the left by dividend_bits
quotient <<= dividend_bits;
return quotient;
}
uint2048 operator%(const uint2048& operand_dividend, const uint2048& operand_divisor){
uint2048 quotient;
uint2048 remainder;
if (operand_dividend < operand_divisor) return operand_dividend;
quotient = operand_dividend / operand_divisor;
remainder = operand_dividend - (operand_divisor * quotient);
return remainder;
}
uint2048 operator&(const uint2048& operand_a, const uint2048& operand_b){
uint2048 ret;
uint64_t a, b;
for (auto i = 0u; i < 32u; ++i){
a = operand_a.parts_[i];
b = operand_b.parts_[i];
ret.parts_[i] = a & b;
}
return ret;
}
uint64_t operator&(const uint2048& operand_a, uint64_t operand_b){
uint64_t ret;
uint64_t a;
a = operand_a.parts_[0u];
ret = a & operand_b;
return ret;
}
uint2048 operator<<(const uint2048& operand_a, uint16_t operand_b){
uint2048 ret;
if (operand_b >= 2048u) return ret;
uint64_t a, b;
auto overflow = 0ull;
auto shift = operand_b / 64u;
for (auto i = 0u; i < (32u - shift); ++i) ret.parts_[i + shift] = operand_a.parts_[i];
if (operand_b %= 64u)
for (auto i = 0u; i < 32u; ++i){
a = ret.parts_[i];
b = a << operand_b;
ret.parts_[i] = b + overflow;
overflow = a >> (64u - operand_b);
}
return ret;
}
uint2048 operator>>(const uint2048& operand_a, uint16_t operand_b){
uint2048 ret;
if (operand_b >= 2048u) return ret;
uint64_t a, b;
auto overflow = 0ull;
auto shift = operand_b / 64u;
for (auto i = 0u; i < (32u - shift); ++i) ret.parts_[i] = operand_a.parts_[i + shift];
if (operand_b %= 64u)
for (auto i = 0u; i < 32u; ++i){
a = ret.parts_[31u - i];
b = a >> operand_b;
ret.parts_[31u - i] = b + overflow;
overflow = a << (64u - operand_b);
}
return ret;
}
// - comparison -
bool operator==(const uint2048& operand_a, const uint2048& operand_b){
uint64_t a, b;
for (auto i = 0u; i < 32u; ++i){
a = operand_a.parts_[31u - i];
b = operand_b.parts_[31u - i];
if (a != b) return false;
}
return true;
}
bool operator==(const uint2048& operand_a, uint64_t operand_b){
uint64_t a;
for (auto i = 0u; i < 31u; ++i){
a = operand_a.parts_[31u - i];
if (a != 0ull) return false;
}
return operand_a.parts_[0] == operand_b;
}
bool operator!=(const uint2048& operand_a, const uint2048& operand_b){
return !(operand_a == operand_b);
}
bool operator<(const uint2048& operand_a, const uint2048& operand_b){
uint64_t a, b;
for (auto i = 0u; i < 32u; ++i){
a = operand_a.parts_[31u - i];
b = operand_b.parts_[31u - i];
if (a < b) return true;
else if (b < a) return false;
}
return false;
}
bool operator>(const uint2048& operand_a, const uint2048& operand_b){
return operand_b < operand_a;
}
bool operator<=(const uint2048& operand_a, const uint2048& operand_b){
return !(operand_b < operand_a);
}
bool operator>=(const uint2048& operand_a, const uint2048& operand_b){
return !(operand_a < operand_b);
}
// --- static functions ---
uint2048 gcd_mod(const uint2048& a, const uint2048& b){
uint2048 temp_a, temp_b;
uint2048 temp_t;
temp_a = a;
temp_b = b;
while (temp_b != 0ull){
temp_t = temp_b;
temp_b = temp_a % temp_b;
temp_a = temp_t;
}
return temp_a;
}
uint2048 gcd_sub(const uint2048& a, const uint2048& b){
uint2048 temp_a, temp_b;
temp_a = a;
temp_b = b;
while (temp_a != temp_b){
if (temp_a > temp_b) temp_a -= temp_b;
else temp_b -= temp_a;
}
return temp_a;
}