From 21fc00af1c404d93280e21d05efc124ec23d575b Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 11 Apr 2013 06:49:24 +0200 Subject: IntBitfield: Add bit-count, O(1) per int-element, using HAKEM. --- src/java/com/jogamp/common/util/IntBitfield.java | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/java/com/jogamp/common/util/IntBitfield.java b/src/java/com/jogamp/common/util/IntBitfield.java index 6b277e0..ad27dff 100644 --- a/src/java/com/jogamp/common/util/IntBitfield.java +++ b/src/java/com/jogamp/common/util/IntBitfield.java @@ -136,4 +136,30 @@ public class IntBitfield { } return prev; } + /** + * Returns the number of set bits within given 32bit integer in O(1) + * using HAKEM Bit Count: + *
+     *   http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html
+     *   http://home.pipeline.com/~hbaker1/hakmem/hacks.html#item169
+     *   http://tekpool.wordpress.com/category/bit-count/
+     * 
+ */ + public static final int getBitCount(final int n) { + int c = n; + c -= (n >> 1) & 033333333333; + c -= (n >> 2) & 011111111111; + return ( (c + ( c >> 3 ) ) & 030707070707 ) % 63; + } + + /** + * Returns the number of set bits within this bitfield. + */ + public long getBitCount() { + long c = 0; + for(int i = storage.length-1; i>=0; i--) { + c += getBitCount(storage[i]); + } + return c; + } } -- cgit v1.2.3