summaryrefslogtreecommitdiff
path: root/compiler/hash.c
blob: a63691c71f0d94aba54139facd557e5e84776e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * 32-bit FNV-1a hash function.
 * Copyright (c) 2023-2024, Quinn Stephens and the OSMORA team.
 * Provided under the BSD 3-Clause license.
 */

#include "hash.h"

hash_t
hash_data(const void *data, size_t length)
{
    hash_t hash;

    hash = FNV_OFFSET_BASIS;
    for (size_t i = 0; i < length; i++) {
        hash ^= ((uint8_t*)data)[i];
        hash *= FNV_PRIME;
    }

    return hash;
}

hash_t
hash_string(const char *str)
{
    hash_t hash;

    hash = FNV_OFFSET_BASIS;
    while (*str) {
        hash ^= (uint8_t)*str++;
        hash *= FNV_PRIME;
    }

    return hash;
}