summary refs log tree commit diff
path: root/crypto/src/util/Integers.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-07-19 00:42:58 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-07-19 00:42:58 +0700
commitb966a4c7e871e8a1bdf50c29626ce40450c3db0c (patch)
treeec7ebe55384a5f92233977dc01e0c541a04af581 /crypto/src/util/Integers.cs
parentFactor out Unshuffle methods (diff)
downloadBouncyCastle.NET-ed25519-b966a4c7e871e8a1bdf50c29626ce40450c3db0c.tar.xz
Use intrinsics in several places
Diffstat (limited to 'crypto/src/util/Integers.cs')
-rw-r--r--crypto/src/util/Integers.cs17
1 files changed, 17 insertions, 0 deletions
diff --git a/crypto/src/util/Integers.cs b/crypto/src/util/Integers.cs
index 116250375..b32cdfc30 100644
--- a/crypto/src/util/Integers.cs
+++ b/crypto/src/util/Integers.cs
@@ -1,4 +1,7 @@
 using System;
+#if NET5_0_OR_GREATER
+using System.Runtime.Intrinsics.X86;
+#endif
 
 using Org.BouncyCastle.Math.Raw;
 
@@ -42,6 +45,13 @@ namespace Org.BouncyCastle.Utilities
 
         public static int NumberOfLeadingZeros(int i)
         {
+#if NET5_0_OR_GREATER
+            if (Lzcnt.IsSupported)
+            {
+                return (int)Lzcnt.LeadingZeroCount((uint)i);
+            }
+#endif
+
             if (i <= 0)
                 return (~i >> (31 - 5)) & (1 << 5);
 
@@ -57,6 +67,13 @@ namespace Org.BouncyCastle.Utilities
 
         public static int NumberOfTrailingZeros(int i)
         {
+#if NET5_0_OR_GREATER
+            if (Bmi1.IsSupported)
+            {
+                return (int)Bmi1.TrailingZeroCount((uint)i);
+            }
+#endif
+
             int n = DeBruijnTZ[(uint)((i & -i) * 0x0EF96A62) >> 27];
             int m = (((i & 0xFFFF) | (int)((uint)i >> 16)) - 1) >> 31;
             return n - m;