Integer.st
changeset 14680 eb312d015dd6
parent 14677 9f2ca6ba577f
child 14767 157f37c1a7f9
child 18017 7fef9e17913f
--- a/Integer.st	Tue Jan 22 14:08:16 2013 +0100
+++ b/Integer.st	Tue Jan 22 15:10:37 2013 +0100
@@ -4111,7 +4111,8 @@
 !
 
 nextMultipleOf: n
-    "return the multiple of n at or above the receiver"
+    "return the multiple of n at or above the receiver.
+     Useful for padding."
 
     |rest|
 
@@ -4129,6 +4130,44 @@
     "
 !
 
+nextPowerOf2
+    "return the power of 2 at or above the receiver.
+     Useful for padding."
+
+    |x t sh|
+
+    x := self - 1.
+    x := x bitOr: (x bitShift: -1).
+    x := x bitOr: (x bitShift: -2).
+    x := x bitOr: (x bitShift: -4).
+    x := x bitOr: (x bitShift: -8).
+    x := x bitOr: (t := x bitShift: -16).
+    t == 0 ifFalse:[
+        sh := -32.
+        [
+            x := x bitOr: (t := x bitShift: sh).
+            sh := sh + sh. 
+        ] doWhile: [t ~~ 0]
+    ].
+    ^ x + 1 
+
+    "
+     1 nextPowerOf2    
+     2 nextPowerOf2    
+     3 nextPowerOf2    
+     4 nextPowerOf2    
+     5 nextPowerOf2    
+     6 nextPowerOf2    
+     7 nextPowerOf2    
+     8 nextPowerOf2    
+
+     22 nextPowerOf2
+     12 factorial nextPowerOf2  isPowerOf:2  
+     100 factorial nextPowerOf2  isPowerOf:2  
+     1000 factorial nextPowerOf2  isPowerOf:2  
+    "
+!
+
 nextPrime
     "return the next prime after the receiver"
 
@@ -4879,11 +4918,11 @@
 !Integer class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.272 2013-01-22 10:11:10 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.273 2013-01-22 14:10:37 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.272 2013-01-22 10:11:10 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.273 2013-01-22 14:10:37 cg Exp $'
 ! !