#REFACTORING
class: Integer
changed:
#integerReciprocal
#primesUpTo:
#raisedToCrtModP:q:ep:eq:u:
optimizations
--- a/Integer.st Tue Feb 16 08:00:37 2016 +0100
+++ b/Integer.st Tue Feb 16 09:45:47 2016 +0100
@@ -751,6 +751,8 @@
"Modified: / 15.11.1999 / 20:35:20 / cg"
! !
+
+
!Integer class methodsFor:'class initialization'!
initialize
@@ -796,6 +798,7 @@
"Modified: 18.7.1996 / 12:26:38 / cg"
! !
+
!Integer class methodsFor:'prime numbers'!
flushPrimeCache
@@ -1007,7 +1010,15 @@
primesUpTo: max
"Return a list of prime integers up to and including the given integer."
- ^ Array streamContents:[:s| self primesUpTo: max do:[:prime| s nextPut: prime]]
+ |cls|
+
+ max < IntegerArray maxVal ifTrue:[
+ cls := IntegerArray.
+ ] ifFalse:[
+ cls := Array.
+ ].
+
+ ^ cls streamContents:[:s| self primesUpTo: max do:[:prime| s nextPut: prime]]
"
Integer primesUpTo: 100
@@ -1163,6 +1174,7 @@
^ self == Integer
! !
+
!Integer methodsFor:'*Roe'!
acceptRoeVisitor: aVisitor
@@ -1433,6 +1445,7 @@
"
! !
+
!Integer methodsFor:'bcd conversion'!
decodeFromBCD
@@ -3516,19 +3529,18 @@
Where an integer is one bit longer than self.
This is a helper for modulu numbers"
- |b rem result|
+ |b "{Class: SmallInteger}" rem result digitBytes|
b := self highBit.
rem := 1 bitShift:b.
result := LargeInteger basicNew numberOfDigits:(b // 8)+1.
- b := b+1.
- [b > 0] whileTrue:[
+ digitBytes := result digitBytes.
+ b+1 to:1 by:-1 do:[:idx|
rem >= self ifTrue:[
- rem := rem -= self.
- result digitBytes bitSetAt:b.
+ rem := rem - self.
+ digitBytes bitSetAt:idx.
].
rem := rem mul2.
- b := b - 1.
].
^ result compressed.
@@ -3944,25 +3956,17 @@
"now p2 is in result, q2 in t"
- t := t -= result.
- t < 0 ifTrue:[
+ t := t - result.
+ t negative ifTrue:[
t := t + q.
].
- t := t *= u.
+ t := t * u.
t := mq modulusOf:t.
- t := t *= p.
- result := result += t.
+ t := t * p.
+ result := result + t.
^ result.
-
-
- "
- 2 raisedTo:2 mod:3
- 20000000000000 raisedTo:200 mod:190
- (20000000000000 raisedTo:200) \\ 190
- "
-
"Created: / 30.4.1999 / 15:53:15 / stefan"
"Modified: / 5.5.1999 / 11:01:15 / stefan"
! !
@@ -4804,6 +4808,7 @@
"Created: / 09-01-2012 / 17:18:06 / cg"
! !
+
!Integer methodsFor:'special modulo arithmetic'!
add_32:anInteger
@@ -5458,7 +5463,7 @@
modulusOf:dividend
"compute the aNumber modulo myself.
- The shortcut works only, if aNumber is < modulo * modulo
+ The shortcut works only, if dividend is < modulo * modulo
(When doing arithmethic modulo something).
Otherwise do it the long way"
@@ -5471,14 +5476,12 @@
abs := dividend.
].
-"/ self assert:aNumber < (modulus * modulus)
+"/ self assert:dividend < (modulus * modulus)
"throw off low nbits(modulus)"
- e := abs bitShift:shift.
- e := e * reciprocal.
- e := e bitShift:shift.
- e := e * modulus.
+ e := (abs bitShift:shift) * reciprocal.
+ e := (e bitShift:shift) * modulus.
e := abs - e.
"this subtract is done max 2 times"
@@ -5525,8 +5528,13 @@
|m|
m := self new modulus:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
- m modulusOf:874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
-10730930127807326146398409623772237722337234475792709784029183368622308259008044569184592041059181058049458041058052 ]
+ Time millisecondsToRun:[
+ 100000 timesRepeat:[
+ m modulusOf:874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658.
+ ]
+ ].
+
+10730930127807326146398409623772237722337234475792709784029183368622308259008044569184592041059181058049458041058052
"
"Modified: / 3.5.1999 / 14:30:32 / stefan"