-
Notifications
You must be signed in to change notification settings - Fork 68
/
LargeIntegers.c
3854 lines (3508 loc) · 105 KB
/
LargeIntegers.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
/* Automatically generated by
SmartSyntaxPluginCodeGenerator VMMaker.oscog-eem.2495 uuid: fcbf4c90-4c50-4ff3-8690-0edfded4f9c4
from
LargeIntegersPlugin VMMaker.oscog-eem.2495 uuid: fcbf4c90-4c50-4ff3-8690-0edfded4f9c4
*/
static char __buildInfo[] = "LargeIntegersPlugin VMMaker.oscog-eem.2495 uuid: fcbf4c90-4c50-4ff3-8690-0edfded4f9c4 " __DATE__ ;
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Default EXPORT macro that does nothing (see comment in sq.h): */
#define EXPORT(returnType) returnType
/* Do not include the entire sq.h file but just those parts needed. */
#include "sqConfig.h" /* Configuration options */
#include "sqVirtualMachine.h" /* The virtual machine proxy definition */
#include "sqPlatformSpecific.h" /* Platform specific definitions */
#define true 1
#define false 0
#define null 0 /* using 'null' because nil is predefined in Think C */
#ifdef SQUEAK_BUILTIN_PLUGIN
# undef EXPORT
# define EXPORT(returnType) static returnType
#endif
#include "sqMemoryAccess.h"
/*** Constants ***/
#define PrimErrBadArgument 3
#define PrimErrNoMemory 9
/*** Function Prototypes ***/
static sqInt anyBitOfLargeIntfromto(sqInt anOop, sqInt start, sqInt stopArg);
static sqInt byteSizeOfCSI(sqInt csi);
static unsigned int cDigitAddlenwithleninto(unsigned int *pWordShort, sqInt shortLen, unsigned int *pWordLong, sqInt longLen, unsigned int *pWordRes);
static sqInt cDigitComparewithlen(unsigned int *pFirst, unsigned int *pSecond, sqInt len);
static sqInt cDigitCopyFromtolen(unsigned int *pFrom, unsigned int *pTo, sqInt len);
static sqInt cDigitDivlenremlenquolen(unsigned int *pDiv, sqInt divLen, unsigned int *pRem, sqInt remLen, unsigned int *pQuo, sqInt quoLen);
static sqInt cDigitHighBitlen(unsigned int *pUint32, sqInt len);
static sqInt cDigitLshiftfromlentolen(sqInt shiftCount, unsigned int *pFrom, sqInt lenFrom, unsigned int *pTo, sqInt lenTo);
static sqInt cDigitMontgomerylentimeslenmodulolenmInvModBinto(unsigned int *pFirst, sqInt firstLen, unsigned int *pSecond, sqInt secondLen, unsigned int *pThird, sqInt thirdLen, unsigned int mInv, unsigned int *pRes);
static unsigned int cDigitMultiplylenwithlenintolen(unsigned int *pWordShort, sqInt shortLen, unsigned int *pWordLong, sqInt longLen, unsigned int *pWordRes, sqInt resLen);
static unsigned int cDigitOfat(unsigned int *cPointer, sqInt zeroBasedDigitIndex);
static unsigned int cDigitOfatput(unsigned int *cPointer, sqInt zeroBasedDigitIndex, unsigned int aValue);
static sqInt cDigitOpshortlenlongleninto(sqInt opIndex, unsigned int *pWordShort, sqInt shortLen, unsigned int *pWordLong, sqInt longLen, unsigned int *pWordRes);
static sqInt cDigitReplacefromtowithstartingAt(unsigned int *pTo, sqInt start, sqInt stop, unsigned int *pFrom, sqInt repStart);
static sqInt cDigitRshiftfromlentolen(sqInt shiftCount, unsigned int *pFrom, sqInt lenFrom, unsigned int *pTo, sqInt lenTo);
static sqInt cDigitSublenwithleninto(unsigned int *pWordSmall, sqInt smallLen, unsigned int *pWordLarge, sqInt largeLen, unsigned int *pWordRes);
static sqInt cHighBit32(unsigned int anUnsignedInt32);
static sqInt createLargeFromSmallInteger(sqInt anOop);
static sqInt digitAddLargewith(sqInt firstInteger, sqInt secondInteger);
static sqInt digitBitLogicwithopIndex(sqInt firstInteger, sqInt secondInteger, sqInt opIx);
static sqInt digitCompareLargewith(sqInt firstInteger, sqInt secondInteger);
static sqInt digitDivLargewithnegative(sqInt firstInteger, sqInt secondInteger, sqInt neg);
static sqInt digitMontgomerytimesmodulomInvModB(sqInt firstLarge, sqInt secondLarge, sqInt thirdLarge, unsigned int mInv);
static sqInt digitMultiplyLargewithnegative(sqInt firstInteger, sqInt secondInteger, sqInt neg);
static sqInt digitOfCSIat(sqInt csi, sqInt ix);
static sqInt digitOfLargeIntat(sqInt aBytesOop, sqInt ix);
static sqInt digitSizeOfCSI(sqInt csi);
static sqInt digitSizeOfLargeInt(sqInt anOop);
static sqInt digitSubLargewith(sqInt firstInteger, sqInt secondInteger);
static sqInt digitLshift(sqInt anOop, sqInt shiftCount);
static sqInt digitRshiftlookfirst(sqInt anOop, sqInt shiftCount, sqInt a);
EXPORT(const char*) getModuleName(void);
static sqInt highBitOfLargeInt(sqInt anOop);
static sqInt isNormalized(sqInt aLargeInteger);
static sqInt largeIntgrowTo(sqInt aBytesObject, sqInt newByteLen);
static sqInt normalizeNegative(sqInt aLargeNegativeInteger);
static sqInt normalizePositive(sqInt aLargePositiveInteger);
static sqInt normalize(sqInt aLargeInteger);
EXPORT(sqInt) primAnyBitFromTo(void);
EXPORT(sqInt) primDigitAdd(void);
EXPORT(sqInt) primDigitBitAnd(void);
EXPORT(sqInt) primDigitBitOr(void);
EXPORT(sqInt) primDigitBitShiftMagnitude(void);
EXPORT(sqInt) primDigitBitXor(void);
EXPORT(sqInt) primDigitCompare(void);
EXPORT(sqInt) primDigitDivNegative(void);
EXPORT(sqInt) primDigitMultiplyNegative(void);
EXPORT(sqInt) primDigitSubtract(void);
EXPORT(sqInt) primGetModuleName(void);
EXPORT(sqInt) primMontgomeryDigitLength(void);
EXPORT(sqInt) primMontgomeryTimesModulo(void);
EXPORT(sqInt) primNormalizeNegative(void);
EXPORT(sqInt) primNormalizePositive(void);
EXPORT(sqInt) setInterpreter(struct VirtualMachine *anInterpreter);
static sqInt sqAssert(sqInt aBool);
static unsigned char unsafeByteOfLargeIntat(sqInt bytesObj, sqInt ix);
/*** Variables ***/
static const int andOpIndex = 0;
#if defined(SQUEAK_BUILTIN_PLUGIN)
# define isIntegerObject(oop) ((oop) & 1)
# if SPURVM
extern sqInt classIndexOf(sqInt);
# define LargeNegativeIntegerClassIndex 32
# define LargePositiveIntegerClassIndex 33
# if BytesPerOop == 4
# define isImmediate(oop) ((oop) & 3)
# else
# define isImmediate(oop) ((oop) & 7)
# endif
# define isKindOfInteger(oop) (isImmediate(oop) ? isIntegerObject(oop) : (unsigned)(classIndexOf(oop) - LargeNegativeIntegerClassIndex) <= 1)
# define isLargeIntegerObject(oop) (!isImmediate(oop) && (unsigned)(classIndexOf(oop) - LargeNegativeIntegerClassIndex) <= 1)
# define isLargeNegativeIntegerObject(oop) (!isImmediate(oop) && classIndexOf(oop) == LargeNegativeIntegerClassIndex)
# define isLargePositiveIntegerObject(oop) (!isImmediate(oop) && classIndexOf(oop) == LargePositiveIntegerClassIndex)
# endif /* SPURVM */
#endif /* defined(SQUEAK_BUILTIN_PLUGIN) */
#if !defined(isKindOfInteger)
# define isLargeNegativeIntegerObject(oop) (fetchClassOf(oop) == classLargeNegativeInteger())
# define isLargePositiveIntegerObject(oop) (fetchClassOf(oop) == classLargePositiveInteger())
# define isLargeIntegerObject(oop) (isLargeNegativeIntegerObject(oop) || isLargePositiveIntegerObject(oop))
# define isKindOfInteger(oop) (isIntegerObject(oop) || isLargeNegativeIntegerObject(oop) || isLargePositiveIntegerObject(oop))
#endif
#if !defined(SQUEAK_BUILTIN_PLUGIN)
static sqInt (*booleanValueOf)(sqInt obj);
static sqInt (*classArray)(void);
static sqInt (*classLargeNegativeInteger)(void);
static sqInt (*classLargePositiveInteger)(void);
static sqInt (*classString)(void);
static sqInt (*failed)(void);
static sqInt (*falseObject)(void);
static sqInt (*fetchClassOf)(sqInt oop);
static void * (*firstIndexableField)(sqInt oop);
static sqInt (*instantiateClassindexableSize)(sqInt classPointer, sqInt size);
static sqInt (*integerObjectOf)(sqInt value);
static sqInt (*integerValueOf)(sqInt oop);
static sqInt (*isBooleanObject)(sqInt oop);
static sqInt (*isIntegerObject)(sqInt objectPointer);
static sqInt (*popthenPush)(sqInt nItems, sqInt oop);
static sqInt (*popRemappableOop)(void);
static usqInt (*positive32BitValueOf)(sqInt oop);
static sqInt (*primitiveFail)(void);
static sqInt (*primitiveFailFor)(sqInt reasonCode);
static sqInt (*pushRemappableOop)(sqInt oop);
static sqInt (*slotSizeOf)(sqInt oop);
static sqInt (*stObjectatput)(sqInt array, sqInt index, sqInt value);
static sqInt (*stackIntegerValue)(sqInt offset);
static sqInt (*stackValue)(sqInt offset);
static sqInt (*success)(sqInt aBoolean);
static sqInt (*trueObject)(void);
#else /* !defined(SQUEAK_BUILTIN_PLUGIN) */
extern sqInt booleanValueOf(sqInt obj);
extern sqInt classArray(void);
extern sqInt classLargeNegativeInteger(void);
extern sqInt classLargePositiveInteger(void);
extern sqInt classString(void);
extern sqInt failed(void);
extern sqInt falseObject(void);
extern sqInt fetchClassOf(sqInt oop);
extern void * firstIndexableField(sqInt oop);
extern sqInt instantiateClassindexableSize(sqInt classPointer, sqInt size);
extern sqInt integerObjectOf(sqInt value);
extern sqInt integerValueOf(sqInt oop);
#if VM_PROXY_MAJOR > 1 || (VM_PROXY_MAJOR == 1 && VM_PROXY_MINOR >= 15)
extern sqInt isBooleanObject(sqInt oop);
#else
# define isBooleanObject(oop) 0
#endif
#if !defined(isIntegerObject)
extern sqInt isIntegerObject(sqInt objectPointer);
#endif
extern sqInt popthenPush(sqInt nItems, sqInt oop);
extern sqInt popRemappableOop(void);
extern usqInt positive32BitValueOf(sqInt oop);
extern sqInt primitiveFail(void);
extern sqInt primitiveFailFor(sqInt reasonCode);
extern sqInt pushRemappableOop(sqInt oop);
extern sqInt slotSizeOf(sqInt oop);
extern sqInt stObjectatput(sqInt array, sqInt index, sqInt value);
extern sqInt stackIntegerValue(sqInt offset);
extern sqInt stackValue(sqInt offset);
extern sqInt success(sqInt aBoolean);
extern sqInt trueObject(void);
extern
#endif
struct VirtualMachine* interpreterProxy;
static const char *moduleName =
#ifdef SQUEAK_BUILTIN_PLUGIN
"LargeIntegers v2.0 VMMaker.oscog-eem.2495 (i)"
#else
"LargeIntegers v2.0 VMMaker.oscog-eem.2495 (e)"
#endif
;
static const int orOpIndex = 1;
static const int xorOpIndex = 2;
/* Argument has to be a Large Integer! */
/* Tests for any magnitude bits in the interval from start to stopArg. */
/* LargeIntegersPlugin>>#anyBitOfLargeInt:from:to: */
static sqInt
anyBitOfLargeIntfromto(sqInt anOop, sqInt start, sqInt stopArg)
{
unsigned int *cPointer;
unsigned int digit;
usqInt firstDigitIx;
unsigned int firstMask;
usqInt ix;
usqInt lastDigitIx;
unsigned int lastMask;
sqInt magnitude;
sqInt stop;
sqInt zeroBasedDigitIndex;
/* missing DebugCode */;
if ((start < 1) || (stopArg < 1)) {
return primitiveFail();
}
magnitude = anOop;
stop = ((stopArg < (cDigitHighBitlen(((unsigned int *) (firstIndexableField(magnitude))), ((slotSizeOf(magnitude)) + 3) / 4))) ? stopArg : (cDigitHighBitlen(((unsigned int *) (firstIndexableField(magnitude))), ((slotSizeOf(magnitude)) + 3) / 4)));
if (start > stop) {
return 0;
}
firstDigitIx = ((start - 1) / 32) + 1;
lastDigitIx = ((stop - 1) / 32) + 1;
/* Note asUnsignedLong required to avoid ULLL suffix bug */
firstMask = (((unsigned long)0xFFFFFFFFU)) << ((start - 1) & 0x1F);
lastMask = ((usqInt) 0xFFFFFFFFU) >> (0x1F - ((stop - 1) & 0x1F));
if (firstDigitIx == lastDigitIx) {
/* begin cDigitOf:at: */
cPointer = ((unsigned int *) (firstIndexableField(magnitude)));
zeroBasedDigitIndex = firstDigitIx - 1;
digit = SQ_SWAP_4_BYTES_IF_BIGENDIAN((cPointer[zeroBasedDigitIndex]));
return (digit & (firstMask & lastMask)) != 0;
}
if (((cDigitOfat(((unsigned int *) (firstIndexableField(magnitude))), firstDigitIx - 1)) & firstMask) != 0) {
return 1;
}
for (ix = (firstDigitIx + 1); ix < lastDigitIx; ix += 1) {
if ((cDigitOfat(((unsigned int *) (firstIndexableField(magnitude))), ix - 1)) != 0) {
return 1;
}
}
if (((cDigitOfat(((unsigned int *) (firstIndexableField(magnitude))), lastDigitIx - 1)) & lastMask) != 0) {
return 1;
}
return 0;
}
/* Answer the number of bytes required to represent the value of a
C-SmallInteger.
*/
/* LargeIntegersPlugin>>#byteSizeOfCSI: */
static sqInt
byteSizeOfCSI(sqInt csi)
{
if (csi >= 0) {
if (csi < 256) {
return 1;
}
if (csi < 65536) {
return 2;
}
if (csi < 0x1000000) {
return 3;
}
# if BytesPerOop == 4
return 4;
# else /* BytesPerOop == 4 */
if (csi < 0x100000000LL) {
return 4;
}
if (csi < 0x10000000000LL) {
return 5;
}
if (csi < 0x1000000000000LL) {
return 6;
}
if (csi < 0x100000000000000LL) {
return 7;
}
return 8;
# endif /* BytesPerOop == 4 */
}
if (csi > -256) {
return 1;
}
if (csi > -65536) {
return 2;
}
if (csi > -16777216) {
return 3;
}
# if BytesPerOop == 4
return 4;
# else /* BytesPerOop == 4 */
if (csi > -4294967296LL) {
return 4;
}
if (csi > -1099511627776LL) {
return 5;
}
if (csi > -281474976710656LL) {
return 6;
}
if (csi > -72057594037927936LL) {
return 7;
}
return 8;
# endif /* BytesPerOop == 4 */
return 0;
}
/* pWordRes len = longLen; returns over.. */
/* LargeIntegersPlugin>>#cDigitAdd:len:with:len:into: */
static unsigned int
cDigitAddlenwithleninto(unsigned int *pWordShort, sqInt shortLen, unsigned int *pWordLong, sqInt longLen, unsigned int *pWordRes)
{
unsigned long long accum;
sqInt i;
accum = 0;
for (i = 0; i < shortLen; i += 1) {
accum = ((accum >> 32) + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pWordShort[i])))) + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pWordLong[i])));
pWordRes[i] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((accum & 0xFFFFFFFFU)));
}
for (i = shortLen; i < longLen; i += 1) {
accum = (accum >> 32) + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pWordLong[i])));
pWordRes[i] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((accum & 0xFFFFFFFFU)));
}
return accum >> 32;
}
/* Precondition: pFirst len = pSecond len. */
/* LargeIntegersPlugin>>#cDigitCompare:with:len: */
static sqInt
cDigitComparewithlen(unsigned int *pFirst, unsigned int *pSecond, sqInt len)
{
unsigned int firstDigit;
sqInt ix;
unsigned int secondDigit;
ix = len - 1;
while (ix >= 0) {
if (((secondDigit = pSecond[ix])) != ((firstDigit = pFirst[ix]))) {
if ((SQ_SWAP_4_BYTES_IF_BIGENDIAN(secondDigit)) < (SQ_SWAP_4_BYTES_IF_BIGENDIAN(firstDigit))) {
return 1;
}
else {
return -1;
}
}
ix -= 1;
}
return 0;
}
/* LargeIntegersPlugin>>#cDigitCopyFrom:to:len: */
static sqInt
cDigitCopyFromtolen(unsigned int *pFrom, unsigned int *pTo, sqInt len)
{
sqInt i;
for (i = 0; i < len; i += 1) {
pTo[i] = (pFrom[i]);
}
return 0;
}
/* LargeIntegersPlugin>>#cDigitDiv:len:rem:len:quo:len: */
static sqInt
cDigitDivlenremlenquolen(unsigned int *pDiv, sqInt divLen, unsigned int *pRem, sqInt remLen, unsigned int *pQuo, sqInt quoLen)
{
unsigned long long a;
unsigned long long b;
sqInt cond;
unsigned int dh;
sqInt dl;
unsigned int dnh;
unsigned long long hi;
sqInt i;
sqInt j;
sqInt k;
sqInt l;
unsigned long long lo;
unsigned long long mul;
unsigned long long q;
sqInt ql;
unsigned long long r1r2;
unsigned int r3;
unsigned long long t;
/* Last actual byte of data (ST ix) */
dl = divLen - 1;
ql = quoLen;
dh = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pDiv[dl - 1]));
if (dl == 1) {
dnh = 0;
}
else {
dnh = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pDiv[dl - 2]));
}
for (k = 1; k <= ql; k += 1) {
/* maintain quo*arg+rem=self */
/* Estimate rem/div by dividing the leading two unint32 of rem by dh. */
/* The estimate is q = qhi*16r100000000+qlo, where qhi and qlo are uint32. */
/* r1 := rem digitAt: j. */
j = (remLen + 1) - k;
if ((SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRem[j - 1]))) == dh) {
q = 0xFFFFFFFFU;
}
else {
/* Compute q = (r1,r2)//dh, t = (r1,r2)\\dh.
Note that r1,r2 are uint64, not uint32. */
/* r2 := (rem digitAt: j - 2). */
r1r2 = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRem[j - 1]));
r1r2 = (r1r2 << 32) + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRem[j - 2])));
t = r1r2 % dh;
/* Next compute (hi,lo) := q*dnh */
q = r1r2 / dh;
mul = q * dnh;
hi = mul >> 32;
/* Correct overestimate of q.
Max of 2 iterations through loop -- see Knuth vol. 2 */
lo = mul & 0xFFFFFFFFU;
if (j < 3) {
r3 = 0;
}
else {
r3 = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRem[j - 3]));
}
while (1) {
if ((t < hi)
|| ((t == hi)
&& (r3 < lo))) {
/* i.e. (t,r3) < (hi,lo) */
q -= 1;
if (hi == 0) {
/* since hi is unsigned we must have this guard */
cond = 0;
}
else {
if (lo < dnh) {
hi -= 1;
lo = (lo + 0x100000000LL) - dnh;
}
else {
lo -= dnh;
}
cond = hi >= dh;
}
}
else {
cond = 0;
}
if (!(cond)) break;
hi -= dh;
}
}
l = j - dl;
a = 0;
for (i = 1; i <= divLen; i += 1) {
hi = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pDiv[i - 1]))) * (q >> 32);
lo = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pDiv[i - 1]))) * (q & 0xFFFFFFFFU);
b = ((SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRem[l - 1]))) - a) - (lo & 0xFFFFFFFFU);
pRem[l - 1] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((b & 0xFFFFFFFFU)));
b = (b >> 32) | ((0 - (b >> 0x3F)) & 0xFFFFFFFF00000000ULL);
a = (hi + (lo >> 32)) - b;
l += 1;
}
if (a > 0) {
/* Add div back into rem, decrease q by 1 */
q -= 1;
l = j - dl;
a = 0;
for (i = 1; i <= divLen; i += 1) {
a = ((a >> 32) + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRem[l - 1])))) + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pDiv[i - 1])));
pRem[l - 1] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((a & 0xFFFFFFFFU)));
l += 1;
}
}
pQuo[quoLen - k] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((((unsigned int) q))));
}
return 0;
}
/* Answer the index (in bits) of the high order bit of the receiver, or zero
if the
receiver is zero. This method is allowed (and needed) for
LargeNegativeIntegers as well, since Squeak's LargeIntegers are
sign/magnitude. Work with 32 bits digits. */
/* LargeIntegersPlugin>>#cDigitHighBit:len: */
static sqInt
cDigitHighBitlen(unsigned int *pUint32, sqInt len)
{
unsigned int lastDigit;
sqInt realLength;
realLength = len;
do {
if (realLength == 0) {
return 0;
}
} while(((lastDigit = cDigitOfat(pUint32, (realLength -= 1)))) == 0);
return (cHighBit32(lastDigit)) + (32 * realLength);
}
/* C indexed! */
/* LargeIntegersPlugin>>#cDigitLshift:from:len:to:len: */
static sqInt
cDigitLshiftfromlentolen(sqInt shiftCount, unsigned int *pFrom, sqInt lenFrom, unsigned int *pTo, sqInt lenTo)
{
sqInt bitShift;
unsigned int carry;
unsigned int digit;
sqInt digitShift;
sqInt i;
sqInt i1;
sqInt limit;
sqInt rshift;
digitShift = shiftCount / 32;
bitShift = shiftCount % 32;
/* Note: 0 is endian neutral, use direct access */
limit = digitShift - 1;
for (i = 0; i <= limit; i += 1) {
pTo[i] = 0;
}
if (bitShift == 0) {
/* Fast version for digit-aligned shifts */
/* C indexed! */
/* begin cDigitCopyFrom:to:len: */
for (i1 = 0; i1 < (((lenTo - 1) - digitShift) + 1); i1 += 1) {
(pTo + digitShift)[i1] = ((pFrom)[i1]);
}
return 0;
}
rshift = 32 - bitShift;
carry = 0;
limit = lenFrom - 1;
for (i = 0; i <= limit; i += 1) {
digit = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pFrom[i]));
pTo[i + digitShift] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN(((carry | (((usqInt)(digit) << bitShift))) & 0xFFFFFFFFU)));
carry = ((usqInt) digit) >> rshift;
}
if (!(carry == 0)) {
pTo[lenTo - 1] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN(carry));
}
return 0;
}
/* LargeIntegersPlugin>>#cDigitMontgomery:len:times:len:modulo:len:mInvModB:into: */
static sqInt
cDigitMontgomerylentimeslenmodulolenmInvModBinto(unsigned int *pFirst, sqInt firstLen, unsigned int *pSecond, sqInt secondLen, unsigned int *pThird, sqInt thirdLen, unsigned int mInv, unsigned int *pRes)
{
unsigned long long accum;
unsigned long long accum2;
unsigned long long accum3;
sqInt i;
sqInt k;
unsigned int lastDigit;
sqInt limit1;
sqInt limit2;
sqInt limit3;
unsigned long long u;
limit1 = firstLen - 1;
limit2 = secondLen - 1;
limit3 = thirdLen - 1;
lastDigit = 0;
for (i = 0; i <= limit1; i += 1) {
accum3 = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pFirst[i]));
accum3 = (accum3 * (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pSecond[0])))) + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRes[0])));
u = (accum3 * mInv) & 0xFFFFFFFFU;
accum2 = u * (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pThird[0])));
accum = (accum2 & 0xFFFFFFFFU) + (accum3 & 0xFFFFFFFFU);
accum = ((accum >> 32) + (accum2 >> 32)) + (accum3 >> 32);
for (k = 1; k <= limit2; k += 1) {
accum3 = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pFirst[i]));
accum3 = (accum3 * (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pSecond[k])))) + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRes[k])));
accum2 = u * (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pThird[k])));
accum = (accum + (accum2 & 0xFFFFFFFFU)) + (accum3 & 0xFFFFFFFFU);
pRes[k - 1] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((accum & 0xFFFFFFFFU)));
accum = ((accum >> 32) + (accum2 >> 32)) + (accum3 >> 32);
}
for (k = secondLen; k <= limit3; k += 1) {
accum2 = u * (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pThird[k])));
accum = (accum + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRes[k])))) + (accum2 & 0xFFFFFFFFU);
pRes[k - 1] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((accum & 0xFFFFFFFFU)));
accum = (accum >> 32) + (accum2 >> 32);
}
accum += lastDigit;
pRes[limit3] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((accum & 0xFFFFFFFFU)));
lastDigit = accum >> 32;
}
for (i = firstLen; i <= limit3; i += 1) {
accum = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRes[0]));
u = (accum * mInv) & 0xFFFFFFFFU;
accum += u * (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pThird[0])));
accum = accum >> 32;
for (k = 1; k <= limit3; k += 1) {
accum2 = u * (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pThird[k])));
accum = (accum + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRes[k])))) + (accum2 & 0xFFFFFFFFU);
pRes[k - 1] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((accum & 0xFFFFFFFFU)));
accum = (accum >> 32) + (accum2 >> 32);
}
accum += lastDigit;
pRes[limit3] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((accum & 0xFFFFFFFFU)));
lastDigit = accum >> 32;
}
if (!((lastDigit == 0)
&& ((cDigitComparewithlen(pThird, pRes, thirdLen)) == 1))) {
/* self cDigitSub: pThird len: thirdLen with: pRes len: thirdLen into: pRes */
accum = 0;
for (i = 0; i <= limit3; i += 1) {
accum = (accum + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pRes[i])))) - (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pThird[i])));
pRes[i] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((accum & 0xFFFFFFFFU)));
accum = 0 - (accum >> 0x3F);
}
}
return 0;
}
/* LargeIntegersPlugin>>#cDigitMultiply:len:with:len:into:len: */
static unsigned int
cDigitMultiplylenwithlenintolen(unsigned int *pWordShort, sqInt shortLen, unsigned int *pWordLong, sqInt longLen, unsigned int *pWordRes, sqInt resLen)
{
unsigned long long ab;
unsigned int carry;
unsigned int digit;
sqInt i;
sqInt j;
sqInt k;
sqInt limitLong;
sqInt limitShort;
if ((shortLen == 1)
&& ((pWordShort[0]) == 0)) {
return 0;
}
if ((longLen == 1)
&& ((pWordLong[0]) == 0)) {
return 0;
}
limitShort = shortLen - 1;
limitLong = longLen - 1;
for (i = 0; i <= limitShort; i += 1) {
if (((digit = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pWordShort[i])))) != 0) {
k = i;
/* Loop invariant: 0<=carry<=16rFFFFFFFF, k=i+j-1 (ST) */
/* -> Loop invariant: 0<=carry<=16rFFFFFFFF, k=i+j (C) (?) */
carry = 0;
for (j = 0; j <= limitLong; j += 1) {
ab = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pWordLong[j]));
ab = ((ab * digit) + carry) + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pWordRes[k])));
carry = ab >> 32;
pWordRes[k] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((ab & 0xFFFFFFFFU)));
k += 1;
}
if (k < resLen) {
pWordRes[k] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN(carry));
}
}
}
return 0;
}
/* LargeIntegersPlugin>>#cDigitOf:at: */
static unsigned int
cDigitOfat(unsigned int *cPointer, sqInt zeroBasedDigitIndex)
{
return SQ_SWAP_4_BYTES_IF_BIGENDIAN((cPointer[zeroBasedDigitIndex]));
}
/* LargeIntegersPlugin>>#cDigitOf:at:put: */
static unsigned int
cDigitOfatput(unsigned int *cPointer, sqInt zeroBasedDigitIndex, unsigned int aValue)
{
return cPointer[zeroBasedDigitIndex] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN(aValue));
}
/* pWordRes len = longLen.
NOTE: we don't bother with endianness here, those bit opes are
endian-neutral
*/
/* LargeIntegersPlugin>>#cDigitOp:short:len:long:len:into: */
static sqInt
cDigitOpshortlenlongleninto(sqInt opIndex, unsigned int *pWordShort, sqInt shortLen, unsigned int *pWordLong, sqInt longLen, unsigned int *pWordRes)
{
sqInt i;
sqInt limit;
limit = shortLen - 1;
if (opIndex == andOpIndex) {
for (i = 0; i <= limit; i += 1) {
pWordRes[i] = ((pWordShort[i]) & (pWordLong[i]));
}
limit = longLen - 1;
for (i = shortLen; i <= limit; i += 1) {
pWordRes[i] = 0;
}
return 0;
}
if (opIndex == orOpIndex) {
for (i = 0; i <= limit; i += 1) {
pWordRes[i] = ((pWordShort[i]) | (pWordLong[i]));
}
limit = longLen - 1;
for (i = shortLen; i <= limit; i += 1) {
pWordRes[i] = (pWordLong[i]);
}
return 0;
}
if (opIndex == xorOpIndex) {
for (i = 0; i <= limit; i += 1) {
pWordRes[i] = ((pWordShort[i]) ^ (pWordLong[i]));
}
limit = longLen - 1;
for (i = shortLen; i <= limit; i += 1) {
pWordRes[i] = (pWordLong[i]);
}
return 0;
}
return primitiveFail();
}
/* C indexed! */
/* LargeIntegersPlugin>>#cDigitReplace:from:to:with:startingAt: */
static sqInt
cDigitReplacefromtowithstartingAt(unsigned int *pTo, sqInt start, sqInt stop, unsigned int *pFrom, sqInt repStart)
{
sqInt i;
/* begin cDigitCopyFrom:to:len: */
for (i = 0; i < ((stop - start) + 1); i += 1) {
(pTo + start)[i] = ((pFrom + repStart)[i]);
}
return 0;
}
/* LargeIntegersPlugin>>#cDigitRshift:from:len:to:len: */
static sqInt
cDigitRshiftfromlentolen(sqInt shiftCount, unsigned int *pFrom, sqInt lenFrom, unsigned int *pTo, sqInt lenTo)
{
sqInt bitShift;
unsigned int carry;
unsigned int digit;
sqInt digitShift;
sqInt i;
sqInt j;
sqInt leftShift;
sqInt limit;
sqInt start;
digitShift = shiftCount / 32;
bitShift = shiftCount % 32;
if (bitShift == 0) {
/* Fast version for digit-aligned shifts */
/* C indexed! */
/* begin cDigitReplace:from:to:with:startingAt: */
for (i = 0; i < (((lenTo - 1)) + 1); i += 1) {
(pTo)[i] = ((pFrom + digitShift)[i]);
}
return 0;
}
leftShift = 32 - bitShift;
carry = ((usqInt) (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pFrom[digitShift])))) >> bitShift;
start = digitShift + 1;
limit = lenFrom - 1;
for (j = start; j <= limit; j += 1) {
/* begin cDigitOf:at: */
digit = SQ_SWAP_4_BYTES_IF_BIGENDIAN((pFrom[j]));
/* begin cDigitOf:at:put: */
pTo[j - start] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN(((carry | (((usqInt)(digit) << leftShift))) & 0xFFFFFFFFU)));
carry = ((usqInt) digit) >> bitShift;
}
if (!(carry == 0)) {
/* begin cDigitOf:at:put: */
pTo[lenTo - 1] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN(carry));
}
return 0;
}
/* LargeIntegersPlugin>>#cDigitSub:len:with:len:into: */
static sqInt
cDigitSublenwithleninto(unsigned int *pWordSmall, sqInt smallLen, unsigned int *pWordLarge, sqInt largeLen, unsigned int *pWordRes)
{
sqInt i;
unsigned long long z;
z = 0;
for (i = 0; i < smallLen; i += 1) {
z = (z + (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pWordLarge[i])))) - (SQ_SWAP_4_BYTES_IF_BIGENDIAN((pWordSmall[i])));
/* begin cDigitOf:at:put: */
pWordRes[i] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((((unsigned int) (z & 0xFFFFFFFFU)))));
z = 0 - (z >> 0x3F);
}
for (i = smallLen; i < largeLen; i += 1) {
z += SQ_SWAP_4_BYTES_IF_BIGENDIAN((pWordLarge[i]));
/* begin cDigitOf:at:put: */
pWordRes[i] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN((((unsigned int) (z & 0xFFFFFFFFU)))));
z = 0 - (z >> 0x3F);
}
return 0;
}
/* Answer the index of the high order bit of the argument, or zero if the
argument is zero. */
/* LargeIntegersPlugin>>#cHighBit32: */
static sqInt
cHighBit32(unsigned int anUnsignedInt32)
{
sqInt bitNo;
unsigned int shifted;
shifted = anUnsignedInt32;
bitNo = 0;
if (!(shifted < (1U << 16))) {
shifted = ((usqInt) shifted) >> 16;
bitNo += 16;
}
if (!(shifted < (1U << 8))) {
shifted = ((usqInt) shifted) >> 8;
bitNo += 8;
}
if (!(shifted < (1U << 4))) {
shifted = ((usqInt) shifted) >> 4;
bitNo += 4;
}
if (!(shifted < (1U << 2))) {
shifted = ((usqInt) shifted) >> 2;
bitNo += 2;
}
if (!(shifted < (1U << 1))) {
shifted = ((usqInt) shifted) >> 1;
bitNo += 1;
}
return bitNo + shifted;
}
/* anOop has to be a SmallInteger! */
/* LargeIntegersPlugin>>#createLargeFromSmallInteger: */
static sqInt
createLargeFromSmallInteger(sqInt anOop)
{
unsigned int aValue;
sqInt byteSize;
sqInt digitSize;
sqInt ix;
unsigned int *pDigit;
sqInt res;
sqInt val;
val = integerValueOf(anOop);
/* begin byteSizeOfCSI: */
if (val >= 0) {
if (val < 256) {
byteSize = 1;
goto l2;
}
if (val < 65536) {
byteSize = 2;
goto l2;
}
if (val < 0x1000000) {
byteSize = 3;
goto l2;
}
# if BytesPerOop == 4
byteSize = 4;
goto l2;
# else /* BytesPerOop == 4 */
if (val < 0x100000000LL) {
byteSize = 4;
goto l2;
}
if (val < 0x10000000000LL) {
byteSize = 5;
goto l2;
}
if (val < 0x1000000000000LL) {
byteSize = 6;
goto l2;
}
if (val < 0x100000000000000LL) {
byteSize = 7;
goto l2;
}
byteSize = 8;
goto l2;
# endif /* BytesPerOop == 4 */
}
if (val > -256) {
byteSize = 1;
goto l2;
}
if (val > -65536) {
byteSize = 2;
goto l2;
}
if (val > -16777216) {
byteSize = 3;
goto l2;
}
# if BytesPerOop == 4
byteSize = 4;
goto l2;
# else /* BytesPerOop == 4 */
if (val > -4294967296LL) {
byteSize = 4;
goto l2;
}
if (val > -1099511627776LL) {
byteSize = 5;
goto l2;
}
if (val > -281474976710656LL) {
byteSize = 6;
goto l2;
}
if (val > -72057594037927936LL) {
byteSize = 7;
goto l2;
}
byteSize = 8;
goto l2;
# endif /* BytesPerOop == 4 */
l2: /* end byteSizeOfCSI: */;
res = instantiateClassindexableSize((val < 0
? classLargeNegativeInteger()
: classLargePositiveInteger()), byteSize);
pDigit = ((unsigned int *) (firstIndexableField(res)));
digitSize = (byteSize + 3) / 4;
for (ix = 1; ix <= digitSize; ix += 1) {
/* begin cDigitOf:at:put: */
aValue = ((unsigned int) (((usqInt) ((val < 0
? 0 - val
: val))) >> ((ix - 1) * 32)));
pDigit[ix - 1] = (SQ_SWAP_4_BYTES_IF_BIGENDIAN(aValue));
}
return res;
}
/* Does not need to normalize! */
/* LargeIntegersPlugin>>#digitAddLarge:with: */
static sqInt
digitAddLargewith(sqInt firstInteger, sqInt secondInteger)
{
unsigned long long accum;
unsigned int *cPointer;
sqInt firstDigitLen;
sqInt i;
sqInt i1;