summary refs log tree commit diff
path: root/crypto/src/util/Longs.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/Longs.cs
parentFactor out Unshuffle methods (diff)
downloadBouncyCastle.NET-ed25519-b966a4c7e871e8a1bdf50c29626ce40450c3db0c.tar.xz
Use intrinsics in several places
Diffstat (limited to 'crypto/src/util/Longs.cs')
-rw-r--r--crypto/src/util/Longs.cs17
1 files changed, 17 insertions, 0 deletions
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;