| 1 |
/* |
| 2 |
xxHash - Extremely Fast Hash algorithm |
| 3 |
Header File |
| 4 |
Copyright (C) 2012-2015, Yann Collet. |
| 5 |
|
| 6 |
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
| 7 |
|
| 8 |
Redistribution and use in source and binary forms, with or without |
| 9 |
modification, are permitted provided that the following conditions are |
| 10 |
met: |
| 11 |
|
| 12 |
* Redistributions of source code must retain the above copyright |
| 13 |
notice, this list of conditions and the following disclaimer. |
| 14 |
* Redistributions in binary form must reproduce the above |
| 15 |
copyright notice, this list of conditions and the following disclaimer |
| 16 |
in the documentation and/or other materials provided with the |
| 17 |
distribution. |
| 18 |
|
| 19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
You can contact the author at : |
| 32 |
- xxHash source repository : https://github.com/Cyan4973/xxHash |
| 33 |
*/ |
| 34 |
|
| 35 |
/* Notice extracted from xxHash homepage : |
| 36 |
|
| 37 |
xxHash is an extremely fast Hash algorithm, running at RAM speed limits. |
| 38 |
It also successfully passes all tests from the SMHasher suite. |
| 39 |
|
| 40 |
Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz) |
| 41 |
|
| 42 |
Name Speed Q.Score Author |
| 43 |
xxHash 5.4 GB/s 10 |
| 44 |
CrapWow 3.2 GB/s 2 Andrew |
| 45 |
MumurHash 3a 2.7 GB/s 10 Austin Appleby |
| 46 |
SpookyHash 2.0 GB/s 10 Bob Jenkins |
| 47 |
SBox 1.4 GB/s 9 Bret Mulvey |
| 48 |
Lookup3 1.2 GB/s 9 Bob Jenkins |
| 49 |
SuperFastHash 1.2 GB/s 1 Paul Hsieh |
| 50 |
CityHash64 1.05 GB/s 10 Pike & Alakuijala |
| 51 |
FNV 0.55 GB/s 5 Fowler, Noll, Vo |
| 52 |
CRC32 0.43 GB/s 9 |
| 53 |
MD5-32 0.33 GB/s 10 Ronald L. Rivest |
| 54 |
SHA1-32 0.28 GB/s 10 |
| 55 |
|
| 56 |
Q.Score is a measure of quality of the hash function. |
| 57 |
It depends on successfully passing SMHasher test set. |
| 58 |
10 is a perfect score. |
| 59 |
|
| 60 |
A 64-bits version, named XXH64, is available since r35. |
| 61 |
It offers much better speed, but for 64-bits applications only. |
| 62 |
Name Speed on 64 bits Speed on 32 bits |
| 63 |
XXH64 13.8 GB/s 1.9 GB/s |
| 64 |
XXH32 6.8 GB/s 6.0 GB/s |
| 65 |
*/ |
| 66 |
|
| 67 |
#pragma once |
| 68 |
|
| 69 |
#if defined (__cplusplus) |
| 70 |
extern "C" { |
| 71 |
#endif |
| 72 |
|
| 73 |
|
| 74 |
/***************************** |
| 75 |
* Definitions |
| 76 |
*****************************/ |
| 77 |
#include <stddef.h> /* size_t */ |
| 78 |
typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; |
| 79 |
|
| 80 |
|
| 81 |
/***************************** |
| 82 |
* Namespace Emulation |
| 83 |
*****************************/ |
| 84 |
/* Motivations : |
| 85 |
|
| 86 |
If you need to include xxHash into your library, |
| 87 |
but wish to avoid xxHash symbols to be present on your library interface |
| 88 |
in an effort to avoid potential name collision if another library also includes xxHash, |
| 89 |
|
| 90 |
you can use XXH_NAMESPACE, which will automatically prefix any symbol from xxHash |
| 91 |
with the value of XXH_NAMESPACE (so avoid to keep it NULL, and avoid numeric values). |
| 92 |
|
| 93 |
Note that no change is required within the calling program : |
| 94 |
it can still call xxHash functions using their regular name. |
| 95 |
They will be automatically translated by this header. |
| 96 |
*/ |
| 97 |
#ifdef XXH_NAMESPACE |
| 98 |
# define XXH_CAT(A,B) A##B |
| 99 |
# define XXH_NAME2(A,B) XXH_CAT(A,B) |
| 100 |
# define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32) |
| 101 |
# define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64) |
| 102 |
# define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState) |
| 103 |
# define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState) |
| 104 |
# define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState) |
| 105 |
# define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState) |
| 106 |
# define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset) |
| 107 |
# define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset) |
| 108 |
# define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update) |
| 109 |
# define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update) |
| 110 |
# define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest) |
| 111 |
# define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest) |
| 112 |
#endif |
| 113 |
|
| 114 |
|
| 115 |
/***************************** |
| 116 |
* Simple Hash Functions |
| 117 |
*****************************/ |
| 118 |
|
| 119 |
unsigned int XXH32 (const void* input, size_t length, unsigned seed); |
| 120 |
unsigned long long XXH64 (const void* input, size_t length, unsigned long long seed); |
| 121 |
|
| 122 |
/* |
| 123 |
XXH32() : |
| 124 |
Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input". |
| 125 |
The memory between input & input+length must be valid (allocated and read-accessible). |
| 126 |
"seed" can be used to alter the result predictably. |
| 127 |
This function successfully passes all SMHasher tests. |
| 128 |
Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s |
| 129 |
XXH64() : |
| 130 |
Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". |
| 131 |
Faster on 64-bits systems. Slower on 32-bits systems. |
| 132 |
*/ |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
/***************************** |
| 137 |
* Advanced Hash Functions |
| 138 |
*****************************/ |
| 139 |
typedef struct { |
| 140 |
long long ll[ 6]; |
| 141 |
} XXH32_state_t; |
| 142 |
typedef struct { |
| 143 |
long long ll[11]; |
| 144 |
} XXH64_state_t; |
| 145 |
|
| 146 |
/* |
| 147 |
These structures allow static allocation of XXH states. |
| 148 |
States must then be initialized using XXHnn_reset() before first use. |
| 149 |
|
| 150 |
If you prefer dynamic allocation, please refer to functions below. |
| 151 |
*/ |
| 152 |
|
| 153 |
XXH32_state_t* XXH32_createState(void); |
| 154 |
XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr); |
| 155 |
|
| 156 |
XXH64_state_t* XXH64_createState(void); |
| 157 |
XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr); |
| 158 |
|
| 159 |
/* |
| 160 |
These functions create and release memory for XXH state. |
| 161 |
States must then be initialized using XXHnn_reset() before first use. |
| 162 |
*/ |
| 163 |
|
| 164 |
|
| 165 |
XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned seed); |
| 166 |
XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length); |
| 167 |
unsigned int XXH32_digest (const XXH32_state_t* statePtr); |
| 168 |
|
| 169 |
XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed); |
| 170 |
XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length); |
| 171 |
unsigned long long XXH64_digest (const XXH64_state_t* statePtr); |
| 172 |
|
| 173 |
/* |
| 174 |
These functions calculate the xxHash of an input provided in multiple smaller packets, |
| 175 |
as opposed to an input provided as a single block. |
| 176 |
|
| 177 |
XXH state space must first be allocated, using either static or dynamic method provided above. |
| 178 |
|
| 179 |
Start a new hash by initializing state with a seed, using XXHnn_reset(). |
| 180 |
|
| 181 |
Then, feed the hash state by calling XXHnn_update() as many times as necessary. |
| 182 |
Obviously, input must be valid, meaning allocated and read accessible. |
| 183 |
The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. |
| 184 |
|
| 185 |
Finally, you can produce a hash anytime, by using XXHnn_digest(). |
| 186 |
This function returns the final nn-bits hash. |
| 187 |
You can nonetheless continue feeding the hash state with more input, |
| 188 |
and therefore get some new hashes, by calling again XXHnn_digest(). |
| 189 |
|
| 190 |
When you are done, don't forget to free XXH state space, using typically XXHnn_freeState(). |
| 191 |
*/ |
| 192 |
|
| 193 |
|
| 194 |
#if defined (__cplusplus) |
| 195 |
} |
| 196 |
#endif |