-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathGaussExpr.c
1164 lines (1112 loc) · 34.8 KB
/
GaussExpr.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 2016-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 <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "bignbr.h"
#include "factor.h"
#include "expression.h"
#define PAREN_STACK_SIZE 5000
#define COMPR_STACK_SIZE 1000000
#define TOKEN_PRIMORIAL 39
#define TOKEN_GCD 40
#define TOKEN_LCM 41
#define TOKEN_MODPOW 42
#define TOKEN_MODINV 43
#define TOKEN_NORM 44
#define TOKEN_RE 45
#define TOKEN_IM 46
#define TOKEN_ISPRIME 47
#define TOKEN_F 48
#define TOKEN_L 49
#define TOKEN_P 50
#define TOKEN_N 51
#define TOKEN_B 52
struct sFuncOperExpr stFuncOperGaussianExpr[] =
{
// First section: functions
{"ANS", TOKEN_ANS + NO_PARMS, 0},
{"GCD", TOKEN_GCD + MANY_PARMS, 0},
{"LCM", TOKEN_LCM + MANY_PARMS, 0},
{"MODPOW", TOKEN_MODPOW + THREE_PARMS, 0},
{"MODINV", TOKEN_MODINV + TWO_PARMS, 0},
{"NORM", TOKEN_NORM + ONE_PARM, 0},
{"RE", TOKEN_RE + ONE_PARM, 0},
{"IM", TOKEN_IM + ONE_PARM, 0},
{"ISPRIME", TOKEN_ISPRIME + ONE_PARM, 0},
{"F", TOKEN_F + ONE_PARM, 0},
{"L", TOKEN_L + ONE_PARM, 0},
{"P", TOKEN_P + ONE_PARM, 0},
{"N", TOKEN_N + ONE_PARM, 0},
{"B", TOKEN_B + ONE_PARM, 0},
{"RANDOM", TOKEN_RANDOM + TWO_PARMS, 0},
{NULL, 0, 0},
// Second section: functions written at right of argument.
{"#", TOKEN_PRIMORIAL, 0},
{NULL, 0, 0},
// Third section: unary operators.
{"-", OPER_UNARY_MINUS, 3},
{NULL, 0, 0},
// Fourth section: binary operators.
{"**", OPER_POWER, 1}, // This must be located before multiplication operator.
{"+", OPER_ADD, 4},
{"-", OPER_SUBT, 4},
{"*", OPER_MULTIPLY, 2},
{"%", OPER_REMAINDER, 2},
{"/", OPER_DIVIDE, 2},
{"^", OPER_POWER, 1},
{NULL, 0, 0},
};
static int comprStackValues[COMPR_STACK_SIZE];
static int comprStackOffset[PAREN_STACK_SIZE];
static int stackIndex;
#ifndef lang
bool lang;
#endif
char output[3000000];
limb Mult1[MAX_LEN];
limb Mult3[MAX_LEN];
limb Mult4[MAX_LEN];
int valueQ[MAX_LEN];
static void ComputeGCD(void);
static enum eExprErr ComputeLCM(void);
static enum eExprErr GaussianDivide(void);
static enum eExprErr GaussianMultiply(void);
static int Modulo(BigInteger* ReNum, BigInteger* ImNum,
const BigInteger* ReDen, const BigInteger* ImDen,
BigInteger* Result);
static enum eExprErr isPrime(void);
static enum eExprErr ComputeModPow(void);
static enum eExprErr ComputeModInv(void);
static enum eExprErr ComputePartition(void);
static enum eExprErr ComputePower(BigInteger* Re1, const BigInteger* Re2,
BigInteger* Im1, const BigInteger* Im2);
static enum eExprErr ModInv(const BigInteger* RealNbr, const BigInteger* ImagNbr,
const BigInteger* RealMod, const BigInteger* ImagMod,
BigInteger* Result);
static BigInteger curStackRe;
static BigInteger curStackIm;
static BigInteger curStack2Re;
static BigInteger curStack2Im;
static BigInteger curStack3Re;
static BigInteger curStack3Im;
static BigInteger curStackReBak;
static BigInteger curStackImBak;
static BigInteger curStack2ReBak;
static BigInteger curStack2ImBak;
static BigInteger curTmp;
static BigInteger norm;
static BigInteger Result[2];
static BigInteger LastAnswerRe;
static BigInteger LastAnswerIm;
static int numLimbs(const int* pLen)
{
int nbrLimbs = *pLen;
if (nbrLimbs < 0)
{
nbrLimbs = -nbrLimbs;
}
return nbrLimbs;
}
static void getCurrentStackValue(BigInteger* pValueRe, BigInteger *pValueIm)
{
assert(stackIndex >= 0);
const int* ptrStackValue = &comprStackValues[comprStackOffset[2*stackIndex]];
NumberLength = numLimbs(ptrStackValue);
IntArray2BigInteger(ptrStackValue, pValueRe);
ptrStackValue = &comprStackValues[comprStackOffset[(2 * stackIndex) + 1]];
NumberLength = numLimbs(ptrStackValue);
IntArray2BigInteger(ptrStackValue, pValueIm);
}
static enum eExprErr setStackValue(const BigInteger* pValueRe, const BigInteger *pValueIm)
{
assert(stackIndex >= 0);
int currentOffset = comprStackOffset[2 * stackIndex];
if (currentOffset >= (COMPR_STACK_SIZE - ((int)sizeof(BigInteger) / (int)sizeof(limb))))
{
return EXPR_OUT_OF_MEMORY;
}
NumberLength = pValueRe->nbrLimbs;
BigInteger2IntArray(&comprStackValues[currentOffset], pValueRe);
currentOffset += pValueRe->nbrLimbs + 1;
comprStackOffset[(2 * stackIndex) + 1] = currentOffset;
NumberLength = pValueIm->nbrLimbs;
BigInteger2IntArray(&comprStackValues[currentOffset], pValueIm);
currentOffset += pValueIm->nbrLimbs + 1;
comprStackOffset[(2 * stackIndex) + 2] = currentOffset;
return EXPR_OK;
}
enum eExprErr ComputeGaussianExpression(const char *expr, BigInteger *ExpressionResult)
{
char* ptrRPNbuffer;
enum eExprErr retcode;
retcode = ConvertToReversePolishNotation(expr, &ptrRPNbuffer, stFuncOperGaussianExpr,
PARSE_EXPR_GAUSSIAN, NULL, NULL);
if (retcode != EXPR_OK)
{
return retcode;
}
stackIndex = -1;
comprStackOffset[0] = 0;
while (*ptrRPNbuffer != '\0')
{
char c = *ptrRPNbuffer;
int currentOffset;
int nbrLenBytes;
int len;
int nbrParameters;
switch (c)
{
case TOKEN_NUMBER:
ptrRPNbuffer++; // Skip token.
stackIndex++;
// Move number to compressed stack.
currentOffset = comprStackOffset[2 * stackIndex];
if (currentOffset >= (COMPR_STACK_SIZE - ((int)sizeof(BigInteger) / (int)sizeof(limb))))
{
return EXPR_OUT_OF_MEMORY;
}
len = ((int)(unsigned char)*ptrRPNbuffer * 256) + (int)(unsigned char)*(ptrRPNbuffer + 1);
comprStackValues[currentOffset] = len;
ptrRPNbuffer += 2; // Skip length.
nbrLenBytes = len * (int)sizeof(limb);
(void)memcpy(&comprStackValues[currentOffset + 1], ptrRPNbuffer, nbrLenBytes);
ptrRPNbuffer += nbrLenBytes;
currentOffset += 1 + len;
comprStackOffset[(2 * stackIndex) + 1] = currentOffset;
comprStackValues[currentOffset] = 1; // Imaginary part is zero.
comprStackValues[currentOffset + 1] = 0;
comprStackOffset[(2 * stackIndex) + 2] = currentOffset + 2;
break;
case TOKEN_VAR: // Push number i.
ptrRPNbuffer++; // Skip token.
stackIndex++;
currentOffset = comprStackOffset[2 * stackIndex];
comprStackValues[currentOffset] = 1; // Real part is zero.
comprStackValues[currentOffset + 1] = 0;
currentOffset += 2;
comprStackOffset[(2 * stackIndex) + 1] = currentOffset;
comprStackValues[currentOffset] = 1; // Imaginary part is one.
comprStackValues[currentOffset + 1] = 1;
comprStackOffset[(2 * stackIndex) + 2] = currentOffset + 2;
break;
case TOKEN_ANS: // Get last answer.
if (LastAnswerRe.nbrLimbs == 0)
{
intToBigInteger(&curStackRe, 0);
}
else
{
CopyBigInt(&curStackRe, &LastAnswerRe);
}
if (LastAnswerIm.nbrLimbs == 0)
{
intToBigInteger(&curStackIm, 0);
}
else
{
CopyBigInt(&curStackIm, &LastAnswerIm);
}
stackIndex++;
break;
case TOKEN_RE:
if (stackIndex < 0)
{
return EXPR_INTERNAL_ERROR;
}
getCurrentStackValue(&curStackRe, &curStackIm);
intToBigInteger(&curStackIm, 0);
break;
case TOKEN_IM:
if (stackIndex < 0)
{
return EXPR_INTERNAL_ERROR;
}
getCurrentStackValue(&curStackIm, &curStackRe);
intToBigInteger(&curStackIm, 0);
break;
case TOKEN_NORM:
if (stackIndex < 0)
{
return EXPR_INTERNAL_ERROR;
}
// norm = Re^2 + Im^2
getCurrentStackValue(&curStackRe, &curStackIm);
retcode = BigIntMultiply(&curStackRe, &curStackRe, &curStack2Re);
if (retcode != EXPR_OK)
{
return retcode;
}
retcode = BigIntMultiply(&curStackIm, &curStackIm, &curStack2Im);
if (retcode != EXPR_OK)
{
return retcode;
}
BigIntAdd(&curStack2Re, &curStack2Im, &curStackRe);
intToBigInteger(&curStackIm, 0);
break;
case TOKEN_GCD:
ptrRPNbuffer++;
nbrParameters = (int)(unsigned char)*ptrRPNbuffer;
getCurrentStackValue(&curStackRe, &curStackIm);
for (int parmNbr = 1; parmNbr < nbrParameters; parmNbr++)
{
stackIndex--;
getCurrentStackValue(&curStack2Re, &curStack2Im);
ComputeGCD();
}
break;
case TOKEN_LCM:
ptrRPNbuffer++;
nbrParameters = (int)(unsigned char)*ptrRPNbuffer;
getCurrentStackValue(&curStackRe, &curStackIm);
for (int parmNbr = 1; parmNbr < nbrParameters; parmNbr++)
{
stackIndex--;
getCurrentStackValue(&curStack2Re, &curStack2Im);
retcode = ComputeLCM();
if (retcode != EXPR_OK)
{
return retcode;
}
}
break;
case TOKEN_MODPOW:
getCurrentStackValue(&curStack3Re, &curStack3Im);
stackIndex--;
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
retcode = ComputeModPow();
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case TOKEN_MODINV:
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
retcode = ComputeModInv();
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case TOKEN_ISPRIME:
getCurrentStackValue(&curStackRe, &curStackIm);
retcode = isPrime();
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case TOKEN_F:
getCurrentStackValue(&curStackRe, &curStackIm);
if (!BigIntIsZero(&curStackIm))
{ // Imaginary part of argument must be zero.
return EXPR_INVALID_PARAM;
}
retcode = ComputeFibLucas(0, &curStackRe);
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case TOKEN_L:
getCurrentStackValue(&curStackRe, &curStackIm);
if (!BigIntIsZero(&curStackIm))
{ // Imaginary part of argument must be zero.
return EXPR_INVALID_PARAM;
}
retcode = ComputeFibLucas(2, &curStackRe);
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case TOKEN_N:
getCurrentStackValue(&curStackRe, &curStackIm);
if (!BigIntIsZero(&curStackIm))
{ // Imaginary part of argument must be zero.
return EXPR_INVALID_PARAM;
}
retcode = ComputeNext(&curStackRe);
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case TOKEN_B:
getCurrentStackValue(&curStackRe, &curStackIm);
if (!BigIntIsZero(&curStackIm))
{ // Imaginary part of argument must be zero.
return EXPR_INVALID_PARAM;
}
retcode = ComputeBack(&curStackRe);
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case TOKEN_P:
getCurrentStackValue(&curStackRe, &curStackIm);
retcode = ComputePartition();
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case TOKEN_RANDOM:
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
BigIntRandom(&curStackRe, &curStack2Re, &curStackRe);
BigIntRandom(&curStackIm, &curStack2Im, &curStackIm);
break;
case TOKEN_FACTORIAL:
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
if (!BigIntIsZero(&curStackIm) || (curStackRe.sign == SIGN_NEGATIVE))
{ // Imaginary part must be zero.
return EXPR_INVALID_PARAM;
}
retcode = factorial(&curStackRe, curStackRe.limbs[0].x, curStack2Re.limbs[0].x);
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case TOKEN_PRIMORIAL:
getCurrentStackValue(&curStackRe, &curStackIm);
if (!BigIntIsZero(&curStackIm) || (curStackRe.sign == SIGN_NEGATIVE))
{ // Imaginary part must be zero.
return EXPR_INVALID_PARAM;
}
if ((curStackRe.nbrLimbs > 1) || (curStackRe.limbs[0].x > 46049))
{
return EXPR_INTERM_TOO_HIGH;
}
retcode = primorial(&curStackRe, curStackRe.limbs[0].x);
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case OPER_ADD:
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
BigIntAdd(&curStackRe, &curStack2Re, &curStackRe);
BigIntAdd(&curStackIm, &curStack2Im, &curStackIm);
break;
case OPER_SUBT:
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
BigIntSubt(&curStackRe, &curStack2Re, &curStackRe);
BigIntSubt(&curStackIm, &curStack2Im, &curStackIm);
break;
case OPER_UNARY_MINUS:
getCurrentStackValue(&curStackRe, &curStackIm);
BigIntChSign(&curStackRe);
BigIntChSign(&curStackIm);
break;
case OPER_MULTIPLY:
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
retcode = GaussianMultiply();
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case OPER_DIVIDE:
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
retcode = GaussianDivide();
if (retcode != EXPR_OK)
{
return retcode;
}
break;
case OPER_REMAINDER:
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
retcode = Modulo(&curStackRe, &curStackIm, &curStack2Re, &curStack2Im, Result);
if (retcode != EXPR_OK)
{
return retcode;
}
CopyBigInt(&curStackRe, &Result[0]);
CopyBigInt(&curStackIm, &Result[1]);
break;
case TOKEN_END_EXPON:
getCurrentStackValue(&curStack2Re, &curStack2Im);
stackIndex--;
getCurrentStackValue(&curStackRe, &curStackIm);
retcode = ComputePower(&curStackRe, &curStack2Re, &curStackIm, &curStack2Im);
if (retcode != EXPR_OK)
{
return retcode;
}
break;
default:
break;
}
if ((c != TOKEN_NUMBER) && (c != TOKEN_VAR))
{
if (c != TOKEN_START_EXPON)
{
retcode = setStackValue(&curStackRe, &curStackIm);
if (retcode != EXPR_OK)
{
return retcode;
}
}
ptrRPNbuffer++; // Skip token.
}
}
getCurrentStackValue(ExpressionResult, ExpressionResult + 1);
if ((ExpressionResult->nbrLimbs > 2215) && // 10000/log_10(32768)
((ExpressionResult+1)->nbrLimbs > 2215))
{
return EXPR_NUMBER_TOO_HIGH;
}
CopyBigInt(&LastAnswerRe, ExpressionResult);
CopyBigInt(&LastAnswerIm, ExpressionResult + 1);
return EXPR_OK;
}
static enum eExprErr isPrime(void)
{
if (BigIntIsZero(&curStackRe))
{
if ((curStackIm.limbs[0].x % 4) != 3)
{
intToBigInteger(&curStackRe, 0); // Number not prime.
intToBigInteger(&curStackIm, 0);
return EXPR_OK;
}
CopyBigInt(&curStack2Re, &curStackIm);
}
else if (BigIntIsZero(&curStackIm))
{
if ((curStackRe.limbs[0].x % 4) != 3)
{
intToBigInteger(&curStackRe, 0); // Number not prime.
intToBigInteger(&curStackIm, 0);
return EXPR_OK;
}
CopyBigInt(&curStack2Re, &curStackRe);
}
else
{
enum eExprErr retcode;
retcode = BigIntMultiply(&curStackRe, &curStackRe, &curStack2Re);
if (retcode != EXPR_OK)
{
return retcode;
}
retcode = BigIntMultiply(&curStackIm, &curStackIm, &curStack2Im);
if (retcode != EXPR_OK)
{
return retcode;
}
BigIntAdd(&curStack2Re, &curStack2Im, &curStack2Re);
}
#ifdef FACTORIZATION_APP
if (BpswPrimalityTest(&curStack2Re, NULL) == 0)
#else
if (BpswPrimalityTest(&curStack2Re) == 0)
#endif
{ // Argument is a probable prime.
intToBigInteger(&curStackRe, -1);
}
else
{ // Argument is not a probable prime.
intToBigInteger(&curStackRe, 0);
}
intToBigInteger(&curStackIm, 0);
return EXPR_OK;
}
static enum eExprErr ComputePartition(void)
{
int val;
if (!BigIntIsZero(&curStackIm))
{ // Imaginary part must be zero.
return EXPR_INVALID_PARAM;
}
if (curStackRe.sign == SIGN_NEGATIVE)
{
return EXPR_INVALID_PARAM;
}
if (curStackRe.nbrLimbs > 1)
{
return EXPR_INTERM_TOO_HIGH;
}
val = curStackRe.limbs[0].x;
if (val > 100000)
{
return EXPR_INVALID_PARAM;
}
partition(val, &curStackRe);
return EXPR_OK;
}
// Replace dividend by the remainder.
// Back-up sign of dividend and then divide positive by positive (divisor is always positive).
static void GetRemainder(const BigInteger *Norm, BigInteger *ReDividend, BigInteger *ImDividend,
const BigInteger *ReDivisor, const BigInteger *ImDivisor,
BigInteger *Re, BigInteger *Im)
{
BigInteger ReTmp;
BigInteger ImTmp;
int signBak;
// Compute Re as ((Re1*Re2+Im1*Im2)*2/norm+1)/2
(void)BigIntMultiply(ReDividend, ReDivisor, &ReTmp);
(void)BigIntMultiply(ImDividend, ImDivisor, &ImTmp);
BigIntAdd(&ReTmp, &ImTmp, &ReTmp);
multint(&ReTmp, &ReTmp, 2);
signBak = ReTmp.sign;
ReTmp.sign = SIGN_POSITIVE;
(void)BigIntDivide(&ReTmp, Norm, Re);
subtractdivide(Re, -1, 2);
if (!BigIntIsZero(Re))
{
Re->sign = signBak;
}
// Compute Im as ((Im1*Re2-Re1*Im2)*2/norm+1)/2
(void)BigIntMultiply(ImDividend, ReDivisor, &ReTmp);
(void)BigIntMultiply(ReDividend, ImDivisor, &ImTmp);
BigIntSubt(&ReTmp, &ImTmp, &ImTmp);
multint(&ImTmp, &ImTmp, 2);
signBak = ImTmp.sign;
ImTmp.sign = SIGN_POSITIVE;
(void)BigIntDivide(&ImTmp, Norm, Im);
subtractdivide(Im, -1, 2);
if (!BigIntIsZero(Im))
{
Im->sign = signBak;
}
// Compute Re1 as Re1 - Re*Re2 + Im*Im2.
(void)BigIntMultiply(Re, ReDivisor, &ReTmp);
BigIntSubt(ReDividend, &ReTmp, ReDividend);
(void)BigIntMultiply(Im, ImDivisor, &ReTmp);
BigIntAdd(ReDividend, &ReTmp, ReDividend);
// Compute Im1 as Im1 - Im*Re2 - Re*Im2.
(void)BigIntMultiply(Im, ReDivisor, &ReTmp);
BigIntSubt(ImDividend, &ReTmp, ImDividend);
(void)BigIntMultiply(Re, ImDivisor, &ReTmp);
BigIntSubt(ImDividend, &ReTmp, ImDividend);
}
// Compute curStack as gcd(curStack, curStack2)
static void ComputeGCD(void)
{
BigInteger ReTmp;
BigInteger ImTmp;
GaussianGCD(&curStackRe, &curStackIm, &curStack2Re, &curStack2Im,
&curStack3Re, &curStack3Im, &ReTmp, &ImTmp);
CopyBigInt(&curStackRe, &curStack3Re);
CopyBigInt(&curStackIm, &curStack3Im);
while ((curStackRe.sign == SIGN_NEGATIVE) || (curStackIm.sign == SIGN_NEGATIVE))
{ // Multiply by i until number is in first quadrant.
CopyBigInt(&curStack3Re, &curStackRe);
BigIntNegate(&curStackIm, &curStackRe);
CopyBigInt(&curStackIm, &curStack3Re);
}
}
static enum eExprErr ComputeLCM(void)
{
enum eExprErr retcode;
if ((BigIntIsZero(&curStackRe) && BigIntIsZero(&curStackIm)) ||
(BigIntIsZero(&curStack2Re) && BigIntIsZero(&curStack2Im)))
{ // If any of the arguments is zero, the LCM is zero.
intToBigInteger(&curStackRe, 0);
intToBigInteger(&curStackIm, 0);
return EXPR_OK;
}
// Save both parameters because ComputeGCD will overwrite them.
CopyBigInt(&curStackReBak, &curStackRe);
CopyBigInt(&curStackImBak, &curStackIm);
CopyBigInt(&curStack2ReBak, &curStack2Re);
CopyBigInt(&curStack2ImBak, &curStack2Im);
ComputeGCD(); // curStackRe + i*curStackIm = GCD.
// Divide first parameter by GCD.
CopyBigInt(&curStack2Re, &curStackRe); // Divisor
CopyBigInt(&curStack2Im, &curStackIm);
CopyBigInt(&curStackRe, &curStackReBak); // Dividend
CopyBigInt(&curStackIm, &curStackImBak);
retcode = GaussianDivide();
if (retcode != EXPR_OK)
{
return retcode;
}
// Multiply the quotient by the second parameter.
CopyBigInt(&curStack2Re, &curStack2ReBak);
CopyBigInt(&curStack2Im, &curStack2ImBak);
return GaussianMultiply();
}
static int ComputePower(BigInteger *Re1, const BigInteger *Re2,
BigInteger *Im1, const BigInteger *Im2)
{
unsigned int expon;
double logNorm;
bool performPower = false;
BigInteger ReTmp;
BigInteger ImTmp;
BigInteger Re;
BigInteger Im;
if (!BigIntIsZero(Im2) || (Re2->sign == SIGN_NEGATIVE))
{ // Exponent must be positive or zero.
return EXPR_INVALID_PARAM;
}
if (Re2->nbrLimbs > 2)
{
return EXPR_INTERM_TOO_HIGH;
}
expon = (unsigned int)Re2->limbs[0].x;
if (Re2->nbrLimbs == 2)
{
expon += (unsigned int)Re2->limbs[1].x << BITS_PER_GROUP;
}
(void)BigIntMultiply(Re1, Re1, &ReTmp);
(void)BigIntMultiply(Im1, Im1, &ImTmp);
BigIntAdd(&ReTmp, &ImTmp, &norm); // norm <- re1^2 + im1^2.
logNorm = logLimbs(norm.limbs, norm.nbrLimbs); // Get logarithm of norm.
if (logNorm*(double)expon > 23026.0)
{ // More than 20000 digits. 23026 = log(10^10000) (norm is already squared).
return EXPR_INTERM_TOO_HIGH;
}
/* Compute actual power */
Re.limbs[0].x = 1; // Initialize Re <- 1, Im <- 0.
Im.limbs[0].x = 0;
Re.nbrLimbs = 1;
Im.nbrLimbs = 1;
Re.sign = SIGN_POSITIVE;
Im.sign = SIGN_POSITIVE;
for (unsigned int mask = HALF_INT_RANGE_U; mask != 0U; mask >>= 1)
{
if ((expon & mask) != 0U)
{
performPower = true;
}
if (performPower)
{
(void)BigIntMultiply(&Re, &Re, &ReTmp); // ReTmp <- re*re - im*im.
(void)BigIntMultiply(&Im, &Im, &ImTmp);
BigIntSubt(&ReTmp, &ImTmp, &ReTmp);
(void)BigIntMultiply(&Re, &Im, &Im); // Im <- 2*re*im
BigIntAdd(&Im, &Im, &Im);
CopyBigInt(&Re, &ReTmp);
if ((expon & mask) != 0U)
{
(void)BigIntMultiply(Re1, &Re, &ReTmp); // Re2 <- re1*re - im1*im.
(void)BigIntMultiply(Im1, &Im, &ImTmp);
BigIntSubt(&ReTmp, &ImTmp, &curTmp);
(void)BigIntMultiply(Re1, &Im, &ReTmp); // Im <- re1*im + im1*re.
(void)BigIntMultiply(Im1, &Re, &ImTmp);
BigIntAdd(&ReTmp, &ImTmp, &Im);
CopyBigInt(&Re, &curTmp);
}
}
}
CopyBigInt(Re1, &Re);
CopyBigInt(Im1, &Im);
return EXPR_OK;
}
static enum eExprErr ComputeModPow(void)
{
static BigInteger ReTmp;
static BigInteger ImTmp;
static BigInteger ReBase;
static BigInteger ImBase;
static BigInteger ReExp;
static BigInteger ImExp;
static BigInteger ReMod;
static BigInteger ImMod;
static BigInteger Re;
static BigInteger Im;
CopyBigInt(&ReBase, &curStackRe);
CopyBigInt(&ImBase, &curStackIm);
CopyBigInt(&ReExp, &curStack2Re);
CopyBigInt(&ImExp, &curStack2Im);
CopyBigInt(&ReMod, &curStack3Re);
CopyBigInt(&ImMod, &curStack3Im);
if (!BigIntIsZero(&ImExp))
{ // Imaginary part must be zero.
return EXPR_INVALID_PARAM;
}
if (ReExp.sign == SIGN_NEGATIVE)
{
enum eExprErr retcode;
BigIntNegate(&ReExp, &ReExp);
retcode = ModInv(&ReBase, &ImBase, &ReMod, &ImMod, Result);
if (retcode != EXPR_OK)
{
return retcode;
}
ReBase = Result[0];
ImBase = Result[1];
}
// Re <- 1, Im <- 0
Re.limbs[0].x = 1;
Im.limbs[0].x = 0;
Re.nbrLimbs = 1;
Im.nbrLimbs = 1;
Re.sign = SIGN_POSITIVE;
Im.sign = SIGN_POSITIVE;
if (BigIntIsZero(&ReMod) && BigIntIsZero(&ImMod))
{ /* Modulus is zero */
(void)ComputePower(&ReBase, &ReExp, &ImBase, &ImExp);
CopyBigInt(&curStackRe, &ReBase);
CopyBigInt(&curStackIm, &ImBase);
}
else
{ /* Modulus is not zero */
(void)BigIntMultiply(&ReMod, &ReMod, &ReTmp);
(void)BigIntMultiply(&ImMod, &ImMod, &ImTmp);
BigIntAdd(&ReTmp, &ImTmp, &norm);
for (int index = ReExp.nbrLimbs - 1; index >= 0; index--)
{
int groupExp = ReExp.limbs[index].x;
for (unsigned int mask = HALF_INT_RANGE_U; mask > 0U; mask >>= 1)
{
// Compute Re + i*Im as (Re + i*Im)^2
(void)BigIntMultiply(&Re, &Re, &ReTmp);
(void)BigIntMultiply(&Im, &Im, &ImTmp);
BigIntSubt(&ReTmp, &ImTmp, &ReTmp);
(void)BigIntMultiply(&Re, &Im, &ImTmp);
BigIntAdd(&ImTmp, &ImTmp, &Im);
CopyBigInt(&Re, &ReTmp);
// Replace (Re + i*Im) by the remainder of
// (ReTmp + i*ImTmp) / (ReMod + i*ImMod)
// ReTmp and ImTmp are used as temporary values.
GetRemainder(&norm, &Re, &Im, &ReMod, &ImMod, &ReTmp, &ImTmp);
if (((unsigned int)groupExp & mask) != 0U)
{
// Compute Re + i*Im as (Re + i*Im)*(ReBase + i*ImBase)
(void)BigIntMultiply(&ReBase, &Re, &ReTmp);
(void)BigIntMultiply(&ImBase, &Im, &ImTmp);
BigIntSubt(&ReTmp, &ImTmp, &ReTmp);
(void)BigIntMultiply(&ImBase, &Re, &ImTmp);
(void)BigIntMultiply(&ReBase, &Im, &Re);
BigIntAdd(&Re, &ImTmp, &Im);
CopyBigInt(&Re, &ReTmp);
// Replace (Re + i*Im) by the remainder of
// (ReTmp + i*ImTmp) / (ReMod + i*ImMod)
// ReTmp and ImTmp are used as temporary values.
GetRemainder(&norm, &Re, &Im, &ReMod, &ImMod, &ReTmp, &ImTmp);
}
}
}
(void)Modulo(&Re, &Im, &ReMod, &ImMod, Result);
CopyBigInt(&curStackRe, &Result[0]);
CopyBigInt(&curStackIm, &Result[1]);
return EXPR_OK;
}
return EXPR_OK;
}
static enum eExprErr ComputeModInv(void)
{
enum eExprErr retcode = ModInv(&curStackRe, &curStackIm,
&curStack2Re, &curStack2Im, Result);
if (retcode != EXPR_OK)
{
return retcode;
}
CopyBigInt(&curStackRe, &Result[0]);
CopyBigInt(&curStackIm, &Result[1]);
return EXPR_OK;
}
static enum eExprErr ModInv(const BigInteger *RealNbr, const BigInteger *ImagNbr,
const BigInteger *RealMod, const BigInteger *ImagMod,
BigInteger *result)
{
static BigInteger ReG0;
static BigInteger ReG1;
static BigInteger ImG0;
static BigInteger ImG1;
static BigInteger ReU0;
static BigInteger ReU1;
static BigInteger ImU0;
static BigInteger ImU1;
static BigInteger Re;
static BigInteger Im;
static BigInteger Tmp;
if (BigIntIsZero(RealMod) && BigIntIsZero(ImagMod))
{ // Argument is zero.
return EXPR_INVALID_PARAM;
}
CopyBigInt(&ReG0, RealNbr);
CopyBigInt(&ImG0, ImagNbr);
CopyBigInt(&ReG1, RealMod);
CopyBigInt(&ImG1, ImagMod);
// Initialize U0 <- 1, U1 <- 0.
ReU0.limbs[0].x = 1;
ReU0.nbrLimbs = 1;
ReU0.sign = SIGN_POSITIVE;
ReU1.limbs[0].x = 0;
ImU0.limbs[0].x = 0;
ImU1.limbs[0].x = 0;
ReU1.nbrLimbs = 1;
ImU0.nbrLimbs = 1;
ImU1.nbrLimbs = 1;
ReU1.sign = SIGN_POSITIVE;
ImU0.sign = SIGN_POSITIVE;
ImU1.sign = SIGN_POSITIVE;
while (!BigIntIsZero(&ReG1) || !BigIntIsZero(&ImG1))
{ // G1 is not zero.
(void)BigIntMultiply(&ReG1, &ReG1, &Re);
(void)BigIntMultiply(&ImG1, &ImG1, &Im);
BigIntAdd(&Re, &Im, &norm);
// Replace G0 by the remainder of G0/G1.
GetRemainder(&norm, &ReG0, &ImG0, &ReG1, &ImG1, &Re, &Im);
// Exchange G0 and G1.
CopyBigInt(&Tmp, &ReG0);
CopyBigInt(&ReG0, &ReG1);
CopyBigInt(&ReG1, &Tmp);
CopyBigInt(&Tmp, &ImG0);
CopyBigInt(&ImG0, &ImG1);
CopyBigInt(&ImG1, &Tmp);
(void)BigIntMultiply(&Re, &ReU1, &Tmp);
BigIntSubt(&ReU0, &Tmp, &ReU0);
(void)BigIntMultiply(&Im, &ImU1, &Tmp);
BigIntAdd(&ReU0, &Tmp, &ReU0);
(void)BigIntMultiply(&Im, &ReU1, &Tmp);
BigIntSubt(&ImU0, &Tmp, &ImU0);
(void)BigIntMultiply(&Re, &ImU1, &Tmp);
BigIntSubt(&ImU0, &Tmp, &ImU0);
// Exchange U0 and U1.
CopyBigInt(&Tmp, &ReU0);
CopyBigInt(&ReU0, &ReU1);
CopyBigInt(&ReU1, &Tmp);
CopyBigInt(&Tmp, &ImU0);
CopyBigInt(&ImU0, &ImU1);
CopyBigInt(&ImU1, &Tmp);
}
for (;;)
{
CopyBigInt(&Tmp, &ImG0);
Tmp.sign = SIGN_POSITIVE; // Tmp <- abs(Tmp)
BigIntSubt(&ReG0, &Tmp, &Tmp);
if (Tmp.sign == SIGN_POSITIVE)
{
break;
}
// Multiply G0 and U0 by i.
CopyBigInt(&Tmp, &ReG0);
BigIntNegate(&ImG0, &ReG0);
CopyBigInt(&ImG0, &Tmp);
CopyBigInt(&Tmp, &ReU0);
BigIntNegate(&ImU0, &ReU0);
CopyBigInt(&ImU0, &Tmp);
}
if ((ReG0.nbrLimbs != 1) || (ReG0.limbs[0].x != 1) || !BigIntIsZero(&ImG0))
{ // G0 is not 1.
return EXPR_INVALID_PARAM;
}
return Modulo(&ReU0, &ImU0, RealMod, ImagMod, result);
}
static int Modulo(BigInteger *ReNum, BigInteger *ImNum,
const BigInteger *ReDen, const BigInteger *ImDen,
BigInteger *result)
{
BigInteger Re;
BigInteger Im;
BigInteger ReMin;
BigInteger ImMin;
BigInteger Tmp;
BigInteger normmin;
if (BigIntIsZero(ReDen) && BigIntIsZero(ImDen))
{ // Denominator is zero.
CopyBigInt(result, ReNum);
CopyBigInt(result+1, ImNum);
return 0;
}
ReMin.limbs[0].x = 0;
ImMin.limbs[0].x = 0;
ReMin.nbrLimbs = 1;
ImMin.nbrLimbs = 1;
ReMin.sign = SIGN_POSITIVE;