using System.Diagnostics;
using System.Runtime.CompilerServices;
+using System.Runtime.Intrinsics.X86;
namespace System.Buffers.Text
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int CountHexDigits(ulong value)
{
- // TODO: When x86 intrinsic support comes online, experiment with implementing this using lzcnt.
- // return 16 - (int)((uint)Lzcnt.LeadingZeroCount(value | 1) >> 3);
+ if (Lzcnt.IsSupported && IntPtr.Size == 8)
+ {
+ int right = 64 - (int)Lzcnt.LeadingZeroCount(value | 1);
+ return (right + 3) >> 2;
+ }
int digits = 1;
using System.Diagnostics;
using System.Runtime.CompilerServices;
+using System.Runtime.Intrinsics.X86;
namespace System.Buffers
{
internal static int SelectBucketIndex(int bufferSize)
{
Debug.Assert(bufferSize >= 0);
+ if (Lzcnt.IsSupported)
+ {
+ uint bits = ((uint)bufferSize - 1) >> 4;
+ return 32 - (int)Lzcnt.LeadingZeroCount(bits);
+ }
// bufferSize of 0 will underflow here, causing a huge
// index which the caller will discard because it is not