-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hash function to generate stable table types (#943)
See: https://gaiaplatform.atlassian.net/browse/GAIAPLAT-1389 for details on this workitem. Some unit tests are updated due to the change of type generation algorithms. We can no longer assume type 1 or 2 always exists.
- Loading branch information
Showing
6 changed files
with
170 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
///////////////////////////////////////////// | ||
// Copyright (c) Gaia Platform LLC | ||
// All rights reserved. | ||
///////////////////////////////////////////// | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <cstring> | ||
|
||
// Adapted from the public domain murmur3 hash implementation at: | ||
// https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp | ||
uint32_t murmurhash3_x86_32(const void* key, int len) | ||
{ | ||
const auto* data = static_cast<const uint8_t*>(key); | ||
const int nblocks = len / 4; | ||
|
||
uint32_t h1 = len; | ||
|
||
const uint32_t c1 = 0xcc9e2d51; | ||
const uint32_t c2 = 0x1b873593; | ||
|
||
//---------- | ||
// body | ||
|
||
const auto* blocks = reinterpret_cast<const uint32_t*>(data + nblocks * 4); | ||
|
||
for (int i = -nblocks; i; i++) | ||
{ | ||
uint32_t k1; | ||
std::memcpy(&k1, (blocks + i), sizeof(k1)); | ||
|
||
k1 *= c1; | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
k1 = (k1 << 15) | (k1 >> (32 - 15)); | ||
k1 *= c2; | ||
|
||
h1 ^= k1; | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
h1 = (k1 << 13) | (k1 >> (32 - 13)); | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
h1 = h1 * 5 + 0xe6546b64; | ||
} | ||
|
||
//---------- | ||
// tail | ||
|
||
const auto* tail = static_cast<const uint8_t*>(data + nblocks * 4); | ||
|
||
uint32_t k1 = 0; | ||
|
||
switch (len & 3) | ||
{ | ||
case 3: | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
k1 ^= tail[2] << 16; | ||
case 2: | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
k1 ^= tail[1] << 8; | ||
case 1: | ||
k1 ^= tail[0]; | ||
k1 *= c1; | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
k1 = (k1 << 15) | (k1 >> (32 - 15)); | ||
k1 *= c2; | ||
h1 ^= k1; | ||
}; | ||
|
||
//---------- | ||
// finalization | ||
|
||
h1 ^= len; | ||
|
||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
h1 ^= h1 >> 16; | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
h1 *= 0x85ebca6b; | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
h1 ^= h1 >> 13; | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
h1 *= 0xc2b2ae35; | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) | ||
h1 ^= h1 >> 16; | ||
|
||
return h1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters