Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectorize HexConverter.EncodeToUtf16 using SSSE3 #44111

Merged
merged 31 commits into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6db1e03
Vectorize HexConverter.EncodeToUtf16 using SSSE3
EgorBo Oct 31, 2020
ea1f6e1
Clean up
EgorBo Oct 31, 2020
d6aefad
Fix typos
EgorBo Oct 31, 2020
96322dd
Fix compilation issue
EgorBo Oct 31, 2020
b67c0d3
Clean up
EgorBo Oct 31, 2020
24fbb3c
Add usings
EgorBo Oct 31, 2020
4d7e386
Extract into a separate method
EgorBo Oct 31, 2020
288de9c
Fix compilation issues
EgorBo Oct 31, 2020
577a4e4
Clean up
EgorBo Oct 31, 2020
3aa5edb
fix build issue
EgorBo Oct 31, 2020
006e9d4
another attempt to fix CI
EgorBo Oct 31, 2020
1962bc6
Address feedback
EgorBo Oct 31, 2020
50828d7
Remove default value for casing arg in EncodeToUtf16_Ssse3
EgorBo Oct 31, 2020
d9d0af8
fix ifdefs
EgorBo Nov 1, 2020
8f61c1b
fix ifdefs
EgorBo Nov 1, 2020
996c164
fix ifdefs
EgorBo Nov 1, 2020
8a8baf6
Update src/libraries/Common/src/System/HexConverter.cs
EgorBo Nov 1, 2020
79f352f
Update HexConverter.cs
EgorBo Nov 1, 2020
3bbb843
Update HexConverter.cs
EgorBo Nov 1, 2020
fdd5d6e
Add a test
EgorBo Nov 1, 2020
587f9ab
Update Convert.ToHexString.cs
EgorBo Nov 1, 2020
af2a755
Update Convert.ToHexString.cs
EgorBo Nov 1, 2020
8597926
Address feedback
EgorBo Nov 3, 2020
ee65beb
Address feedback
EgorBo Nov 3, 2020
7ff83bd
use nint
EgorBo Nov 3, 2020
c99e9a4
Merge branch 'master' of github.com:dotnet/runtime into vectorize-hex…
EgorBo Nov 3, 2020
adefb21
Update HexConverter.cs
EgorBo Nov 3, 2020
f2785e5
Merge branch 'master' of github.com:dotnet/runtime into vectorize-hex…
EgorBo Nov 9, 2020
fb2b13e
remove redundant int cast
EgorBo Nov 9, 2020
a9ceecc
Merge branch 'master' of github.com:dotnet/runtime into vectorize-hex…
EgorBo Nov 26, 2020
d7fb72c
Fix indent
EgorBo Nov 26, 2020
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
72 changes: 71 additions & 1 deletion src/libraries/Common/src/System/HexConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#nullable disable
using System.Diagnostics;
using System.Runtime.CompilerServices;
#if SYSTEM_PRIVATE_CORELIB
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using Internal.Runtime.CompilerServices;
#endif

namespace System
{
Expand Down Expand Up @@ -84,11 +90,75 @@ public static void ToCharsBuffer(byte value, Span<char> buffer, int startingInde
buffer[startingIndex] = (char)(packedResult >> 8);
}

#if SYSTEM_PRIVATE_CORELIB
private static void EncodeToUtf16_Ssse3(ReadOnlySpan<byte> bytes, Span<char> chars, Casing casing)
{
Debug.Assert(bytes.Length >= 4);
nint pos = 0;

Vector128<byte> shuffleMask = Vector128.Create(
0xFF, 0xFF, 0, 0xFF, 0xFF, 0xFF, 1, 0xFF,
0xFF, 0xFF, 2, 0xFF, 0xFF, 0xFF, 3, 0xFF);

Vector128<byte> asciiTable = (casing == Casing.Upper) ?
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') :
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');

do
{
// Read 32bits from "bytes" span at "pos" offset
uint block = Unsafe.ReadUnaligned<uint>(
ref Unsafe.Add(ref MemoryMarshal.GetReference(bytes), pos));

// Calculate nibbles
Vector128<byte> lowNibbles = Ssse3.Shuffle(
Vector128.CreateScalarUnsafe(block).AsByte(), shuffleMask);
Vector128<byte> highNibbles = Sse2.ShiftRightLogical(
Sse2.ShiftRightLogical128BitLane(lowNibbles, 2).AsInt32(), 4).AsByte();

// Lookup the hex values at the positions of the indices
Vector128<byte> indices = Sse2.And(
Sse2.Or(lowNibbles, highNibbles), Vector128.Create((byte)0xF));
Vector128<byte> hex = Ssse3.Shuffle(asciiTable, indices);

// The high bytes (0x00) of the chars have also been converted
// to ascii hex '0', so clear them out.
hex = Sse2.And(hex, Vector128.Create((ushort)0xFF).AsByte());

// Save to "chars" at pos*2 offset
Unsafe.WriteUnaligned(
ref Unsafe.As<char, byte>(
ref Unsafe.Add(ref MemoryMarshal.GetReference(chars), pos * 2)), hex);

pos += 4;
} while (pos < bytes.Length - 3);

// Process trailing elements (bytes.Length % 4)
for (; pos < bytes.Length; pos++)
{
ToCharsBuffer(Unsafe.Add(ref MemoryMarshal.GetReference(bytes), pos), chars, (int)pos * 2, casing);
}
}
#endif

public static void EncodeToUtf16(ReadOnlySpan<byte> bytes, Span<char> chars, Casing casing = Casing.Upper)
{
Debug.Assert(chars.Length >= bytes.Length * 2);

for (int pos = 0; pos < bytes.Length; ++pos)
#if SYSTEM_PRIVATE_CORELIB
if (Ssse3.IsSupported && bytes.Length >= 4)
{
EncodeToUtf16_Ssse3(bytes, chars, casing);
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
return;
}
#endif
for (int pos = 0; pos < bytes.Length; pos++)
{
ToCharsBuffer(bytes[pos], chars, pos * 2, casing);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Text;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;
using System.Text;
using System.Collections.Generic;

namespace System.Tests
{
Expand Down Expand Up @@ -62,5 +66,45 @@ public static unsafe void InputTooLarge()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("bytes", () => Convert.ToHexString(new ReadOnlySpan<byte>((void*)0, Int32.MaxValue)));
}

public static IEnumerable<object[]> ToHexStringTestData()
{
yield return new object[] { new byte[0], "" };
yield return new object[] { new byte[] { 0x00 }, "00" };
yield return new object[] { new byte[] { 0x01 }, "01" };
yield return new object[] { new byte[] { 0xFF }, "FF" };
yield return new object[] { new byte[] { 0x00, 0x00 }, "0000" };
yield return new object[] { new byte[] { 0xAB, 0xCD }, "ABCD" };
yield return new object[] { new byte[] { 0xFF, 0xFF }, "FFFF" };
yield return new object[] { new byte[] { 0x00, 0x00, 0x00 }, "000000" };
yield return new object[] { new byte[] { 0x01, 0x02, 0x03 }, "010203" };
yield return new object[] { new byte[] { 0xFF, 0xFF, 0xFF }, "FFFFFF" };
yield return new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00 }, "00000000" };
yield return new object[] { new byte[] { 0xAB, 0xCD, 0xEF, 0x12 }, "ABCDEF12" };
yield return new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, "FFFFFFFF" };
yield return new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00 }, "0000000000" };
yield return new object[] { new byte[] { 0xAB, 0xCD, 0xEF, 0x12, 0x34 }, "ABCDEF1234" };
yield return new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, "FFFFFFFFFF" };
yield return new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, "000000000000" };
yield return new object[] { new byte[] { 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56 }, "ABCDEF123456" };
yield return new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, "FFFFFFFFFFFF" };
yield return new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, "00000000000000" };
yield return new object[] { new byte[] { 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78 }, "ABCDEF12345678" };
yield return new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, "FFFFFFFFFFFFFF" };
yield return new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, "0000000000000000" };
yield return new object[] { new byte[] { 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90 }, "ABCDEF1234567890" };
yield return new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, "FFFFFFFFFFFFFFFF" };
yield return new object[] { new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, "000000000000000000" };
yield return new object[] { new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }, "010203040506070809" };
yield return new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, "FFFFFFFFFFFFFFFFFF" };
}

[Theory]
[MemberData(nameof(ToHexStringTestData))]
public static unsafe void ToHexString(byte[] input, string expected)
{
string actual = Convert.ToHexString(input);
Assert.Equal(expected, actual);
}
}
}