| | | 1 | | /******************************************************************************** |
| | | 2 | | * ReadOnlyMemoryCharComparer.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | |
| | | 11 | | namespace NanoRoute.Internals |
| | | 12 | | { |
| | | 13 | | internal sealed class ReadOnlyMemoryCharComparer: IEqualityComparer<ReadOnlyMemory<char>> |
| | | 14 | | { |
| | 2 | 15 | | public static ReadOnlyMemoryCharComparer Instance { get; } = new(); |
| | | 16 | | |
| | | 17 | | public bool Equals(ReadOnlyMemory<char> x, ReadOnlyMemory<char> y) |
| | 2 | 18 | | { |
| | 2 | 19 | | int length = x.Length; |
| | 2 | 20 | | if (length != y.Length) |
| | 1 | 21 | | return false; |
| | | 22 | | |
| | 2 | 23 | | ref char |
| | 2 | 24 | | leftRef = ref MemoryMarshal.GetReference(x.Span), |
| | 2 | 25 | | rightRef = ref MemoryMarshal.GetReference(y.Span); |
| | | 26 | | |
| | 2 | 27 | | int i = 0; |
| | | 28 | | |
| | 2 | 29 | | for (int bulkLength = length & -4; i < bulkLength; i += 4) |
| | 2 | 30 | | { |
| | 2 | 31 | | ulong |
| | 2 | 32 | | leftBlock = Unsafe.As<char, ulong>(ref Unsafe.Add(ref leftRef, i)), |
| | 2 | 33 | | rightBlock = Unsafe.As<char, ulong>(ref Unsafe.Add(ref rightRef, i)); |
| | | 34 | | |
| | 2 | 35 | | if (BlockToUpper(leftBlock) != BlockToUpper(rightBlock)) |
| | 1 | 36 | | return false; |
| | 2 | 37 | | } |
| | | 38 | | |
| | 2 | 39 | | int remainingChars = length & 3; |
| | | 40 | | |
| | 2 | 41 | | switch (remainingChars) |
| | | 42 | | { |
| | | 43 | | case 3: |
| | 1 | 44 | | if (CharToUpper(Unsafe.Add(ref leftRef, i + 2)) != CharToUpper(Unsafe.Add(ref rightRef, i + 2))) |
| | 1 | 45 | | return false; |
| | 1 | 46 | | goto case 2; |
| | | 47 | | case 2: |
| | 2 | 48 | | if (CharToUpper(Unsafe.Add(ref leftRef, i + 1)) != CharToUpper(Unsafe.Add(ref rightRef, i + 1))) |
| | 1 | 49 | | return false; |
| | 2 | 50 | | goto case 1; |
| | | 51 | | case 1: |
| | 2 | 52 | | if (CharToUpper(Unsafe.Add(ref leftRef, i)) != CharToUpper(Unsafe.Add(ref rightRef, i))) |
| | 1 | 53 | | return false; |
| | 2 | 54 | | break; |
| | | 55 | | } |
| | | 56 | | |
| | 2 | 57 | | return true; |
| | | 58 | | |
| | | 59 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 60 | | static char CharToUpper(char chr) |
| | | 61 | | { |
| | | 62 | | if ((chr & ~0x007Fu) is 0) |
| | | 63 | | { |
| | | 64 | | uint |
| | | 65 | | lowerIndicator = chr + 0x0080u - 0x0061u, |
| | | 66 | | upperIndicator = chr + 0x0080u - 0x007Bu, |
| | | 67 | | combinedIndicator = lowerIndicator ^ upperIndicator, |
| | | 68 | | mask = (combinedIndicator & 0x0080u) >> 2; |
| | | 69 | | |
| | | 70 | | return (char)(chr ^ mask); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | // Slow... |
| | | 74 | | return char.ToUpperInvariant(chr); |
| | | 75 | | } |
| | 2 | 76 | | } |
| | | 77 | | |
| | | 78 | | public int GetHashCode(ReadOnlyMemory<char> obj) |
| | 2 | 79 | | { |
| | 2 | 80 | | ref char inputRef = ref MemoryMarshal.GetReference(obj.Span); |
| | | 81 | | |
| | | 82 | | unchecked |
| | 2 | 83 | | { |
| | 2 | 84 | | uint |
| | 2 | 85 | | p0 = 0xD6E8_FEB8u, |
| | 2 | 86 | | p1 = 0xA5A5_A5A5u; |
| | | 87 | | |
| | 2 | 88 | | int i = 0; |
| | | 89 | | |
| | 2 | 90 | | for (int bulkLength = obj.Length & -4; i < bulkLength; i += 4) |
| | 2 | 91 | | { |
| | 2 | 92 | | ulong |
| | 2 | 93 | | block = Unsafe.As<char, ulong>(ref Unsafe.Add(ref inputRef, i)), |
| | 2 | 94 | | upperBlock = BlockToUpper(block); |
| | | 95 | | |
| | | 96 | | // Feed Marvin with the uppercased UTF-16 bytes, two chars at a time. |
| | 2 | 97 | | ref uint upperBlockRef = ref Unsafe.As<ulong, uint>(ref upperBlock); |
| | | 98 | | |
| | 2 | 99 | | p0 += upperBlockRef; |
| | 2 | 100 | | MarvinBlock(ref p0, ref p1); |
| | | 101 | | |
| | 2 | 102 | | p0 += Unsafe.Add(ref upperBlockRef, 1); |
| | 2 | 103 | | MarvinBlock(ref p0, ref p1); |
| | 2 | 104 | | } |
| | | 105 | | |
| | 2 | 106 | | int remainingChars = obj.Length & 3; |
| | | 107 | | |
| | 2 | 108 | | if (remainingChars is 0) |
| | 1 | 109 | | p0 += 0x80u; |
| | | 110 | | else |
| | 2 | 111 | | { |
| | | 112 | | // Pack the 1-3 char tail into a local block so BlockToUpper can handle it too. |
| | 2 | 113 | | ulong tail = 0; |
| | | 114 | | |
| | 2 | 115 | | switch (remainingChars) |
| | | 116 | | { |
| | | 117 | | case 3: |
| | 1 | 118 | | tail |= (ulong) Unsafe.Add(ref inputRef, i + 2) << 32; |
| | 1 | 119 | | goto case 2; |
| | | 120 | | case 2: |
| | 2 | 121 | | tail |= (ulong) Unsafe.Add(ref inputRef, i + 1) << 16; |
| | 2 | 122 | | goto case 1; |
| | | 123 | | case 1: |
| | 2 | 124 | | tail |= Unsafe.Add(ref inputRef, i); |
| | 2 | 125 | | break; |
| | | 126 | | } |
| | | 127 | | |
| | 2 | 128 | | tail = BlockToUpper(tail); |
| | | 129 | | |
| | 2 | 130 | | ref char tailRef = ref Unsafe.As<ulong, char>(ref tail); |
| | | 131 | | // A 2- or 3-char tail has one complete Marvin block before padding. |
| | 2 | 132 | | if (remainingChars >= 2) |
| | 2 | 133 | | { |
| | 2 | 134 | | p0 += Unsafe.As<char, uint>(ref tailRef); |
| | 2 | 135 | | MarvinBlock(ref p0, ref p1); |
| | 2 | 136 | | } |
| | | 137 | | |
| | | 138 | | // Add Marvin's final 0x80 padding after the remaining UTF-16 bytes. |
| | 2 | 139 | | p0 += remainingChars is 2 |
| | 2 | 140 | | ? 0x80u |
| | 2 | 141 | | : (uint) Unsafe.Add(ref tailRef, remainingChars - 1) | 0x0080_0000u; |
| | 2 | 142 | | } |
| | | 143 | | |
| | 2 | 144 | | MarvinBlock(ref p0, ref p1); |
| | 2 | 145 | | MarvinBlock(ref p0, ref p1); |
| | | 146 | | |
| | 2 | 147 | | return (int) (p1 ^ p0); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 151 | | static void MarvinBlock(ref uint p0, ref uint p1) |
| | | 152 | | { |
| | | 153 | | p1 ^= p0; |
| | | 154 | | p0 = RotateLeft(p0, 20); |
| | | 155 | | p0 += p1; |
| | | 156 | | p1 = RotateLeft(p1, 9); |
| | | 157 | | p1 ^= p0; |
| | | 158 | | p0 = RotateLeft(p0, 27); |
| | | 159 | | p0 += p1; |
| | | 160 | | p1 = RotateLeft(p1, 19); |
| | | 161 | | |
| | | 162 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 163 | | static uint RotateLeft(uint value, int offset) => value << offset | value >> (32 - offset); |
| | | 164 | | } |
| | 2 | 165 | | } |
| | | 166 | | |
| | | 167 | | // https://github.com/dotnet/runtime/blob/ecc8cb5bc0411e0fb0549230f70dfe8ab302c65c/src/libraries/System.Private. |
| | | 168 | | private static ulong BlockToUpper(ulong input) |
| | 2 | 169 | | { |
| | 2 | 170 | | ulong |
| | 2 | 171 | | // Each 16-bit lane is non-zero here only when that char is outside ASCII. |
| | 2 | 172 | | nonAsciiMask = input & ~0x007F_007F_007F_007Ful, |
| | 2 | 173 | | lowerIndicator = input + 0x0080_0080_0080_0080ul - 0x0061_0061_0061_0061ul, |
| | 2 | 174 | | upperIndicator = input + 0x0080_0080_0080_0080ul - 0x007B_007B_007B_007Bul, |
| | 2 | 175 | | combinedIndicator = lowerIndicator ^ upperIndicator, |
| | 2 | 176 | | asciiUpper = input ^ ((combinedIndicator & 0x0080_0080_0080_0080ul) >> 2); |
| | | 177 | | |
| | 2 | 178 | | if (nonAsciiMask is not 0) |
| | 1 | 179 | | { |
| | | 180 | | // ASCII lanes are already uppercased; only non-ASCII lanes need the slow path. |
| | 1 | 181 | | ref char inputRef = ref Unsafe.As<ulong, char>(ref input); |
| | 1 | 182 | | ref char resultRef = ref Unsafe.As<ulong, char>(ref asciiUpper); |
| | | 183 | | |
| | 1 | 184 | | if ((nonAsciiMask & 0xFF80ul) is not 0) |
| | 1 | 185 | | resultRef = char.ToUpperInvariant(inputRef); |
| | | 186 | | |
| | 1 | 187 | | if ((nonAsciiMask & 0xFF80_0000ul) is not 0) |
| | 1 | 188 | | Unsafe.Add(ref resultRef, 1) = char.ToUpperInvariant(Unsafe.Add(ref inputRef, 1)); |
| | | 189 | | |
| | 1 | 190 | | if ((nonAsciiMask & 0xFF80_0000_0000ul) is not 0) |
| | 1 | 191 | | Unsafe.Add(ref resultRef, 2) = char.ToUpperInvariant(Unsafe.Add(ref inputRef, 2)); |
| | | 192 | | |
| | 1 | 193 | | if ((nonAsciiMask & 0xFF80_0000_0000_0000ul) is not 0) |
| | 1 | 194 | | Unsafe.Add(ref resultRef, 3) = char.ToUpperInvariant(Unsafe.Add(ref inputRef, 3)); |
| | 1 | 195 | | } |
| | | 196 | | |
| | 2 | 197 | | return asciiUpper; |
| | 2 | 198 | | } |
| | | 199 | | } |
| | | 200 | | } |