From bde892c363bb037f50db6d27098187e1eb5acc90 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sun, 13 Oct 2024 17:34:14 +0100 Subject: [PATCH] Avoid unnecessary sign extension broadcasting to vector References: * https://github.com/dotnet/runtime/issues/108820 --- src/libraries/Common/src/System/HexConverter.cs | 2 +- .../Tensors/netcore/TensorPrimitives.IndexOfMax.cs | 6 +++--- .../Buffers/Text/Base64Helper/Base64DecoderHelper.cs | 4 ++-- .../Buffers/Text/Base64Helper/Base64EncoderHelper.cs | 4 ++-- .../System/SearchValues/IndexOfAnyAsciiSearcher.cs | 4 ++-- .../src/System/Text/Unicode/Utf8Utility.cs | 12 ++++++------ 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/libraries/Common/src/System/HexConverter.cs b/src/libraries/Common/src/System/HexConverter.cs index bcc64dcf1a704..5509e048a73cf 100644 --- a/src/libraries/Common/src/System/HexConverter.cs +++ b/src/libraries/Common/src/System/HexConverter.cs @@ -290,7 +290,7 @@ public static bool TryDecodeFromUtf16_Vector128(ReadOnlySpan chars, Span(ReadOnlySpan x static Vector512 CreateVector512T(int i) => sizeof(T) == sizeof(long) ? Vector512.Create((long)i).As() : sizeof(T) == sizeof(int) ? Vector512.Create(i).As() : - sizeof(T) == sizeof(short) ? Vector512.Create((short)i).As() : + sizeof(T) == sizeof(short) ? Vector512.Create((ushort)i).As() : Vector512.Create((byte)i).As(); ref T xRef = ref MemoryMarshal.GetReference(x); @@ -244,7 +244,7 @@ static Vector512 CreateVector512T(int i) => static Vector256 CreateVector256T(int i) => sizeof(T) == sizeof(long) ? Vector256.Create((long)i).As() : sizeof(T) == sizeof(int) ? Vector256.Create(i).As() : - sizeof(T) == sizeof(short) ? Vector256.Create((short)i).As() : + sizeof(T) == sizeof(short) ? Vector256.Create((ushort)i).As() : Vector256.Create((byte)i).As(); ref T xRef = ref MemoryMarshal.GetReference(x); @@ -335,7 +335,7 @@ static Vector256 CreateVector256T(int i) => static Vector128 CreateVector128T(int i) => sizeof(T) == sizeof(long) ? Vector128.Create((long)i).As() : sizeof(T) == sizeof(int) ? Vector128.Create(i).As() : - sizeof(T) == sizeof(short) ? Vector128.Create((short)i).As() : + sizeof(T) == sizeof(short) ? Vector128.Create((ushort)i).As() : Vector128.Create((byte)i).As(); ref T xRef = ref MemoryMarshal.GetReference(x); diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs index 97765e340acff..e0c7719c5eb9d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs @@ -755,8 +755,8 @@ private static unsafe void Avx2Decode(TBase64Decoder decoder, -1, -1, -1, -1, -1, -1, -1, -1).AsInt32(); - Vector256 maskSlashOrUnderscore = Vector256.Create((sbyte)decoder.MaskSlashOrUnderscore); - Vector256 shiftForUnderscore = Vector256.Create((sbyte)33); + Vector256 maskSlashOrUnderscore = Vector256.Create(decoder.MaskSlashOrUnderscore).AsSByte(); + Vector256 shiftForUnderscore = Vector256.Create((byte)33).AsSByte(); Vector256 mergeConstant0 = Vector256.Create(0x01400140).AsSByte(); Vector256 mergeConstant1 = Vector256.Create(0x00011000).AsInt16(); diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64EncoderHelper.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64EncoderHelper.cs index 23c42e547b6fa..ed467feea42d9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64EncoderHelper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64EncoderHelper.cs @@ -254,7 +254,7 @@ private static unsafe void Avx2Encode(TBase64Encoder encoder, Vector256 shiftAC = Vector256.Create(0x04000040).AsUInt16(); Vector256 shiftBB = Vector256.Create(0x01000010).AsInt16(); Vector256 const51 = Vector256.Create((byte)51); - Vector256 const25 = Vector256.Create((sbyte)25); + Vector256 const25 = Vector256.Create((byte)25).AsSByte(); byte* src = srcBytes; T* dest = destBytes; @@ -467,7 +467,7 @@ private static unsafe void Vector128Encode(TBase64Encoder enc Vector128 shiftAC = Vector128.Create(0x04000040).AsUInt16(); Vector128 shiftBB = Vector128.Create(0x01000010).AsInt16(); Vector128 const51 = Vector128.Create((byte)51); - Vector128 const25 = Vector128.Create((sbyte)25); + Vector128 const25 = Vector128.Create((byte)25).AsSByte(); Vector128 mask8F = Vector128.Create((byte)0x8F); byte* src = srcBytes; diff --git a/src/libraries/System.Private.CoreLib/src/System/SearchValues/IndexOfAnyAsciiSearcher.cs b/src/libraries/System.Private.CoreLib/src/System/SearchValues/IndexOfAnyAsciiSearcher.cs index c722387c4f4e1..b0a0cf6c9b28a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SearchValues/IndexOfAnyAsciiSearcher.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SearchValues/IndexOfAnyAsciiSearcher.cs @@ -1217,7 +1217,7 @@ private static Vector128 IndexOfAnyLookup(Vector128 source Vector128 bitmask = Vector128.ShuffleUnsafe(Vector128.Create(0x8040201008040201).AsByte(), highNibbles); - Vector128 mask = Vector128.GreaterThan(highNibbles.AsSByte(), Vector128.Create((sbyte)0x7)).AsByte(); + Vector128 mask = Vector128.GreaterThan(highNibbles.AsSByte(), Vector128.Create((byte)0x7).AsSByte()).AsByte(); Vector128 bitsets = Vector128.ConditionalSelect(mask, row1, row0); Vector128 result = Vector128.Equals(bitsets & bitmask, bitmask); @@ -1240,7 +1240,7 @@ private static Vector256 IndexOfAnyLookup(Vector256 source Vector256 bitmask = Avx2.Shuffle(Vector256.Create(0x8040201008040201).AsByte(), highNibbles); - Vector256 mask = Vector256.GreaterThan(highNibbles.AsSByte(), Vector256.Create((sbyte)0x7)).AsByte(); + Vector256 mask = Vector256.GreaterThan(highNibbles.AsSByte(), Vector256.Create((byte)0x7).AsSByte()).AsByte(); Vector256 bitsets = Vector256.ConditionalSelect(mask, row1, row0); Vector256 result = Vector256.Equals(bitsets & bitmask, bitmask); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8Utility.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8Utility.cs index 9708c32e49e24..178d935f70cdd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8Utility.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8Utility.cs @@ -270,20 +270,20 @@ internal static bool Vector128OrdinalIgnoreCaseAscii(Vector128 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 lowIndicator1 = Vector128.Create((sbyte)(0x80 - 'A')) + vec1.AsSByte(); - Vector128 lowIndicator2 = Vector128.Create((sbyte)(0x80 - 'A')) + vec2.AsSByte(); + Vector128 lowIndicator1 = Vector128.Create((byte)(0x80 - 'A')).AsSByte() + vec1.AsSByte(); + Vector128 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 combIndicator1 = - Vector128.LessThan(Vector128.Create(unchecked((sbyte)(('Z' - 'A') - 0x80))), lowIndicator1); + Vector128.LessThan(Vector128.Create(unchecked((byte)(('Z' - 'A') - 0x80))).AsSByte(), lowIndicator1); Vector128 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 lcVec1 = - Vector128.AndNot(Vector128.Create((sbyte)0x20), combIndicator1) + vec1.AsSByte(); + Vector128.AndNot(Vector128.Create((byte)0x20).AsSByte(), combIndicator1) + vec1.AsSByte(); Vector128 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.Zero;