Add FNV-1a hash functions (#12502)
This commit is contained in:
1 parent
24d7e9dd23
commit
839139df36
4 files changed
+158
-2
No files matched your search
@@ -143,17 +143,29 @@ uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly,
|
||||
return refout ? (crc ^ 0xffff) : crc;
|
||||
}
|
||||
|
||||
// FNV-1 hash - deprecated, use fnv1a_hash() for new code
|
||||
uint32_t fnv1_hash(const char *str) {
|
||||
uint32_t hash = 2166136261UL;
|
||||
uint32_t hash = FNV1_OFFSET_BASIS;
|
||||
if (str) {
|
||||
while (*str) {
|
||||
hash *= 16777619UL;
|
||||
hash *= FNV1_PRIME;
|
||||
hash ^= *str++;
|
||||
}
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
// FNV-1a hash - preferred for new code
|
||||
uint32_t fnv1a_hash_extend(uint32_t hash, const char *str) {
|
||||
if (str) {
|
||||
while (*str) {
|
||||
hash ^= *str++;
|
||||
hash *= FNV1_PRIME;
|
||||
}
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
float random_float() { return static_cast<float>(random_uint32()) / static_cast<float>(UINT32_MAX); }
|
||||
|
||||
// Strings
|
||||
|
||||
Reference in new issue
Block a user