Change immobile space free pointers to alien vars
[sbcl.git] / src / runtime / murmur_hash.c
blob6023abd3e5d0e9def81a1da6a5a7ea869b1fb679
1 /*
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "murmur_hash.h"
36 #define ROTL32(x, r) ((x) << (r)) | ((x) >> (32 - (r)))
38 #define FMIX32(h) \
39 (h) ^= (h) >> 16; \
40 (h) *= 0x85ebca6b; \
41 (h) ^= (h) >> 13; \
42 (h) *= 0xc2b2ae35; \
43 (h) ^= (h) >> 16;
45 /* Block read - if your platform needs to do endian-swapping or can only
46 handle aligned reads, do the conversion here */
47 #define GETBLOCK32(p, i) (p)[(i)]
49 uint32_t gpr_murmur_hash3(const void *key, size_t len, uint32_t seed) {
50 const uint8_t *data = (const uint8_t *)key;
51 const size_t nblocks = len / 4;
52 int i;
54 uint32_t h1 = seed;
55 uint32_t k1;
57 const uint32_t c1 = 0xcc9e2d51;
58 const uint32_t c2 = 0x1b873593;
60 const uint32_t *blocks = ((const uint32_t *)key) + nblocks;
61 const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);
63 /* body */
64 for (i = -(int)nblocks; i; i++) {
65 k1 = GETBLOCK32(blocks, i);
67 k1 *= c1;
68 k1 = ROTL32(k1, 15);
69 k1 *= c2;
71 h1 ^= k1;
72 h1 = ROTL32(h1, 13);
73 h1 = h1 * 5 + 0xe6546b64;
76 k1 = 0;
78 /* tail */
79 switch (len & 3) {
80 case 3:
81 k1 ^= ((uint32_t)tail[2]) << 16;
82 case 2:
83 k1 ^= ((uint32_t)tail[1]) << 8;
84 case 1:
85 k1 ^= tail[0];
86 k1 *= c1;
87 k1 = ROTL32(k1, 15);
88 k1 *= c2;
89 h1 ^= k1;
92 /* finalization */
93 h1 ^= (uint32_t)len;
94 FMIX32(h1);
95 return h1;