Skip to content

Commit

Permalink
Avoid unnecessary sign extension broadcasting to vector
Browse files Browse the repository at this point in the history
References:
* dotnet#108820
  • Loading branch information
xtqqczze committed Oct 14, 2024
1 parent 6221ddb commit bde892c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/HexConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public static bool TryDecodeFromUtf16_Vector128(ReadOnlySpan<char> chars, Span<b
if (Ssse3.IsSupported)
{
output = Ssse3.MultiplyAddAdjacent(nibbles,
Vector128.Create((short)0x0110).AsSByte()).AsByte();
Vector128.Create((ushort)0x0110).AsSByte()).AsByte();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private static unsafe int IndexOfMinMaxCore<T, TIndexOfMinMax>(ReadOnlySpan<T> x
static Vector512<T> CreateVector512T(int i) =>
sizeof(T) == sizeof(long) ? Vector512.Create((long)i).As<long, T>() :
sizeof(T) == sizeof(int) ? Vector512.Create(i).As<int, T>() :
sizeof(T) == sizeof(short) ? Vector512.Create((short)i).As<short, T>() :
sizeof(T) == sizeof(short) ? Vector512.Create((ushort)i).As<ushort, T>() :
Vector512.Create((byte)i).As<byte, T>();

ref T xRef = ref MemoryMarshal.GetReference(x);
Expand Down Expand Up @@ -244,7 +244,7 @@ static Vector512<T> CreateVector512T(int i) =>
static Vector256<T> CreateVector256T(int i) =>
sizeof(T) == sizeof(long) ? Vector256.Create((long)i).As<long, T>() :
sizeof(T) == sizeof(int) ? Vector256.Create(i).As<int, T>() :
sizeof(T) == sizeof(short) ? Vector256.Create((short)i).As<short, T>() :
sizeof(T) == sizeof(short) ? Vector256.Create((ushort)i).As<ushort, T>() :
Vector256.Create((byte)i).As<byte, T>();

ref T xRef = ref MemoryMarshal.GetReference(x);
Expand Down Expand Up @@ -335,7 +335,7 @@ static Vector256<T> CreateVector256T(int i) =>
static Vector128<T> CreateVector128T(int i) =>
sizeof(T) == sizeof(long) ? Vector128.Create((long)i).As<long, T>() :
sizeof(T) == sizeof(int) ? Vector128.Create(i).As<int, T>() :
sizeof(T) == sizeof(short) ? Vector128.Create((short)i).As<short, T>() :
sizeof(T) == sizeof(short) ? Vector128.Create((ushort)i).As<ushort, T>() :
Vector128.Create((byte)i).As<byte, T>();

ref T xRef = ref MemoryMarshal.GetReference(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,8 @@ private static unsafe void Avx2Decode<TBase64Decoder, T>(TBase64Decoder decoder,
-1, -1, -1, -1,
-1, -1, -1, -1).AsInt32();

Vector256<sbyte> maskSlashOrUnderscore = Vector256.Create((sbyte)decoder.MaskSlashOrUnderscore);
Vector256<sbyte> shiftForUnderscore = Vector256.Create((sbyte)33);
Vector256<sbyte> maskSlashOrUnderscore = Vector256.Create(decoder.MaskSlashOrUnderscore).AsSByte();
Vector256<sbyte> shiftForUnderscore = Vector256.Create((byte)33).AsSByte();
Vector256<sbyte> mergeConstant0 = Vector256.Create(0x01400140).AsSByte();
Vector256<short> mergeConstant1 = Vector256.Create(0x00011000).AsInt16();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private static unsafe void Avx2Encode<TBase64Encoder, T>(TBase64Encoder encoder,
Vector256<ushort> shiftAC = Vector256.Create(0x04000040).AsUInt16();
Vector256<short> shiftBB = Vector256.Create(0x01000010).AsInt16();
Vector256<byte> const51 = Vector256.Create((byte)51);
Vector256<sbyte> const25 = Vector256.Create((sbyte)25);
Vector256<sbyte> const25 = Vector256.Create((byte)25).AsSByte();

byte* src = srcBytes;
T* dest = destBytes;
Expand Down Expand Up @@ -467,7 +467,7 @@ private static unsafe void Vector128Encode<TBase64Encoder, T>(TBase64Encoder enc
Vector128<ushort> shiftAC = Vector128.Create(0x04000040).AsUInt16();
Vector128<short> shiftBB = Vector128.Create(0x01000010).AsInt16();
Vector128<byte> const51 = Vector128.Create((byte)51);
Vector128<sbyte> const25 = Vector128.Create((sbyte)25);
Vector128<sbyte> const25 = Vector128.Create((byte)25).AsSByte();
Vector128<byte> mask8F = Vector128.Create((byte)0x8F);

byte* src = srcBytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ private static Vector128<byte> IndexOfAnyLookup<TNegator>(Vector128<byte> source

Vector128<byte> bitmask = Vector128.ShuffleUnsafe(Vector128.Create(0x8040201008040201).AsByte(), highNibbles);

Vector128<byte> mask = Vector128.GreaterThan(highNibbles.AsSByte(), Vector128.Create((sbyte)0x7)).AsByte();
Vector128<byte> mask = Vector128.GreaterThan(highNibbles.AsSByte(), Vector128.Create((byte)0x7).AsSByte()).AsByte();
Vector128<byte> bitsets = Vector128.ConditionalSelect(mask, row1, row0);

Vector128<byte> result = Vector128.Equals(bitsets & bitmask, bitmask);
Expand All @@ -1240,7 +1240,7 @@ private static Vector256<byte> IndexOfAnyLookup<TNegator>(Vector256<byte> source

Vector256<byte> bitmask = Avx2.Shuffle(Vector256.Create(0x8040201008040201).AsByte(), highNibbles);

Vector256<byte> mask = Vector256.GreaterThan(highNibbles.AsSByte(), Vector256.Create((sbyte)0x7)).AsByte();
Vector256<byte> mask = Vector256.GreaterThan(highNibbles.AsSByte(), Vector256.Create((byte)0x7).AsSByte()).AsByte();
Vector256<byte> bitsets = Vector256.ConditionalSelect(mask, row1, row0);

Vector256<byte> result = Vector256.Equals(bitsets & bitmask, bitmask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,20 @@ internal static bool Vector128OrdinalIgnoreCaseAscii(Vector128<byte> vec1, Vecto
// ASSUMPTION: Caller has validated that input values are ASCII.

// the 0x80 bit of each word of 'lowerIndicator' will be set iff the word has value >= 'A'
Vector128<sbyte> lowIndicator1 = Vector128.Create((sbyte)(0x80 - 'A')) + vec1.AsSByte();
Vector128<sbyte> lowIndicator2 = Vector128.Create((sbyte)(0x80 - 'A')) + vec2.AsSByte();
Vector128<sbyte> lowIndicator1 = Vector128.Create((byte)(0x80 - 'A')).AsSByte() + vec1.AsSByte();
Vector128<sbyte> lowIndicator2 = Vector128.Create((byte)(0x80 - 'A')).AsSByte() + vec2.AsSByte();

// the 0x80 bit of each word of 'combinedIndicator' will be set iff the word has value >= 'A' and <= 'Z'
Vector128<sbyte> combIndicator1 =
Vector128.LessThan(Vector128.Create(unchecked((sbyte)(('Z' - 'A') - 0x80))), lowIndicator1);
Vector128.LessThan(Vector128.Create(unchecked((byte)(('Z' - 'A') - 0x80))).AsSByte(), lowIndicator1);
Vector128<sbyte> combIndicator2 =
Vector128.LessThan(Vector128.Create(unchecked((sbyte)(('Z' - 'A') - 0x80))), lowIndicator2);
Vector128.LessThan(Vector128.Create(unchecked((byte)(('Z' - 'A') - 0x80))).AsSByte(), lowIndicator2);

// Convert both vectors to lower case by adding 0x20 bit for all [A-Z][a-z] characters
Vector128<sbyte> lcVec1 =
Vector128.AndNot(Vector128.Create((sbyte)0x20), combIndicator1) + vec1.AsSByte();
Vector128.AndNot(Vector128.Create((byte)0x20).AsSByte(), combIndicator1) + vec1.AsSByte();
Vector128<sbyte> lcVec2 =
Vector128.AndNot(Vector128.Create((sbyte)0x20), combIndicator2) + vec2.AsSByte();
Vector128.AndNot(Vector128.Create((byte)0x20).AsSByte(), combIndicator2) + vec2.AsSByte();

// Compare two lowercased vectors
return (lcVec1 ^ lcVec2) == Vector128<sbyte>.Zero;
Expand Down

1 comment on commit bde892c

@xtqqczze
Copy link
Owner Author

@xtqqczze xtqqczze commented on bde892c Oct 14, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.