Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
94 changes: 94 additions & 0 deletions src/Microsoft.ML.Core/Utilities/Hashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.ML.Runtime;
Expand Down Expand Up @@ -174,6 +175,87 @@ public static uint MurmurHash(uint hash, ReadOnlySpan<char> span, bool toUpper =
return hash;
}

public static uint MurmurHashV2(uint hash, ReadOnlySpan<char> span, bool toUpper = false)
{
// Byte length (in pseudo UTF-8 form).
int len = 0;

// Current bits, value and count.
ulong cur = 0;
int bits = 0;
for (int ich = 0; ich < span.Length; ich++)
{
Contracts.Assert((bits & 0x7) == 0);
Contracts.Assert((uint)bits <= 24);
Contracts.Assert(cur <= 0x00FFFFFF);

uint ch = toUpper ? char.ToUpperInvariant(span[ich]) : span[ich];
if (ch <= 0x007F)
{
cur |= ch << bits;
bits += 8;
}
else if (ch <= 0x07FF)
{
cur |= (ulong)((ch & 0x003F) | ((ch << 2) & 0x1F00) | 0xC080) << bits;
cur = (cur & 0xFF) << 8 | cur >> 8;
bits += 16;
}
else if (ch <= 0xFFFF)
{
cur |= (ulong)((ch & 0x003F) | ((ch << 2) & 0x3F00) | ((ch << 4) & 0x0F0000) | 0xE08080) << bits;
cur = (cur & 0xFF) << 16 | ((cur >> 8) & 0xFF) << 8 | cur >> 16;
bits += 24;
}
else
{
Contracts.Assert(ch <= 0x10FFFF);
cur |= (ulong)((ch & 0x003F) | ((ch << 2) & 0x3F00) | ((ch << 4) & 0x3F0000) | ((ch << 6) & 0x07000000) | 0xF0808080) << bits;
cur = (cur & 0xFF) << 24 | ((cur >> 8) & 0xFF) << 16 | ((cur >> 16) & 0xFF) << 8 | cur >> 24;
bits += 32;
}

if (bits >= 32)
{
hash = MurmurRound(hash, (uint)cur);
cur = cur >> 32;
bits -= 32;
len += 4;
}
}
Contracts.Assert((bits & 0x7) == 0);
Contracts.Assert((uint)bits <= 24);
Contracts.Assert(cur <= 0x00FFFFFF);

if (bits > 0)
{
len += bits / 8;
}

// tail processing
uint k1 = 0;
switch (len & 3)
{
case 3:
k1 ^= (uint)(((cur >> 16) & 0xFF) << 16);
goto case 2;
case 2:
k1 ^= (uint)((cur >> 8) & 0xFF) << 8;
goto case 1;
case 1:
k1 ^= (uint)(cur & 0xFF);
k1 *= 0xCC9E2D51; k1 = Rotate(k1, 15);
k1 *= 0x1B873593;
hash ^= k1;
break;
}

// Final mixing ritual for the hash.
hash = MixHashV2(hash, len);

return hash;
}

/// <summary>
/// Implements the murmur hash 3 algorithm, using a mock UTF-8 encoding.
/// The UTF-8 conversion ignores the possibilities of unicode planes other than the 0th.
Expand Down Expand Up @@ -284,6 +366,18 @@ public static uint MixHash(uint hash)
return hash;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint MixHashV2(uint hash, int len)
{
hash ^= (uint)len;
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint Rotate(uint x, int r)
{
Expand Down
Loading