-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxxhash32-ref.c
199 lines (173 loc) · 7.07 KB
/
xxhash32-ref.c
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* xxHash - Fast Hash algorithm
* Copyright (C) 2012-2020 Yann Collet
* Copyright (C) 2019-2020 Devin Hussey (easyaspi314)
*
* BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You can contact the author at :
* - xxHash homepage: http://www.xxhash.com
* - xxHash source repository : https://github.com/Cyan4973/xxHash */
/* This is a compact, 100% standalone reference XXH32 single-run implementation.
* Instead of focusing on performance hacks, this focuses on cleanliness,
* conformance, portability and simplicity.
*
* This file aims to be 100% compatible with C90/C++98, with the additional
* requirement of stdint.h. No library functions are used. */
#include <stddef.h> /* size_t, NULL */
#include <stdint.h> /* uint8_t, uint32_t */
#ifdef __cplusplus
extern "C"
#endif
uint32_t XXH32(void const *const input, size_t const length, uint32_t const seed);
static uint32_t const PRIME32_1 = 0x9E3779B1U; /* 0b10011110001101110111100110110001 */
static uint32_t const PRIME32_2 = 0x85EBCA77U; /* 0b10000101111010111100101001110111 */
static uint32_t const PRIME32_3 = 0xC2B2AE3DU; /* 0b11000010101100101010111000111101 */
static uint32_t const PRIME32_4 = 0x27D4EB2FU; /* 0b00100111110101001110101100101111 */
static uint32_t const PRIME32_5 = 0x165667B1U; /* 0b00010110010101100110011110110001 */
/* Rotates value left by amt. */
static uint32_t XXH_rotl32(uint32_t const value, uint32_t const amt)
{
return (value << (amt % 32)) | (value >> (32 - (amt % 32)));
}
/* Portably reads a 32-bit little endian integer from data at the given offset. */
static uint32_t XXH_read32(uint8_t const *const data, size_t const offset)
{
return (uint32_t) data[offset + 0]
| ((uint32_t) data[offset + 1] << 8)
| ((uint32_t) data[offset + 2] << 16)
| ((uint32_t) data[offset + 3] << 24);
}
/* Mixes input into acc. */
static uint32_t XXH32_round(uint32_t acc, uint32_t const input)
{
acc += input * PRIME32_2;
acc = XXH_rotl32(acc, 13);
acc *= PRIME32_1;
return acc;
}
/* Mixes all bits to finalize the hash. */
static uint32_t XXH32_avalanche(uint32_t hash)
{
hash ^= hash >> 15;
hash *= PRIME32_2;
hash ^= hash >> 13;
hash *= PRIME32_3;
hash ^= hash >> 16;
return hash;
}
/* The XXH32 hash function.
* input: The data to hash.
* length: The length of input. It is undefined behavior to have length larger than the
* capacity of input.
* seed: A 32-bit value to seed the hash with.
* returns: The 32-bit calculated hash value. */
uint32_t XXH32(void const *const input, size_t const length, uint32_t const seed)
{
uint8_t const *const data = (uint8_t const *) input;
uint32_t hash;
size_t remaining = length;
size_t offset = 0;
/* Don't dereference a null pointer. The reference implementation notably doesn't
* check for this by default. */
if (input == NULL) {
return XXH32_avalanche(seed + PRIME32_5);
}
if (remaining >= 16) {
/* Initialize our accumulators */
uint32_t acc1 = seed + PRIME32_1 + PRIME32_2;
uint32_t acc2 = seed + PRIME32_2;
uint32_t acc3 = seed + 0;
uint32_t acc4 = seed - PRIME32_1;
while (remaining >= 16) {
acc1 = XXH32_round(acc1, XXH_read32(data, offset)); offset += 4;
acc2 = XXH32_round(acc2, XXH_read32(data, offset)); offset += 4;
acc3 = XXH32_round(acc3, XXH_read32(data, offset)); offset += 4;
acc4 = XXH32_round(acc4, XXH_read32(data, offset)); offset += 4;
remaining -= 16;
}
hash = XXH_rotl32(acc1, 1) + XXH_rotl32(acc2, 7) + XXH_rotl32(acc3, 12) + XXH_rotl32(acc4, 18);
} else {
/* Not enough data for the main loop, put something in there instead. */
hash = seed + PRIME32_5;
}
hash += (uint32_t) length;
/* Process the remaining data. */
while (remaining >= 4) {
hash += XXH_read32(data, offset) * PRIME32_3;
hash = XXH_rotl32(hash, 17);
hash *= PRIME32_4;
offset += 4;
remaining -= 4;
}
while (remaining != 0) {
hash += (uint32_t) data[offset] * PRIME32_5;
hash = XXH_rotl32(hash, 11);
hash *= PRIME32_1;
--remaining;
++offset;
}
return XXH32_avalanche(hash);
}
#ifdef XXH_SELFTEST
#include <stdio.h> /* fprintf, puts */
#include <stdlib.h> /* exit */
#define TEST_DATA_SIZE 101
static int test_num = 0;
/* Checks a hash value. */
static void test_sequence(uint8_t const *const test_data, size_t const length,
uint32_t const seed, uint32_t const expected)
{
uint32_t const result = XXH32(test_data, length, seed);
if (result != expected) {
fprintf(stderr, "Error: Test %i: XXH32 test failed!\n", ++test_num);
fprintf(stderr, "Expected value: 0x%08X. Actual value: 0x%08X.\n", expected, result);
exit(1);
}
}
int main(void)
{
uint32_t const prime = PRIME32_1;
uint8_t test_data[TEST_DATA_SIZE] = {0};
uint32_t byte_gen = prime;
int i = 0;
/* Fill the test_data buffer with "random" data */
for (; i < TEST_DATA_SIZE; i++) {
test_data[i] = (uint8_t) (byte_gen >> 24);
byte_gen *= byte_gen;
}
test_sequence(NULL , 0 , 0 , 0x02CC5D05U);
test_sequence(NULL , 0 , prime, 0x36B78AE7U);
test_sequence(test_data, 1 , 0 , 0xB85CBEE5U);
test_sequence(test_data, 1 , prime, 0xD5845D64U);
test_sequence(test_data, 14 , 0 , 0xE5AA0AB4U);
test_sequence(test_data, 14 , prime, 0x4481951DU);
test_sequence(test_data, TEST_DATA_SIZE, 0, 0x1F1AA412U);
test_sequence(test_data, TEST_DATA_SIZE, prime, 0x498EC8E2U);
puts("XXH32 reference implementation: OK");
return 0;
}
#endif /* XXH_SELFTEST */