libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

libutil/src/fnv.c (37 lines, 1.1 KiB) -rw-r--r-- file download

8a5f06f Jamozed 2022-02-09 20:04:50
0
// util/fnv.c, version 1.0.2
98e5939 Jamozed 2021-11-27 17:53:46
1
// FNV hash source file from libutil
12c01d4 Jamozed 2021-09-04 14:07:48
2
// Copyright (C) 2021, Jakob Wakeling
7f427d9 Jamozed 2022-03-06 12:55:13
3
// MIT Licence
12c01d4 Jamozed 2021-09-04 14:07:48
4
12c01d4 Jamozed 2021-09-04 14:07:48
5
#include "fnv.h"
7d31e3a Jamozed 2021-12-08 13:59:42
6
#include "util.h"
12c01d4 Jamozed 2021-09-04 14:07:48
7
7d31e3a Jamozed 2021-12-08 13:59:42
8
static const u32 FNV_PRIME_32 = 0x01000193;
7d31e3a Jamozed 2021-12-08 13:59:42
9
static const u32 FNV_BASIS_32 = 0x811C9DC5;
7d31e3a Jamozed 2021-12-08 13:59:42
10
static const u64 FNV_PRIME_64 = 0x00000100000001B3;
7d31e3a Jamozed 2021-12-08 13:59:42
11
static const u64 FNV_BASIS_64 = 0xCBF29CE484222325;
12c01d4 Jamozed 2021-09-04 14:07:48
12
12c01d4 Jamozed 2021-09-04 14:07:48
13
/* Compute the FNV1a-32 hash of some data. */
7d31e3a Jamozed 2021-12-08 13:59:42
14
u32 fnv1a32(const char *dat, UINT len) {
7d31e3a Jamozed 2021-12-08 13:59:42
15
	register u32 fnv = FNV_BASIS_32;
7d31e3a Jamozed 2021-12-08 13:59:42
16
	for (; len; len -= 1, dat += 1) { fnv ^= *dat; fnv *= FNV_PRIME_32; }
7d31e3a Jamozed 2021-12-08 13:59:42
17
	return fnv;
12c01d4 Jamozed 2021-09-04 14:07:48
18
}
12c01d4 Jamozed 2021-09-04 14:07:48
19
12c01d4 Jamozed 2021-09-04 14:07:48
20
/* Compute the FNV1a-64 hash of some data. */
7d31e3a Jamozed 2021-12-08 13:59:42
21
u64 fnv1a64(const char *dat, UINT len) {
7d31e3a Jamozed 2021-12-08 13:59:42
22
	register u64 fnv = FNV_BASIS_64;
7d31e3a Jamozed 2021-12-08 13:59:42
23
	for (; len; len -= 1, dat += 1) { fnv ^= *dat; fnv *= FNV_PRIME_64; }
7d31e3a Jamozed 2021-12-08 13:59:42
24
	return fnv;
12c01d4 Jamozed 2021-09-04 14:07:48
25
}
12c01d4 Jamozed 2021-09-04 14:07:48
26
7d31e3a Jamozed 2021-12-08 13:59:42
27
void fnv1a32_init(u32 *ctx) { *ctx = FNV_BASIS_32; }
7d31e3a Jamozed 2021-12-08 13:59:42
28
void fnv1a32_hash(u32 *ctx, char *dat, UINT len) {
7d31e3a Jamozed 2021-12-08 13:59:42
29
	for (; len; len -= 1, dat += 1) { *ctx ^= *dat; *ctx *= FNV_PRIME_32; }
12c01d4 Jamozed 2021-09-04 14:07:48
30
}
12c01d4 Jamozed 2021-09-04 14:07:48
31
7d31e3a Jamozed 2021-12-08 13:59:42
32
void fnv1a64_init(u64 *ctx) { *ctx = FNV_BASIS_64; }
7d31e3a Jamozed 2021-12-08 13:59:42
33
void fnv1a64_hash(u64 *ctx, char *dat, UINT len) {
7d31e3a Jamozed 2021-12-08 13:59:42
34
	for (; len; len -= 1, dat += 1) { *ctx ^= *dat; *ctx *= FNV_PRIME_64; }
12c01d4 Jamozed 2021-09-04 14:07:48
35
}
36