--- a/Integer.st Wed Jan 27 22:25:24 2016 +0100
+++ b/Integer.st Wed Jan 27 23:21:53 2016 +0100
@@ -5145,21 +5145,30 @@
!Integer::ModuloNumber methodsFor:'arithmetic'!
-modulusOf:aNumber
+modulusOf:dividend
"compute the aNumber modulo myself.
The shortcut works only, if aNumber is < modulo * modulo
(When doing arithmethic modulo something).
Otherwise do it the long way"
- |e t cnt|
+ |e t cnt abs isNegative|
+
+ isNegative := dividend negative.
+ isNegative ifTrue:[
+ abs := dividend negated.
+ ] ifFalse:[
+ abs := dividend.
+ ].
+
+"/ self assert:aNumber < (modulus * modulus)
"throw off low nbits(modulus)"
- e := aNumber bitShift:shift.
+ e := abs bitShift:shift.
e := e * reciprocal.
e := e bitShift:shift.
e := e * modulus.
- e := aNumber - e.
+ e := abs - e.
"this subtract is done max 2 times"
cnt := 2.
@@ -5171,6 +5180,9 @@
].
cnt := cnt - 1.
].
+ isNegative ifTrue:[
+ ^ modulus - e.
+ ].
^ e.
"