Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/System.Private.CoreLib/shared/System/SpanHelpers.Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Numerics;
using System.Runtime.Intrinsics.X86;

using Internal.Runtime.CompilerServices;

Expand Down Expand Up @@ -1109,23 +1110,39 @@ private static int LocateLastFoundByte(Vector<byte> match)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int LocateFirstFoundByte(ulong match)
{
// Flag least significant power of two bit
var powerOfTwoFlag = match ^ (match - 1);
// Shift all powers of two into the high byte and extract
return (int)((powerOfTwoFlag * XorPowerOfTwoToHighByte) >> 57);
// TODO: Arm variants
if (Bmi1.X64.IsSupported)
{
return (int)(Bmi1.X64.TrailingZeroCount(match) >> 3);
}
else
{
// Flag least significant power of two bit
var powerOfTwoFlag = match ^ (match - 1);
// Shift all powers of two into the high byte and extract
return (int)((powerOfTwoFlag * XorPowerOfTwoToHighByte) >> 57);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int LocateLastFoundByte(ulong match)
{
// Find the most significant byte that has its highest bit set
int index = 7;
while ((long)match > 0)
// TODO: Arm variants
if (Lzcnt.X64.IsSupported)
{
match = match << 8;
index--;
return 7 - (int)(Lzcnt.X64.LeadingZeroCount(match) >> 3);
Copy link
Member

@am11 am11 Dec 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This yields a different result than the else case, e.g. LocateLastFoundByte((ulong)10) // when Lzcnt.X64.IsSupported is 0 and !Lzcnt.X64.IsSupported (else case) returns -1. Is it known/intentional?
Aside from this, the result of Lzcnt.X64.LeadingZeroCount((ulong)10) is 6010, shouldn't it be 5810?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 isn't a value that can be passed to the method. The bytes in match can only be 0x00 or 0xff

}
else
{
// Find the most significant byte that has its highest bit set
int index = 7;
while ((long)match > 0)
{
match = match << 8;
index--;
}
return index;
}
return index;
}

private const ulong XorPowerOfTwoToHighByte = (0x07ul |
Expand Down
41 changes: 28 additions & 13 deletions src/System.Private.CoreLib/shared/System/SpanHelpers.Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Numerics;
using System.Runtime.Intrinsics.X86;

#if !netstandard
using Internal.Runtime.CompilerServices;
#endif

#if BIT64
using nuint = System.UInt64;
Expand Down Expand Up @@ -822,12 +821,20 @@ private static int LocateFirstFoundChar(Vector<ushort> match)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int LocateFirstFoundChar(ulong match)
{
unchecked
// TODO: Arm variants
if (Bmi1.X64.IsSupported)
{
return (int)(Bmi1.X64.TrailingZeroCount(match) >> 4);
}
else
{
// Flag least significant power of two bit
var powerOfTwoFlag = match ^ (match - 1);
// Shift all powers of two into the high byte and extract
return (int)((powerOfTwoFlag * XorPowerOfTwoToHighChar) >> 49);
unchecked
{
// Flag least significant power of two bit
var powerOfTwoFlag = match ^ (match - 1);
// Shift all powers of two into the high byte and extract
return (int)((powerOfTwoFlag * XorPowerOfTwoToHighChar) >> 49);
}
}
}

Expand Down Expand Up @@ -859,14 +866,22 @@ private static int LocateLastFoundChar(Vector<ushort> match)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int LocateLastFoundChar(ulong match)
{
// Find the most significant char that has its highest bit set
int index = 3;
while ((long)match > 0)
// TODO: Arm variants
if (Lzcnt.X64.IsSupported)
{
match = match << 16;
index--;
return 3 - (int)(Lzcnt.X64.LeadingZeroCount(match) >> 4);
}
else
{
// Find the most significant char that has its highest bit set
int index = 3;
while ((long)match > 0)
{
match = match << 16;
index--;
}
return index;
}
return index;
}
}
}