diff --git a/City.cpp b/City.cpp index 8c48198f..f729ec32 100644 --- a/City.cpp +++ b/City.cpp @@ -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(len); - + uint32_t h = static_cast(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(s[i]); @@ -179,23 +178,23 @@ static uint32_t Hash32Len0to4(const char *s, size_t len) { return fmix(Mur(b, Mur(static_cast(len), c))); } -static uint32_t Hash32Len5to12(const char *s, size_t len) { - uint32_t a = static_cast(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(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(len), g = c1 * h, f = g; + uint32_t h = static_cast(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; diff --git a/City.h b/City.h index 9143902a..310f4198 100644 --- a/City.h +++ b/City.h @@ -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 diff --git a/CityTest.cpp b/CityTest.cpp index 84ce8f47..d2901067 100644 --- a/CityTest.cpp +++ b/CityTest.cpp @@ -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) { diff --git a/main.cpp b/main.cpp index fba5920d..f483ec56 100644 --- a/main.cpp +++ b/main.cpp @@ -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, {} },