forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteger.c
2819 lines (2418 loc) · 106 KB
/
integer.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
/****************************************************************************
**
*W integer.c GAP source Martin Schoenert
** & Alice Niemeyer
** & Werner Nickel
**
*H @(#)$Id$
**
*Y Copyright (C) 1996, Lehrstuhl D fuer Mathematik, RWTH Aachen, Germany
*Y (C) 1998 School Math and Comp. Sci., University of St. Andrews, Scotland
**
** This file implements the functions handling arbitrary size integers.
**
** There are three integer types in GAP: 'T_INT', 'T_INTPOS' and 'T_INTNEG'.
** Each integer has a unique representation, e.g., an integer that can be
** represented as 'T_INT' is never represented as 'T_INTPOS' or 'T_INTNEG'.
**
** 'T_INT' is the type of those integers small enough to fit into 29 bits.
** Therefor the value range of this small integers is: $-2^{28}...2^{28}-1$.
** This range contains about 99\% of all integers that usually occur in GAP.
** (I just made up this number, obviously it depends on the application :-)
** Only these small integers can be used as index expression into sequences.
**
** Small integers are represented by an immediate integer handle, containing
** the value instead of pointing to it, which has the following form:
**
** +-------+-------+-------+-------+- - - -+-------+-------+-------+
** | guard | sign | bit | bit | | bit | tag | tag |
** | bit | bit | 27 | 26 | | 0 | 0 | 1 |
** +-------+-------+-------+-------+- - - -+-------+-------+-------+
**
** Immediate integers handles carry the tag 'T_INT', i.e. the last bit is 1.
** This distuingishes immediate integers from other handles which point to
** structures aligned on 4 byte boundaries and therefor have last bit zero.
** (The second bit is reserved as tag to allow extensions of this scheme.)
** Using immediates as pointers and dereferencing them gives address errors.
**
** To aid overflow check the most significant two bits must always be equal,
** that is to say that the sign bit of immediate integers has a guard bit.
**
** The macros 'INTOBJ_INT' and 'INT_INTOBJ' should be used to convert between
** a small integer value and its representation as immediate integer handle.
**
** 'T_INTPOS' and 'T_INTPOS' are the types of positive respective negative
** integer values that can not be represented by immediate integers.
**
** This large integers values are represented in signed base 65536 notation.
** That means that the bag of a large integer has the following form:
**
** +-------+-------+-------+-------+- - - -+-------+-------+-------+
** | digit | digit | digit | digit | | digit | digit | digit |
** | 0 | 1 | 2 | 3 | | <n>-2 | <n>-1 | <n> |
** +-------+-------+-------+-------+- - - -+-------+-------+-------+
**
** The value of this is: $d0 + d1 65536 + d2 65536^2 + ... + d_n 65536^n$,
** respectivly the negative of this if the type of this object is T_INTNEG'.
**
** Each digit is of course stored as a 16 bit wide unsigned short.
** Note that base 65536 allows us to multiply 2 digits and add a carry digit
** without overflow in 32 bit long arithmetic, available on most processors.
**
** The number of digits in every large integer is a multiple of four.
** Therefor the leading three digits of some values will actually be zero.
** Note that the uniqueness of representation implies that not four or more
** leading digits may be zero, since |d0|d1|d2|d3| and |d0|d1|d2|d3|0|0|0|0|
** have the same value only one, the first, can be a legal representation.
**
** Because of this it is possible to do a little bit of loop unrolling.
** Thus instead of looping <n> times, handling one digit in each iteration,
** we can loop <n>/4 times, handling four digits during each iteration.
** This reduces the overhead of the loop by a factor of approximatly four.
**
** Using base 65536 representation has advantages over using other bases.
** Integers in base 65536 representation can be packed dense and therefor
** use roughly 20\% less space than integers in base 10000 representation.
** 'SumInt' is 20\% and 'ProdInt' is 40\% faster for 65536 than for 10000,
** as their runtime is linear respectivly quadratic in the number of digits.
** Dividing by 65536 and computing the remainder mod 65536 can be done fast
** by shifting 16 bit to the right and by taking the lower 16 bits.
** Larger bases are difficult because the product of two digits will not fit
** into 32 bit, which is the word size of most modern micro processors.
** Base 10000 would have the advantage that printing is very much easier,
** but 'PrInt' keeps a terminal at 9600 baud busy for almost all integers.
*/
#include "system.h" /* Ints, UInts */
const char * Revision_integer_c =
"@(#)$Id$";
#include "gasman.h" /* garbage collector */
#include "objects.h" /* objects */
#include "scanner.h" /* scanner */
#include "gvars.h" /* global variables */
#include "calls.h" /* generic call mechanism */
#include "opers.h" /* generic operations */
#include "ariths.h" /* basic arithmetic */
#include "bool.h" /* booleans */
#define INCLUDE_DECLARATION_PART
#include "integer.h" /* integers */
#undef INCLUDE_DECLARATION_PART
#include "gap.h" /* error handling, initialisation */
#include "records.h" /* generic records */
#include "precord.h" /* plain records */
#include "lists.h" /* generic lists */
#include "string.h" /* strings */
#include "saveload.h" /* saving and loading */
/****************************************************************************
**
*T TypDigit . . . . . . . . . . . . . . . . . . . . type of a single digit
**
** 'TypDigit' is the type of a single digit of an arbitrary size integer.
** This is of course unsigned short int, which gives us the 16 bits we want.
**
** 'TypDigit' is defined in the declaration file of the package as follows:
**
#ifdef SYS_IS_64_BIT
typedef UInt4 TypDigit;
#else
typedef UInt2 TypDigit;
#endif
#define NR_DIGIT_BITS (8 * sizeof(TypDigit))
#define INTBASE (1L << NR_DIGIT_BITS)
#define NR_SMALL_INT_BITS (2*NR_DIGIT_BITS - 4)
*/
#define SIZE_INT(op) (SIZE_OBJ(op) / sizeof(TypDigit))
#define ADDR_INT(op) ((TypDigit*)ADDR_OBJ(op))
/****************************************************************************
**
*F TypeInt(<int>) . . . . . . . . . . . . . . . . . . . . . kind of integer
**
** 'TypeInt' returns the kind of the integer <int>.
**
** 'TypeInt' is the function in 'TypeObjFuncs' for integers.
*/
Obj TYPE_INT_SMALL_ZERO;
Obj TYPE_INT_SMALL_POS;
Obj TYPE_INT_SMALL_NEG;
Obj TYPE_INT_LARGE_POS;
Obj TYPE_INT_LARGE_NEG;
Obj TypeIntSmall (
Obj val )
{
if ( 0 == INT_INTOBJ(val) ) {
return TYPE_INT_SMALL_ZERO;
}
else if ( 0 < INT_INTOBJ(val) ) {
return TYPE_INT_SMALL_POS;
}
else /* if ( 0 > INT_INTOBJ(val) ) */ {
return TYPE_INT_SMALL_NEG;
}
}
Obj TypeIntLargePos (
Obj val )
{
return TYPE_INT_LARGE_POS;
}
Obj TypeIntLargeNeg (
Obj val )
{
return TYPE_INT_LARGE_NEG;
}
/****************************************************************************
**
*F PrintInt( <int> ) . . . . . . . . . . . . . . . print an integer constant
**
** 'PrintInt' prints the integer <int> in the usual decimal notation.
** 'PrintInt' handles objects of type 'T_INT', 'T_INTPOS' and 'T_INTNEG'.
**
** Large integers are first converted into base 10000 and then printed.
** The time for a conversion depends quadratically on the number of digits.
** For 2000 decimal digit integers, a screenfull, it is reasonable fast.
**
** The number of digits needed in PrIntD[] is the ceiling of the logarithm
** with respect to base PRINT_BASE of
**
** ( (1<<NR_DIGIT_BITS) )^1000 - 1.
**
** The latter is the largest number that can be represented with 1000 digits
** of type TypDigit.
**
** If NR_DIGIT_BITS is 16, we get 1205.
** If NR_DIGIT_BITS is 32, we get 1071.
*/
TypDigit PrIntC [1000]; /* copy of integer to be printed */
#ifdef SYS_IS_64_BIT
#define PRINT_BASE 1000000000L /* 10^9 */
#define PRINT_FORMAT "%09d" /* print 9 decimals at a time */
TypDigit PrIntD [1071]; /* integer converted to base 10^9 */
#else
#define PRINT_BASE 10000
#define PRINT_FORMAT "%04d" /* print 4 decimals at a time */
TypDigit PrIntD [1205]; /* integer converted to base 10000 */
#endif
void PrintInt (
Obj op )
{
Int i, k; /* loop counter */
TypDigit * p; /* loop pointer */
UInt c; /* carry in division step */
/* print a small integer */
if ( IS_INTOBJ(op) ) {
Pr( "%>%d%<", INT_INTOBJ(op), 0L );
}
/* print a large integer */
else if ( SIZE_INT(op) < 1000 ) {
/* start printing, %> means insert '\' before a linebreak */
Pr("%>",0L,0L);
if ( TNUM_OBJ(op) == T_INTNEG )
Pr("-",0L,0L);
/* convert the integer into base PRINT_BASE */
i = 0;
for ( k = 0; k < SIZE_INT(op); k++ )
PrIntC[k] = ADDR_INT(op)[k];
while ( k > 0 && PrIntC[k-1] == 0 ) k--;
while ( k > 0 ) {
for ( c = 0, p = PrIntC+k-1; p >= PrIntC; p-- ) {
c = (c<<NR_DIGIT_BITS) + *p;
*p = c / PRINT_BASE;
c = c - PRINT_BASE * *p;
}
PrIntD[i++] = c;
while ( k > 0 && PrIntC[k-1] == 0 ) k--;
}
/* print the base PRINT_BASE digits */
Pr( "%d", (Int)PrIntD[--i], 0L );
while ( i > 0 )
Pr( PRINT_FORMAT, (Int)PrIntD[--i], 0L );
Pr("%<",0L,0L);
}
else {
Pr("<<an integer too large to be printed>>",0L,0L);
}
}
/****************************************************************************
**
*F EqInt( <intL>, <intR> ) . . . . . . . . . test if two integers are equal
**
** 'EqInt' returns 1 if the two integer arguments <intL> and <intR> are
** equal and 0 otherwise.
*/
Int EqInt (
Obj opL,
Obj opR )
{
Int k; /* loop counter */
TypDigit * l; /* pointer into the left operand */
TypDigit * r; /* pointer into the right operand */
/* compare two small integers */
if ( ARE_INTOBJS( opL, opR ) ) {
if ( INT_INTOBJ(opL) == INT_INTOBJ(opR) ) return 1L;
else return 0L;
}
/* compare a small and a large integer */
else if ( IS_INTOBJ(opL) ) {
return 0L;
}
else if ( IS_INTOBJ(opR) ) {
return 0L;
}
/* compare two large integers */
else {
/* compare the sign and size */
if ( TNUM_OBJ(opL) != TNUM_OBJ(opR)
|| SIZE_INT(opL) != SIZE_INT(opR) )
return 0L;
/* set up the pointers */
l = ADDR_INT(opL);
r = ADDR_INT(opR);
/* run through the digits, four at a time */
for ( k = SIZE_INT(opL)/4-1; k >= 0; k-- ) {
if ( *l++ != *r++ ) return 0L;
if ( *l++ != *r++ ) return 0L;
if ( *l++ != *r++ ) return 0L;
if ( *l++ != *r++ ) return 0L;
}
/* no differences found, so they must be equal */
return 1L;
}
}
/****************************************************************************
**
*F LtInt( <intL>, <intR> ) . . . . . test if an integer is less than another
**
** 'LtInt' returns 1 if the integer <intL> is strictly less than the integer
** <intR> and 0 otherwise.
*/
Int LtInt (
Obj opL,
Obj opR )
{
Int k; /* loop counter */
TypDigit * l; /* pointer into the left operand */
TypDigit * r; /* pointer into the right operand */
/* compare two small integers */
if ( ARE_INTOBJS( opL, opR ) ) {
if ( INT_INTOBJ(opL) < INT_INTOBJ(opR) ) return 1L;
else return 0L;
}
/* compare a small and a large integer */
else if ( IS_INTOBJ(opL) ) {
if ( TNUM_OBJ(opR) == T_INTPOS ) return 1L;
else return 0L;
}
else if ( IS_INTOBJ(opR) ) {
if ( TNUM_OBJ(opL) == T_INTPOS ) return 0L;
else return 1L;
}
/* compare two large integers */
else {
/* compare the sign and size */
if ( TNUM_OBJ(opL) == T_INTNEG
&& TNUM_OBJ(opR) == T_INTPOS )
return 1L;
else if ( TNUM_OBJ(opL) == T_INTPOS
&& TNUM_OBJ(opR) == T_INTNEG )
return 0L;
else if ( (TNUM_OBJ(opL) == T_INTPOS
&& SIZE_INT(opL) < SIZE_INT(opR))
|| (TNUM_OBJ(opL) == T_INTNEG
&& SIZE_INT(opL) > SIZE_INT(opR)) )
return 1L;
else if ( (TNUM_OBJ(opL) == T_INTPOS
&& SIZE_INT(opL) > SIZE_INT(opR))
|| (TNUM_OBJ(opL) == T_INTNEG
&& SIZE_INT(opL) < SIZE_INT(opR)) )
return 0L;
/* set up the pointers */
l = ADDR_INT(opL);
r = ADDR_INT(opR);
/* run through the digits, from the end downwards */
for ( k = SIZE_INT(opL)-1; k >= 0; k-- ) {
if ( l[k] != r[k] ) {
if ( (TNUM_OBJ(opL) == T_INTPOS
&& l[k] < r[k])
|| (TNUM_OBJ(opL) == T_INTNEG
&& l[k] > r[k]) )
return 1L;
else
return 0L;
}
}
/* no differences found, so they must be equal */
return 0L;
}
}
/****************************************************************************
**
*F SumInt( <intL>, <intR> ) . . . . . . . . . . . . . . sum of two integers
**
** 'SumInt' returns the sum of the two integer arguments <intL> and <intR>.
** 'SumInt' handles operands of type 'T_INT', 'T_INTPOS' and 'T_INTNEG'.
**
** It can also be used in the cases that both operands are small integers
** and the result is a small integer too, i.e., that no overflow occurs.
** This case is usually already handled in 'EvSum' for a better efficiency.
**
** Is called from the 'EvSum' binop so both operands are already evaluated.
**
** 'SumInt' is a little bit difficult since there are 16 different cases to
** handle, each operand can be positive or negative, small or large integer.
** If the operands have opposite sign 'SumInt' calls 'DiffInt', this helps
** reduce the total amount of code by a factor of two.
*/
Obj SumInt (
Obj opL,
Obj opR )
{
Int i; /* loop variable */
Int k; /* loop variable */
Int c; /* sum of two digits */
TypDigit * l; /* pointer into the left operand */
TypDigit * r; /* pointer into the right operand */
TypDigit * s; /* pointer into the sum */
UInt * l2; /* pointer to get 2 digits at once */
UInt * s2; /* pointer to put 2 digits at once */
Obj sum; /* handle of the result bag */
/* adding two small integers */
if ( ARE_INTOBJS( opL, opR ) ) {
/* add two small integers with a small sum */
/* add and compare top two bits to check that no overflow occured */
if ( SUM_INTOBJS( sum, opL, opR ) ) {
return sum;
}
/* add two small integers with a large sum */
c = INT_INTOBJ(opL) + INT_INTOBJ(opR);
if ( 0 < c ) {
sum = NewBag( T_INTPOS, 4*sizeof(TypDigit) );
ADDR_INT(sum)[0] = c;
ADDR_INT(sum)[1] = c >> NR_DIGIT_BITS;
}
else {
sum = NewBag( T_INTNEG, 4*sizeof(TypDigit) );
ADDR_INT(sum)[0] = (-c);
ADDR_INT(sum)[1] = (-c) >> NR_DIGIT_BITS;
}
}
/* adding one large integer and one small integer */
else if ( IS_INTOBJ(opL) || IS_INTOBJ(opR) ) {
/* make the right operand the small one */
if ( IS_INTOBJ(opL) ) {
sum = opL; opL = opR; opR = sum;
}
/* if the integers have different sign, let 'DiffInt' do the work */
if ( (TNUM_OBJ(opL) == T_INTNEG && 0 <= INT_INTOBJ(opR))
|| (TNUM_OBJ(opL) == T_INTPOS && INT_INTOBJ(opR) < 0) ) {
if ( TNUM_OBJ(opL) == T_INTPOS ) RetypeBag( opL, T_INTNEG );
else RetypeBag( opL, T_INTPOS );
sum = DiffInt( opR, opL );
if ( TNUM_OBJ(opL) == T_INTPOS ) RetypeBag( opL, T_INTNEG );
else RetypeBag( opL, T_INTPOS );
return sum;
}
/* allocate the result bag and set up the pointers */
if ( TNUM_OBJ(opL) == T_INTPOS ) {
i = INT_INTOBJ(opR);
sum = NewBag( T_INTPOS, (SIZE_INT(opL)+4)*sizeof(TypDigit) );
}
else {
i = -INT_INTOBJ(opR);
sum = NewBag( T_INTNEG, (SIZE_INT(opL)+4)*sizeof(TypDigit) );
}
l = ADDR_INT(opL);
s = ADDR_INT(sum);
/* add the first four digits,the right operand has only two digits */
c = (Int)*l++ + (TypDigit)i; *s++ = c;
c = (Int)*l++ + (i>>NR_DIGIT_BITS) + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
/* propagate the carry, this loop is almost never executed */
for ( k = SIZE_INT(opL)/4-1; k != 0 && (c>>NR_DIGIT_BITS) != 0; k-- ) {
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
}
/* just copy the remaining digits, do it two digits at once */
for ( l2 = (UInt*)l, s2 = (UInt*)s; k != 0; k-- ) {
*s2++ = *l2++;
*s2++ = *l2++;
}
/* if there is a carry, enter it, otherwise shrink the sum */
if ( (c>>NR_DIGIT_BITS) != 0 )
*s++ = (c>>NR_DIGIT_BITS);
else
ResizeBag( sum, (SIZE_INT(sum)-4)*sizeof(TypDigit) );
}
/* add two large integers */
else {
/* if the integers have different sign, let 'DiffInt' do the work */
if ( (TNUM_OBJ(opL) == T_INTPOS && TNUM_OBJ(opR) == T_INTNEG)
|| (TNUM_OBJ(opL) == T_INTNEG && TNUM_OBJ(opR) == T_INTPOS) ) {
if ( TNUM_OBJ(opL) == T_INTPOS ) RetypeBag( opL, T_INTNEG );
else RetypeBag( opL, T_INTPOS );
sum = DiffInt( opR, opL );
if ( TNUM_OBJ(opL) == T_INTPOS ) RetypeBag( opL, T_INTNEG );
else RetypeBag( opL, T_INTPOS );
return sum;
}
/* make the right operand the smaller one */
if ( SIZE_INT(opL) < SIZE_INT(opR) ) {
sum = opL; opL = opR; opR = sum;
}
/* allocate the result bag and set up the pointers */
if ( TNUM_OBJ(opL) == T_INTPOS ) {
sum = NewBag( T_INTPOS, (SIZE_INT(opL)+4)*sizeof(TypDigit) );
}
else {
sum = NewBag( T_INTNEG, (SIZE_INT(opL)+4)*sizeof(TypDigit) );
}
l = ADDR_INT(opL);
r = ADDR_INT(opR);
s = ADDR_INT(sum);
/* add the digits, convert to Int to get maximum precision */
c = 0;
for ( k = SIZE_INT(opR)/4; k != 0; k-- ) {
c = (Int)*l++ + (Int)*r++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (Int)*r++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (Int)*r++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (Int)*r++ + (c>>NR_DIGIT_BITS); *s++ = c;
}
/* propagate the carry, this loop is almost never executed */
for ( k=(SIZE_INT(opL)-SIZE_INT(opR))/4;
k!=0 && (c>>NR_DIGIT_BITS)!=0; k-- ) {
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *s++ = c;
}
/* just copy the remaining digits, do it two digits at once */
for ( l2 = (UInt*)l, s2 = (UInt*)s; k != 0; k-- ) {
*s2++ = *l2++;
*s2++ = *l2++;
}
/* if there is a carry, enter it, otherwise shrink the sum */
if ( (c>>NR_DIGIT_BITS) != 0 )
*s++ = (c>>NR_DIGIT_BITS);
else
ResizeBag( sum, (SIZE_INT(sum)-4)*sizeof(TypDigit) );
}
/* return the sum */
return sum;
}
/****************************************************************************
**
*F ZeroInt(<int>) . . . . . . . . . . . . . . . . . . . . zero of integers
*/
Obj ZeroInt (
Obj op )
{
return INTOBJ_INT(0);
}
/****************************************************************************
**
*F AInvInt(<int>) . . . . . . . . . . . . . additive inverse of an integer
*/
Obj AInvInt (
Obj op )
{
Obj inv;
UInt i;
/* handle small integer */
if ( IS_INTOBJ( op ) ) {
/* special case (ugh) */
if ( op == INTOBJ_INT( -(1L<<NR_SMALL_INT_BITS) ) ) {
inv = NewBag( T_INTPOS, 4*sizeof(TypDigit) );
ADDR_INT(inv)[0] = 0;
ADDR_INT(inv)[1] = (1L<<(NR_SMALL_INT_BITS-NR_DIGIT_BITS));
}
/* general case */
else {
inv = INTOBJ_INT( - INT_INTOBJ( op ) );
}
}
/* invert a large integer */
else {
/* special case (ugh) */
if ( TNUM_OBJ(op) == T_INTPOS && SIZE_INT(op) == 4
&& ADDR_INT(op)[3] == 0
&& ADDR_INT(op)[2] == 0
&& ADDR_INT(op)[1] == (1L<<(NR_SMALL_INT_BITS-NR_DIGIT_BITS))
&& ADDR_INT(op)[0] == 0 ) {
inv = INTOBJ_INT( -(1L<<NR_SMALL_INT_BITS) );
}
/* general case */
else {
if ( TNUM_OBJ(op) == T_INTPOS ) {
inv = NewBag( T_INTNEG, SIZE_OBJ(op) );
}
else {
inv = NewBag( T_INTPOS, SIZE_OBJ(op) );
}
for ( i = 0; i < SIZE_INT(op); i++ ) {
ADDR_INT(inv)[i] = ADDR_INT(op)[i];
}
}
}
/* return the inverse */
return inv;
}
/****************************************************************************
**
*F DiffInt( <intL>, <intR> ) . . . . . . . . . . difference of two integers
**
** 'DiffInt' returns the difference of the two integer arguments <intL> and
** <intR>. 'DiffInt' handles operands of type 'T_INT', 'T_INTPOS' and
** 'T_INTNEG'.
**
** It can also be used in the cases that both operands are small integers
** and the result is a small integer too, i.e., that no overflow occurs.
** This case is usually already handled in 'EvDiff' for a better efficiency.
**
** Is called from the 'EvDiff' binop so both operands are already evaluated.
**
** 'DiffInt' is a little bit difficult since there are 16 different cases to
** handle, each operand can be positive or negative, small or large integer.
** If the operands have opposite sign 'DiffInt' calls 'SumInt', this helps
** reduce the total amount of code by a factor of two.
*/
Obj DiffInt (
Obj opL,
Obj opR )
{
Int i; /* loop variable */
Int k; /* loop variable */
Int c; /* difference of two digits */
TypDigit * l; /* pointer into the left operand */
TypDigit * r; /* pointer into the right operand */
TypDigit * d; /* pointer into the difference */
UInt * l2; /* pointer to get 2 digits at once */
UInt * d2; /* pointer to put 2 digits at once */
Obj dif; /* handle of the result bag */
/* subtracting two small integers */
if ( ARE_INTOBJS( opL, opR ) ) {
/* subtract two small integers with a small difference */
/* sub and compare top two bits to check that no overflow occured */
if ( DIFF_INTOBJS( dif, opL, opR ) ) {
return dif;
}
/* subtract two small integers with a large difference */
c = INT_INTOBJ(opL) - INT_INTOBJ(opR);
if ( 0 < c ) {
dif = NewBag( T_INTPOS, 4*sizeof(TypDigit) );
ADDR_INT(dif)[0] = c;
ADDR_INT(dif)[1] = c >> NR_DIGIT_BITS;
}
else {
dif = NewBag( T_INTNEG, 4*sizeof(TypDigit) );
ADDR_INT(dif)[0] = (-c);
ADDR_INT(dif)[1] = (-c) >> NR_DIGIT_BITS;
}
}
/* subtracting one small integer and one large integer */
else if ( IS_INTOBJ( opL ) || IS_INTOBJ( opR ) ) {
/* make the right operand the small one */
if ( IS_INTOBJ( opL ) ) {
dif = opL; opL = opR; opR = dif;
c = -1;
}
else {
c = 1;
}
/* if the integers have different sign, let 'SumInt' do the work */
if ( (TNUM_OBJ(opL) == T_INTNEG && 0 <= INT_INTOBJ(opR))
|| (TNUM_OBJ(opL) == T_INTPOS && INT_INTOBJ(opR) < 0) ) {
if ( TNUM_OBJ(opL) == T_INTPOS ) RetypeBag( opL, T_INTNEG );
else RetypeBag( opL, T_INTPOS );
dif = SumInt( opL, opR );
if ( TNUM_OBJ(opL) == T_INTPOS ) RetypeBag( opL, T_INTNEG );
else RetypeBag( opL, T_INTPOS );
if ( c == 1 ) {
if ( TNUM_OBJ(dif) == T_INTPOS ) RetypeBag( dif, T_INTNEG );
else RetypeBag( dif, T_INTPOS );
}
return dif;
}
/* allocate the result bag and set up the pointers */
if ( TNUM_OBJ(opL) == T_INTPOS ) {
i = INT_INTOBJ(opR);
if ( c == 1 ) dif = NewBag( T_INTPOS, SIZE_OBJ(opL) );
else dif = NewBag( T_INTNEG, SIZE_OBJ(opL) );
}
else {
i = - INT_INTOBJ(opR);
if ( c == 1 ) dif = NewBag( T_INTNEG, SIZE_OBJ(opL) );
else dif = NewBag( T_INTPOS, SIZE_OBJ(opL) );
}
l = ADDR_INT(opL);
d = ADDR_INT(dif);
/* sub the first four digit, note the left operand has only two */
/*N (c>>16<) need not work, replace by (c<0?-1:0) */
c = (Int)*l++ - (TypDigit)i; *d++ = c;
c = (Int)*l++ - (i>>NR_DIGIT_BITS) + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
/* propagate the carry, this loop is almost never executed */
for ( k = SIZE_INT(opL)/4-1; k != 0 && (c>>NR_DIGIT_BITS) != 0; k-- ) {
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
}
/* just copy the remaining digits, do it two digits at once */
for ( l2 = (UInt*)l, d2 = (UInt*)d; k != 0; k-- ) {
*d2++ = *l2++;
*d2++ = *l2++;
}
/* no underflow since we subtracted a small int from a large one */
/* but there may be leading zeroes in the result, get rid of them */
/* occurs almost never, so it doesn't matter that it is expensive */
if ( ((UInt*)d == d2
&& d[-4] == 0 && d[-3] == 0 && d[-2] == 0 && d[-1] == 0)
|| (SIZE_INT(dif) == 4 && d[-2] == 0 && d[-1] == 0) ) {
/* find the number of significant digits */
d = ADDR_INT(dif);
for ( k = SIZE_INT(dif); k != 0; k-- ) {
if ( d[k-1] != 0 )
break;
}
/* reduce to small integer if possible, otherwise shrink bag */
if ( k <= 2 && TNUM_OBJ(dif) == T_INTPOS
&& (UInt)(INTBASE*d[1]+d[0])<(1L<<NR_SMALL_INT_BITS) )
dif = INTOBJ_INT( INTBASE*d[1]+d[0] );
else if ( k <= 2 && TNUM_OBJ(dif) == T_INTNEG
&& (UInt)(INTBASE*d[1]+d[0])<=(1L<<NR_SMALL_INT_BITS) )
dif = INTOBJ_INT( -(INTBASE*d[1]+d[0]) );
else
ResizeBag( dif, (((k + 3) / 4) * 4) * sizeof(TypDigit) );
}
}
/* subtracting two large integers */
else {
/* if the integers have different sign, let 'SumInt' do the work */
if ( (TNUM_OBJ(opL) == T_INTPOS && TNUM_OBJ(opR) == T_INTNEG)
|| (TNUM_OBJ(opL) == T_INTNEG && TNUM_OBJ(opR) == T_INTPOS) ) {
if ( TNUM_OBJ(opR) == T_INTPOS ) RetypeBag( opR, T_INTNEG );
else RetypeBag( opR, T_INTPOS );
dif = SumInt( opL, opR );
if ( TNUM_OBJ(opR) == T_INTPOS ) RetypeBag( opR, T_INTNEG );
else RetypeBag( opR, T_INTPOS );
return dif;
}
/* make the right operand the smaller one */
if ( SIZE_INT(opL) < SIZE_INT(opR)
|| (TNUM_OBJ(opL) == T_INTPOS && LtInt(opL,opR) )
|| (TNUM_OBJ(opL) == T_INTNEG && LtInt(opR,opL) ) ) {
dif = opL; opL = opR; opR = dif; c = -1;
}
else {
c = 1;
}
/* allocate the result bag and set up the pointers */
if ( (TNUM_OBJ(opL) == T_INTPOS && c == 1)
|| (TNUM_OBJ(opL) == T_INTNEG && c == -1) )
dif = NewBag( T_INTPOS, SIZE_OBJ(opL) );
else
dif = NewBag( T_INTNEG, SIZE_OBJ(opL) );
l = ADDR_INT(opL);
r = ADDR_INT(opR);
d = ADDR_INT(dif);
/* subtract the digits */
c = 0;
for ( k = SIZE_INT(opR)/4; k != 0; k-- ) {
c = (Int)*l++ - (Int)*r++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ - (Int)*r++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ - (Int)*r++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ - (Int)*r++ + (c>>NR_DIGIT_BITS); *d++ = c;
}
/* propagate the carry, this loop is almost never executed */
for ( k=(SIZE_INT(opL)-SIZE_INT(opR))/4;
k!=0 && (c>>NR_DIGIT_BITS)!=0; k-- ) {
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
c = (Int)*l++ + (c>>NR_DIGIT_BITS); *d++ = c;
}
/* just copy the remaining digits, do it two digits at once */
for ( d2 = (UInt*)d, l2 = (UInt*)l; k != 0; k-- ) {
*d2++ = *l2++;
*d2++ = *l2++;
}
/* no underflow since we subtracted a small int from a large one */
/* but there may be leading zeroes in the result, get rid of them */
/* occurs almost never, so it doesn't matter that it is expensive */
if ( ((UInt*)d == d2
&& d[-4] == 0 && d[-3] == 0 && d[-2] == 0 && d[-1] == 0)
|| (SIZE_INT(dif) == 4 && d[-2] == 0 && d[-1] == 0) ) {
/* find the number of significant digits */
d = ADDR_INT(dif);
for ( k = SIZE_INT(dif); k != 0; k-- ) {
if ( d[k-1] != 0 )
break;
}
/* reduce to small integer if possible, otherwise shrink bag */
if ( k <= 2 && TNUM_OBJ(dif) == T_INTPOS
&& (UInt)(INTBASE*d[1]+d[0]) < (1L<<NR_SMALL_INT_BITS) )
dif = INTOBJ_INT( INTBASE*d[1]+d[0] );
else if ( k <= 2 && TNUM_OBJ(dif) == T_INTNEG
&& (UInt)(INTBASE*d[1]+d[0])<=(1L<<NR_SMALL_INT_BITS))
dif = INTOBJ_INT( -(INTBASE*d[1]+d[0]) );
else
ResizeBag( dif, (((k + 3) / 4) * 4) * sizeof(TypDigit) );
}
}
/* return the difference */
return dif;
}
/****************************************************************************
**
*F ProdInt( <intL>, <intR> ) . . . . . . . . . . . . product of two integers
**
** 'ProdInt' returns the product of the two integer arguments <intL> and
** <intR>. 'ProdInt' handles operands of type 'T_INT', 'T_INTPOS' and
** 'T_INTNEG'.
**
** It can also be used in the cases that both operands are small integers
** and the result is a small integer too, i.e., that no overflow occurs.
** This case is usually already handled in 'EvProd' for a better efficiency.
**
** Is called from the 'EvProd' binop so both operands are already evaluated.
**
** The only difficult about this function is the fact that is has two handle
** 3 different situation, depending on how many arguments are small ints.
*/
Obj ProdInt (
Obj opL,
Obj opR )
{
Int i; /* loop count, value for small int */
Int k; /* loop count, value for small int */
UInt c; /* product of two digits */
TypDigit l; /* one digit of the left operand */
TypDigit * r; /* pointer into the right operand */
TypDigit * p; /* pointer into the product */
Obj prd; /* handle of the result bag */
/* multiplying two small integers */
if ( ARE_INTOBJS( opL, opR ) ) {
/* multiply two small integers with a small product */
/* multiply and divide back to check that no overflow occured */
if ( PROD_INTOBJS( prd, opL, opR ) ) {
return prd;
}
/* get the integer values */
i = INT_INTOBJ(opL);
k = INT_INTOBJ(opR);
/* allocate the product bag */
if ( (0 < i && 0 < k) || (i < 0 && k < 0) )
prd = NewBag( T_INTPOS, 4*sizeof(TypDigit) );
else
prd = NewBag( T_INTNEG, 4*sizeof(TypDigit) );
p = ADDR_INT(prd);
/* make both operands positive */
if ( i < 0 ) i = -i;
if ( k < 0 ) k = -k;
/* multiply digitwise */
c = (Int)(TypDigit)i * (TypDigit)k; p[0] = c;
c = (Int)(TypDigit)i * (k>>NR_DIGIT_BITS)
+ (c>>NR_DIGIT_BITS); p[1] = c;
p[2] = c>>NR_DIGIT_BITS;
c = (Int)(TypDigit)(i>>NR_DIGIT_BITS) * (TypDigit)k
+ p[1]; p[1] = c;
c = (Int)(TypDigit)(i>>NR_DIGIT_BITS) * (TypDigit)(k>>NR_DIGIT_BITS)
+ p[2] + (c>>NR_DIGIT_BITS); p[2] = c;
p[3] = c>>NR_DIGIT_BITS;
}
/* multiply a small and a large integer */
else if ( IS_INTOBJ(opL) || IS_INTOBJ(opR) ) {
/* make the left operand the small one */
if ( IS_INTOBJ(opR) ) {
i = INT_INTOBJ(opR); opR = opL;
}
else {
i = INT_INTOBJ(opL);
}
/* handle trivial cases first */
if ( i == 0 )
return INTOBJ_INT(0);
if ( i == 1 )
return opR;
/* the large integer 1<<28 times -1 is the small integer -(1<<28) */
if ( i == -1
&& TNUM_OBJ(opR) == T_INTPOS && SIZE_INT(opR) == 4
&& ADDR_INT(opR)[3] == 0
&& ADDR_INT(opR)[2] == 0
&& ADDR_INT(opR)[1] == (1L<<(NR_SMALL_INT_BITS-NR_DIGIT_BITS))
&& ADDR_INT(opR)[0] == 0 )
return INTOBJ_INT( -(1L<<NR_SMALL_INT_BITS) );
/* multiplication by -1 is easy, just switch the sign and copy */
if ( i == -1 ) {
if ( TNUM_OBJ(opR) == T_INTPOS )
prd = NewBag( T_INTNEG, SIZE_OBJ(opR) );
else
prd = NewBag( T_INTPOS, SIZE_OBJ(opR) );
r = ADDR_INT(opR);
p = ADDR_INT(prd);
for ( k = SIZE_INT(opR)/4; k != 0; k-- ) {
/*N should be: *p2++=*r2++; *p2++=*r2++; */
*p++ = *r++; *p++ = *r++; *p++ = *r++; *p++ = *r++;
}
return prd;