Integer.st
branchjv
changeset 19236 e6403ba50de1
parent 19167 699eef1bc815
parent 19231 89380e45244b
child 19277 e9182dc00c6d
--- a/Integer.st	Tue Feb 16 07:24:34 2016 +0000
+++ b/Integer.st	Wed Feb 17 07:03:44 2016 +0100
@@ -752,6 +752,8 @@
     "Modified: / 15.11.1999 / 20:35:20 / cg"
 ! !
 
+
+
 !Integer class methodsFor:'class initialization'!
 
 initialize
@@ -797,6 +799,7 @@
     "Modified: 18.7.1996 / 12:26:38 / cg"
 ! !
 
+
 !Integer class methodsFor:'prime numbers'!
 
 flushPrimeCache
@@ -1008,7 +1011,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
@@ -1164,6 +1175,7 @@
     ^ self == Integer
 ! !
 
+
 !Integer methodsFor:'*Roe'!
 
 acceptRoeVisitor: aVisitor
@@ -1434,6 +1446,7 @@
     "
 ! !
 
+
 !Integer methodsFor:'bcd conversion'!
 
 decodeFromBCD
@@ -3517,19 +3530,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.
 
@@ -3945,25 +3957,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"
 ! !
@@ -4806,6 +4810,7 @@
     "Created: / 09-01-2012 / 17:18:06 / cg"
 ! !
 
+
 !Integer methodsFor:'special modulo arithmetic'!
 
 add_32:anInteger
@@ -5460,7 +5465,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"
 
@@ -5473,14 +5478,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"
@@ -5527,8 +5530,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"