--- a/LargeInteger.st Tue Nov 17 14:32:08 2015 +0100
+++ b/LargeInteger.st Tue Nov 17 20:53:56 2015 +0100
@@ -4499,8 +4499,8 @@
|nr r1 r2|
nr := (3 raisedTo:exp) asInteger.
Transcript show:exp; show:' nbytes: '; showCR:nr digitLength;
- show:' normal: '; showCR:(Time microsecondsToRun:[ UseKarazuba := false. r1 := nr * nr ]);
- show:' karazuba: '; showCR:(Time microsecondsToRun:[ UseKarazuba := true. r2 := nr absMulKarazuba: nr ]).
+ show:' normal: '; show:(Time microsecondsToRun:[ UseKarazuba := false. r1 := nr * nr ]); showCR:'us';
+ show:' karazuba: '; show:(Time microsecondsToRun:[ UseKarazuba := true. r2 := nr absMulKarazuba: nr]); showCR:'us'.
self assert:(r1 = r2).
]
"
@@ -5508,11 +5508,50 @@
^ self setSign:aNumber
! !
+!LargeInteger methodsFor:'queries'!
+
+nextPowerOf2
+ "return the power of 2 at or above the receiver.
+ Notice, that for a powerOf2, the receiver is returned."
+
+ |newBytes nBytes hi|
+
+ "/ assume I am normalized
+ nBytes := digitByteArray size.
+ hi := digitByteArray at:nBytes.
+ self assert:(hi ~~ 0).
+
+ newBytes := ByteArray new:(nBytes) withAll:16rFF.
+ (hi isPowerOf:2) ifTrue:[
+ newBytes at:nBytes put:((hi bitShift:1)- 1).
+ ] ifFalse:[
+ newBytes at:nBytes put:(hi nextPowerOf2 - 1).
+ ].
+ ^ (LargeInteger digitBytes:newBytes) + 1
+
+ "
+ 100 factorial nextPowerOf2 isPowerOf:2
+ 1000 factorial nextPowerOf2 isPowerOf:2
+ Time millisecondsToRun:[
+ |v|
+ v := 1000 factorial.
+ 1000 timesRepeat:[
+ v nextPowerOf2
+ ]
+ ]
+ "
+! !
+
!LargeInteger methodsFor:'testing'!
even
"return true if the receiver is even"
+%{
+#ifdef __SCHTEAM__
+ return context._RETURN( ((STLargeInteger)self).isEven() ? STObject.True : STObject.False);
+#endif
+%}.
^ (digitByteArray at:1) even
!
@@ -5530,6 +5569,11 @@
odd
"return true if the receiver is odd"
+%{
+#ifdef __SCHTEAM__
+ return context._RETURN( ((STLargeInteger)self).isEven() ? STObject.False : STObject.True);
+#endif
+%}.
^ (digitByteArray at:1) odd
"Modified: 18.11.1996 / 22:19:19 / cg"