Branch data Line data Source code
1 : : // metrohash64.cpp
2 : : //
3 : : // The MIT License (MIT)
4 : : //
5 : : // Copyright (c) 2015 J. Andrew Rogers
6 : : //
7 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
8 : : // of this software and associated documentation files (the "Software"), to deal
9 : : // in the Software without restriction, including without limitation the rights
10 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 : : // copies of the Software, and to permit persons to whom the Software is
12 : : // furnished to do so, subject to the following conditions:
13 : : //
14 : : // The above copyright notice and this permission notice shall be included in all
15 : : // copies or substantial portions of the Software.
16 : : //
17 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 : : // SOFTWARE.
24 : : //
25 : :
26 : : #include "cmetrohash.h"
27 : :
28 : :
29 : 3677 : void cmetrohash64_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out)
30 : : {
31 : : static const uint64_t k0 = 0xC83A91E1;
32 : : static const uint64_t k1 = 0x8648DBDB;
33 : : static const uint64_t k2 = 0x7BDEC03B;
34 : : static const uint64_t k3 = 0x2F5870A5;
35 : :
36 : : const uint8_t * ptr = key;
37 : 3677 : const uint8_t * const end = ptr + len;
38 : :
39 : 3677 : uint64_t hash = ((((uint64_t) seed) + k2) * k0) + len;
40 : :
41 [ - + ]: 3677 : if (len >= 32)
42 : : {
43 : : uint64_t v[4];
44 : : v[0] = hash;
45 : : v[1] = hash;
46 : : v[2] = hash;
47 : : v[3] = hash;
48 : :
49 : : do
50 : : {
51 : 0 : v[0] += cread_u64(ptr) * k0; ptr += 8; v[0] = crotate_right(v[0],29) + v[2];
52 : 0 : v[1] += cread_u64(ptr) * k1; ptr += 8; v[1] = crotate_right(v[1],29) + v[3];
53 : 0 : v[2] += cread_u64(ptr) * k2; ptr += 8; v[2] = crotate_right(v[2],29) + v[0];
54 : 0 : v[3] += cread_u64(ptr) * k3; ptr += 8; v[3] = crotate_right(v[3],29) + v[1];
55 : : }
56 [ # # ]: 0 : while (ptr <= (end - 32));
57 : :
58 : 0 : v[2] ^= crotate_right(((v[0] + v[3]) * k0) + v[1], 33) * k1;
59 : 0 : v[3] ^= crotate_right(((v[1] + v[2]) * k1) + v[0], 33) * k0;
60 : 0 : v[0] ^= crotate_right(((v[0] + v[2]) * k0) + v[3], 33) * k1;
61 : 0 : v[1] ^= crotate_right(((v[1] + v[3]) * k1) + v[2], 33) * k0;
62 : 0 : hash += v[0] ^ v[1];
63 : : }
64 : :
65 [ + + ]: 3677 : if ((end - ptr) >= 16)
66 : : {
67 : : uint64_t v0, v1;
68 : 768 : v0 = hash + (cread_u64(ptr) * k0); ptr += 8; v0 = crotate_right(v0,33) * k1;
69 : 768 : v1 = hash + (cread_u64(ptr) * k1); ptr += 8; v1 = crotate_right(v1,33) * k2;
70 : 768 : v0 ^= crotate_right(v0 * k0, 35) + v1;
71 : 768 : v1 ^= crotate_right(v1 * k3, 35) + v0;
72 : 384 : hash += v1;
73 : : }
74 : :
75 [ + + ]: 3677 : if ((end - ptr) >= 8)
76 : : {
77 : 1528 : hash += cread_u64(ptr) * k3; ptr += 8;
78 : 1528 : hash ^= crotate_right(hash, 33) * k1;
79 : :
80 : : }
81 : :
82 [ + + ]: 3677 : if ((end - ptr) >= 4)
83 : : {
84 : 1402 : hash += cread_u32(ptr) * k3; ptr += 4;
85 : 1402 : hash ^= crotate_right(hash, 15) * k1;
86 : : }
87 : :
88 [ + + ]: 3677 : if ((end - ptr) >= 2)
89 : : {
90 : 1352 : hash += cread_u16(ptr) * k3; ptr += 2;
91 : 1352 : hash ^= crotate_right(hash, 13) * k1;
92 : : }
93 : :
94 [ + + ]: 3677 : if ((end - ptr) >= 1)
95 : : {
96 : 1642 : hash += cread_u8 (ptr) * k3;
97 : 1642 : hash ^= crotate_right(hash, 25) * k1;
98 : : }
99 : :
100 : 3677 : hash ^= crotate_right(hash, 33);
101 : 3677 : hash *= k0;
102 : 3677 : hash ^= crotate_right(hash, 33);
103 : :
104 : 3677 : memcpy(out, &hash, 8);
105 : 3677 : }
106 : :
107 : :
108 : 0 : void cmetrohash64_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out)
109 : : {
110 : : static const uint64_t k0 = 0xD6D018F5;
111 : : static const uint64_t k1 = 0xA2AA033B;
112 : : static const uint64_t k2 = 0x62992FC1;
113 : : static const uint64_t k3 = 0x30BC5B29;
114 : :
115 : : const uint8_t * ptr = key;
116 : 0 : const uint8_t * const end = ptr + len;
117 : :
118 : 0 : uint64_t hash = ((((uint64_t) seed) + k2) * k0) + len;
119 : :
120 [ # # ]: 0 : if (len >= 32)
121 : : {
122 : : uint64_t v[4];
123 : : v[0] = hash;
124 : : v[1] = hash;
125 : : v[2] = hash;
126 : : v[3] = hash;
127 : :
128 : : do
129 : : {
130 : 0 : v[0] += cread_u64(ptr) * k0; ptr += 8; v[0] = crotate_right(v[0],29) + v[2];
131 : 0 : v[1] += cread_u64(ptr) * k1; ptr += 8; v[1] = crotate_right(v[1],29) + v[3];
132 : 0 : v[2] += cread_u64(ptr) * k2; ptr += 8; v[2] = crotate_right(v[2],29) + v[0];
133 : 0 : v[3] += cread_u64(ptr) * k3; ptr += 8; v[3] = crotate_right(v[3],29) + v[1];
134 : : }
135 [ # # ]: 0 : while (ptr <= (end - 32));
136 : :
137 : 0 : v[2] ^= crotate_right(((v[0] + v[3]) * k0) + v[1], 30) * k1;
138 : 0 : v[3] ^= crotate_right(((v[1] + v[2]) * k1) + v[0], 30) * k0;
139 : 0 : v[0] ^= crotate_right(((v[0] + v[2]) * k0) + v[3], 30) * k1;
140 : 0 : v[1] ^= crotate_right(((v[1] + v[3]) * k1) + v[2], 30) * k0;
141 : 0 : hash += v[0] ^ v[1];
142 : : }
143 : :
144 [ # # ]: 0 : if ((end - ptr) >= 16)
145 : : {
146 : : uint64_t v0, v1;
147 : 0 : v0 = hash + (cread_u64(ptr) * k2); ptr += 8; v0 = crotate_right(v0,29) * k3;
148 : 0 : v1 = hash + (cread_u64(ptr) * k2); ptr += 8; v1 = crotate_right(v1,29) * k3;
149 : 0 : v0 ^= crotate_right(v0 * k0, 34) + v1;
150 : 0 : v1 ^= crotate_right(v1 * k3, 34) + v0;
151 : 0 : hash += v1;
152 : : }
153 : :
154 [ # # ]: 0 : if ((end - ptr) >= 8)
155 : : {
156 : 0 : hash += cread_u64(ptr) * k3; ptr += 8;
157 : 0 : hash ^= crotate_right(hash, 36) * k1;
158 : : }
159 : :
160 [ # # ]: 0 : if ((end - ptr) >= 4)
161 : : {
162 : 0 : hash += cread_u32(ptr) * k3; ptr += 4;
163 : 0 : hash ^= crotate_right(hash, 15) * k1;
164 : : }
165 : :
166 [ # # ]: 0 : if ((end - ptr) >= 2)
167 : : {
168 : 0 : hash += cread_u16(ptr) * k3; ptr += 2;
169 : 0 : hash ^= crotate_right(hash, 15) * k1;
170 : : }
171 : :
172 [ # # ]: 0 : if ((end - ptr) >= 1)
173 : : {
174 : 0 : hash += cread_u8 (ptr) * k3;
175 : 0 : hash ^= crotate_right(hash, 23) * k1;
176 : : }
177 : :
178 : 0 : hash ^= crotate_right(hash, 28);
179 : 0 : hash *= k0;
180 : 0 : hash ^= crotate_right(hash, 29);
181 : :
182 : 0 : memcpy(out, &hash, 8);
183 : 0 : }
184 : :
185 : :
|