-
Notifications
You must be signed in to change notification settings - Fork 39
/
bignbr.c
2770 lines (2687 loc) · 77.9 KB
/
bignbr.c
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
//
// This file is part of Alpertron Calculators.
//
// Copyright 2015-2021 Dario Alejandro Alpern
//
// Alpertron Calculators is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Alpertron Calculators is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
//
#include <string.h>
#include <math.h>
#include <assert.h>
#include "bignbr.h"
#include "factor.h"
#include "expression.h"
#include "skiptest.h"
enum eOper
{
OPERATION_AND = 0,
OPERATION_OR,
OPERATION_XOR,
};
static struct
{
unsigned int seed[4];
} randomSeed;
static BigInteger Temp;
static BigInteger Temp2;
static BigInteger Temp3;
static BigInteger Temp4;
static BigInteger Base;
static BigInteger Power;
static BigInteger expon;
static bool ProcessExpon[(MAX_LEN*BITS_PER_GROUP) + 1000];
static bool primes[(MAX_LEN*BITS_PER_GROUP) + 1000];
extern limb Mult1[MAX_LEN];
extern limb Mult2[MAX_LEN];
extern limb Mult3[MAX_LEN];
extern limb Mult4[MAX_LEN];
extern int valueQ[MAX_LEN];
extern limb TestNbr[MAX_LEN];
extern limb MontgomeryMultR1[MAX_LEN];
int groupLen = 6;
int smallPrimes[SMALL_PRIMES_ARRLEN+1];
#ifdef __EMSCRIPTEN__
int percentageBPSW;
#ifdef FACTORIZATION_APP
static int savedSecond;
#endif
#endif
void CopyBigInt(BigInteger *pDest, const BigInteger *pSrc)
{
if (pDest != pSrc)
{
int lenBytes;
assert(pSrc->nbrLimbs >= 1);
pDest->sign = pSrc->sign;
pDest->nbrLimbs = pSrc->nbrLimbs;
lenBytes = (pSrc->nbrLimbs) * (int)sizeof(limb);
(void)memcpy(pDest->limbs, pSrc->limbs, lenBytes);
}
}
void AddBigInt(const limb *pAddend1, const limb *pAddend2, limb *pSum, int nbrLimbs)
{
const limb *ptrAddend1 = pAddend1;
const limb *ptrAddend2 = pAddend2;
limb *ptrSum = pSum;
assert(nbrLimbs >= 1);
unsigned int carry = 0;
for (int i = 0; i < nbrLimbs; i++)
{
carry = (carry >> BITS_PER_GROUP) + (unsigned int)ptrAddend1->x +
(unsigned int)ptrAddend2->x;
ptrAddend1++;
ptrAddend2++;
ptrSum->x = (int)carry & MAX_INT_NBR;
ptrSum++;
}
}
// If address of num and result match, BigIntDivide will overwrite num, so it must be executed after processing num.
void floordiv(const BigInteger *num, const BigInteger *den, BigInteger *result)
{
static BigInteger rem;
(void)BigIntRemainder(num, den, &rem);
if ((((num->sign == SIGN_NEGATIVE) && (den->sign == SIGN_POSITIVE)) ||
((num->sign == SIGN_POSITIVE) && !BigIntIsZero(num) && (den->sign == SIGN_NEGATIVE))) && !BigIntIsZero(&rem))
{
(void)BigIntDivide(num, den, result);
addbigint(result, -1);
}
else
{
(void)BigIntDivide(num, den, result);
}
}
void BigIntChSign(BigInteger *value)
{
if ((value->nbrLimbs == 1) && (value->limbs[0].x == 0))
{ // Value is zero. Do not change sign.
return;
}
if (value->sign == SIGN_POSITIVE)
{
value->sign = SIGN_NEGATIVE;
}
else
{
value->sign = SIGN_POSITIVE;
}
}
static void InternalBigIntAdd(const BigInteger *pAdd1, const BigInteger *pAdd2,
BigInteger *pSum, enum eSign addend2sign)
{
const BigInteger* pAddend1 = pAdd1;
const BigInteger* pAddend2 = pAdd2;
int ctr;
int nbrLimbs;
const limb *ptrAddend1;
const limb *ptrAddend2;
limb *ptrSum;
const BigInteger *pTemp;
enum eSign addend1Sign = pAddend1->sign;
enum eSign addend2Sign = addend2sign;
enum eSign tmpSign;
assert(pAddend1->nbrLimbs >= 1);
assert(pAddend2->nbrLimbs >= 1);
if (pAddend1->nbrLimbs < pAddend2->nbrLimbs)
{
tmpSign = addend1Sign;
addend1Sign = addend2Sign;
addend2Sign = tmpSign;
pTemp = pAddend1;
pAddend1 = pAddend2;
pAddend2 = pTemp;
} // At this moment, the absolute value of addend1 is greater than
// or equal than the absolute value of addend2.
else if (pAddend1->nbrLimbs == pAddend2->nbrLimbs)
{
for (ctr = pAddend1->nbrLimbs - 1; ctr >= 0; ctr--)
{
if (pAddend1->limbs[ctr].x != pAddend2->limbs[ctr].x)
{
break;
}
}
if ((ctr >= 0) && (pAddend1->limbs[ctr].x < pAddend2->limbs[ctr].x))
{
tmpSign = addend1Sign;
addend1Sign = addend2Sign;
addend2Sign = tmpSign;
pTemp = pAddend1;
pAddend1 = pAddend2;
pAddend2 = pTemp;
} // At this moment, the absolute value of addend1 is greater than
// or equal than the absolute value of addend2.
}
else
{ // Nothing to do.
}
nbrLimbs = pAddend2->nbrLimbs;
ptrAddend1 = pAddend1->limbs;
ptrAddend2 = pAddend2->limbs;
ptrSum = pSum->limbs;
if (addend1Sign == addend2Sign)
{ // Both addends have the same sign. Sum their absolute values.
unsigned int carry = 0;
unsigned int limbValue;
for (ctr = 0; ctr < nbrLimbs; ctr++)
{
carry = (carry >> BITS_PER_GROUP) + (unsigned int)ptrAddend1->x +
(unsigned int)ptrAddend2->x;
ptrAddend1++;
ptrAddend2++;
limbValue = carry & MAX_INT_NBR_U;
ptrSum->x = (int)limbValue;
ptrSum++;
}
nbrLimbs = pAddend1->nbrLimbs;
for (; ctr < nbrLimbs; ctr++)
{
carry = (carry >> BITS_PER_GROUP) + (unsigned int)ptrAddend1->x;
ptrAddend1++;
limbValue = carry & MAX_INT_NBR_U;
ptrSum->x = (int)limbValue;
ptrSum++;
}
if (carry >= LIMB_RANGE)
{
ptrSum->x = 1;
nbrLimbs++;
}
}
else
{ // Both addends have different sign. Subtract their absolute values.
unsigned int borrow = 0U;
for (ctr = 0; ctr < nbrLimbs; ctr++)
{
borrow = (unsigned int)ptrAddend1->x - (unsigned int)ptrAddend2->x -
(borrow >> BITS_PER_GROUP);
ptrSum->x = UintToInt(borrow & MAX_VALUE_LIMB);
ptrAddend1++;
ptrAddend2++;
ptrSum++;
}
nbrLimbs = pAddend1->nbrLimbs;
for (; ctr < nbrLimbs; ctr++)
{
borrow = (unsigned int)ptrAddend1->x - (borrow >> BITS_PER_GROUP);
ptrSum->x = UintToInt(borrow & MAX_VALUE_LIMB);
ptrAddend1++;
ptrSum++;
}
while ((nbrLimbs > 1) && (pSum->limbs[nbrLimbs - 1].x == 0))
{ // Loop that deletes non-significant zeros.
nbrLimbs--;
}
}
pSum->nbrLimbs = nbrLimbs;
pSum->sign = addend1Sign;
if ((pSum->nbrLimbs == 1) && (pSum->limbs[0].x == 0))
{ // Result is zero.
pSum->sign = SIGN_POSITIVE;
}
}
void BigIntAdd(const BigInteger* pAddend1, const BigInteger* pAddend2, BigInteger* pSum)
{
InternalBigIntAdd(pAddend1, pAddend2, pSum, pAddend2->sign);
}
void BigIntNegate(const BigInteger *pSrc, BigInteger *pDest)
{
if (pSrc != pDest)
{
CopyBigInt(pDest, pSrc);
}
BigIntChSign(pDest);
}
void BigIntSubt(const BigInteger *pMinuend, const BigInteger *pSubtrahend, BigInteger *pDifference)
{
if (pSubtrahend->sign == SIGN_POSITIVE)
{
InternalBigIntAdd(pMinuend, pSubtrahend, pDifference, SIGN_NEGATIVE);
}
else
{
InternalBigIntAdd(pMinuend, pSubtrahend, pDifference, SIGN_POSITIVE);
}
}
enum eExprErr BigIntMultiply(const BigInteger *pFact1, const BigInteger *pFact2, BigInteger *pProduct)
{
const BigInteger* pFactor1 = pFact1;
const BigInteger* pFactor2 = pFact2;
int nbrLimbsFactor1 = pFactor1->nbrLimbs;
int nbrLimbsFactor2 = pFactor2->nbrLimbs;
int nbrLimbs;
const BigInteger *temp;
assert(pFactor1->nbrLimbs >= 1);
assert(pFactor2->nbrLimbs >= 1);
if ((pFactor1->nbrLimbs == 1) || (pFactor2->nbrLimbs == 1))
{ // At least one the factors has only one limb.
int factor2;
if (pFactor1->nbrLimbs == 1)
{ // Force the second factor to have only one limb.
temp = pFactor1;
pFactor1 = pFactor2;
pFactor2 = temp;
}
// Multiply BigInteger by integer.
factor2 = ((pFactor2->sign == SIGN_POSITIVE)? pFactor2->limbs[0].x : -pFactor2->limbs[0].x);
multint(pProduct, pFactor1, factor2);
return EXPR_OK;
}
#ifdef FACTORIZATION_APP
// The maximum number that can be represented is 2^664380 ~ 10^200000
if ((pFactor1->nbrLimbs + pFactor2->nbrLimbs) > ((664380 / BITS_PER_GROUP) + 1))
#else
// The maximum number that can be represented is 2^66438 ~ 10^20000
if ((pFactor1->nbrLimbs + pFactor2->nbrLimbs) > ((66438 / BITS_PER_GROUP) + 1))
#endif
{
return EXPR_INTERM_TOO_HIGH;
}
multiplyWithBothLen(&pFactor1->limbs[0], &pFactor2->limbs[0], &pProduct->limbs[0],
nbrLimbsFactor1, nbrLimbsFactor2, &nbrLimbs);
nbrLimbs = nbrLimbsFactor1 + nbrLimbsFactor2;
if (pProduct->limbs[nbrLimbs - 1].x == 0)
{
nbrLimbs--;
}
pProduct->nbrLimbs = nbrLimbs;
if ((nbrLimbs == 1) && (pProduct->limbs[0].x == 0))
{
pProduct->sign = SIGN_POSITIVE;
}
else
{
if (pFactor1->sign == pFactor2->sign)
{
pProduct->sign = SIGN_POSITIVE;
}
else
{
pProduct->sign = SIGN_NEGATIVE;
}
}
return EXPR_OK;
}
enum eExprErr BigIntRemainder(const BigInteger *pDividend,
const BigInteger *pDivisor, BigInteger *pRemainder)
{
enum eExprErr rc;
assert(pDividend->nbrLimbs >= 1);
assert(pDivisor->nbrLimbs >= 1);
if (BigIntIsZero(pDivisor))
{ // If divisor = 0, then remainder is the dividend.
CopyBigInt(pRemainder, pDividend);
return EXPR_OK;
}
CopyBigInt(&Temp2, pDividend);
rc = BigIntDivide(pDividend, pDivisor, &Base); // Get quotient of division.
if (rc != EXPR_OK)
{
return rc;
}
rc = BigIntMultiply(&Base, pDivisor, &Base);
if (rc != EXPR_OK)
{
return rc;
}
BigIntSubt(&Temp2, &Base, pRemainder);
return EXPR_OK;
}
void intToBigInteger(BigInteger *bigint, int value)
{
if (value >= 0)
{
bigint->limbs[0].x = value;
bigint->sign = SIGN_POSITIVE;
}
else
{
bigint->limbs[0].x = -value;
bigint->sign = SIGN_NEGATIVE;
}
bigint->nbrLimbs = 1;
}
void longToBigInteger(BigInteger *bigint, int64_t value)
{
uint64_t u64Value;
int nbrLimbs = 0;
if (value >= 0)
{
bigint->sign = SIGN_POSITIVE;
u64Value = (uint64_t)value;
}
else
{
bigint->sign = SIGN_NEGATIVE;
u64Value = (uint64_t)(-value);
}
do
{
unsigned int limbValue = (unsigned int)u64Value & MAX_VALUE_LIMB;
bigint->limbs[nbrLimbs].x = (int)limbValue;
nbrLimbs++;
u64Value >>= BITS_PER_GROUP;
} while (u64Value != 0U);
bigint->nbrLimbs = nbrLimbs;
}
void expBigNbr(BigInteger *bignbr, double logar)
{
unsigned int mostSignificantLimb;
double dShLeft = floor(logar / LOG_2);
double dMant = logar - (dShLeft * LOG_2);
double argExp;
unsigned int shLeft = (unsigned int)dShLeft;
bignbr->sign = SIGN_POSITIVE;
if (shLeft < (unsigned int)BITS_PER_GROUP)
{
argExp = logar;
shLeft = 0;
}
else
{
argExp = dMant + ((double)BITS_PER_GROUP_MINUS_1 * LOG_2);
shLeft -= (unsigned int)BITS_PER_GROUP_MINUS_1;
}
mostSignificantLimb = (unsigned int)floor(exp(argExp) + 0.5);
if (mostSignificantLimb == LIMB_RANGE)
{
bignbr->limbs[0].x = 0;
bignbr->limbs[1].x = 1;
bignbr->nbrLimbs = 2;
}
else
{
bignbr->limbs[0].x = (int)mostSignificantLimb;
bignbr->nbrLimbs = 1;
}
(void)BigIntMultiplyPower2(bignbr, shLeft);
}
double logBigNbr(const BigInteger *pBigNbr)
{
int nbrLimbs;
double logar;
nbrLimbs = pBigNbr->nbrLimbs;
assert(nbrLimbs >= 1);
if (nbrLimbs == 1)
{
logar = log((double)(pBigNbr->limbs[0].x));
}
else
{
int nbrBits;
double value = pBigNbr->limbs[nbrLimbs - 2].x +
(double)pBigNbr->limbs[nbrLimbs - 1].x * LIMB_RANGE;
if (nbrLimbs == 2)
{
logar = log(value);
}
else
{
logar = log(value + (double)pBigNbr->limbs[nbrLimbs - 3].x / LIMB_RANGE);
}
nbrBits = (nbrLimbs - 2) * BITS_PER_GROUP;
logar += (double)nbrBits * LOG_2;
}
return logar;
}
double logLimbs(const limb *pBigNbr, int nbrLimbs)
{
double logar;
int nbrBits;
assert(nbrLimbs >= 1);
if (nbrLimbs > 1)
{
nbrBits = (nbrLimbs - 2) * BITS_PER_GROUP;
logar = log((double)(pBigNbr + nbrLimbs - 2)->x +
((double)(pBigNbr + nbrLimbs - 1)->x * (double)LIMB_RANGE)) +
(double)nbrBits * LOG_2;
}
else
{
nbrBits = (nbrLimbs - 1) * BITS_PER_GROUP;
logar = log((double)((pBigNbr + nbrLimbs - 1)->x)) +
(double)nbrBits * LOG_2;
}
return logar;
}
double BigInt2double(const BigInteger* value)
{
double result;
int nbrLimbs = value->nbrLimbs;
assert(nbrLimbs >= 1);
if (nbrLimbs == 1)
{
result = (double)(value->limbs[0].x);
}
else if (nbrLimbs == 2)
{
result = ((double)(value->limbs[0].x) / (double)LIMB_RANGE) +
(double)(value->limbs[1].x);
}
else
{
result = ((((double)(value->limbs[nbrLimbs-3].x) / (double)LIMB_RANGE) +
(double)(value->limbs[nbrLimbs - 2].x)) / (double)LIMB_RANGE) +
(double)(value->limbs[nbrLimbs - 1].x);
}
while (nbrLimbs > 1)
{
result *= (double)LIMB_RANGE;
nbrLimbs--;
}
if (value->sign == SIGN_NEGATIVE)
{
result = -result;
}
return result;
}
enum eExprErr BigIntPowerIntExp(const BigInteger *pBase, int exponent, BigInteger *pPower)
{
double base;
enum eExprErr rc;
assert(pBase->nbrLimbs >= 1);
if (BigIntIsZero(pBase))
{ // Base = 0 -> power = 0
pPower->limbs[0].x = 0;
pPower->nbrLimbs = 1;
pPower->sign = SIGN_POSITIVE;
return EXPR_OK;
}
base = logBigNbr(pBase);
#ifdef FACTORIZATION_APP
if (base*(double)exponent > 460510)
#else
if (base*(double)exponent > 46051)
#endif
{ // More than 20000 digits. 46051 = log(10^20000)
return EXPR_INTERM_TOO_HIGH;
}
CopyBigInt(&Base, pBase);
pPower->sign = SIGN_POSITIVE;
pPower->nbrLimbs = 1;
pPower->limbs[0].x = 1;
for (unsigned int mask = HALF_INT_RANGE_U; mask != 0U; mask >>= 1)
{
if (((unsigned int)exponent & mask) != 0U)
{
for (unsigned int mask2 = mask; mask2 != 0U; mask2 >>= 1)
{
rc = BigIntMultiply(pPower, pPower, pPower);
if (rc != EXPR_OK)
{
return rc;
}
if (((unsigned int)exponent & mask2) != 0U)
{
rc = BigIntMultiply(pPower, &Base, pPower);
if (rc != EXPR_OK)
{
return rc;
}
}
}
break;
}
}
return EXPR_OK;
}
enum eExprErr BigIntPower(const BigInteger *pBase, const BigInteger *pExponent, BigInteger *pPower)
{
assert(pBase->nbrLimbs >= 1);
assert(pExponent->nbrLimbs >= 1);
if (pExponent->sign == SIGN_NEGATIVE)
{ // Negative exponent not accepted.
return EXPR_INVALID_PARAM;
}
if (pExponent->nbrLimbs > 1)
{ // Exponent too high.
if ((pBase->nbrLimbs == 1) && (pBase->limbs[0].x < 2))
{ // If base equals -1, 0 or 1, set power to the value of base.
pPower->limbs[0].x = pBase->limbs[0].x;
pPower->nbrLimbs = 1;
if ((pBase->sign == SIGN_NEGATIVE) && ((pExponent->limbs[0].x & 1) != 0))
{ // Base negative and exponent odd means power negative.
pPower->sign = SIGN_NEGATIVE;
}
else
{
pPower->sign = SIGN_POSITIVE;
}
return EXPR_OK;
}
return EXPR_INTERM_TOO_HIGH;
}
return BigIntPowerIntExp(pBase, pExponent->limbs[0].x, pPower);
}
// GCD of two numbers:
// Input: a, b positive integers
// Output : g and d such that g is odd and gcd(a, b) = g×2d
// d : = 0
// while a and b are both even do
// a : = a / 2
// b : = b / 2
// d : = d + 1
// while a != b do
// if a is even then a : = a / 2
// else if b is even then b : = b / 2
// else if a > b then a : = (a – b) / 2
// else b : = (b – a) / 2
// g : = a
// output g, d
void BigIntDivide2(BigInteger *pArg)
{
int nbrLimbs = pArg->nbrLimbs;
int ctr = nbrLimbs - 1;
unsigned int carry;
assert(nbrLimbs >= 1);
limb *ptrLimb = &pArg->limbs[ctr];
carry = 0;
for (; ctr >= 0; ctr--)
{
carry = (carry << BITS_PER_GROUP) + (unsigned int)ptrLimb->x;
ptrLimb->x = (int)(carry >> 1);
ptrLimb--;
carry &= 1;
}
if ((nbrLimbs > 1) && (pArg->limbs[nbrLimbs - 1].x == 0))
{ // Most significant limb is zero, so reduce size by one limb.
pArg->nbrLimbs--;
}
}
enum eExprErr BigIntMultiplyPower2(BigInteger *pArg, int powerOf2)
{
int nbrLimbs = pArg->nbrLimbs;
assert(nbrLimbs >= 1);
if (BigIntIsZero(pArg))
{ // Nothing to do if number to be shifted is zero.
return EXPR_OK;
}
limb *ptrLimbs = pArg->limbs;
int limbsToShiftLeft = powerOf2 / BITS_PER_GROUP;
unsigned int bitsToShiftLeft = (unsigned int)(powerOf2 % BITS_PER_GROUP);
unsigned int bitsToShiftRight = BITS_PER_GROUP - bitsToShiftLeft;
if ((nbrLimbs + limbsToShiftLeft) >= MAX_LEN)
{
return EXPR_INTERM_TOO_HIGH;
}
if (bitsToShiftLeft > 0)
{
unsigned int carry = 0U;
for (int ctr = 0; ctr < nbrLimbs; ctr++)
{
unsigned int newCarry = (unsigned int)(ptrLimbs + ctr)->x >> bitsToShiftRight;
carry = (((unsigned int)(ptrLimbs + ctr)->x << bitsToShiftLeft) +
carry) & MAX_INT_NBR_U;
(ptrLimbs + ctr)->x = (int)carry;
carry = newCarry;
}
if (carry != 0UL)
{
(ptrLimbs + nbrLimbs)->x = (int)carry;
nbrLimbs++; // Indicate new significant limb.
}
}
// Shift left entire limbs.
if (limbsToShiftLeft > 0)
{
int bytesToMove = nbrLimbs * (int)sizeof(limb);
nbrLimbs += limbsToShiftLeft;
(void)memmove(&pArg->limbs[limbsToShiftLeft], pArg->limbs, bytesToMove);
bytesToMove = limbsToShiftLeft * (int)sizeof(limb);
(void)memset(pArg->limbs, 0, bytesToMove);
}
pArg->nbrLimbs = nbrLimbs;
return EXPR_OK;
}
bool TestBigNbrEqual(const BigInteger *pNbr1, const BigInteger *pNbr2)
{
const limb *ptrLimbs1 = pNbr1->limbs;
const limb *ptrLimbs2 = pNbr2->limbs;
assert(pNbr1->nbrLimbs >= 1);
assert(pNbr2->nbrLimbs >= 1);
if (pNbr1->nbrLimbs != pNbr2->nbrLimbs)
{ // Sizes of numbers are different.
return false;
}
if (pNbr1->sign != pNbr2->sign)
{ // Sign of numbers are different.
if ((pNbr1->nbrLimbs == 1) && (pNbr1->limbs[0].x == 0) && (pNbr2->limbs[0].x == 0))
{ // Both numbers are zero.
return true;
}
return false;
}
// Check whether both numbers are equal.
for (int ctr = pNbr1->nbrLimbs - 1; ctr >= 0; ctr--)
{
if ((ptrLimbs1 + ctr)->x != (ptrLimbs2 + ctr)->x)
{ // Numbers are different.
return false;
}
} // Numbers are equal.
return true;
}
void BigIntGcd(const BigInteger *pArg1, const BigInteger *pArg2, BigInteger *pResult)
{
int power2;
if (BigIntIsZero(pArg1))
{ // First argument is zero, so the GCD is second argument.
CopyBigInt(pResult, pArg2);
return;
}
if (BigIntIsZero(pArg2))
{ // Second argument is zero, so the GCD is first argument.
CopyBigInt(pResult, pArg1);
return;
}
// Reuse Base and Power temporary variables.
CopyBigInt(&Base, pArg1);
CopyBigInt(&Power, pArg2);
Base.sign = SIGN_POSITIVE;
Power.sign = SIGN_POSITIVE;
power2 = 0;
while (((Base.limbs[0].x | Power.limbs[0].x) & 1) == 0)
{ // Both values are even
BigIntDivide2(&Base);
BigIntDivide2(&Power);
power2++;
}
while (TestBigNbrEqual(&Base, &Power) == 0)
{ // Main GCD loop.
if ((Base.limbs[0].x & 1) == 0)
{ // Number is even. Divide it by 2.
BigIntDivide2(&Base);
continue;
}
if ((Power.limbs[0].x & 1) == 0)
{ // Number is even. Divide it by 2.
BigIntDivide2(&Power);
continue;
}
BigIntSubt(&Base, &Power, pResult);
if (pResult->sign == SIGN_POSITIVE)
{
CopyBigInt(&Base, pResult);
BigIntDivide2(&Base);
}
else
{
CopyBigInt(&Power, pResult);
Power.sign = SIGN_POSITIVE;
BigIntDivide2(&Power);
}
}
CopyBigInt(pResult, &Base);
(void)BigIntMultiplyPower2(pResult, power2);
pResult->sign = SIGN_POSITIVE;
}
enum eExprErr BigIntLcm(const BigInteger* pArg1, const BigInteger* pArg2,
BigInteger* pResult)
{
enum eExprErr retcode;
if (BigIntIsZero(pArg1) || BigIntIsZero(pArg2))
{ // If any of the arguments is zero, the LCM is zero.
intToBigInteger(pResult, 0);
return EXPR_OK;
}
BigIntGcd(pArg1, pArg2, &Temp4);
retcode = BigIntDivide(pArg1, &Temp4, &Temp4);
if (retcode != EXPR_OK)
{
return retcode;
}
retcode = BigIntMultiply(&Temp4, pArg2, pResult);
pResult->sign = SIGN_POSITIVE;
return retcode;
}
static void addToAbsValue(limb *pLimbs, int *pNbrLimbs, int addend)
{
limb* ptrLimbs = pLimbs;
int nbrLimbs = *pNbrLimbs;
ptrLimbs->x += addend;
if ((unsigned int)ptrLimbs->x < LIMB_RANGE)
{ // No overflow. Go out of routine.
return;
}
ptrLimbs->x -= (int)LIMB_RANGE;
for (int ctr = 1; ctr < nbrLimbs; ctr++)
{
ptrLimbs++; // Point to next most significant limb.
if (ptrLimbs->x != MAX_INT_NBR)
{ // No overflow. Go out of routine.
(ptrLimbs->x)++; // Add carry.
return;
}
ptrLimbs->x = 0;
}
(*pNbrLimbs)++; // Result has an extra limb.
(ptrLimbs + 1)->x = 1; // Most significant limb must be 1.
}
static void subtFromAbsValue(limb *pLimbs, int *pNbrLimbs, int subt)
{
int nbrLimbs = *pNbrLimbs;
limb* ptrLimb = pLimbs;
pLimbs->x -= subt;
if (pLimbs->x < 0)
{
int ctr = 0;
do
{ // Loop that adjust number if there is borrow.
unsigned int tempLimb = (unsigned int)ptrLimb->x & MAX_VALUE_LIMB;
ptrLimb->x = (int)tempLimb;
ctr++;
if (ctr == nbrLimbs)
{ // All limbs processed. Exit loop.
break;
}
ptrLimb++; // Point to next most significant limb.
ptrLimb->x--;
} while (ptrLimb->x < 0); // Continue loop if there is borrow.
if ((nbrLimbs > 1) && ((pLimbs + nbrLimbs - 1)->x == 0))
{
nbrLimbs--;
}
}
*pNbrLimbs = nbrLimbs;
}
void subtractdivide(BigInteger *pBigInt, int subt, int divisor)
{
int nbrLimbs = pBigInt->nbrLimbs;
assert(nbrLimbs >= 1);
// Point to most significant limb.
double dDivisor = (double)divisor;
double dInvDivisor = 1.0 / dDivisor;
double dLimb = (double)LIMB_RANGE;
if (subt >= 0)
{
if (pBigInt->sign == SIGN_POSITIVE)
{ // Subtract subt to absolute value.
subtFromAbsValue(pBigInt->limbs, &nbrLimbs, subt);
}
else
{ // Add subt to absolute value.
addToAbsValue(pBigInt->limbs, &nbrLimbs, subt);
}
}
else
{
if (pBigInt->sign == SIGN_POSITIVE)
{ // Subtract subt to absolute value.
addToAbsValue(pBigInt->limbs, &nbrLimbs, -subt);
}
else
{ // Add subt to absolute value.
subtFromAbsValue(pBigInt->limbs, &nbrLimbs, -subt);
}
}
if (divisor == 2)
{ // Use shifts for divisions by 2.
limb* ptrDest = pBigInt->limbs;
unsigned int curLimb = (unsigned int)ptrDest->x;
for (int ctr = 1; ctr < nbrLimbs; ctr++)
{ // Process starting from least significant limb.
unsigned int nextLimb = (unsigned int)(ptrDest + 1)->x;
ptrDest->x = UintToInt(((curLimb >> 1) | (nextLimb << BITS_PER_GROUP_MINUS_1)) &
MAX_VALUE_LIMB);
ptrDest++;
curLimb = nextLimb;
}
ptrDest->x = UintToInt((curLimb >> 1) & MAX_VALUE_LIMB);
}
else
{
int remainder = 0;
limb* pLimbs = pBigInt->limbs + nbrLimbs - 1;
// Divide number by divisor.
for (int ctr = nbrLimbs - 1; ctr >= 0; ctr--)
{
unsigned int dividend = ((unsigned int)remainder << BITS_PER_GROUP) +
(unsigned int)pLimbs->x;
double dDividend = ((double)remainder * dLimb) + (double)pLimbs->x;
double dQuotient = (dDividend * dInvDivisor) + 0.5;
unsigned int quotient = (unsigned int)dQuotient; // quotient has correct value or 1 more.
remainder = UintToInt(dividend - (quotient * (unsigned int)divisor));
if (remainder < 0)
{ // remainder not in range 0 <= remainder < divisor. Adjust.
quotient--;
remainder += divisor;
}
pLimbs->x = (int)quotient;
pLimbs--;
}
}
if ((nbrLimbs > 1) && (pBigInt->limbs[nbrLimbs - 1].x == 0))
{ // Most significant limb is now zero, so discard it.
nbrLimbs--;
}
pBigInt->nbrLimbs = nbrLimbs;
}
int getRemainder(const BigInteger *pBigInt, int divisor)
{
int remainder = 0;
int nbrLimbs = pBigInt->nbrLimbs;
assert(nbrLimbs >= 1);
double dDivisor = (double)divisor;
double dLimb = 0x80000000;
const limb *pLimb = &pBigInt->limbs[nbrLimbs - 1];
for (int ctr = nbrLimbs - 1; ctr >= 0; ctr--)
{
int dividend = UintToInt(((unsigned int)remainder << BITS_PER_GROUP) +
(unsigned int)pLimb->x);
double dDividend = ((double)remainder * dLimb) + (double)pLimb->x;
double dQuotient = floor((dDividend / dDivisor) + 0.5);
int quotient = (int)(unsigned int)dQuotient; // quotient has correct value or 1 more.
remainder = dividend - (quotient * divisor);
// Adjust remainder if not in range 0 <= remainder < divisor..
remainder += divisor & (remainder >> BITS_PER_GROUP);
pLimb--;
}
if ((pBigInt->sign == SIGN_NEGATIVE) && (remainder != 0))
{
remainder = divisor - remainder;
}
return remainder;
}
void addbigint(BigInteger *pResult, int addend)
{
int intAddend = addend;
enum eSign sign;
int nbrLimbs = pResult->nbrLimbs;
assert(nbrLimbs >= 1);
limb *pResultLimbs = pResult->limbs;
sign = pResult->sign;
if (intAddend < 0)
{
intAddend = -intAddend;
if (sign == SIGN_POSITIVE)
{
sign = SIGN_NEGATIVE;
}
else
{
sign = SIGN_POSITIVE;
}
}
if (sign == SIGN_POSITIVE)
{ // Add addend to absolute value of pResult.
addToAbsValue(pResultLimbs, &nbrLimbs, intAddend);
}
else
{ // Subtract addend from absolute value of pResult.
if (nbrLimbs == 1)
{
pResultLimbs->x -= intAddend;
if (pResultLimbs->x < 0)
{
pResultLimbs->x = -pResultLimbs->x;
BigIntNegate(pResult, pResult);
}
}
else
{ // More than one limb.
subtFromAbsValue(pResultLimbs, &nbrLimbs, intAddend);
}
}
pResult->nbrLimbs = nbrLimbs;
}
void multint(BigInteger *pResult, const BigInteger *pMult, int factor)
{
#ifdef _USING64BITS_
int64_t carry;
#else
int carry;
double dFactor;
double dVal = 1.0 / (double)LIMB_RANGE;
#endif
int intMult = factor;
bool factorPositive = true;
int nbrLimbs = pMult->nbrLimbs;
assert(nbrLimbs >= 1);
const limb *pLimb = pMult->limbs;
limb *pResultLimb = pResult->limbs;
if (intMult == 0)
{ // Any number multiplied by zero is zero.
intToBigInteger(pResult, 0);
return;
}
if (intMult < 0)
{ // If factor is negative, indicate it and compute its absolute value.
factorPositive = false;
intMult = -intMult;
}
#ifndef _USING64BITS_
dFactor = (double)intMult;
#endif