-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinteger.cpp
executable file
·1434 lines (1217 loc) · 40.6 KB
/
integer.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "integer.h"
constexpr INTEGER_DIGIT_T integer::NEG1;
constexpr std::size_t integer::OCTETS;
constexpr std::size_t integer::BITS;
constexpr INTEGER_DIGIT_T integer::HIGH_BIT;
constexpr integer::Sign integer::POSITIVE;
constexpr integer::Sign integer::NEGATIVE;
integer & integer::trim(){ // remove top 0 digits to save memory
while (!_value.empty() && !_value[0]){
_value.pop_front();
}
if (_value.empty()){ // change sign to false if _value is 0
_sign = integer::POSITIVE;
}
return *this;
}
// Constructors
integer::integer() :
_sign(integer::POSITIVE),
_value()
{}
integer::integer(const integer & copy) :
_sign(copy._sign),
_value(copy._value)
{
trim();
}
integer::integer(integer && copy) :
_sign(std::move(copy._sign)),
_value(std::move(copy._value))
{
copy = 0;
trim();
}
integer::integer(const integer::REP & rhs, const integer::Sign & sign) :
_sign(sign),
_value(rhs)
{
trim();
}
integer::integer(const bool & b) :
_sign(false),
_value(1, b)
{
trim();
}
// Special Constructor for Strings
// bases 2-16 and 256 are allowed
// Written by Corbin http://codereview.stackexchange.com/a/13452
// Modified by me
integer::integer(const std::string & str, const integer & base) : integer()
{
if ((2 <= base) && (base <= 16)){
if (!str.size()){
return;
}
integer::Sign sign = integer::POSITIVE;
std::string::size_type index = 0;
// minus sign indicates negative value
if (str[0] == '-'){
// make sure there are more digits
if (str.size() < 2){
throw std::runtime_error("Error: Input string is too short");
}
sign = integer::NEGATIVE;
index++;
}
// process characters
for(; index < str.size(); index++){
uint8_t d = std::tolower(str[index]);
if (std::isdigit(d)){ // 0-9
d -= '0';
if (d >= base){
throw std::runtime_error(std::string("Error: Not a digit in base ") + base.str(10) + ": '"+ str[index] + "'");
}
}
else if (std::isxdigit(d)){ // a-f
d -= 'a' - 10;
if (d >= base){
throw std::runtime_error(std::string("Error: Not a digit in base ") + base.str(10) + ": '"+ str[index] + "'");
}
}
else{ // bad character
throw std::runtime_error(std::string("Error: Not a digit in base ") + base.str(10) + ": '"+ str[index] + "'");
}
*this = (*this * base) + d;
}
_sign = sign;
}
else if (base == 256){
// process characters
for(unsigned char const & c : str){
*this = (*this << 8) | (c & 0xff);
}
}
else{
throw std::runtime_error("Error: Cannot convert from base " + base.str(10));
}
trim();
}
// RHS input args only
// Assignment Operators
integer & integer::operator=(const integer & rhs){
_sign = rhs._sign;
_value = rhs._value;
return trim();
}
integer & integer::operator=(integer && rhs){
if (*this != rhs){
_sign = rhs._sign;
_value = rhs._value;
rhs = 0;
}
return trim();
}
// Typecast Operators
integer::operator bool() const {
return !_value.empty();
}
integer::operator uint8_t() const {
const uint8_t out = static_cast <uint8_t> (_value.empty()?0:_value.back() & 255);
return _sign?-out:out;
}
integer::operator uint16_t() const {
uint16_t out = 0;
const integer::REP_SIZE_T d = std::min(digits(), std::max((integer::REP_SIZE_T) 2 / integer::OCTETS, (integer::REP_SIZE_T) 1));
for(integer::REP_SIZE_T x = 0; x < d; x++){
out += static_cast <uint16_t> (_value[digits() - x - 1]) << (x * integer::BITS);
}
return _sign?-out:out;
}
integer::operator uint32_t() const {
uint32_t out = 0;
const integer::REP_SIZE_T d = std::min(digits(), std::max((integer::REP_SIZE_T) 4 / integer::OCTETS, (integer::REP_SIZE_T) 1));
for(integer::REP_SIZE_T x = 0; x < d; x++){
out += static_cast <uint32_t> (_value[digits() - x - 1]) << (x * integer::BITS);
}
return _sign?-out:out;
}
integer::operator uint64_t() const {
uint64_t out = 0;
const integer::REP_SIZE_T d = std::min(digits(), std::max((integer::REP_SIZE_T) 8 / integer::OCTETS, (integer::REP_SIZE_T) 1));
for(integer::REP_SIZE_T x = 0; x < d; x++){
out += static_cast <uint64_t> (_value[digits() - x - 1]) << (x * integer::BITS);
}
return _sign?-out:out;
}
integer::operator int8_t() const {
const int8_t out = static_cast <int8_t> (_value.empty()?0:_value.back() & 255);
return _sign?-out:out;
}
integer::operator int16_t() const {
int16_t out = 0;
const integer::REP_SIZE_T d = std::min(digits(), std::max((integer::REP_SIZE_T) 2 / integer::OCTETS, (integer::REP_SIZE_T) 1));
for(integer::REP_SIZE_T x = 0; x < d; x++){
out += static_cast <int16_t> (_value[digits() - x - 1]) << (x * integer::BITS);
}
return _sign?-out:out;
}
integer::operator int32_t() const {
int32_t out = 0;
const integer::REP_SIZE_T d = std::min(digits(), std::max((integer::REP_SIZE_T) 4 / integer::OCTETS, (integer::REP_SIZE_T) 1));
for(integer::REP_SIZE_T x = 0; x < d; x++){
out += static_cast <int32_t> (_value[digits() - x - 1]) << (x * integer::BITS);
}
return _sign?-out:out;
}
integer::operator int64_t() const {
int64_t out = 0;
const integer::REP_SIZE_T d = std::min(digits(), std::max((integer::REP_SIZE_T) 8 / integer::OCTETS, (integer::REP_SIZE_T) 1));
for(integer::REP_SIZE_T x = 0; x < d; x++){
out += static_cast <int64_t> (_value[digits() - x - 1]) << (x * integer::BITS);
}
return _sign?-out:out;
}
// Bitwise Operators
integer integer::operator&(const integer & rhs) const {
integer::REP out;
const integer::REP_SIZE_T max_bits = std::max(bits(), rhs.bits());
const integer left = ( _sign == integer::POSITIVE)?*this:twos_complement(max_bits);
const integer right = (rhs._sign == integer::POSITIVE)?rhs:rhs.twos_complement(max_bits);
// AND matching digits
for(integer::REP::const_reverse_iterator i = left._value.rbegin(), j = right._value.rbegin(); (i != left._value.rend()) && (j != right._value.rend()); i++, j++){
out.push_front(*i & *j);
}
// drop any digits that don't match up
integer OUT(out, integer::POSITIVE);
if (_sign & rhs._sign){
OUT = OUT.twos_complement(max_bits);
}
return OUT.trim();
}
integer & integer::operator&=(const integer & rhs){
return *this = *this & rhs;
}
integer integer::operator|(const integer & rhs) const {
const integer::REP_SIZE_T max_bits = std::max(bits(), rhs.bits());
const integer left = ( _sign == integer::POSITIVE)?*this:twos_complement(max_bits);
const integer right = (rhs._sign == integer::POSITIVE)?rhs:rhs.twos_complement(max_bits);
integer::REP out;
integer::REP::const_reverse_iterator i = left._value.rbegin(), j = right._value.rbegin();
// OR matching digits
for(; (i != left._value.rend()) && (j != right._value.rend()); i++, j++){
out.push_front(*i | *j);
}
// push rest of *this into value
while (i != left._value.rend()){
out.push_front(*i++);
}
// push rest of rhs into value
while (j != right._value.rend()){
out.push_front(*j++);
}
integer OUT(out, integer::POSITIVE);
if (_sign | rhs._sign){
OUT = OUT.twos_complement(max_bits);
}
return OUT.trim();
}
integer & integer::operator|=(const integer & rhs){
return *this = *this | rhs;
}
integer integer::operator^(const integer & rhs) const {
const integer::REP_SIZE_T max_bits = std::max(bits(), rhs.bits());
const integer left = ( _sign == integer::POSITIVE)?*this:twos_complement(max_bits);
const integer right = (rhs._sign == integer::POSITIVE)?rhs:rhs.twos_complement(max_bits);
integer::REP out;
integer::REP::const_reverse_iterator i = left._value.rbegin(), j = right._value.rbegin();
// XOR matching digits
for(; (i != left._value.rend()) && (j != right._value.rend()); i++, j++){
out.push_front(*i ^ *j);
}
// push *this into value
while (i != left._value.rend()){
out.push_front(*i++);
}
// push rhs into value
while (j != right._value.rend()){
out.push_front(*j++);
}
integer OUT(out, integer::POSITIVE);
if (_sign ^ rhs._sign){
OUT = OUT.twos_complement(max_bits);
}
return OUT.trim();
}
integer & integer::operator^=(const integer & rhs){
return *this = *this ^ rhs;
}
integer integer::operator~() const {
// in case value is 0
if (_value.empty()){
return 1;
}
integer::REP out = _value;
// invert whole digits
for(integer::REP_SIZE_T i = 1; i < out.size(); i++){
out[i] ^= integer::NEG1;
}
INTEGER_DIGIT_T mask = HIGH_BIT;
while (!(out[0] & mask)){
mask >>= 1;
}
// invert bits of partial digit
while (mask){
out[0] ^= mask;
mask >>= 1;
}
return integer(out, _sign);
}
// Bit Shift Operators
// left bit shift. sign is maintained
integer integer::operator<<(const integer & shift) const {
if (!*this || !shift){
return *this;
}
if (shift < 0){
throw std::runtime_error("Error: Negative shift amount");
}
const std::pair <integer, integer> qr = dm(shift, integer::BITS);
const integer & whole = qr.first; // number of zeros to add to the back
const INTEGER_DIGIT_T push = qr.second; // push left by this many bits
const INTEGER_DIGIT_T pull = integer::BITS - push; // pull "push" bits from the right
integer::REP out = _value;
out.push_front(0); // extra digit for shifting into
out.push_back(0); // extra digit for shifting from
// do this part first to avoid shifting zeros
for(integer::REP_SIZE_T i = 0; i < (out.size() - 1); i++){
INTEGER_DOUBLE_DIGIT_T d = out[i];
d = (d << push) | (out[i + 1] >> pull);
out[i] = d & NEG1;
// out[i] = (out[i] << push) | (out[i + 1] >> pull);
}
if (!out[0]){ // if the top digit is still 0
out.pop_front(); // remove it
}
if (!whole){ // if there was no need for the 0 at the end
out.pop_back(); // remove it
}
else{
// push back zeros, excluding the one already there
out.insert(out.end(), whole - 1, 0);
}
return integer(out, _sign);
}
integer & integer::operator<<=(const integer & shift){
return *this = *this << integer(shift);
}
// right bit shift. sign is maintained
integer integer::operator>>(const integer & shift) const {
if (shift < 0){
throw std::runtime_error("Error: Negative shift amount");
}
if (shift >= bits()){
return 0;
}
const std::pair <integer, integer> qr = dm(shift, integer::BITS);
const integer & whole = qr.first; // number of digits to pop off
const INTEGER_DIGIT_T push = qr.second; // push right by this many bits
const INTEGER_DIGIT_T pull = integer::BITS - push; // pull "push" bits from the left
integer::REP out = _value;
// pop off whole digits
for(integer i = 0; i < whole; i++){
out.pop_back();
}
if (push){
out.push_front(0); // extra 0 for shifting from
for(integer::REP_SIZE_T i = 1; i < out.size(); i++){
out[out.size() - i] = (out[out.size() - i - 1] << pull) | (out[out.size() - i] >> push);
}
out.pop_front();
}
return integer(out, _sign);
}
integer & integer::operator>>=(const integer & shift){
return *this = *this >> integer(shift);
}
// Logical Operators
bool integer::operator!(){
return !static_cast <bool> (*this);
}
// Comparison Operators
bool integer::operator==(const integer & rhs) const {
return ((_sign == rhs._sign) && (_value == rhs._value));
}
bool integer::operator!=(const integer & rhs) const {
return !(*this == rhs);
}
// operator> not considering signs
bool integer::gt(const integer & lhs, const integer & rhs) const {
if (lhs._value.size() > rhs._value.size()){
return true;
}
if (lhs._value.size() < rhs._value.size()){
return false;
}
if (lhs._value == rhs._value){
return false;
}
for(integer::REP_SIZE_T i = 0; i < lhs._value.size(); i++){
if (lhs._value[i] != rhs._value[i]){
return lhs._value[i] > rhs._value[i];
}
}
return false;
}
bool integer::operator>(const integer & rhs) const {
if ( (_sign == integer::NEGATIVE) && // - > +
(rhs._sign == integer::POSITIVE)){
return false;
}
else if ( (_sign == integer::POSITIVE) && // + > -
(rhs._sign == integer::NEGATIVE)){
return true;
}
else if ( (_sign == integer::NEGATIVE) && // - > -
(rhs._sign == integer::NEGATIVE)){
return gt(rhs, *this);
}
// else if ( (_sign == integer::POSITIVE) && // + > +
// (rhs._sign == integer::POSITIVE)){
return gt(*this, rhs);
}
bool integer::operator>=(const integer & rhs) const {
return ((*this > rhs) | (*this == rhs));
}
// operator< not considering signs
bool integer::lt(const integer & lhs, const integer & rhs) const {
if (lhs._value.size() < rhs._value.size()){
return true;
}
if (lhs._value.size() > rhs._value.size()){
return false;
}
if (lhs._value == rhs._value){
return false;
}
for(integer::REP_SIZE_T i = 0; i < lhs._value.size(); i++){
if (lhs._value[i] != rhs._value[i]){
return lhs._value[i] < rhs._value[i];
}
}
return false;
}
bool integer::operator<(const integer & rhs) const {
if ( (_sign == integer::NEGATIVE) && // - < +
(rhs._sign == integer::POSITIVE)){
return true;
}
else if ( (_sign == integer::POSITIVE) && // + < -
(rhs._sign == integer::NEGATIVE)){
return false;
}
else if ( (_sign == integer::NEGATIVE) && // - < -
(rhs._sign == integer::NEGATIVE)){
return lt(rhs, *this);
}
// else if ( (_sign == integer::POSITIVE) && // + < +
// (rhs._sign == integer::POSITIVE)){
return lt(*this, rhs);
}
bool integer::operator<=(const integer & rhs) const {
return ((*this < rhs) | (*this == rhs));
}
// Arithmetic Operators
integer integer::add(const integer & lhs, const integer & rhs) const {
integer::REP out;
integer::REP::const_reverse_iterator i = lhs._value.rbegin(), j = rhs._value.rbegin();
bool carry = false;
INTEGER_DOUBLE_DIGIT_T sum;
// add up matching digits
for(; ((i != lhs._value.rend()) && (j != rhs._value.rend())); i++, j++){
sum = static_cast <INTEGER_DOUBLE_DIGIT_T> (*i) + static_cast <INTEGER_DOUBLE_DIGIT_T> (*j) + carry;
out.push_front(sum);
carry = (sum > integer::NEG1);
}
// copy in lhs extra digits
for(; i != lhs._value.rend(); i++){
sum = static_cast <INTEGER_DOUBLE_DIGIT_T> (*i) + carry;
out.push_front(sum);
carry = (sum > integer::NEG1);
}
// copy in rhs extra digits
for(; j != rhs._value.rend(); j++){
sum = static_cast <INTEGER_DOUBLE_DIGIT_T> (*j) + carry;
out.push_front(sum);
carry = (sum > integer::NEG1);
}
if (carry){
out.push_front(1);
}
return integer(out);
}
integer integer::operator+(const integer & rhs) const {
if (!rhs){
return *this;
}
if (!*this){
return rhs;
}
integer out = *this;
if (gt(out, rhs)){ // lhs > rhs
if (_sign == rhs._sign){ // same sign: lhs + rhs
out = add(out, rhs);
}
else{ // different signs: lhs - rhs
out = sub(out, rhs);
}
out._sign = _sign; // lhs sign dominates
}
else if (lt(out, rhs)){ // lhs < rhs
if (_sign == rhs._sign){ // same sign: rhs + lhs
out = add(rhs, out);
}
else{ // different sign: rhs - lhs
out = sub(rhs, out);
}
out._sign = rhs._sign; // rhs sign dominates
}
else{ // lhs == rhs
if (_sign == rhs._sign){ // same sign: double value
out <<= 1;
out._sign = _sign;
}
else{ // different signs: 0
return 0;
}
}
out.trim();
return out;
}
integer & integer::operator+=(const integer & rhs){
return *this = *this + rhs;
}
// Subtraction as done by hand
integer integer::long_sub(const integer & lhs, const integer & rhs) const {
// rhs always smaller than lhs
integer out = lhs;
integer::REP_SIZE_T lsize = out._value.size() - 1;
integer::REP_SIZE_T rsize = rhs._value.size() - 1;
for(integer::REP_SIZE_T x = 0; x <= rsize; x++){
// if top is bigger than or equal to the bottom, just substract
if (out._value[lsize - x] >= rhs._value[rsize - x]){
out._value[lsize - x] -= rhs._value[rsize - x];
}
else{// find a higher digit to carry from
integer::REP_SIZE_T y = lsize - x - 1;
// if this goes out of bounds, something is wrong
while (!out._value[y]){
y--;
}
out._value[y]--;
y++;
for(; y < lsize - x; y++){
out._value[y] = integer::NEG1;
}
out._value[y] = static_cast <INTEGER_DOUBLE_DIGIT_T> (out._value[y]) + (static_cast <uint64_t> (1) << integer::BITS) - rhs._value[rsize - x];
}
}
return out;
}
//// Two's Complement Subtraction
//integer integer::two_comp_sub(const integer & lhs, const integer & rhs){
// rhs = rhs.twos_complement(lhs.bits());
// return add(lhs, rhs) & (~(integer(1) << lhs.bits())); // Flip bits to get max of 1 << x
//}
// subtraction not considering signs
// lhs must be larger than rhs
integer integer::sub(const integer & lhs, const integer & rhs) const {
if (!rhs){
return lhs;
}
if (!lhs){
return -rhs;
}
if (lhs == rhs){
return 0;
}
return long_sub(lhs, rhs);
// return two_comp_sub(lhs, rhs);
}
integer integer::operator-(const integer & rhs) const {
integer out = *this;
if (gt(out, rhs)){ // if lhs > rhs
if (out._sign == rhs._sign){ // same signs
out = sub(out, rhs);
}
else if (out._sign != rhs._sign){ // different signs
out = add(out, rhs);
}
out._sign = _sign; // lhs sign dominates
}
else if (lt(out, rhs)){ // if lhs < rhs
if ( (_sign == integer::NEGATIVE) && // - - -
(rhs._sign == integer::NEGATIVE)){
out = sub(rhs, out);
out._sign = integer::POSITIVE;
}
else if ( (_sign == integer::NEGATIVE) && // - - +
(rhs._sign == integer::POSITIVE)){
out = add(rhs, out);
out._sign = integer::NEGATIVE;
}
else if ( (_sign == integer::POSITIVE) && // + - -
(rhs._sign == integer::NEGATIVE)){
out = add(out, rhs);
out._sign = integer::POSITIVE;
}
else if ( (_sign == integer::POSITIVE) && // + - +
(rhs._sign == integer::POSITIVE)){
out = sub(rhs, out);
out._sign = integer::NEGATIVE;
}
}
else{ // if lhs == rhs
if (_sign == rhs._sign){ // same signs: 0
return 0;
}
else{ // different signs: double value
out <<= 1;
out._sign = _sign;
}
}
out.trim();
return out;
}
integer & integer::operator-=(const integer & rhs){
return *this = *this - rhs;
}
// // Peasant Multiplication
// integer integer::peasant(const integer & lhs, const integer & rhs) const {
// integer rhs_copy = rhs;
// integer sum = 0;
// for(integer::REP_SIZE_T x = 0; x < lhs.bits(); x++){
// if (lhs[x]){
// sum += add(sum, rhs_copy);
// }
// rhs_copy <<= 1;
// }
// return sum;
// }
// // Recurseive Peasant Algorithm
// integer integer::recursive_peasant(const integer & lhs, const integer & rhs) const {
// if (!rhs){
// return 0;
// }
// if (rhs & 1){
// return lhs + recursive_peasant(lhs << 1, rhs >> 1);
// }
// return recursive_peasant(lhs << 1, rhs >> 1);
// }
// // Recursive Multiplication
// integer integer::recursive_mult(const integer & lhs, const integer & rhs) const {
// if (!rhs){
// return 0;
// }
// integer z = recursive_mult(lhs, rhs >> 1);
// if (!(rhs & 1)){
// return z << 1;
// }
// return add(lhs, z << 1);
// }
// // Karatsuba Algorithm
// integer integer::karatsuba(const integer & lhs, const integer & rhs, integer bm) const {
// // b is integer::REP = 256
// // m is chars = 4
// // bm is max _value = b ^ m
// if ((lhs <= bm) | (rhs <= bm))
// return peasant(lhs, rhs);
// std::pair <integer, integer> x = dm(lhs, bm);
// std::pair <integer, integer> y = dm(rhs, bm);
// integer x0 = x.second;
// integer x1 = x.first;
// integer y0 = y.second;
// integer y1 = y.first;
// integer z0 = karatsuba(x0, y0);
// integer z2 = karatsuba(x1, y1);
// integer z1 = sub(sub(karatsuba(add(x1, x0), add(y1, y0)), z2), z0);
// return add(karatsuba(add(karatsuba(z2, bm), z1), bm), z0);
// }
// // Toom-Cook multiplication
// // as described at http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplications
// // This implementation is a bit weird. In the pointwise Multiplcation step, using
// // operator* and long_mult works, but everything else fails.
// // It's also kind of slow.
// integer integer::toom_cook_3(integer m, integer n, integer bm){
// if ((m <= bm) | (n <= bm)){
// return peasant(m, n);
// }
// // Splitting
// integer i = integer(std::max(m.log(3), n.log(3))) / 3 + 1;
// integer bi = pow(integer(3), i);
// integer B = 1;
// integer integer::REP = 10;
// while (B < bi){
// B *= integer::REP;
// }
// integer M[3], N[3];
// for(uint8_t i = 0; i < 3; i++){
// std::pair <integer, integer> tm = dm(m, B);
// std::pair <integer, integer> tn = dm(n, B);
// m = tm.first;
// n = tn.first;
// M[i] = tm.second;
// N[i] = tn.second;
// }
// // Evaluation
// // {0, 1, -1, -2, inf}
// integer p[5] = {M[0], M[0] + M[1] + M[2], M[0] - M[1] + M[2], M[0] - M[1] - M[1] + M[2] + M[2] + M[2] + M[2], M[2]};
// integer q[5] = {N[0], N[0] + N[1] + N[2], N[0] - N[1] + N[2], N[0] - N[1] - N[1] + N[2] + N[2] + N[2] + N[2], N[2]};
// // Pointwise Multiplication
// integer r[5];
// for(uint8_t i = 0; i < 5; i++)
// r[i] = p[i] * q[i]; // don't understand why only integer::operator* and long_mult can be used here
// // Interpolation
// integer r0 = r[0];
// integer r4 = r[4];
// integer r3 = (r[3] - r[1]) / 3;
// integer r1 = (r[1] - r[2]) / 2;
// integer r2 = r[2] - r[0];
// r3 = (r2 - r3) / 2 + r4 + r4;
// r2 = r2 + r1 - r4;
// r1 = r1 - r3;
// // Recomposition
// return peasant(peasant(peasant(peasant(r4, B) + r3, B) + r2, B) + r1, B) + r0;
// }
// // Long multiplication
// integer integer::long_mult(const integer & lhs, const integer & rhs) const {
// unsigned int zeros = 0;
// integer row, out = 0;
// for(integer::REP::const_reverse_iterator i = lhs._value.rbegin(); i != lhs._value.rend(); i++){
// row._value = integer::REP(zeros++, 0); // zeros on the right hand side
// INTEGER_DIGIT_T carry = 0;
// for(integer::REP::const_reverse_iterator j = rhs._value.rbegin(); j != rhs._value.rend(); j++){
// INTEGER_DOUBLE_DIGIT_T prod = (INTEGER_DOUBLE_DIGIT_T) *i * (INTEGER_DOUBLE_DIGIT_T) *j + carry;// multiply through
// row._value.push_front(prod & integer::NEG1);
// carry = prod >> integer::BITS;
// }
// if (carry){
// row._value.push_front(carry);
// }
// out = add(out, row);
// }
// return out;
// }
//Private FFT helper function
int integer::fft(std::deque<double>& data, bool dir) const
{
//Verify size is a power of two
std::size_t n = data.size()/2;
if ((n == 0) || (n & (n-1))) return 1;
//rearrange data for signal flow chart
std::size_t bitr_j = 1;
for (std::size_t i = 3; i < 2*n-1; i += 2)
{
std::size_t msz = n;
while (bitr_j >= msz)
{
bitr_j -= msz;
msz >>= 1;
}
bitr_j += msz;
if (bitr_j > i)
{
double swap = data[bitr_j-1];
data[bitr_j-1] = data[i-1];
data[i-1] = swap;
swap = data[bitr_j];
data[bitr_j] = data[i];
data[i] = swap;
}
}
//Perform "butterfly" calculations
std::size_t lmax = 2;
while (lmax <= n)
{
double wr = 1;
double wi = 0;
double theta = (2*M_PI)/double(lmax*(dir?1.0:-1.0));
double wpr = cos(theta);
double wpi = sin(theta);
int pstep = 2*lmax;
for (std::size_t l = 1; l < lmax; l += 2)
{
for (std::size_t p = l; p < 2*n; p += pstep)
{
std::size_t q = p + lmax;
double tempr = wr*data[q-1] - wi*data[q];
double tempi = wr*data[q] + wi*data[q-1];
data[q-1] = data[p-1] - tempr;
data[q] = data[p] - tempi;
data[p-1] = data[p-1] + tempr;
data[p] = data[p] + tempi;
}
//Find the next power of W
double wtemp = wr;
wr = wr*wpr - wi*wpi;
wi = wi*wpr + wtemp*wpi;
}
lmax = pstep;
}
//All is good
return 0;
}
// FFT-based multiplication
//Based on the convolution theorem which states that the Fourier
//transform of a convolution is the pointwise product of their
//Fourier transforms.
integer integer::fft_mult(const integer& lhs, const integer& rhs) const {
//Convert each integer to input wanted by fft()
size_t size = 1;
while (size < lhs._value.size()*2){
size <<= 1;
}
while (size < rhs._value.size()*2){
size <<= 1;
}
std::deque<double> lhs_fft;
lhs_fft.resize(size*2, 0);
for (size_t i = 0; i < lhs._value.size(); i++){
lhs_fft[i*2] = double(lhs._value[lhs._value.size()-1-i]);
}
std::deque<double> rhs_fft;
rhs_fft.resize(size*2, 0);
for (size_t i = 0; i < rhs._value.size(); i++){
rhs_fft[i*2] = double(rhs._value[rhs._value.size()-1-i]);
}
//Compute the FFT of each
fft(lhs_fft);
fft(rhs_fft);
//Perform pointwise multiplication (numbers are complex)
std::deque<double> out_fft(2*size);
for (size_t i = 0; i < 2*size; i+=2){
out_fft[i] = lhs_fft[i]*rhs_fft[i] - lhs_fft[i+1]*rhs_fft[i+1];
out_fft[i+1] = lhs_fft[i]*rhs_fft[i+1] + lhs_fft[i+1]*rhs_fft[i];
}
//Compute the inverse FFT of this number
//remember to properly scale afterwards!
fft(out_fft, false);
for (size_t i = 0; i < 2*size; i++){
out_fft[i] /= size;
}
//Convert back to integer, carrying along the way
double carry = 0;
integer out;
for (size_t i = 0; i < 2*size; i+=2){
double current = out_fft[i]+carry;
if (current > double(integer::NEG1)){
carry = current / (double(integer::NEG1)+1);
carry = double(floor(carry+0.0001));
current = current - (carry*(integer::NEG1+1));
}
else {
carry = 0;
}
out._value.push_front(INTEGER_DIGIT_T(current+0.0001));
}
//Finish up
return out;
}
integer integer::operator*(const integer & rhs) const {
// quick checks
if (!*this || !rhs){ // if multiplying by 0
return 0;
}
if (*this == 1){ // if multiplying by 1
return rhs;
}
if (rhs == 1){ // if multiplying by 1
return *this;
}
// integer out = peasant(*this, rhs);
// integer out = recursive_peasant(*this, rhs);
// integer out = recursive_mult(*this, rhs);
// integer out = karatsuba(*this, rhs);
// integer out = toom_cook_3(*this, rhs);
// integer out = long_mult(*this, rhs);
integer out = fft_mult(*this, rhs);
out._sign = _sign ^ rhs._sign;
out.trim();
return out;
}
integer & integer::operator*=(const integer & rhs){
return *this = *this * rhs;
}
// // Naive Division: keep subtracting until lhs == 0
// std::pair <integer, integer> integer::naive_divmod(const integer & lhs, const integer & rhs) const {