-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgiants.c
executable file
·3479 lines (3102 loc) · 59.4 KB
/
giants.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
/**************************************************************
*
* giants.c
*
* Library for large-integer arithmetic.
*
* The large-gcd implementation is due to J. P. Buhler.
* Special mod routines use ideas of R. McIntosh.
* Contributions from G. Woltman, A. Powell acknowledged.
*
* Updates:
* 11 Feb 03 REC R. McIntosh fixes to fermatpowermodg, mersennepowermodg
* 20 Oct 01 REC R. McIntosh fixes to: mersennemod, fermatmod, fer_mod
* 18 Jul 99 REC Added routine fer_mod(), for use when Fermat
giant itself is available.
* 17 Jul 99 REC Fixed sign bug in fermatmod()
* 17 Apr 99 REC Fixed various comment/line wraps
* 25 Mar 99 REC G. Woltman/A. Powell fixes Karat. routines
* 05 Mar 99 REC Moved invaux, binvaux giants to stack
* 05 Mar 99 REC Moved gread/gwrite giants to stack
* 05 Mar 99 REC No static on cur_den, cur_recip (A. Powell)
* 28 Feb 99 REC Error detection added atop newgiant().
* 27 Feb 99 REC Reduced wasted work in addsignal().
* 27 Feb 99 REC Reduced wasted work in FFTmulg().
* 19 Feb 99 REC Generalized iaddg() per R. Mcintosh.
* 2 Feb 99 REC Fixed comments.
* 6 Dec 98 REC Fixed yet another Karatsuba glitch.
* 1 Dec 98 REC Fixed errant case of addg().
* 28 Nov 98 REC Installed A. Powell's (correct) variant of
Karatsuba multiply.
* 15 May 98 REC Modified gwrite() to handle huge integers.
* 13 May 98 REC Changed to static stack declarations
* 11 May 98 REC Installed Karatsuba multiply, to handle
* medregion 'twixt grammar- and FFT-multiply.
* 1 May 98 JF gshifts now handle bits < 0 correctly.
* 30 Apr 98 JF 68k assembler code removed,
* stack giant size now invariant and based
* on first call of newgiant(),
* stack memory leaks fixed.
* 29 Apr 98 JF function prototyes cleaned up,
* GCD no longer uses personal stack,
* optimized shifts for bits%16 == 0.
* 27 Apr 98 JF scratch giants now replaced with stack
* 20 Apr 98 JF grammarsquareg fixed for asize == 0.
* scratch giants now static.
* 29 Jan 98 JF Corrected out-of-range errors in
* mersennemod and fermatmod.
* 23 Dec 97 REC Sped up divide routines via split-shift.
* 18 Nov 97 JF Improved mersennemod, fermatmod.
* 9 Nov 97 JF Sped up grammarsquareg.
* 20 May 97 RDW Fixed Win32 compiler warnings.
* 18 May 97 REC Installed new, fast divide.
* 17 May 97 REC Reduced memory for FFT multiply.
* 26 Apr 97 REC Creation.
*
* c. 1997,1998 Perfectly Scientific, Inc.
* All Rights Reserved.
*
**************************************************************/
/* Include Files. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "giants.h"
/* Compiler options. */
#ifdef _WIN32
#pragma warning( disable : 4127 4706 ) /* disable conditional is constant warning */
#endif
/* Global variables. */
int error = 0;
int mulmode = AUTO_MUL;
int cur_prec = 0;
int cur_shift = 0;
static int cur_stack_size = 0;
static int cur_stack_elem = 0;
static int stack_glen = 0;
static giant *stack;
giant cur_den = NULL,
cur_recip = NULL;
int current_max_size = 0,
cur_run = 0;
double * sinCos=NULL;
int checkFFTerror = 0;
double maxFFTerror;
static double *z = NULL,
*z2 = NULL;
/* stack handling functions. */
static giant popg(void);
static void pushg(int);
/* Private function prototypes. */
int gerr(void);
double gfloor(double);
int radixdiv(int, int, giant);
void columnwrite(FILE *, short *, const char *, short, int);
void normal_addg(giant, giant);
void normal_subg(giant, giant);
void reverse_subg(giant, giant);
int binvaux(giant, giant);
int invaux(giant, giant);
int allzeros(int, int, giant);
void auxmulg(giant a, giant b);
void karatmulg(giant a, giant b);
void karatsquareg(giant b);
void grammarmulg(giant a, giant b);
void grammarsquareg(giant b);
int lpt(int, int *);
void addsignal(giant, double *, int);
void FFTsquareg(giant x);
void FFTmulg(giant y, giant x);
void scramble_real();
void fft_real_to_hermitian(double *z, int n);
void fftinv_hermitian_to_real(double *z, int n);
void mul_hermitian(double *a, double *b, int n);
void square_hermitian(double *b, int n);
void giant_to_double(giant x, int sizex, double *z, int L);
void gswap(giant *, giant *);
void onestep(giant, giant, gmatrix);
void mulvM(gmatrix, giant, giant);
void mulmM(gmatrix, gmatrix);
void writeM(gmatrix);
static void punch(giant, gmatrix);
static void dotproduct(giant, giant, giant, giant);
void fix(giant *, giant *);
void hgcd(int, giant, giant, gmatrix);
void shgcd(int, int, gmatrix);
/**************************************************************
*
* Functions
*
**************************************************************/
/**************************************************************
*
* Initialization and utility functions
*
**************************************************************/
double
gfloor(
double f
)
{
return floor(f);
}
void
init_sinCos(
int n
)
{
int j;
double e = TWOPI/n;
if (n<=cur_run)
return;
cur_run = n;
if (sinCos)
free(sinCos);
sinCos = (double *)malloc(sizeof(double)*(1+(n>>2)));
for (j=0;j<=(n>>2);j++)
{
sinCos[j] = sin(e*j);
}
}
double
s_sin(
int n
)
{
int seg = n/(cur_run>>2);
switch (seg)
{
case 0: return(sinCos[n]);
case 1: return(sinCos[(cur_run>>1)-n]);
case 2: return(-sinCos[n-(cur_run>>1)]);
case 3: return(-sinCos[cur_run-n]);
}
return 0;
}
double
s_cos(
int n
)
{
int quart = (cur_run>>2);
if (n < quart)
return(s_sin(n+quart));
return(-s_sin(n-quart));
}
int
gerr(void)
{
return(error);
}
giant
popg (
void
)
{
int i;
if (current_max_size <= 0) current_max_size = MAX_SHORTS;
if (cur_stack_size == 0) {
/* Initialize the stack if we're just starting out.
* Note that all stack giants will be whatever current_max_size is
* when newgiant() is first called. */
cur_stack_size = STACK_GROW;
stack = (giant *) malloc (cur_stack_size * sizeof(giant));
for(i = 0; i < STACK_GROW; i++)
stack[i] = NULL;
if (stack_glen == 0) stack_glen = current_max_size;
} else if (cur_stack_elem >= cur_stack_size) {
/* Expand the stack if we need to. */
i = cur_stack_size;
cur_stack_size += STACK_GROW;
stack = (giant *) realloc (stack,cur_stack_size * sizeof(giant));
for (; i < cur_stack_size; i++)
stack[i] = NULL;
} else if (cur_stack_elem < cur_stack_size - 2*STACK_GROW) {
/* Prune the stack if it's too big. Disabled, so the stack can only expand */
/* cur_stack_size -= STACK_GROW;
for (i = cur_stack_size - STACK_GROW; i < cur_stack_size; i++)
free(stack[i]);
stack = (giant *) realloc (stack,cur_stack_size * sizeof(giant)); */
}
/* Malloc our giant. */
if (stack[cur_stack_elem] == NULL)
stack[cur_stack_elem] = (giant ) malloc(stack_glen*sizeof(short)+sizeof(int));
stack[cur_stack_elem]->sign = 0;
return(stack[cur_stack_elem++]);
}
void
pushg (
int a
)
{
if (a < 0) return;
cur_stack_elem -= a;
if (cur_stack_elem < 0) cur_stack_elem = 0;
}
giant
newgiant(
int numshorts
)
{
int size;
giant thegiant;
if (numshorts > MAX_SHORTS) {
fprintf(stderr, "Requested giant too big.\n");
fflush(stderr);
}
if (numshorts<=0)
numshorts = MAX_SHORTS;
size = numshorts*sizeof(short)+sizeof(int);
thegiant = (giant)malloc(size);
thegiant->sign = 0;
if (newmin(2*numshorts,MAX_SHORTS) > current_max_size)
current_max_size = newmin(2*numshorts,MAX_SHORTS);
/* If newgiant() is being called for the first time, set the
* size of the stack giants. */
if (stack_glen == 0) stack_glen = current_max_size;
return(thegiant);
}
gmatrix
newgmatrix(
void
)
/* Allocates space for a gmatrix struct, but does not
* allocate space for the giants. */
{
return((gmatrix) malloc (4*sizeof(giant)));
}
int
bitlen(
giant n
)
{
int b = 16, c = 1<<15, w;
if (isZero(n))
return(0);
w = n->n[abs(n->sign) - 1];
while ((w&c) == 0)
{
b--;
c >>= 1;
}
return (16*(abs(n->sign)-1) + b);
}
int
bitval(
giant n,
int pos
)
{
int i = abs(pos)>>4, c = 1 << (pos&15);
return (((n->n[i]) & c) != 0);
}
int
isone(
giant g
)
{
return((g->sign==1)&&(g->n[0]==1));
}
int isZero(
giant thegiant
)
/* Returns TR if thegiant == 0. */
{
register int count;
int length = abs(thegiant->sign);
register unsigned short * numpointer = thegiant->n;
if (length)
{
for(count = 0; count<length; ++count,++numpointer)
{
if (*numpointer != 0 )
return(FA);
}
}
return(TR);
}
void
gtog(
giant srcgiant,
giant destgiant
)
/* destgiant becomes equal to srcgiant. */
{
int numbytes = sizeof(int) + abs(srcgiant->sign)*sizeof(short);
memcpy((char *)destgiant,(char *)srcgiant,numbytes);
}
void
itog(
int i,
giant g
)
/* The giant g becomes set to the integer value i. */
{
unsigned int j = abs(i);
if (i==0)
{
g->sign = 0;
g->n[0] = 0;
return;
}
g->n[0] = (unsigned short)(j & 0xFFFF);
j >>= 16;
if (j)
{
g->n[1] = (unsigned short)j;
g->sign = 2;
}
else
{
g->sign = 1;
}
if (i<0)
g->sign = -(g->sign);
}
signed int
gtoi(
giant x
)
/* Calculate the value of an int-sized giant NOT exceeding 31 bits. */
{
register int size = abs(x->sign);
register int sign = (x->sign < 0) ? -1 : 1;
switch(size)
{
case 0:
break;
case 1:
return sign * x->n[0];
case 2:
return sign * (x->n[0]+((x->n[1])<<16));
default:
fprintf(stderr,"Giant too large for gtoi\n");
break;
}
return 0;
}
int
gsign(
giant g
)
/* Returns the sign of g. */
{
if (isZero(g))
return(0);
if (g->sign >0)
return(1);
return(-1);
}
#if 0
int gcompg(a,b)
/* Returns -1,0,1 if a<b, a=b, a>b, respectively. */
giant a,b;
{
int size = abs(a->sign);
if(isZero(a)) size = 0;
if (size == 0) {
if (isZero(b)) return(0); else return(-gsign(b));
}
if (b->sign == 0) return(gsign(a));
if (gsign(a)!=gsign(b)) return(gsign(a));
if (size>abs(b->sign)) return(gsign(a));
if (size<abs(b->sign)) return(-gsign(a));
do {
--size;
if (a->n[size] > b->n[size]) return(gsign(a));
if (a->n[size] < b->n[size]) return(-gsign(a));
} while(size>0);
return(0);
}
#else
int
gcompg(
giant a,
giant b
)
/* Returns -1,0,1 if a<b, a=b, a>b, respectively. */
{
int sa = a->sign, j, sb = b->sign, va, vb, sgn;
if(sa > sb)
return(1);
if(sa < sb)
return(-1);
if(sa < 0)
{
sa = -sa; /* Take absolute value of sa. */
sgn = -1;
}
else
{
sgn = 1;
}
for(j = sa-1; j >= 0; j--)
{
va = a->n[j];
vb = b->n[j];
if (va > vb)
return(sgn);
if (va < vb)
return(-sgn);
}
return(0);
}
#endif
void
setmulmode(
int mode
)
{
mulmode = mode;
}
/**************************************************************
*
* Private I/O Functions
*
**************************************************************/
int
radixdiv(
int base,
int divisor,
giant thegiant
)
/* Divides giant of arbitrary base by divisor.
* Returns remainder. Used by idivg and gread. */
{
int first = TR;
int finalsize = abs(thegiant->sign);
int j = finalsize-1;
unsigned short *digitpointer=&thegiant->n[j];
unsigned int num,rem=0;
if (divisor == 0)
{
error = DIVIDEBYZERO;
exit(error);
}
while (j>=0)
{
num=rem*base + *digitpointer;
*digitpointer = (unsigned short)(num/divisor);
if (first)
{
if (*digitpointer == 0)
--finalsize;
else
first = FA;
}
rem = num % divisor;
--digitpointer;
--j;
}
if ((divisor<0) ^ (thegiant->sign<0))
finalsize=-finalsize;
thegiant->sign=finalsize;
return(rem);
}
void
columnwrite(
FILE *filepointer,
short *column,
const char *format,
short arg,
int newlines
)
/* Used by gwriteln. */
{
char outstring[10];
short i;
sprintf(outstring,format,arg);
for (i=0; outstring[i]!=0; ++i)
{
if (newlines)
{
if (*column >= COLUMNWIDTH)
{
fputs("\\\n",filepointer);
*column = 0;
}
}
fputc(outstring[i],filepointer);
++*column;
}
}
void
gwrite(
giant thegiant,
FILE *filepointer,
int newlines
)
/* Outputs thegiant to filepointer. Output is terminated by a newline. */
{
short column;
unsigned int i;
unsigned short *numpointer;
giant garbagegiant, basetengrand;
basetengrand = popg();
garbagegiant = popg();
if (isZero(thegiant))
{
fputs("0",filepointer);
}
else
{
numpointer = basetengrand->n;
gtog(thegiant,garbagegiant);
basetengrand->sign = 0;
do
{
*numpointer = (unsigned short)idivg(10000,garbagegiant);
++numpointer;
if (++basetengrand->sign >= current_max_size)
{
error = OVFLOW;
exit(error);
}
} while (!isZero(garbagegiant));
if (!error)
{
i = basetengrand->sign-1;
column = 0;
if (thegiant->sign<0 && basetengrand->n[i]!=0)
columnwrite(filepointer,&column,"-",0, newlines);
columnwrite(filepointer,&column,"%d",basetengrand->n[i],newlines);
for( ; i>0; )
{
--i;
columnwrite(filepointer,&column,"%04d",basetengrand->n[i],newlines);
}
}
}
pushg(2);
}
void
gwriteln(
giant theg,
FILE *filepointer
)
{
gwrite(theg, filepointer, 1);
fputc('\n',filepointer);
}
void
gread(
giant theg,
FILE *filepointer
)
{
char currentchar;
int isneg,size,backslash=FA,numdigits=0;
unsigned short *numpointer;
giant basetenthousand;
static char *inbuf = NULL;
basetenthousand = popg();
if (inbuf == NULL)
inbuf = (char*)malloc(MAX_DIGITS);
currentchar = (char)fgetc(filepointer);
if (currentchar=='-')
{
isneg=TR;
}
else
{
isneg=FA;
if (currentchar!='+')
ungetc(currentchar,filepointer);
}
do
{
currentchar = (char)fgetc(filepointer);
if ((currentchar>='0') && (currentchar<='9'))
{
inbuf[numdigits]=currentchar;
if(++numdigits==MAX_DIGITS)
break;
backslash=FA;
}
else
{
if (currentchar=='\\')
backslash=TR;
}
} while(((currentchar!=' ') && (currentchar!='\n') &&
(currentchar!='\t')) || (backslash) );
if (numdigits)
{
size = 0;
do
{
inbuf[numdigits] = 0;
numdigits-=4;
if (numdigits<0)
numdigits=0;
basetenthousand->n[size] = (unsigned short)strtol(&inbuf[numdigits],NULL,10);
++size;
} while (numdigits>0);
basetenthousand->sign = size;
theg->sign = 0;
numpointer = theg->n;
do
{
*numpointer = (unsigned short)
radixdiv(10000,1<<(8*sizeof(short)),basetenthousand);
++numpointer;
if (++theg->sign >= current_max_size)
{
error = OVFLOW;
exit(error);
}
} while (!isZero(basetenthousand));
if (isneg)
theg->sign = -theg->sign;
}
pushg(1);
}
/**************************************************************
*
* Private Math Functions
*
**************************************************************/
void
negg(
giant g
)
/* g becomes -g. */
{
g->sign = -g->sign;
}
void
absg(
giant g
)
{
/* g becomes the absolute value of g. */
if (g->sign <0)
g->sign=-g->sign;
}
void
iaddg(
int i,
giant g
)
/* Giant g becomes g + (int)i. */
{
int w,j=0,carry = 0, size = abs(g->sign);
giant tmp;
if (isZero(g))
{
itog(i,g);
}
else if(g->sign < 0) {
tmp = popg();
itog(i, tmp);
addg(tmp, g);
pushg(1);
return;
}
else
{
w = g->n[0]+i;
do
{
g->n[j] = (unsigned short) (w & 65535L);
carry = w >> 16;
w = g->n[++j]+carry;
} while ((carry!=0) && (j<size));
}
if (carry)
{
++g->sign;
g->n[size] = (unsigned short)carry;
}
}
/* New subtract routines.
The basic subtract "subg()" uses the following logic table:
a b if(b > a) if(a > b)
+ + b := b - a b := -(a - b)
- + b := b + (-a) N.A.
+ - N.A. b := -((-b) + a)
- - b := (-a) - (-b) b := -((-b) - (-a))
The basic addition routine "addg()" uses:
a b if(b > -a) if(-a > b)
+ + b := b + a N.A.
- + b := b - (-a) b := -((-a) - b)
+ - b := a - (-b) b := -((-b) - a)
- - N.A. b := -((-b) + (-a))
In this way, internal routines "normal_addg," "normal_subg,"
and "reverse_subg;" each of which assumes non-negative
operands and a non-negative result, are now used for greater
efficiency.
*/
void
normal_addg(
giant a,
giant b
)
/* b := a + b, both a,b assumed non-negative. */
{
int carry = 0;
int asize = a->sign, bsize = b->sign;
long k;
int j=0;
unsigned short *aptr = a->n, *bptr = b->n;
if (asize < bsize)
{
for (j=0; j<asize; j++)
{
k = *aptr++ + *bptr + carry;
carry = 0;
if (k >= 65536L)
{
k -= 65536L;
++carry;
}
*bptr++ = (unsigned short)k;
}
for (j=asize; j<bsize; j++)
{
k = *bptr + carry;
carry = 0;
if (k >= 65536L)
{
k -= 65536L;
++carry;
}
*bptr++ = (unsigned short)k;
}
}
else
{
for (j=0; j<bsize; j++)
{
k = *aptr++ + *bptr + carry;
carry = 0;
if (k >= 65536L)
{
k -= 65536L;
++carry;
}
*bptr++ = (unsigned short)k;
}
for (j=bsize; j<asize; j++)
{
k = *aptr++ + carry;
carry = 0;
if (k >= 65536L)
{
k -= 65536L;
++carry;
}
*bptr++ = (unsigned short)k;
}
}
if (carry)
{
*bptr = 1; ++j;
}
b->sign = j;
}
void
normal_subg(
giant a,
giant b
)
/* b := b - a; requires b, a non-negative and b >= a. */
{
int j, size = b->sign;
unsigned int k;
if (a->sign == 0)
return;
k = 0;
for (j=0; j<a->sign; ++j)
{
k += 0xffff - a->n[j] + b->n[j];
b->n[j] = (unsigned short)(k & 0xffff);
k >>= 16;
}
for (j=a->sign; j<size; ++j)
{
k += 0xffff + b->n[j];
b->n[j] = (unsigned short)(k & 0xffff);
k >>= 16;
}
if (b->n[0] == 0xffff)
iaddg(1,b);
else
++b->n[0];
while ((size-- > 0) && (b->n[size]==0));
b->sign = (b->n[size]==0) ? 0 : size+1;
}
void
reverse_subg(
giant a,
giant b
)
/* b := a - b; requires b, a non-negative and a >= b. */
{
int j, size = a->sign;
unsigned int k;
k = 0;
for (j=0; j<b->sign; ++j)
{
k += 0xffff - b->n[j] + a->n[j];
b->n[j] = (unsigned short)(k & 0xffff);
k >>= 16;
}
for (j=b->sign; j<size; ++j)
{
k += 0xffff + a->n[j];
b->n[j] = (unsigned short)(k & 0xffff);
k >>= 16;
}
b->sign = size; /* REC, 21 Apr 1996. */
if (b->n[0] == 0xffff)
iaddg(1,b);
else
++b->n[0];
while (!b->n[--size]);
b->sign = size+1;
}
void
addg(
giant a,