lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [lucenenet] 02/07: Lucene.Net.Support.Number::BitCount(long): Replaced implementation with the one from Apache Harmony, which benchmarked 9.5x faster
Date Sun, 15 Dec 2019 11:44:51 GMT
This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit aa1da683bb48e6a483543c2a044d26d7371b78b0
Author: Shad Storhaug <shad@shadstorhaug.com>
AuthorDate: Wed Dec 4 19:43:50 2019 +0700

    Lucene.Net.Support.Number::BitCount(long): Replaced implementation with the one from Apache
Harmony, which benchmarked 9.5x faster
---
 src/Lucene.Net/Support/Number.cs | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/src/Lucene.Net/Support/Number.cs b/src/Lucene.Net/Support/Number.cs
index 9f572b8..73dafa8 100644
--- a/src/Lucene.Net/Support/Number.cs
+++ b/src/Lucene.Net/Support/Number.cs
@@ -325,15 +325,14 @@ namespace Lucene.Net.Support
         // Returns the number of 1-bits in the number
         public static int BitCount(long num)
         {
-            int bitcount = 0;
-            // To use the > 0 condition
-            ulong nonNegNum = (ulong) num;
-            while (nonNegNum > 0)
-            {
-                bitcount += (int)(nonNegNum & 0x1);
-                nonNegNum >>= 1;
-            }
-            return bitcount;
+            num = (num & 0x5555555555555555L) + ((num >> 1) & 0x5555555555555555L);
+            num = (num & 0x3333333333333333L) + ((num >> 2) & 0x3333333333333333L);
+            // adjust for 64-bit integer
+            int i = (int)(URShift(num, 32) + num);
+            i = (i & 0x0F0F0F0F) + ((i >> 4) & 0x0F0F0F0F);
+            i = (i & 0x00FF00FF) + ((i >> 8) & 0x00FF00FF);
+            i = (i & 0x0000FFFF) + ((i >> 16) & 0x0000FFFF);
+            return i;
         }
 
         public static int Signum(long a)


Mime
View raw message