Move portable thread pool to shared partition (dotnet/corert#7828)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Marvin.cs
blob67123a77067e5a38c7dd1bc3d1fafdcd3a5f04e4
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.Diagnostics;
6 using System.Numerics;
7 using System.Runtime.CompilerServices;
8 using System.Runtime.InteropServices;
9 using Internal.Runtime.CompilerServices;
11 #pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
12 #if BIT64
13 using nuint = System.UInt64;
14 #else
15 using nuint = System.UInt32;
16 #endif
18 namespace System
20 internal static partial class Marvin
22 /// <summary>
23 /// Compute a Marvin hash and collapse it into a 32-bit hash.
24 /// </summary>
25 [MethodImpl(MethodImplOptions.AggressiveInlining)]
26 public static int ComputeHash32(ReadOnlySpan<byte> data, ulong seed) => ComputeHash32(ref MemoryMarshal.GetReference(data), (uint)data.Length, (uint)seed, (uint)(seed >> 32));
28 /// <summary>
29 /// Compute a Marvin hash and collapse it into a 32-bit hash.
30 /// </summary>
31 public static int ComputeHash32(ref byte data, uint count, uint p0, uint p1)
33 // Control flow of this method generally flows top-to-bottom, trying to
34 // minimize the number of branches taken for large (>= 8 bytes, 4 chars) inputs.
35 // If small inputs (< 8 bytes, 4 chars) are given, this jumps to a "small inputs"
36 // handler at the end of the method.
38 if (count < 8)
40 // We can't run the main loop, but we might still have 4 or more bytes available to us.
41 // If so, jump to the 4 .. 7 bytes logic immediately after the main loop.
43 if (count >= 4)
45 goto Between4And7BytesRemain;
47 else
49 goto InputTooSmallToEnterMainLoop;
53 // Main loop - read 8 bytes at a time.
54 // The block function is unrolled 2x in this loop.
56 uint loopCount = count / 8;
57 Debug.Assert(loopCount > 0, "Shouldn't reach this code path for small inputs.");
61 // Most x86 processors have two dispatch ports for reads, so we can read 2x 32-bit
62 // values in parallel. We opt for this instead of a single 64-bit read since the
63 // typical use case for Marvin32 is computing String hash codes, and the particular
64 // layout of String instances means the starting data is never 8-byte aligned when
65 // running in a 64-bit process.
67 p0 += Unsafe.ReadUnaligned<uint>(ref data);
68 uint nextUInt32 = Unsafe.ReadUnaligned<uint>(ref Unsafe.AddByteOffset(ref data, 4));
70 // One block round for each of the 32-bit integers we just read, 2x rounds total.
72 Block(ref p0, ref p1);
73 p0 += nextUInt32;
74 Block(ref p0, ref p1);
76 // Bump the data reference pointer and decrement the loop count.
78 // Decrementing by 1 every time and comparing against zero allows the JIT to produce
79 // better codegen compared to a standard 'for' loop with an incrementing counter.
80 // Requires https://github.com/dotnet/coreclr/issues/7566 to be addressed first
81 // before we can realize the full benefits of this.
83 data = ref Unsafe.AddByteOffset(ref data, 8);
84 } while (--loopCount > 0);
86 // n.b. We've not been updating the original 'count' parameter, so its actual value is
87 // still the original data length. However, we can still rely on its least significant
88 // 3 bits to tell us how much data remains (0 .. 7 bytes) after the loop above is
89 // completed.
91 if ((count & 0b_0100) == 0)
93 goto DoFinalPartialRead;
96 Between4And7BytesRemain:
98 // If after finishing the main loop we still have 4 or more leftover bytes, or if we had
99 // 4 .. 7 bytes to begin with and couldn't enter the loop in the first place, we need to
100 // consume 4 bytes immediately and send them through one round of the block function.
102 Debug.Assert(count >= 4, "Only should've gotten here if the original count was >= 4.");
104 p0 += Unsafe.ReadUnaligned<uint>(ref data);
105 Block(ref p0, ref p1);
107 DoFinalPartialRead:
109 // Finally, we have 0 .. 3 bytes leftover. Since we know the original data length was at
110 // least 4 bytes (smaller lengths are handled at the end of this routine), we can safely
111 // read the 4 bytes at the end of the buffer without reading past the beginning of the
112 // original buffer. This necessarily means the data we're about to read will overlap with
113 // some data we've already processed, but we can handle that below.
115 Debug.Assert(count >= 4, "Only should've gotten here if the original count was >= 4.");
117 // Read the last 4 bytes of the buffer.
119 uint partialResult = Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref Unsafe.AddByteOffset(ref data, (nuint)count & 7), -4));
121 // The 'partialResult' local above contains any data we have yet to read, plus some number
122 // of bytes which we've already read from the buffer. An example of this is given below
123 // for little-endian architectures. In this table, AA BB CC are the bytes which we still
124 // need to consume, and ## are bytes which we want to throw away since we've already
125 // consumed them as part of a previous read.
127 // (partialResult contains) (we want it to contain)
128 // count mod 4 = 0 -> [ ## ## ## ## | ] -> 0x####_#### -> 0x0000_0080
129 // count mod 4 = 1 -> [ ## ## ## ## | AA ] -> 0xAA##_#### -> 0x0000_80AA
130 // count mod 4 = 2 -> [ ## ## ## ## | AA BB ] -> 0xBBAA_#### -> 0x0080_BBAA
131 // count mod 4 = 3 -> [ ## ## ## ## | AA BB CC ] -> 0xCCBB_AA## -> 0x80CC_BBAA
133 count = ~count << 3;
135 if (BitConverter.IsLittleEndian)
137 partialResult >>= 8; // make some room for the 0x80 byte
138 partialResult |= 0x8000_0000u; // put the 0x80 byte at the beginning
139 partialResult >>= (int)count & 0x1F; // shift out all previously consumed bytes
141 else
143 partialResult <<= 8; // make some room for the 0x80 byte
144 partialResult |= 0x80u; // put the 0x80 byte at the end
145 partialResult <<= (int)count & 0x1F; // shift out all previously consumed bytes
148 DoFinalRoundsAndReturn:
150 // Now that we've computed the final partial result, merge it in and run two rounds of
151 // the block function to finish out the Marvin algorithm.
153 p0 += partialResult;
154 Block(ref p0, ref p1);
155 Block(ref p0, ref p1);
157 return (int)(p1 ^ p0);
159 InputTooSmallToEnterMainLoop:
161 // We had only 0 .. 3 bytes to begin with, so we can't perform any 32-bit reads.
162 // This means that we're going to be building up the final result right away and
163 // will only ever run two rounds total of the block function. Let's initialize
164 // the partial result to "no data".
166 if (BitConverter.IsLittleEndian)
168 partialResult = 0x80u;
170 else
172 partialResult = 0x80000000u;
175 if ((count & 0b_0001) != 0)
177 // If the buffer is 1 or 3 bytes in length, let's read a single byte now
178 // and merge it into our partial result. This will result in partialResult
179 // having one of the two values below, where AA BB CC are the buffer bytes.
181 // (little-endian / big-endian)
182 // [ AA ] -> 0x0000_80AA / 0xAA80_0000
183 // [ AA BB CC ] -> 0x0000_80CC / 0xCC80_0000
185 partialResult = Unsafe.AddByteOffset(ref data, (nuint)count & 2);
187 if (BitConverter.IsLittleEndian)
189 partialResult |= 0x8000;
191 else
193 partialResult <<= 24;
194 partialResult |= 0x800000u;
198 if ((count & 0b_0010) != 0)
200 // If the buffer is 2 or 3 bytes in length, let's read a single ushort now
201 // and merge it into the partial result. This will result in partialResult
202 // having one of the two values below, where AA BB CC are the buffer bytes.
204 // (little-endian / big-endian)
205 // [ AA BB ] -> 0x0080_BBAA / 0xAABB_8000
206 // [ AA BB CC ] -> 0x80CC_BBAA / 0xAABB_CC80 (carried over from above)
208 if (BitConverter.IsLittleEndian)
210 partialResult <<= 16;
211 partialResult |= (uint)Unsafe.ReadUnaligned<ushort>(ref data);
213 else
215 partialResult |= (uint)Unsafe.ReadUnaligned<ushort>(ref data);
216 partialResult = BitOperations.RotateLeft(partialResult, 16);
220 // Everything is consumed! Go perform the final rounds and return.
222 goto DoFinalRoundsAndReturn;
225 [MethodImpl(MethodImplOptions.AggressiveInlining)]
226 private static void Block(ref uint rp0, ref uint rp1)
228 uint p0 = rp0;
229 uint p1 = rp1;
231 p1 ^= p0;
232 p0 = BitOperations.RotateLeft(p0, 20);
234 p0 += p1;
235 p1 = BitOperations.RotateLeft(p1, 9);
237 p1 ^= p0;
238 p0 = BitOperations.RotateLeft(p0, 27);
240 p0 += p1;
241 p1 = BitOperations.RotateLeft(p1, 19);
243 rp0 = p0;
244 rp1 = p1;
247 public static ulong DefaultSeed { get; } = GenerateSeed();
249 private static unsafe ulong GenerateSeed()
251 ulong seed;
252 Interop.GetRandomBytes((byte*)&seed, sizeof(ulong));
253 return seed;