-
Notifications
You must be signed in to change notification settings - Fork 71
/
LargeIntegersPlugin.class.st
1546 lines (1443 loc) · 54.4 KB
/
LargeIntegersPlugin.class.st
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
"
LargeIntegersPlugin provides functions for speeding up LargeInteger arithmetics. Usually it is part of your installation as 'LargeIntegers' plugin (a C-compiled binary).
Correctly installed?
----------------------
Probably you are just working with it.
To be really sure try
100 factorial. ""to force plugin loading""
SmalltalkImage current listLoadedModules.
Then you should see 'LargeIntegers' somewhere in the output strings. If this should not be the case, you probably have a problem.
Variables
-----------
Inst vars:
andOpIndex C constant
orOpIndex C constant
xorOpIndex C constant
Used like an enum, in ST one would use symbols instead.
Class vars:
none
History
--------
v2.0
Rewrite primitives to use 32-bits digits instead of 8-bits digits. This can fasten many operation up to 2x and multiplication up to 7x.
Since Large Integers are still variable byte object, care is taken to allocate just enough bytes to contain integer value (not necessarily a multiple of 4 bytes).
Primitives will use the extra bytes beyond necessary bytes up to next multiple of 4, so it's mandatory that those extra bytes be correctly set to zero
At image side, digits are still 8-bits so that fallback code involve only SmallInteger.
Primitives assume that byte ordering (endianness) is allways little endian, because LargeIntegers remain regular byte Objects.
On (hypothetic) big endian machines, conversion is performed at fetch/store time thru usage of dedicated macro.
v1.5
- no code change at all compared to v1.4
- made to outsource testing code (LargeIntegersPluginTest) introduced in earlier versions
- updated class comment: reference to LargeIntegersPluginTest removed
v1.4
- no semantic change compared to v1.3
- >>cHighBit: improved (could be faster now)
- fix: class comment
- improved class comment
- >>flag: introduced to allow #flag: messages (does nothing)
- new: class>>buildCodeGeneratorUpTo: as hook for switching debugMode (default is not to change anything)
- removed: class>>new (obsolete)
- minor cleanup of source code layout
v1.3
- fix: >>primDigitDiv:negative: now checks if its Integer args are normalized; without this change the plugin crashes, if a division by zero through a non normalized - filled with zero bytes - arg occurs. This can happen through printing by the inspector windows after changing the bytes of a LargeInteger manually.
v1.2
- fix: >>anyBitOfBytes: aBytesOop from: start to: stopArg
v1.1
- >>primGetModuleName for checking the version of the plugin;
- >>primDigitBitShiftMagnitude and >>primAnyBitFrom:to: for supporting - not installing! - unification of shift semantics of negative Integers;
v1.0
- speeds up digitDiv:neg: at about 20%.
In >>cCoreDigitDivDiv:len:rem:len:quo:len: the 'nibble' arithmetic is removed.
"
Class {
#name : #LargeIntegersPlugin,
#superclass : #SmartSyntaxInterpreterPlugin,
#instVars : [
'andOpIndex',
'orOpIndex',
'xorOpIndex'
],
#category : #'VMMaker-Plugins'
}
{ #category : #translation }
LargeIntegersPlugin class >> declareCVarsIn: cg [
cg var: 'andOpIndex' declareC: 'const int andOpIndex = 0'.
cg var: 'orOpIndex' declareC: 'const int orOpIndex = 1'.
cg var: 'xorOpIndex' declareC: 'const int xorOpIndex = 2'
]
{ #category : #translation }
LargeIntegersPlugin class >> moduleName [
^'LargeIntegers'
]
{ #category : #translation }
LargeIntegersPlugin class >> moduleNameAndVersion [
"Answer the receiver's module name and version info that is used for the plugin's C code. The default is to append the code generation date, but any useful text is ok (keep it short)"
^ self moduleName, ' ', self version
]
{ #category : #translation }
LargeIntegersPlugin class >> version [
"Answer the receiver's version info as String."
^ 'v2.0'
]
{ #category : #util }
LargeIntegersPlugin >> anyBitOfLargeInt: anOop from: start to: stopArg [
"Argument has to be a Large Integer!"
"Tests for any magnitude bits in the interval from start to stopArg."
| magnitude stop firstDigitIx lastDigitIx firstMask lastMask |
<var: #digit type: #'unsigned int'>
<var: #firstMask type: #'unsigned int'>
<var: #lastMask type: #'unsigned int'>
<var: #firstDigitIx type: #usqInt>
<var: #lastDigitIx type: #usqInt>
<var: #ix type: #usqInt>
self
debugCode: [self msg: 'anyBitOfLargeInt: anOop from: start to: stopArg'].
start < 1 | (stopArg < 1)
ifTrue: [^ interpreterProxy primitiveFail].
magnitude := anOop.
stop := stopArg min: (self highBitOfLargeInt: magnitude).
start > stop
ifTrue: [^ false].
firstDigitIx := start - 1 // 32 + 1.
lastDigitIx := stop - 1 // 32 + 1.
firstMask := 16rFFFFFFFF asUnsignedLong << (start - 1 bitAnd: 31). "Note asUnsignedLong required to avoid ULLL suffix bug"
lastMask := 16rFFFFFFFF >> (31 - (stop - 1 bitAnd: 31)).
firstDigitIx = lastDigitIx
ifTrue: [| digit |
digit := self unsafeDigitOfLargeInt: magnitude at: firstDigitIx.
^ (digit bitAnd: (firstMask bitAnd: lastMask))
~= 0].
((self unsafeDigitOfLargeInt: magnitude at: firstDigitIx) bitAnd: firstMask)
~= 0
ifTrue: [^ true].
firstDigitIx + 1
to: lastDigitIx - 1
do: [:ix | (self unsafeDigitOfLargeInt: magnitude at: ix)
~= 0
ifTrue: [^ true]].
((self unsafeDigitOfLargeInt: magnitude at: lastDigitIx) bitAnd: lastMask)
~= 0
ifTrue: [^ true].
^ false
]
{ #category : #util }
LargeIntegersPlugin >> byteSizeOfCSI: csi [
"Answer the number of bytes required to represent the value of a C-SmallInteger."
csi >= 0 ifTrue:
[csi < 256 ifTrue:
[^1].
csi < 65536 ifTrue:
[^2].
csi < 16777216 ifTrue:
[^3].
self cppIf: interpreterProxy bytesPerOop = 4
ifTrue:
[^4]
ifFalse:
[csi < 4294967296 ifTrue:
[^4].
csi < 1099511627776 ifTrue:
[^5].
csi < 281474976710656 ifTrue:
[^6].
csi < 72057594037927936 ifTrue:
[^7].
^8]].
csi > -256 ifTrue:
[^1].
csi > -65536 ifTrue:
[^2].
csi > -16777216 ifTrue:
[^3].
self cppIf: interpreterProxy bytesPerOop = 4
ifTrue:
[^4]
ifFalse:
[csi > -4294967296 ifTrue:
[^4].
csi > -1099511627776 ifTrue:
[^5].
csi > -281474976710656 ifTrue:
[^6].
csi > -72057594037927936 ifTrue:
[^7].
^8]
]
{ #category : #util }
LargeIntegersPlugin >> byteSizeOfLargeInt: bytesOop [
"Answer the number of bytes required to represent a LargeInteger.
Precondition: bytesOop is not a small integer."
<inline: #always>
^ interpreterProxy slotSizeOf: bytesOop
]
{ #category : #'C core util' }
LargeIntegersPlugin >> byteSwapped32IfBigEndian: anInteger [
"This is only for simulation purposes"
<doNotGenerate>
^interpreterProxy byteSwapped32IfBigEndian: anInteger
]
{ #category : #'C core util' }
LargeIntegersPlugin >> byteSwapped64IfBigEndian: anInteger [
"This is only for simulation purposes"
<doNotGenerate>
^interpreterProxy byteSwapped64IfBigEndian: anInteger
]
{ #category : #'C core' }
LargeIntegersPlugin >> cDigitAdd: pWordShort len: shortLen with: pWordLong len: longLen into: pWordRes [
"pWordRes len = longLen; returns over.."
| accum |
<returnTypeC: #'unsigned int'>
<var: #pWordShort type: #'unsigned int *'>
<var: #pWordLong type: #'unsigned int *'>
<var: #pWordRes type: #'unsigned int *'>
<var: #accum type: #'unsigned long long'>
accum := 0.
0 to: shortLen - 1 do:
[:i |
accum := (accum >> 32)
+ (self cDigitOf: pWordShort at: i)
+ (self cDigitOf: pWordLong at: i).
self cDigitOf: pWordRes at: i put: (accum bitAnd: 16rFFFFFFFF)].
shortLen to: longLen - 1 do:
[:i |
accum := (accum >> 32)
+ (self cDigitOf: pWordLong at: i).
self cDigitOf: pWordRes at: i put: (accum bitAnd: 16rFFFFFFFF)].
^ accum >> 32
]
{ #category : #'C core' }
LargeIntegersPlugin >> cDigitCompare: pFirst with: pSecond len: len [
"Precondition: pFirst len = pSecond len."
| secondDigit ix firstDigit |
<var: #pFirst type: #'unsigned int *'>
<var: #pSecond type: #'unsigned int *'>
<var: #firstDigit type: #'unsigned int'>
<var: #secondDigit type: #'unsigned int'>
ix := len - 1.
[ix >= 0]
whileTrue:
[(secondDigit := pSecond at: ix) ~= (firstDigit := pFirst at: ix)
ifTrue: [(self byteSwapped32IfBigEndian: secondDigit) < (self byteSwapped32IfBigEndian: firstDigit)
ifTrue: [^ 1]
ifFalse: [^ -1]].
ix := ix - 1].
^ 0
]
{ #category : #'C core util' }
LargeIntegersPlugin >> cDigitCopyFrom: pFrom to: pTo len: len [
<var: #pFrom type: #'unsigned int *'>
<var: #pTo type: #'unsigned int *'>
"Note: don't care about endianness here, copy operation is endian neutral"
0 to: len - 1 do: [:i | pTo at: i put: (pFrom at: i)].
^ 0
]
{ #category : #'C core' }
LargeIntegersPlugin >> cDigitDiv: pDiv len: divLen rem: pRem len: remLen quo: pQuo len: quoLen [
| dl ql dh dnh j t hi lo r3 l a b cond q r1r2 mul |
<var: #pDiv type: #'unsigned int *'>
<var: #pRem type: #'unsigned int *'>
<var: #pQuo type: #'unsigned int *'>
<var: #dh type: #'unsigned int'>
<var: #dnh type: #'unsigned int'>
<var: #r3 type: #'unsigned int'>
<var: #q type: #'unsigned long long'>
<var: #a type: #'unsigned long long'>
<var: #b type: #'unsigned long long'>
<var: #t type: #'unsigned long long'>
<var: #mul type: #'unsigned long long'>
<var: #hi type: #'unsigned long long'>
<var: #lo type: #'unsigned long long'>
<var: #r1r2 type: #'unsigned long long'>
dl := divLen - 1.
"Last actual byte of data (ST ix)"
ql := quoLen.
dh := self cDigitOf: pDiv at: dl - 1.
dl = 1
ifTrue: [dnh := 0]
ifFalse: [dnh := self cDigitOf: pDiv at: dl - 2].
1 to: ql do:
[:k |
"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."
j := remLen + 1 - k.
"r1 := rem digitAt: j."
(self cDigitOf: pRem at: j - 1)
= dh
ifTrue: [q := 16rFFFFFFFF]
ifFalse:
["Compute q = (r1,r2)//dh, t = (r1,r2)\\dh.
Note that r1,r2 are uint64, not uint32."
"r2 := (rem digitAt: j - 2)."
r1r2 := self cDigitOf: pRem at: j - 1.
r1r2 := (r1r2 << 32) + (self cDigitOf: pRem at: j - 2).
t := r1r2 \\ dh.
q := r1r2 // dh.
"Next compute (hi,lo) := q*dnh"
mul := q * dnh.
hi := mul >> 32.
lo := mul bitAnd: 16rFFFFFFFF.
"Correct overestimate of q.
Max of 2 iterations through loop -- see Knuth vol. 2"
j < 3
ifTrue: [r3 := 0]
ifFalse: [r3 := self cDigitOf: pRem at: j - 3].
[(t < hi
or: [t = hi and: [r3 < lo]])
ifTrue:
["i.e. (t,r3) < (hi,lo)"
q := q - 1.
hi = 0 "since hi is unsigned we must have this guard"
ifTrue: [cond := false]
ifFalse:
[lo < dnh
ifTrue:
[hi := hi - 1.
lo := lo + 16r100000000 - dnh]
ifFalse:
[lo := lo - dnh].
cond := hi >= dh]]
ifFalse: [cond := false].
cond]
whileTrue: [hi := hi - dh]].
"Subtract q*div from rem"
l := j - dl.
a := 0.
1 to: divLen do:
[:i |
hi := (self cDigitOf: pDiv at: i - 1) * (q >> 32).
lo := (self cDigitOf: pDiv at: i - 1) * (q bitAnd: 16rFFFFFFFF).
b := (self cDigitOf: pRem at: l - 1) - a - (lo bitAnd: 16rFFFFFFFF).
self cDigitOf: pRem at: l - 1 put: (b bitAnd: 16rFFFFFFFF).
"simulate arithmetic shift (preserving sign of b)"
b := b >> 32 bitOr: (0 - (b >> 63) bitAnd: 16rFFFFFFFF00000000).
a := hi + (lo >> 32) - b.
l := l + 1].
a > 0
ifTrue:
["Add div back into rem, decrease q by 1"
q := q - 1.
l := j - dl.
a := 0.
1 to: divLen do:
[:i |
a := (a >> 32)
+ (self cDigitOf: pRem at: l - 1) + (self cDigitOf: pDiv at: i - 1).
self cDigitOf: pRem at: l - 1 put: (a bitAnd: 16rFFFFFFFF).
l := l + 1]].
self cDigitOf: pQuo at: quoLen - k put: (self cCoerceSimple: q to: #'unsigned int')].
^0
]
{ #category : #'C core util' }
LargeIntegersPlugin >> cDigitHighBit: pUint32 len: len [
"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."
| realLength lastDigit |
<var: #pUint32 type: #'unsigned int *'>
<var: #lastDigit type: #'unsigned int'>
realLength := len.
[realLength = 0 ifTrue: [^0].
(lastDigit := self cDigitOf: pUint32 at: (realLength := realLength - 1)) = 0]
whileTrue.
^ (self cHighBit32: lastDigit) + (32 * realLength)
]
{ #category : #'C core' }
LargeIntegersPlugin >> cDigitLshift: shiftCount from: pFrom len: lenFrom to: pTo len: lenTo [
"C indexed!"
| digitShift bitShift carry limit digit rshift |
<var: #pTo type: #'unsigned int *'>
<var: #pFrom type: #'unsigned int *'>
<var: #carry type: #'unsigned int'>
<var: #digit type: #'unsigned int'>
digitShift := shiftCount // 32.
bitShift := shiftCount \\ 32.
limit := digitShift - 1.
"Note: 0 is endian neutral, use direct access"
0 to: limit do: [:i | pTo at: i put: 0].
bitShift = 0 ifTrue: ["Fast version for digit-aligned shifts"
"C indexed!"
^ self
cDigitReplace: pTo
from: digitShift
to: lenTo - 1
with: pFrom
startingAt: 0].
"This implementation use at most 31 bits of carry.
bitAnd: 16rFFFFFFFF is only for simulator, useless in C"
rshift := 32 - bitShift.
carry := 0.
limit := lenFrom - 1.
0 to: limit do:
[:i |
digit := self cDigitOf: pFrom at: i.
self cDigitOf: pTo at: i + digitShift put: ((carry bitOr: digit << bitShift) bitAnd: 16rFFFFFFFF).
carry := digit >> rshift].
carry = 0 ifFalse: [self cDigitOf: pTo at: lenTo - 1 put: carry].
^0
]
{ #category : #'C core' }
LargeIntegersPlugin >> cDigitMontgomery: pFirst
len: firstLen
times: pSecond
len: secondLen
modulo: pThird
len: thirdLen
mInvModB: mInv
into: pRes [
| u limit1 limit2 limit3 accum accum2 accum3 lastDigit |
<var: #pFirst type: #'unsigned int *'>
<var: #pSecond type: #'unsigned int *'>
<var: #pThird type: #'unsigned int *'>
<var: #mInv type: #'unsigned int'>
<var: #pRes type: #'unsigned int *'>
<var: #accum type: #'unsigned long long'>
<var: #accum2 type: #'unsigned long long'>
<var: #accum3 type: #'unsigned long long'>
<var: #u type: #'unsigned long long '>
<var: #lastDigit type: #'unsigned int '>
limit1 := firstLen - 1.
limit2 := secondLen - 1.
limit3 := thirdLen - 1.
lastDigit := 0.
0 to: limit1 do:
[:i |
accum3 := self cDigitOf: pFirst at: i.
accum3 := accum3*(self cDigitOf: pSecond at: 0) + (self cDigitOf: pRes at: 0).
u := accum3 * mInv bitAnd: 16rFFFFFFFF.
accum2 := u * (self cDigitOf: pThird at: 0).
accum := (accum2 bitAnd: 16rFFFFFFFF) + (accum3 bitAnd: 16rFFFFFFFF).
accum := (accum >> 32) + (accum2 >> 32) + (accum3 >> 32).
1 to: limit2 do: [:k |
accum3 := self cDigitOf: pFirst at: i.
accum3 := accum3*(self cDigitOf: pSecond at: k) + (self cDigitOf: pRes at: k).
accum2 := u * (self cDigitOf: pThird at: k).
accum := accum + (accum2 bitAnd: 16rFFFFFFFF) + (accum3 bitAnd: 16rFFFFFFFF).
self cDigitOf: pRes at: k-1 put: (accum bitAnd: 16rFFFFFFFF).
accum := (accum >> 32) + (accum2 >> 32) + (accum3 >> 32)].
secondLen to: limit3 do: [:k |
accum2 := u * (self cDigitOf: pThird at: k).
accum := accum + (self cDigitOf: pRes at: k) + (accum2 bitAnd: 16rFFFFFFFF).
self cDigitOf: pRes at: k-1 put: (accum bitAnd: 16rFFFFFFFF).
accum := (accum >> 32) + (accum2 >> 32)].
accum := accum + lastDigit.
self cDigitOf: pRes at: limit3 put: (accum bitAnd: 16rFFFFFFFF).
lastDigit := accum >> 32].
firstLen to: limit3 do:
[:i |
accum := (self cDigitOf: pRes at: 0).
u := accum * mInv bitAnd: 16rFFFFFFFF.
accum := accum + (u * (self cDigitOf: pThird at: 0)).
accum := accum >> 32.
1 to: limit3 do: [:k |
accum2 := u * (self cDigitOf: pThird at: k).
accum := accum + (self cDigitOf: pRes at: k) + (accum2 bitAnd: 16rFFFFFFFF).
self cDigitOf: pRes at: k-1 put: (accum bitAnd: 16rFFFFFFFF).
accum := (accum >> 32) + (accum2 >> 32)].
accum := accum + lastDigit.
self cDigitOf: pRes at: limit3 put: (accum bitAnd: 16rFFFFFFFF).
lastDigit := accum >> 32].
(lastDigit = 0 and: [(self cDigitCompare: pThird with: pRes len: thirdLen) = 1]) ifFalse: [
"self cDigitSub: pThird len: thirdLen with: pRes len: thirdLen into: pRes"
accum := 0.
0 to: limit3 do:
[:i |
accum := accum + (self cDigitOf: pRes at: i) - (self cDigitOf: pThird at: i).
self cDigitOf: pRes at: i put: (accum bitAnd: 16rFFFFFFFF).
accum := 0 - (accum >> 63)]].
^0
]
{ #category : #'C core' }
LargeIntegersPlugin >> cDigitMultiply: pWordShort len: shortLen with: pWordLong len: longLen into: pWordRes len: resLen [
| limitLong digit k carry limitShort ab |
<returnTypeC: #'unsigned int'>
<var: #pWordShort type: #'unsigned int *'>
<var: #pWordLong type: #'unsigned int *'>
<var: #pWordRes type: #'unsigned int *'>
<var: #digit type: #'unsigned int'>
<var: #carry type: #'unsigned int'>
<var: #ab type: #'unsigned long long'>
(shortLen = 1 and: [(pWordShort at: 0) = 0])
ifTrue: [^ 0].
(longLen = 1 and: [(pWordLong at: 0) = 0])
ifTrue: [^ 0].
"prod starts out all zero"
limitShort := shortLen - 1.
limitLong := longLen - 1.
0 to: limitShort do: [:i | (digit := self cDigitOf: pWordShort at: i) ~= 0
ifTrue:
[k := i.
carry := 0.
"Loop invariant: 0<=carry<=16rFFFFFFFF, k=i+j-1 (ST)"
"-> Loop invariant: 0<=carry<=16rFFFFFFFF, k=i+j (C) (?)"
0 to: limitLong do:
[:j |
ab := (self cDigitOf: pWordLong at: j).
ab := ab * digit + carry + (self cDigitOf: pWordRes at: k).
carry := ab >> 32.
self cDigitOf: pWordRes at: k put: (ab bitAnd: 16rFFFFFFFF).
k := k + 1].
k < resLen ifTrue: [self cDigitOf: pWordRes at: k put: carry]]].
^ 0
]
{ #category : #'C core util' }
LargeIntegersPlugin >> cDigitOf: cPointer at: zeroBasedDigitIndex [
<inline: true>
<returnTypeC: #'unsigned int'>
<var: 'cPointer' type: #'unsigned int *'>
^self byteSwapped32IfBigEndian: ((self cCode: [cPointer] inSmalltalk: [interpreterProxy cCoerce: cPointer to: #'unsigned int *']) at: zeroBasedDigitIndex)
]
{ #category : #'C core util' }
LargeIntegersPlugin >> cDigitOf: cPointer at: zeroBasedDigitIndex put: aValue [
<inline: true>
<returnTypeC: #'unsigned int'>
<var: 'cPointer' type: #'unsigned int *'>
<var: 'aValue' type: #'unsigned int'>
^(self cCode: [cPointer] inSmalltalk: [interpreterProxy cCoerce: cPointer to: #'unsigned int *'])
at: zeroBasedDigitIndex
put: (self byteSwapped32IfBigEndian: aValue)
]
{ #category : #'C core' }
LargeIntegersPlugin >> cDigitOp: opIndex short: pWordShort len: shortLen long: pWordLong len: longLen into: pWordRes [
"pWordRes len = longLen.
NOTE: we don't bother with endianness here, those bit opes are endian-neutral"
| limit |
<var: #pWordShort type: #'unsigned int *'>
<var: #pWordLong type: #'unsigned int *'>
<var: #pWordRes type: #'unsigned int *'>
limit := shortLen - 1.
opIndex = andOpIndex
ifTrue:
[0 to: limit do: [:i | pWordRes at: i put: ((pWordShort at: i)
bitAnd: (pWordLong at: i))].
limit := longLen - 1.
shortLen to: limit do: [:i | pWordRes at: i put: 0].
^ 0].
opIndex = orOpIndex
ifTrue:
[0 to: limit do: [:i | pWordRes at: i put: ((pWordShort at: i)
bitOr: (pWordLong at: i))].
limit := longLen - 1.
shortLen to: limit do: [:i | pWordRes at: i put: (pWordLong at: i)].
^ 0].
opIndex = xorOpIndex
ifTrue:
[0 to: limit do: [:i | pWordRes at: i put: ((pWordShort at: i)
bitXor: (pWordLong at: i))].
limit := longLen - 1.
shortLen to: limit do: [:i | pWordRes at: i put: (pWordLong at: i)].
^ 0].
^ interpreterProxy primitiveFail
]
{ #category : #'C core util' }
LargeIntegersPlugin >> cDigitReplace: pTo from: start to: stop with: pFrom startingAt: repStart [
"C indexed!"
<var: #pTo type: #'unsigned int *'>
<var: #pFrom type: #'unsigned int *'>
^ self
cDigitCopyFrom: pFrom + repStart
to: pTo + start
len: stop - start + 1
]
{ #category : #'C core' }
LargeIntegersPlugin >> cDigitRshift: shiftCount from: pFrom len: lenFrom to: pTo len: lenTo [
| carry digit digitShift bitShift leftShift limit start |
<var: #pTo type: #'unsigned int *'>
<var: #pFrom type: #'unsigned int *'>
<var: #carry type: #'unsigned int'>
<var: #digit type: #'unsigned int'>
digitShift := shiftCount // 32.
bitShift := shiftCount \\ 32.
bitShift = 0 ifTrue: ["Fast version for digit-aligned shifts"
"C indexed!"
^self
cDigitReplace: pTo
from: 0
to: lenTo - 1
with: pFrom
startingAt: digitShift].
"This implementation use at most 31 bits of carry.
bitAnd: 16rFFFFFFFF is only for simulator, useless in C"
leftShift := 32 - bitShift.
carry := (self cDigitOf: pFrom at: digitShift) >> bitShift.
start := digitShift + 1.
limit := lenFrom - 1.
start to: limit do:
[:j |
digit := self cDigitOf: pFrom at: j.
self cDigitOf: pTo at: j - start put: ((carry bitOr: digit << leftShift) bitAnd: 16rFFFFFFFF).
carry := digit >> bitShift].
carry = 0 ifFalse: [self cDigitOf: pTo at: lenTo - 1 put: carry].
^0
]
{ #category : #'C core' }
LargeIntegersPlugin >> cDigitSub: pWordSmall
len: smallLen
with: pWordLarge
len: largeLen
into: pWordRes [
| z |
<var: #pWordSmall type: #'unsigned int *'>
<var: #pWordLarge type: #'unsigned int *'>
<var: #pWordRes type: #'unsigned int *'>
<var: #z type: #'unsigned long long'>
z := 0.
0 to: smallLen - 1 do:
[:i |
z := z + (self cDigitOf: pWordLarge at: i) - (self cDigitOf: pWordSmall at: i).
self cDigitOf: pWordRes at: i put: (z bitAnd: 16rFFFFFFFF).
z := 0 - (self cCode: [z >> 63] inSmalltalk: [z >> 63 bitAnd: 1])].
smallLen to: largeLen - 1 do:
[:i |
z := z + (self cDigitOf: pWordLarge at: i) .
self cDigitOf: pWordRes at: i put: (z bitAnd: 16rFFFFFFFF).
z := 0 - (self cCode: [z >> 63] inSmalltalk: [z >> 63 bitAnd: 1])].
^0
]
{ #category : #'C core util' }
LargeIntegersPlugin >> cHighBit32: anUnsignedInt32 [
"Answer the index of the high order bit of the argument, or zero if the
argument is zero."
| shifted bitNo |
<var: #anUnsignedInt32 type: #'unsigned int'>
<var: #shifted type: #'unsigned int'>
shifted := anUnsignedInt32.
bitNo := 0.
shifted < (1 << 16)
ifFalse: [shifted := shifted >> 16.
bitNo := bitNo + 16].
shifted < (1 << 8)
ifFalse: [shifted := shifted >> 8.
bitNo := bitNo + 8].
shifted < (1 << 4)
ifFalse: [shifted := shifted >> 4.
bitNo := bitNo + 4].
shifted < (1 << 2)
ifFalse: [shifted := shifted >> 2.
bitNo := bitNo + 2].
shifted < (1 << 1)
ifFalse: [shifted := shifted >> 1.
bitNo := bitNo + 1].
"shifted 0 or 1 now"
^ bitNo + shifted
]
{ #category : #'oop util' }
LargeIntegersPlugin >> createLargeFromSmallInteger: anOop [
"anOop has to be a SmallInteger!"
| val res pDigit byteSize digitSize |
<var: #pDigit type: #'unsigned int *'>
val := interpreterProxy integerValueOf: anOop.
byteSize := self byteSizeOfCSI: val.
res := self createLargeIntegerNeg: val < 0 byteLength: byteSize.
pDigit := self pointerToFirstDigitOfLargeInt: res.
digitSize := byteSize + 3 // 4.
1 to: digitSize do: [:ix | self cDigitOf: pDigit at: ix - 1 put: (self digitOfCSI: val at: ix)].
^ res
]
{ #category : #util }
LargeIntegersPlugin >> createLargeIntegerNeg: neg byteLength: byteLength [
<inline: #always>
^interpreterProxy
instantiateClass: (neg
ifTrue: [interpreterProxy classLargeNegativeInteger]
ifFalse: [interpreterProxy classLargePositiveInteger])
indexableSize: byteLength
]
{ #category : #util }
LargeIntegersPlugin >> createLargeIntegerNeg: neg digitLength: digitLength [
<inline: #always>
^self createLargeIntegerNeg: neg byteLength: digitLength * 4
]
{ #category : #'oop functions' }
LargeIntegersPlugin >> digit: anOop Lshift: shiftCount [
"Attention: this method invalidates all oop's! Only newOop is valid at return."
"Does not normalize."
| newOop highBit newDigitLen newByteLen oldDigitLen |
oldDigitLen := self digitSizeOfLargeInt: anOop.
(highBit := self cDigitHighBit: (self pointerToFirstDigitOfLargeInt: anOop)
len: oldDigitLen) = 0 ifTrue: [^ interpreterProxy instantiateClass: (interpreterProxy fetchClassOf: anOop) indexableSize: 1].
newByteLen := highBit + shiftCount + 7 // 8.
self remapOop: anOop in:
[newOop := interpreterProxy
instantiateClass: (interpreterProxy fetchClassOf: anOop)
indexableSize: newByteLen].
newOop ifNil:
[interpreterProxy primitiveFailFor: PrimErrNoMemory.
^nil].
newDigitLen := newByteLen + 3 // 4.
self
cDigitLshift: shiftCount
from: (self pointerToFirstDigitOfLargeInt: anOop)
len: oldDigitLen
to: (self pointerToFirstDigitOfLargeInt: newOop)
len: newDigitLen.
^ newOop
]
{ #category : #'oop functions' }
LargeIntegersPlugin >> digit: anOop Rshift: shiftCount lookfirst: a [
"Attention: this method invalidates all oop's! Only newBytes is valid at return."
"Shift right 32*digitShift+bitShift bits, 0<=bitShift<32.
Discard all digits beyond a, and all zeroes at or below a."
"Does not normalize."
| newByteLen newOop oldBitLen newBitLen oldDigitLen newDigitLen |
oldBitLen := self cDigitHighBit: (self pointerToFirstDigitOfLargeInt: anOop) len: a.
oldDigitLen := oldBitLen + 31 // 32.
newBitLen := oldBitLen - shiftCount.
newBitLen <= 0 ifTrue: ["All bits lost"
^ interpreterProxy
instantiateClass: (interpreterProxy fetchClassOf: anOop)
indexableSize: 0].
newByteLen := newBitLen + 7 // 8.
newDigitLen := newByteLen + 3 // 4.
self remapOop: anOop in:
[newOop := interpreterProxy
instantiateClass: (interpreterProxy fetchClassOf: anOop)
indexableSize: newByteLen].
newOop ifNil: [^interpreterProxy primitiveFailFor: PrimErrNoMemory].
self
cDigitRshift: shiftCount
from: (self pointerToFirstDigitOfLargeInt: anOop)
len: oldDigitLen
to: (self pointerToFirstDigitOfLargeInt: newOop)
len: newDigitLen.
^ newOop
]
{ #category : #'oop functions' }
LargeIntegersPlugin >> digitAddLarge: firstInteger with: secondInteger [
"Does not need to normalize!"
| over firstDigitLen secondDigitLen shortInt shortDigitLen longInt longDigitLen sum newSum neg |
<var: #over type: #'unsigned int'>
firstDigitLen := self digitSizeOfLargeInt: firstInteger.
secondDigitLen := self digitSizeOfLargeInt: secondInteger.
neg := interpreterProxy isLargeNegativeIntegerObject: firstInteger.
firstDigitLen <= secondDigitLen
ifTrue:
[shortInt := firstInteger.
shortDigitLen := firstDigitLen.
longInt := secondInteger.
longDigitLen := secondDigitLen]
ifFalse:
[shortInt := secondInteger.
shortDigitLen := secondDigitLen.
longInt := firstInteger.
longDigitLen := firstDigitLen].
" sum := Integer new: len neg: firstInteger negative."
self remapOop: #(shortInt longInt ) in: [sum := self createLargeIntegerNeg: neg digitLength: longDigitLen].
sum ifNil: [^interpreterProxy primitiveFailFor: PrimErrNoMemory].
over := self
cDigitAdd: (self pointerToFirstDigitOfLargeInt: shortInt)
len: shortDigitLen
with: (self pointerToFirstDigitOfLargeInt: longInt)
len: longDigitLen
into: (self pointerToFirstDigitOfLargeInt: sum).
over > 0
ifTrue:
["sum := sum growby: 1."
self remapOop: sum in: [newSum := self createLargeIntegerNeg: neg byteLength: longDigitLen * 4 + 1].
newSum ifNil: [^interpreterProxy primitiveFailFor: PrimErrNoMemory].
self
cDigitCopyFrom: (self pointerToFirstDigitOfLargeInt: sum)
to: (self pointerToFirstDigitOfLargeInt: newSum)
len: longDigitLen.
sum := newSum.
"C index!"
self cDigitOf: (self pointerToFirstDigitOfLargeInt: sum)
at: longDigitLen put: over]
ifFalse:
[sum := neg
ifTrue: [self normalizeNegative: sum]
ifFalse: [self normalizePositive: sum]].
^ sum
]
{ #category : #'oop functions' }
LargeIntegersPlugin >> digitBitLogic: firstInteger with: secondInteger opIndex: opIx [
"Bit logic here is only implemented for positive integers or Zero;
if rec or arg is negative, it fails."
| firstLarge secondLarge firstLen secondLen shortLen shortLarge longLen longLarge result |
(interpreterProxy isIntegerObject: firstInteger)
ifTrue:
[(interpreterProxy integerValueOf: firstInteger) < 0 ifTrue:
[^ interpreterProxy primitiveFail].
"convert it to a not normalized LargeInteger"
self remapOop: secondInteger in: [firstLarge := self createLargeFromSmallInteger: firstInteger]]
ifFalse:
[(interpreterProxy isLargePositiveIntegerObject: firstInteger) ifFalse: [^ interpreterProxy primitiveFail].
firstLarge := firstInteger].
(interpreterProxy isIntegerObject: secondInteger)
ifTrue:
[(interpreterProxy integerValueOf: secondInteger) < 0 ifTrue:
[^ interpreterProxy primitiveFail].
"convert it to a not normalized LargeInteger"
self remapOop: firstLarge in: [secondLarge := self createLargeFromSmallInteger: secondInteger]]
ifFalse:
[(interpreterProxy isLargePositiveIntegerObject: secondInteger) ifFalse: [^ interpreterProxy primitiveFail].
secondLarge := secondInteger].
firstLen := self byteSizeOfLargeInt: firstLarge.
secondLen := self byteSizeOfLargeInt: secondLarge.
firstLen < secondLen
ifTrue:
[shortLen := firstLen.
shortLarge := firstLarge.
longLen := secondLen.
longLarge := secondLarge]
ifFalse:
[shortLen := secondLen.
shortLarge := secondLarge.
longLen := firstLen.
longLarge := firstLarge].
self remapOop: #(shortLarge longLarge) in:
[result := interpreterProxy instantiateClass: interpreterProxy classLargePositiveInteger indexableSize: longLen].
result ifNil: [^interpreterProxy primitiveFailFor: PrimErrNoMemory].
self
cDigitOp: opIx
short: (self pointerToFirstDigitOfLargeInt: shortLarge)
len: shortLen + 3 // 4
long: (self pointerToFirstDigitOfLargeInt: longLarge)
len: longLen + 3 // 4
into: (self pointerToFirstDigitOfLargeInt: result).
interpreterProxy failed ifTrue: [^ 0].
^self normalizePositive: result
]
{ #category : #'oop functions' }
LargeIntegersPlugin >> digitCompareLarge: firstInteger with: secondInteger [
"Compare the magnitude of firstInteger with that of secondInteger.
Return a code of 1, 0, -1 for firstInteger >, = , < secondInteger"
| firstDigitLen secondDigitLen |
firstDigitLen := self digitSizeOfLargeInt: firstInteger.
secondDigitLen := self digitSizeOfLargeInt: secondInteger.
secondDigitLen ~= firstDigitLen
ifTrue: [secondDigitLen > firstDigitLen
ifTrue: [^ -1 asOop: SmallInteger]
ifFalse: [^ 1 asOop: SmallInteger]].
^ (self
cDigitCompare: (self pointerToFirstDigitOfLargeInt: firstInteger)
with: (self pointerToFirstDigitOfLargeInt: secondInteger)
len: firstDigitLen)
asOop: SmallInteger
]
{ #category : #'oop functions' }
LargeIntegersPlugin >> digitDivLarge: firstInteger with: secondInteger negative: neg [
"Does not normalize."
"Division by zero has to be checked in caller."
| firstDigitLen secondDigitLen quoDigitLen d div rem quo result |
firstDigitLen := self digitSizeOfLargeInt: firstInteger.
secondDigitLen := self digitSizeOfLargeInt: secondInteger.
quoDigitLen := firstDigitLen - secondDigitLen + 1.
quoDigitLen <= 0
ifTrue:
[self remapOop: firstInteger in: [result := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 2].
result ifNotNil:
[interpreterProxy stObject: result at: 1 put: (0 asOop: SmallInteger).
interpreterProxy stObject: result at: 2 put: firstInteger].
^ result].
"set rem and div to copies of firstInteger and secondInteger, respectively.
However, to facilitate use of Knuth's algorithm, multiply rem and div by 2
(that is, shift) until the high word of div is >=16r80000000"
d := 32 - (self cHighBit32: (self unsafeDigitOfLargeInt: secondInteger at: secondDigitLen)).
self remapOop: firstInteger
in: [div := self digit: secondInteger Lshift: d.
div ifNotNil:
[div := self largeInt: div growTo: (self digitSizeOfLargeInt: div) + 1 * 4].
div ifNil: [^div]].
self remapOop: div
in: [rem := self digit: firstInteger Lshift: d.
rem ifNotNil:
[(self digitSizeOfLargeInt: rem) = firstDigitLen ifTrue:
[rem := self largeInt: rem growTo: firstDigitLen + 1 * 4]].
rem ifNil: [^rem]].
self remapOop: #(div rem)
in: [quo := self createLargeIntegerNeg: neg digitLength: quoDigitLen].
self
cDigitDiv: (self pointerToFirstDigitOfLargeInt: div)
len: (self digitSizeOfLargeInt: div)
rem: (self pointerToFirstDigitOfLargeInt: rem)
len: (self digitSizeOfLargeInt: rem)
quo: (self pointerToFirstDigitOfLargeInt: quo)
len: (self digitSizeOfLargeInt: quo).
self remapOop: quo
in: [rem := self digit: rem Rshift: d lookfirst: (self digitSizeOfLargeInt: div) - 1].
"^ Array with: quo with: rem"
self remapOop: #(quo rem)
in: [result := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 2].
result ifNotNil:
[interpreterProxy stObject: result at: 1 put: quo.
interpreterProxy stObject: result at: 2 put: rem].
^result
]
{ #category : #'oop functions' }
LargeIntegersPlugin >> digitMontgomery: firstLarge times: secondLarge modulo: thirdLarge mInvModB: mInv [
<var: #mInv type: #'unsigned int'>
| firstLen secondLen thirdLen prod |
firstLen := self digitSizeOfLargeInt: firstLarge.
secondLen := self digitSizeOfLargeInt: secondLarge.
thirdLen := self digitSizeOfLargeInt: thirdLarge.
(firstLen <= thirdLen and: [secondLen <= thirdLen]) ifFalse: [^interpreterProxy primitiveFail].
self remapOop: #(firstLarge secondLarge thirdLarge)
in: [prod := interpreterProxy instantiateClass: interpreterProxy classLargePositiveInteger indexableSize: thirdLen * 4].
prod ifNil: [^interpreterProxy primitiveFailFor: PrimErrNoMemory].
self
cDigitMontgomery: (self pointerToFirstDigitOfLargeInt: firstLarge)
len: firstLen
times: (self pointerToFirstDigitOfLargeInt: secondLarge)
len: secondLen
modulo: (self pointerToFirstDigitOfLargeInt: thirdLarge)
len: thirdLen
mInvModB: mInv
into: (self pointerToFirstDigitOfLargeInt: prod).
^self normalizePositive: prod
]
{ #category : #'oop functions' }
LargeIntegersPlugin >> digitMultiplyLarge: firstInteger with: secondInteger negative: neg [
"Normalizes."
| firstLen secondLen shortInt shortLen longInt longLen prod |
firstLen := self byteSizeOfLargeInt: firstInteger.
secondLen := self byteSizeOfLargeInt: secondInteger.
firstLen <= secondLen
ifTrue:
[shortInt := firstInteger.
shortLen := firstLen.
longInt := secondInteger.
longLen := secondLen]
ifFalse:
[shortInt := secondInteger.
shortLen := secondLen.
longInt := firstInteger.
longLen := firstLen].
self remapOop: #(shortInt longInt) in: [prod := self createLargeIntegerNeg: neg byteLength: longLen + shortLen].
prod ifNil: [^interpreterProxy primitiveFailFor: PrimErrNoMemory].
self
cDigitMultiply: (self pointerToFirstDigitOfLargeInt: shortInt)
len: shortLen + 3 // 4
with: (self pointerToFirstDigitOfLargeInt: longInt)
len: longLen + 3 // 4
into: (self pointerToFirstDigitOfLargeInt: prod)
len: longLen + shortLen + 3 // 4.
^neg
ifTrue: [self normalizeNegative: prod]
ifFalse: [self normalizePositive: prod]
]
{ #category : #util }
LargeIntegersPlugin >> digitOfCSI: csi at: ix [
"Answer the value of a 32 bits digit in a C-SmallInteger."
"ST indexed!"
^(csi < 0
ifTrue: [0 - csi]
ifFalse: [csi]) >> (ix - 1 * 32)