libcss
Loading...
Searching...
No Matches
arena_hash.h
Go to the documentation of this file.
1/*
2 * This file is part of LibCSS
3 * Licensed under the MIT License,
4 * http://www.opensource.org/licenses/mit-license.php
5 *
6 * Copyright 2015 Michael Drake <tlsa@netsurf-browser.org>
7 */
8
9#ifndef css_select_arena_hash_h_
10#define css_select_arena_hash_h_
11
12#include <stdint.h>
13
22static inline uint32_t css__arena_hash(const uint8_t *data, size_t len)
23{
24 /* Hashing constants */
25 const uint32_t m = 0x5bd1e995;
26 const int r = 24;
27
28 /* Start with length */
29 uint32_t h = len;
30
31 /* Hash four bytes at a time */
32 while (len >= 4) {
33 /* If we could ensure 4-byte alignment of the input, this
34 * could be faster. */
35 uint32_t k =
36 (((uint32_t) data[0]) ) |
37 (((uint32_t) data[1]) << 8) |
38 (((uint32_t) data[2]) << 16) |
39 (((uint32_t) data[3]) << 24);
40
41 k *= m;
42 k ^= k >> r;
43 k *= m;
44 h *= m;
45 h ^= k;
46 data += 4;
47 len -= 4;
48 }
49
50 /* Hash any left over bytes */
51 switch (len) {
52 case 3: h ^= data[2] << 16; /* Fall through */
53 case 2: h ^= data[1] << 8; /* Fall through */
54 case 1: h ^= data[0];
55 h *= m;
56 }
57
58 /* Finalise */
59 h ^= h >> 13;
60 h *= m;
61 h ^= h >> 15;
62
63 return h;
64}
65
66#endif
67