Implement OBLISTs
[muddle-interpreter.git] / src / hash.h
1 /*
2 Copyright (C) 2018 Keziah Wesley
3
4 You can redistribute and/or modify this file under the terms of the
5 GNU Affero General Public License as published by the Free Software
6 Foundation, either version 3 of the License, or (at your option) any
7 later version.
8
9 This file is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Affero General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public
15 License along with this file. If not, see
16 <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef HASH_H
20 #define HASH_H
21
22 #include <stddef.h>
23
24 // Very fast, non collision-resistant hash
25
26 inline static uint32_t
27 fnv_32a_init ()
28 {
29   return 0x811c9dc5;
30 }
31
32 inline static uint32_t
33 fnv_32a_extend (const void *buf, size_t len, uint32_t hval)
34 {
35   unsigned char *bp = (unsigned char *) buf;
36   unsigned char *be = bp + len;
37   while (bp < be)
38     {
39       hval ^= (uint32_t) * bp++;
40       /* multiply by the 32 bit FNV magic prime mod 2^32 */
41       hval +=
42         (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
43     }
44   return hval;
45 }
46
47 #endif // HASH_H