0123456789101112131415161718192021222324252627282930313233343536
|
// util/fnv.c, version 1.0.2
// FNV hash source file from libutil
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence
#include "fnv.h"
#include "util.h"
static const u32 FNV_PRIME_32 = 0x01000193;
static const u32 FNV_BASIS_32 = 0x811C9DC5;
static const u64 FNV_PRIME_64 = 0x00000100000001B3;
static const u64 FNV_BASIS_64 = 0xCBF29CE484222325;
/* Compute the FNV1a-32 hash of some data. */
u32 fnv1a32(const char *dat, UINT len) {
register u32 fnv = FNV_BASIS_32;
for (; len; len -= 1, dat += 1) { fnv ^= *dat; fnv *= FNV_PRIME_32; }
return fnv;
}
/* Compute the FNV1a-64 hash of some data. */
u64 fnv1a64(const char *dat, UINT len) {
register u64 fnv = FNV_BASIS_64;
for (; len; len -= 1, dat += 1) { fnv ^= *dat; fnv *= FNV_PRIME_64; }
return fnv;
}
void fnv1a32_init(u32 *ctx) { *ctx = FNV_BASIS_32; }
void fnv1a32_hash(u32 *ctx, char *dat, UINT len) {
for (; len; len -= 1, dat += 1) { *ctx ^= *dat; *ctx *= FNV_PRIME_32; }
}
void fnv1a64_init(u64 *ctx) { *ctx = FNV_BASIS_64; }
void fnv1a64_hash(u64 *ctx, char *dat, UINT len) {
for (; len; len -= 1, dat += 1) { *ctx ^= *dat; *ctx *= FNV_PRIME_64; }
}
|