From 825bf7577271fa9b38892eb95eb90ba73874acf9 Mon Sep 17 00:00:00 2001 From: Deepak Rajendrakumaran Date: Wed, 27 Mar 2024 14:55:20 -0700 Subject: [PATCH] Addressing Review Comments. --- .../src/System/Text/Ascii.Utility.cs | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Ascii.Utility.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Ascii.Utility.cs index 2b97588e410ddb..507a1f6138d679 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Ascii.Utility.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Ascii.Utility.cs @@ -2151,9 +2151,9 @@ internal static unsafe nuint WidenAsciiToUtf16(byte* pAsciiBuffer, char* pUtf16B } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe void WidenAsciiToUtf1_Vector(byte* pAsciiBuffer, char* pUtf16Buffer, ref nuint currentOffset, nuint elementCount) + private static unsafe void WidenAsciiToUtf1_Vector(byte* pAsciiBuffer, char* pUtf16Buffer, ref nuint currentOffset, nuint elementCount) where TVectorByte : unmanaged, ISimdVector - where TVectorUShort : unmanaged, ISimdVector + where TVectorUInt16 : unmanaged, ISimdVector { ushort* pCurrentWriteAddress = (ushort*)pUtf16Buffer; // Calculating the destination address outside the loop results in significant @@ -2163,14 +2163,15 @@ private static unsafe void WidenAsciiToUtf1_Vector(b TVectorByte asciiVector = TVectorByte.Load(pAsciiBuffer + currentOffset); if (!HasMatch(asciiVector)) { - (TVectorUShort utf16LowVector, TVectorUShort utf16HighVector) = Widen(asciiVector); + (TVectorUInt16 utf16LowVector, TVectorUInt16 utf16HighVector) = Widen(asciiVector); utf16LowVector.Store(pCurrentWriteAddress); - utf16HighVector.Store(pCurrentWriteAddress + TVectorUShort.Count); - pCurrentWriteAddress += (nuint)(TVectorUShort.Count * 2); - if (((int)pCurrentWriteAddress & 1) == 0) + utf16HighVector.Store(pCurrentWriteAddress + TVectorUInt16.Count); + pCurrentWriteAddress += (nuint)(TVectorUInt16.Count * 2); + bool canAlign = (((nuint)pCurrentWriteAddress % sizeof(char)) == 0); + if (((nuint)pCurrentWriteAddress % sizeof(char)) == 0) { // Bump write buffer up to the next aligned boundary - pCurrentWriteAddress = (ushort*)((nuint)pCurrentWriteAddress & ~(nuint)(TVectorUShort.Alignment - 1)); + pCurrentWriteAddress = (ushort*)((nuint)pCurrentWriteAddress & ~(nuint)(TVectorUInt16.Alignment - 1)); nuint numBytesWritten = (nuint)pCurrentWriteAddress - (nuint)pUtf16Buffer; currentOffset += (nuint)numBytesWritten / 2; } @@ -2186,11 +2187,12 @@ private static unsafe void WidenAsciiToUtf1_Vector(b { break; } - (utf16LowVector, utf16HighVector) = Widen(asciiVector); - utf16LowVector.StoreAligned(pCurrentWriteAddress); - utf16HighVector.StoreAligned(pCurrentWriteAddress + TVectorUShort.Count); + (utf16LowVector, utf16HighVector) = Widen(asciiVector); + utf16LowVector.Store(pCurrentWriteAddress); + utf16HighVector.Store(pCurrentWriteAddress + TVectorUInt16.Count); + currentOffset += (nuint)TVectorByte.Count; - pCurrentWriteAddress += (nuint)(TVectorUShort.Count * 2); + pCurrentWriteAddress += (nuint)(TVectorUInt16.Count * 2); } } return; @@ -2205,22 +2207,26 @@ private static unsafe bool HasMatch(TVectorByte vector) [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe (TVectorUShort Lower, TVectorUShort Upper) Widen(TVectorByte vector) + private static unsafe (TVectorUInt16 Lower, TVectorUInt16 Upper) Widen(TVectorByte vector) where TVectorByte : unmanaged, ISimdVector - where TVectorUShort : unmanaged, ISimdVector + where TVectorUInt16 : unmanaged, ISimdVector { if (typeof(TVectorByte) == typeof(Vector256)) { (Vector256 Lower256, Vector256 Upper256) = Vector256.Widen((Vector256)(object)vector); - return ((TVectorUShort)(object)Lower256, (TVectorUShort)(object)Upper256); + return ((TVectorUInt16)(object)Lower256, (TVectorUInt16)(object)Upper256); } else if (typeof(TVectorByte) == typeof(Vector512)) { (Vector512 Lower512, Vector512 Upper512) = Vector512.Widen((Vector512)(object)vector); - return ((TVectorUShort)(object)Lower512, (TVectorUShort)(object)Upper512); + return ((TVectorUInt16)(object)Lower512, (TVectorUInt16)(object)Upper512); + } + else + { + Debug.Assert(typeof(TVectorByte) == typeof(Vector128)); + (Vector128 Lower128, Vector128 Upper128) = Vector128.Widen((Vector128)(object)vector); + return ((TVectorUInt16)(object)Lower128, (TVectorUInt16)(object)Upper128); } - (Vector128 Lower128, Vector128 Upper128) = Vector128.Widen((Vector128)(object)vector); - return ((TVectorUShort)(object)Lower128, (TVectorUShort)(object)Upper128); }