Skip to content

Commit

Permalink
Adds seed parameter to CityHash32.
Browse files Browse the repository at this point in the history
- city32's known bad seed is added back to g_hashes.
  • Loading branch information
SuperioOne authored and rurban committed Jun 20, 2024
1 parent 6d21a32 commit 5efdfb7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
21 changes: 10 additions & 11 deletions City.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,19 @@ static uint32_t Mur(uint32_t a, uint32_t h) {
return h * 5 + 0xe6546b64;
}

static uint32_t Hash32Len13to24(const char *s, size_t len) {
static uint32_t Hash32Len13to24(const char *s, size_t len, uint32_t seed) {
uint32_t a = Fetch32(s - 4 + (len >> 1));
uint32_t b = Fetch32(s + 4);
uint32_t c = Fetch32(s + len - 8);
uint32_t d = Fetch32(s + (len >> 1));
uint32_t e = Fetch32(s);
uint32_t f = Fetch32(s + len - 4);
uint32_t h = static_cast<uint32_t>(len);

uint32_t h = static_cast<uint32_t>(len + seed);
return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
}

static uint32_t Hash32Len0to4(const char *s, size_t len) {
uint32_t b = 0;
static uint32_t Hash32Len0to4(const char *s, size_t len, uint32_t seed) {
uint32_t b = seed;
uint32_t c = 9;
for (size_t i = 0; i < len; i++) {
signed char v = static_cast<signed char>(s[i]);
Expand All @@ -179,23 +178,23 @@ static uint32_t Hash32Len0to4(const char *s, size_t len) {
return fmix(Mur(b, Mur(static_cast<uint32_t>(len), c)));
}

static uint32_t Hash32Len5to12(const char *s, size_t len) {
uint32_t a = static_cast<uint32_t>(len), b = a * 5, c = 9, d = b;
static uint32_t Hash32Len5to12(const char *s, size_t len, uint32_t seed) {
uint32_t a = static_cast<uint32_t>(len + seed), b = a * 5, c = 9, d = b;
a += Fetch32(s);
b += Fetch32(s + len - 4);
c += Fetch32(s + ((len >> 1) & 4));
return fmix(Mur(c, Mur(b, Mur(a, d))));
}

uint32_t CityHash32(const char *s, size_t len) {
uint32_t CityHash32WithSeed(const char *s, size_t len, uint32_t seed) {
if (len <= 24) {
return len <= 12
? (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len))
: Hash32Len13to24(s, len);
? (len <= 4 ? Hash32Len0to4(s, len, seed) : Hash32Len5to12(s, len, seed))
: Hash32Len13to24(s, len, seed);
}

// len > 24
uint32_t h = static_cast<uint32_t>(len), g = c1 * h, f = g;
uint32_t h = static_cast<uint32_t>(len + seed), g = c1 * h, f = g;
uint32_t a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
uint32_t a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
uint32_t a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
Expand Down
2 changes: 1 addition & 1 deletion City.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ inline uint64_t Uint128Low64(const uint128 &x) { return x.first; }
inline uint64_t Uint128High64(const uint128 &x) { return x.second; }

// Hash functions for a byte array.
uint32_t CityHash32(const char *buf, size_t len);
uint32_t CityHash32WithSeed(const char *buf, size_t len, uint32_t seed);
uint64_t CityHash64(const char *buf, size_t len);

// Hash function for a byte array. For convenience, a 64-bit seed is also
Expand Down
3 changes: 1 addition & 2 deletions CityTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

void CityHash32_test(const void *key, int len, uint32_t seed, void *out) {
// objsize 0-527: 1319
*(uint32_t *)out = CityHash32((const char *)key, len);
(void)seed;
*(uint32_t *)out = CityHash32WithSeed((const char *)key, len, seed);
}

void CityHash64_test(const void *key, int len, uint32_t seed, void *out) {
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ HashInfo g_hashes[] =
# endif
# endif
#endif
{ CityHash32_test, 32, 0x68254F81, "City32", "Google CityHash32 (v1.1)", POOR, {} /* !! */},
{ CityHash32_test, 32, 0xEDED9084, "City32", "Google CityHash32WithSeed (v1.1)", POOR, {0x2eb38c9f} /* !! */},
#ifdef HAVE_INT64
{ metrohash64_test, 64, 0x6FA828C9, "metrohash64", "MetroHash64, 64-bit", POOR, {} },
{ metrohash64_1_test, 64, 0xEE88F7D2, "metrohash64_1", "MetroHash64_1, 64-bit (legacy)", POOR, {} },
Expand Down

0 comments on commit 5efdfb7

Please sign in to comment.