summary refs log tree commit diff
path: root/crypto/src/util
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
parentFactor out Unshuffle methods (diff)
downloadBouncyCastle.NET-ed25519-b966a4c7e871e8a1bdf50c29626ce40450c3db0c.tar.xz
Use intrinsics in several places
Diffstat (limited to 'crypto/src/util')
-rw-r--r--crypto/src/util/Integers.cs17
-rw-r--r--crypto/src/util/Longs.cs17
2 files changed, 34 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;
diff --git a/crypto/src/util/Longs.cs b/crypto/src/util/Longs.cs
index e6ff2ea39..ff45a8143 100644
--- a/crypto/src/util/Longs.cs
+++ b/crypto/src/util/Longs.cs
@@ -1,4 +1,7 @@
 using System;
+#if NET5_0_OR_GREATER
+using System.Runtime.Intrinsics.X86;
+#endif
 
 using Org.BouncyCastle.Math.Raw;
 
@@ -45,6 +48,13 @@ namespace Org.BouncyCastle.Utilities
 
         public static int NumberOfLeadingZeros(long i)
         {
+#if NET5_0_OR_GREATER
+            if (Lzcnt.X64.IsSupported)
+            {
+                return (int)Lzcnt.X64.LeadingZeroCount((ulong)i);
+            }
+#endif
+
             int x = (int)(i >> 32), n = 0;
             if (x == 0)
             {
@@ -56,6 +66,13 @@ namespace Org.BouncyCastle.Utilities
 
         public static int NumberOfTrailingZeros(long i)
         {
+#if NET5_0_OR_GREATER
+            if (Bmi1.X64.IsSupported)
+            {
+                return (int)Bmi1.X64.TrailingZeroCount((ulong)i);
+            }
+#endif
+
             int n = DeBruijnTZ[(uint)((ulong)((i & -i) * 0x045FBAC7992A70DAL) >> 58)];
             long m = (((i & 0xFFFFFFFFL) | (long)((ulong)i >> 32)) - 1L) >> 63;
             return n - (int)m;