1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * xxHash - Extremely Fast Hash algorithm
3*4882a593Smuzhiyun * Copyright (C) 2012-2016, Yann Collet.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
8*4882a593Smuzhiyun * modification, are permitted provided that the following conditions are
9*4882a593Smuzhiyun * met:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * * Redistributions of source code must retain the above copyright
12*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
13*4882a593Smuzhiyun * * Redistributions in binary form must reproduce the above
14*4882a593Smuzhiyun * copyright notice, this list of conditions and the following disclaimer
15*4882a593Smuzhiyun * in the documentation and/or other materials provided with the
16*4882a593Smuzhiyun * distribution.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify it under
31*4882a593Smuzhiyun * the terms of the GNU General Public License version 2 as published by the
32*4882a593Smuzhiyun * Free Software Foundation. This program is dual-licensed; you may select
33*4882a593Smuzhiyun * either version 2 of the GNU General Public License ("GPL") or BSD license
34*4882a593Smuzhiyun * ("BSD").
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * You can contact the author at:
37*4882a593Smuzhiyun * - xxHash homepage: https://cyan4973.github.io/xxHash/
38*4882a593Smuzhiyun * - xxHash source repository: https://github.com/Cyan4973/xxHash
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun * Notice extracted from xxHash homepage:
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
45*4882a593Smuzhiyun * It also successfully passes all tests from the SMHasher suite.
46*4882a593Smuzhiyun *
47*4882a593Smuzhiyun * Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2
48*4882a593Smuzhiyun * Duo @3GHz)
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * Name Speed Q.Score Author
51*4882a593Smuzhiyun * xxHash 5.4 GB/s 10
52*4882a593Smuzhiyun * CrapWow 3.2 GB/s 2 Andrew
53*4882a593Smuzhiyun * MumurHash 3a 2.7 GB/s 10 Austin Appleby
54*4882a593Smuzhiyun * SpookyHash 2.0 GB/s 10 Bob Jenkins
55*4882a593Smuzhiyun * SBox 1.4 GB/s 9 Bret Mulvey
56*4882a593Smuzhiyun * Lookup3 1.2 GB/s 9 Bob Jenkins
57*4882a593Smuzhiyun * SuperFastHash 1.2 GB/s 1 Paul Hsieh
58*4882a593Smuzhiyun * CityHash64 1.05 GB/s 10 Pike & Alakuijala
59*4882a593Smuzhiyun * FNV 0.55 GB/s 5 Fowler, Noll, Vo
60*4882a593Smuzhiyun * CRC32 0.43 GB/s 9
61*4882a593Smuzhiyun * MD5-32 0.33 GB/s 10 Ronald L. Rivest
62*4882a593Smuzhiyun * SHA1-32 0.28 GB/s 10
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * Q.Score is a measure of quality of the hash function.
65*4882a593Smuzhiyun * It depends on successfully passing SMHasher test set.
66*4882a593Smuzhiyun * 10 is a perfect score.
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * A 64-bits version, named xxh64 offers much better speed,
69*4882a593Smuzhiyun * but for 64-bits applications only.
70*4882a593Smuzhiyun * Name Speed on 64 bits Speed on 32 bits
71*4882a593Smuzhiyun * xxh64 13.8 GB/s 1.9 GB/s
72*4882a593Smuzhiyun * xxh32 6.8 GB/s 6.0 GB/s
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun #ifndef XXHASH_H
76*4882a593Smuzhiyun #define XXHASH_H
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #include <linux/types.h>
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*-****************************
81*4882a593Smuzhiyun * Simple Hash Functions
82*4882a593Smuzhiyun *****************************/
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /**
85*4882a593Smuzhiyun * xxh32() - calculate the 32-bit hash of the input with a given seed.
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * @input: The data to hash.
88*4882a593Smuzhiyun * @length: The length of the data to hash.
89*4882a593Smuzhiyun * @seed: The seed can be used to alter the result predictably.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * Return: The 32-bit hash of the data.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun uint32_t xxh32(const void *input, size_t length, uint32_t seed);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /**
98*4882a593Smuzhiyun * xxh64() - calculate the 64-bit hash of the input with a given seed.
99*4882a593Smuzhiyun *
100*4882a593Smuzhiyun * @input: The data to hash.
101*4882a593Smuzhiyun * @length: The length of the data to hash.
102*4882a593Smuzhiyun * @seed: The seed can be used to alter the result predictably.
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * This function runs 2x faster on 64-bit systems, but slower on 32-bit systems.
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * Return: The 64-bit hash of the data.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun uint64_t xxh64(const void *input, size_t length, uint64_t seed);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /**
111*4882a593Smuzhiyun * xxhash() - calculate wordsize hash of the input with a given seed
112*4882a593Smuzhiyun * @input: The data to hash.
113*4882a593Smuzhiyun * @length: The length of the data to hash.
114*4882a593Smuzhiyun * @seed: The seed can be used to alter the result predictably.
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun * If the hash does not need to be comparable between machines with
117*4882a593Smuzhiyun * different word sizes, this function will call whichever of xxh32()
118*4882a593Smuzhiyun * or xxh64() is faster.
119*4882a593Smuzhiyun *
120*4882a593Smuzhiyun * Return: wordsize hash of the data.
121*4882a593Smuzhiyun */
122*4882a593Smuzhiyun
xxhash(const void * input,size_t length,uint64_t seed)123*4882a593Smuzhiyun static inline unsigned long xxhash(const void *input, size_t length,
124*4882a593Smuzhiyun uint64_t seed)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun #if BITS_PER_LONG == 64
127*4882a593Smuzhiyun return xxh64(input, length, seed);
128*4882a593Smuzhiyun #else
129*4882a593Smuzhiyun return xxh32(input, length, seed);
130*4882a593Smuzhiyun #endif
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /*-****************************
134*4882a593Smuzhiyun * Streaming Hash Functions
135*4882a593Smuzhiyun *****************************/
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun * These definitions are only meant to allow allocation of XXH state
139*4882a593Smuzhiyun * statically, on stack, or in a struct for example.
140*4882a593Smuzhiyun * Do not use members directly.
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun * struct xxh32_state - private xxh32 state, do not use members directly
145*4882a593Smuzhiyun */
146*4882a593Smuzhiyun struct xxh32_state {
147*4882a593Smuzhiyun uint32_t total_len_32;
148*4882a593Smuzhiyun uint32_t large_len;
149*4882a593Smuzhiyun uint32_t v1;
150*4882a593Smuzhiyun uint32_t v2;
151*4882a593Smuzhiyun uint32_t v3;
152*4882a593Smuzhiyun uint32_t v4;
153*4882a593Smuzhiyun uint32_t mem32[4];
154*4882a593Smuzhiyun uint32_t memsize;
155*4882a593Smuzhiyun };
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /**
158*4882a593Smuzhiyun * struct xxh32_state - private xxh64 state, do not use members directly
159*4882a593Smuzhiyun */
160*4882a593Smuzhiyun struct xxh64_state {
161*4882a593Smuzhiyun uint64_t total_len;
162*4882a593Smuzhiyun uint64_t v1;
163*4882a593Smuzhiyun uint64_t v2;
164*4882a593Smuzhiyun uint64_t v3;
165*4882a593Smuzhiyun uint64_t v4;
166*4882a593Smuzhiyun uint64_t mem64[4];
167*4882a593Smuzhiyun uint32_t memsize;
168*4882a593Smuzhiyun };
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /**
171*4882a593Smuzhiyun * xxh32_reset() - reset the xxh32 state to start a new hashing operation
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * @state: The xxh32 state to reset.
174*4882a593Smuzhiyun * @seed: Initialize the hash state with this seed.
175*4882a593Smuzhiyun *
176*4882a593Smuzhiyun * Call this function on any xxh32_state to prepare for a new hashing operation.
177*4882a593Smuzhiyun */
178*4882a593Smuzhiyun void xxh32_reset(struct xxh32_state *state, uint32_t seed);
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /**
181*4882a593Smuzhiyun * xxh32_update() - hash the data given and update the xxh32 state
182*4882a593Smuzhiyun *
183*4882a593Smuzhiyun * @state: The xxh32 state to update.
184*4882a593Smuzhiyun * @input: The data to hash.
185*4882a593Smuzhiyun * @length: The length of the data to hash.
186*4882a593Smuzhiyun *
187*4882a593Smuzhiyun * After calling xxh32_reset() call xxh32_update() as many times as necessary.
188*4882a593Smuzhiyun *
189*4882a593Smuzhiyun * Return: Zero on success, otherwise an error code.
190*4882a593Smuzhiyun */
191*4882a593Smuzhiyun int xxh32_update(struct xxh32_state *state, const void *input, size_t length);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /**
194*4882a593Smuzhiyun * xxh32_digest() - produce the current xxh32 hash
195*4882a593Smuzhiyun *
196*4882a593Smuzhiyun * @state: Produce the current xxh32 hash of this state.
197*4882a593Smuzhiyun *
198*4882a593Smuzhiyun * A hash value can be produced at any time. It is still possible to continue
199*4882a593Smuzhiyun * inserting input into the hash state after a call to xxh32_digest(), and
200*4882a593Smuzhiyun * generate new hashes later on, by calling xxh32_digest() again.
201*4882a593Smuzhiyun *
202*4882a593Smuzhiyun * Return: The xxh32 hash stored in the state.
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun uint32_t xxh32_digest(const struct xxh32_state *state);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /**
207*4882a593Smuzhiyun * xxh64_reset() - reset the xxh64 state to start a new hashing operation
208*4882a593Smuzhiyun *
209*4882a593Smuzhiyun * @state: The xxh64 state to reset.
210*4882a593Smuzhiyun * @seed: Initialize the hash state with this seed.
211*4882a593Smuzhiyun */
212*4882a593Smuzhiyun void xxh64_reset(struct xxh64_state *state, uint64_t seed);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /**
215*4882a593Smuzhiyun * xxh64_update() - hash the data given and update the xxh64 state
216*4882a593Smuzhiyun * @state: The xxh64 state to update.
217*4882a593Smuzhiyun * @input: The data to hash.
218*4882a593Smuzhiyun * @length: The length of the data to hash.
219*4882a593Smuzhiyun *
220*4882a593Smuzhiyun * After calling xxh64_reset() call xxh64_update() as many times as necessary.
221*4882a593Smuzhiyun *
222*4882a593Smuzhiyun * Return: Zero on success, otherwise an error code.
223*4882a593Smuzhiyun */
224*4882a593Smuzhiyun int xxh64_update(struct xxh64_state *state, const void *input, size_t length);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /**
227*4882a593Smuzhiyun * xxh64_digest() - produce the current xxh64 hash
228*4882a593Smuzhiyun *
229*4882a593Smuzhiyun * @state: Produce the current xxh64 hash of this state.
230*4882a593Smuzhiyun *
231*4882a593Smuzhiyun * A hash value can be produced at any time. It is still possible to continue
232*4882a593Smuzhiyun * inserting input into the hash state after a call to xxh64_digest(), and
233*4882a593Smuzhiyun * generate new hashes later on, by calling xxh64_digest() again.
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * Return: The xxh64 hash stored in the state.
236*4882a593Smuzhiyun */
237*4882a593Smuzhiyun uint64_t xxh64_digest(const struct xxh64_state *state);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /*-**************************
240*4882a593Smuzhiyun * Utils
241*4882a593Smuzhiyun ***************************/
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /**
244*4882a593Smuzhiyun * xxh32_copy_state() - copy the source state into the destination state
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * @src: The source xxh32 state.
247*4882a593Smuzhiyun * @dst: The destination xxh32 state.
248*4882a593Smuzhiyun */
249*4882a593Smuzhiyun void xxh32_copy_state(struct xxh32_state *dst, const struct xxh32_state *src);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /**
252*4882a593Smuzhiyun * xxh64_copy_state() - copy the source state into the destination state
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * @src: The source xxh64 state.
255*4882a593Smuzhiyun * @dst: The destination xxh64 state.
256*4882a593Smuzhiyun */
257*4882a593Smuzhiyun void xxh64_copy_state(struct xxh64_state *dst, const struct xxh64_state *src);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun #endif /* XXHASH_H */
260