summary refs log tree commit diff
path: root/crypto/src/util
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-03-01 17:20:21 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-03-01 17:20:21 +0700
commit4de18964f35fc169cb94058dcfe2e1ccb59b2524 (patch)
tree289c3b209782ce0af94f5bf0362a68b3e3764ec2 /crypto/src/util
parentBIKE init perf. opts. (diff)
downloadBouncyCastle.NET-ed25519-4de18964f35fc169cb94058dcfe2e1ccb59b2524.tar.xz
Add Integers.PopCount
Diffstat (limited to 'crypto/src/util')
-rw-r--r--crypto/src/util/Integers.cs21
1 files changed, 21 insertions, 0 deletions
diff --git a/crypto/src/util/Integers.cs b/crypto/src/util/Integers.cs
index eff7056b6..87ba9cbb0 100644
--- a/crypto/src/util/Integers.cs
+++ b/crypto/src/util/Integers.cs
@@ -79,6 +79,27 @@ namespace Org.BouncyCastle.Utilities
 #endif
         }
 
+        public static int PopCount(int i)
+        {
+            return PopCount((uint)i);
+        }
+
+        [CLSCompliant(false)]
+        public static int PopCount(uint u)
+        {
+#if NETCOREAPP3_0_OR_GREATER
+            return BitOperations.PopCount(u);
+#else
+            u -= (u >> 1) & 0x55555555;
+            u = (u & 0x33333333) + ((u >> 2) & 0x33333333);
+            u = (u + (u >> 4)) & 0x0f0f0f0f;
+            u += (u >> 8);
+            u += (u >> 16);
+            u &= 0x3f;
+            return (int)u;
+#endif
+        }
+
         public static int Reverse(int i)
         {
             return (int)Reverse((uint)i);