const uint32_t FNV_32_HASH_START = 2166136261UL;
const uint64_t FNV_64_HASH_START = 14695981039346656037ULL;
-inline uint32_t fnv32(const char* s,
- uint32_t hash = FNV_32_HASH_START) {
+inline uint32_t fnv32(const char* buf, uint32_t hash = FNV_32_HASH_START) {
+ // forcing signed char, since other platforms can use unsigned
+ const signed char* s = reinterpret_cast<const signed char*>(buf);
+
for (; *s; ++s) {
hash += (hash << 1) + (hash << 4) + (hash << 7) +
(hash << 8) + (hash << 24);
return fnv32_buf(str.data(), str.size(), hash);
}
-inline uint64_t fnv64(const char* s,
- uint64_t hash = FNV_64_HASH_START) {
+inline uint64_t fnv64(const char* buf, uint64_t hash = FNV_64_HASH_START) {
+ // forcing signed char, since other platforms can use unsigned
+ const signed char* s = reinterpret_cast<const signed char*>(buf);
+
for (; *s; ++s) {
hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) +
(hash << 8) + (hash << 40);
const uint32_t s3_res = 2166136261UL;
EXPECT_EQ(fnv32(s3), s3_res);
EXPECT_EQ(fnv32(s3), fnv32_buf(s3, strlen(s3)));
+
+ const uint8_t s4_data[] = {0xFF, 0xFF, 0xFF, 0x00};
+ const char* s4 = reinterpret_cast<const char*>(s4_data);
+ const uint32_t s4_res = 2420936562UL;
+ EXPECT_EQ(fnv32(s4), s4_res);
+ EXPECT_EQ(fnv32(s4), fnv32_buf(s4, strlen(s4)));
}
TEST(Hash, Fnv64) {
EXPECT_EQ(fnv64(s3), s3_res);
EXPECT_EQ(fnv64(s3), fnv64_buf(s3, strlen(s3)));
+ const uint8_t s4_data[] = {0xFF, 0xFF, 0xFF, 0x00};
+ const char* s4 = reinterpret_cast<const char*>(s4_data);
+ const uint64_t s4_res = 2787597222566293202ULL;
+ EXPECT_EQ(fnv64(s4), s4_res);
+ EXPECT_EQ(fnv64(s4), fnv64_buf(s4, strlen(s4)));
+
// note: Use fnv64_buf to make a single hash value from multiple
// fields/datatypes.
const char* t4_a = "E Pluribus";