diff --git a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs --- a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs +++ b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs @@ -865,6 +865,11 @@ // (int.MaxValue / 3) == 715,827,882 Bytes == 699 MB ArgumentOutOfRangeException.ThrowIfGreaterThan(length, int.MaxValue / 3); + if (Ssse3.IsSupported && length >= 9) + { + return ToDashedHexVectorized(new ReadOnlySpan(value, startIndex, length)); + } + string result = string.FastAllocateString(length * 3 - 1); var dst = new Span(ref result.GetRawStringData(), result.Length); @@ -885,6 +890,66 @@ return result; } + [MethodImpl(MethodImplOptions.NoInlining)] + [CompExactlyDependsOn(typeof(Ssse3))] + private static string ToDashedHexVectorized(ReadOnlySpan source) + { + string result = string.FastAllocateString(source.Length * 3 - 1); + var destination = new Span(ref result.GetRawStringData(), result.Length); + byte first = source[0]; + destination[0] = HexConverter.ToCharUpper(first >> 4); + destination[1] = HexConverter.ToCharUpper(first); + FormatDashedHexBlocks(source.Slice(1), destination.Slice(2)); + return result; + } + + [CompExactlyDependsOn(typeof(Ssse3))] + private static void FormatDashedHexBlocks(ReadOnlySpan source, Span destination) + { + const byte Zero = 0x80; + const byte Dash = (byte)'-'; + Vector128 hexMap = Vector128.Create( + (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', + (byte)'8', (byte)'9', (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F'); + + // Each block of eight bytes has 16 hex digits and eight leading separators. + // Shuffle inserts zero at the separator positions, then OR fills them with '-'. + Vector128 headIndices = Vector128.Create(Zero, 0, 1, Zero, 2, 3, Zero, 4, 5, Zero, 6, 7, Zero, 8, 9, Zero); + Vector128 tailIndices = Vector128.Create(10, 11, Zero, 12, 13, Zero, 14, 15, Zero, Zero, Zero, Zero, Zero, Zero, Zero, Zero); + Vector128 headSeparators = Vector128.Create(Dash, 0, 0, Dash, 0, 0, Dash, 0, 0, Dash, 0, 0, Dash, 0, 0, Dash); + Vector128 tailSeparators = Vector128.Create(0, 0, Dash, 0, 0, Dash, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + + ref byte input = ref MemoryMarshal.GetReference(source); + ref ushort output = ref Unsafe.As(ref MemoryMarshal.GetReference(destination)); + int last = source.Length - 8; + for (int i = 0; ; i = Math.Min(i + 8, last)) + { + Vector128 bytes = Vector128.CreateScalar(Unsafe.ReadUnaligned(ref Unsafe.Add(ref input, i))).AsByte(); + (Vector128 digits, _) = HexConverter.AsciiToHexVector128(bytes, hexMap); + Vector128 head = Ssse3.Shuffle(digits, headIndices) | headSeparators; + Vector128 tail = Ssse3.Shuffle(digits, tailIndices) | tailSeparators; + + nuint offset = (nuint)i * 3; +#pragma warning disable IntrinsicsInSystemPrivateCoreLibAttributeNotSpecificEnough // Both paths write identical UTF-16 characters. + if (Avx2.IsSupported) +#pragma warning restore IntrinsicsInSystemPrivateCoreLibAttributeNotSpecificEnough + { + Avx2.ConvertToVector256Int16(head).AsUInt16().StoreUnsafe(ref output, offset); + } + else + { + Vector128.WidenLower(head).StoreUnsafe(ref output, offset); + Vector128.WidenUpper(head).StoreUnsafe(ref output, offset + 8); + } + Vector128.WidenLower(tail).StoreUnsafe(ref output, offset + 16); + + if (i == last) + { + return; + } + } + } + /// /// Converts the numeric value of each element of a specified array of bytes /// to its equivalent hexadecimal string representation. diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/BitConverter.cs b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/BitConverter.cs --- a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/BitConverter.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/BitConverter.cs @@ -289,6 +289,19 @@ } [Fact] + public static void ToString_ByteArray_Slice() + { + byte[] bytes = Enumerable.Range(0, 29).Select(i => (byte)(i * 9)).ToArray(); + int startIndex = 5; + + for (int length = 8; length <= 18; length++) + { + string expected = string.Join("-", bytes.Skip(startIndex).Take(length).Select(b => b.ToString("X2"))); + Assert.Equal(expected, BitConverter.ToString(bytes, startIndex, length)); + } + } + + [Fact] public static void ToString_ByteArrayTooLong_Throws() { byte[] arr; # End of patch