xref: /dragonfly/sys/vfs/hammer2/xxhash/xxhash.h (revision e5a92d33)
1 /*
2    xxHash - Extremely Fast Hash algorithm
3    Header File
4    Copyright (C) 2012-2016, 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 /* DRAGONFLY ADDITION - allows inclusion in conf/files */
36 #define XXH_NAMESPACE h2_
37 
38 /* Notice extracted from xxHash homepage :
39 
40 xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
41 It also successfully passes all tests from the SMHasher suite.
42 
43 Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)
44 
45 Name            Speed       Q.Score   Author
46 xxHash          5.4 GB/s     10
47 CrapWow         3.2 GB/s      2       Andrew
48 MumurHash 3a    2.7 GB/s     10       Austin Appleby
49 SpookyHash      2.0 GB/s     10       Bob Jenkins
50 SBox            1.4 GB/s      9       Bret Mulvey
51 Lookup3         1.2 GB/s      9       Bob Jenkins
52 SuperFastHash   1.2 GB/s      1       Paul Hsieh
53 CityHash64      1.05 GB/s    10       Pike & Alakuijala
54 FNV             0.55 GB/s     5       Fowler, Noll, Vo
55 CRC32           0.43 GB/s     9
56 MD5-32          0.33 GB/s    10       Ronald L. Rivest
57 SHA1-32         0.28 GB/s    10
58 
59 Q.Score is a measure of quality of the hash function.
60 It depends on successfully passing SMHasher test set.
61 10 is a perfect score.
62 
63 A 64-bits version, named XXH64, is available since r35.
64 It offers much better speed, but for 64-bits applications only.
65 Name     Speed on 64 bits    Speed on 32 bits
66 XXH64       13.8 GB/s            1.9 GB/s
67 XXH32        6.8 GB/s            6.0 GB/s
68 */
69 
70 #ifndef XXHASH_H_5627135585666179
71 #define XXHASH_H_5627135585666179 1
72 
73 #if defined (__cplusplus)
74 extern "C" {
75 #endif
76 
77 
78 /* ****************************
79 *  Definitions
80 ******************************/
81 #if !defined(_KERNEL)
82 #include <stddef.h>   /* size_t */
83 #endif
84 typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;
85 
86 
87 /* ****************************
88 *  API modifier
89 ******************************/
90 /*!XXH_PRIVATE_API
91 *  Transforms all publics symbols within `xxhash.c` into private ones.
92 *  Methodology :
93 *  instead of : #include "xxhash.h"
94 *  do :
95 *     #define XXH_PRIVATE_API
96 *     #include "xxhash.c"   // note the .c , instead of .h
97 *  also : don't compile and link xxhash.c separately
98 */
99 #ifdef XXH_PRIVATE_API
100 #  if defined(__GNUC__)
101 #    define XXH_PUBLIC_API static __attribute__((unused))
102 #  elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
103 #    define XXH_PUBLIC_API static inline
104 #  elif defined(_MSC_VER)
105 #    define XXH_PUBLIC_API static __inline
106 #  else
107 #    define XXH_PUBLIC_API static   /* this version may generate warnings for unused static functions; disable the relevant warning */
108 #  endif
109 #else
110 #  define XXH_PUBLIC_API   /* do nothing */
111 #endif
112 
113 /*!XXH_NAMESPACE, aka Namespace Emulation :
114 
115 If you want to include _and expose_ xxHash functions from within your own library,
116 but also want to avoid symbol collisions with another library which also includes xxHash,
117 
118 you can use XXH_NAMESPACE, to automatically prefix any public symbol from `xxhash.c`
119 with the value of XXH_NAMESPACE (so avoid to keep it NULL and avoid numeric values).
120 
121 Note that no change is required within the calling program as long as it also includes `xxhash.h` :
122 regular symbol name will be automatically translated by this header.
123 */
124 #ifdef XXH_NAMESPACE
125 #  define XXH_CAT(A,B) A##B
126 #  define XXH_NAME2(A,B) XXH_CAT(A,B)
127 #  define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
128 #  define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
129 #  define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
130 #  define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
131 #  define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
132 #  define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
133 #  define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
134 #  define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
135 #  define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
136 #  define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
137 #  define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
138 #  define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
139 #  define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
140 #endif
141 
142 
143 /* *************************************
144 *  Version
145 ***************************************/
146 #define XXH_VERSION_MAJOR    0
147 #define XXH_VERSION_MINOR    6
148 #define XXH_VERSION_RELEASE  0
149 #define XXH_VERSION_NUMBER  (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
150 XXH_PUBLIC_API unsigned XXH_versionNumber (void);
151 
152 
153 /* ****************************
154 *  Simple Hash Functions
155 ******************************/
156 typedef unsigned int       XXH32_hash_t;
157 typedef unsigned long long XXH64_hash_t;
158 
159 XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed);
160 XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed);
161 
162 /*!
163 XXH32() :
164     Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input".
165     The memory between input & input+length must be valid (allocated and read-accessible).
166     "seed" can be used to alter the result predictably.
167     Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
168 XXH64() :
169     Calculate the 64-bits hash of sequence of length "len" stored at memory address "input".
170     "seed" can be used to alter the result predictably.
171     This function runs faster on 64-bits systems, but slower on 32-bits systems (see benchmark).
172 */
173 
174 
175 /* ****************************
176 *  Streaming Hash Functions
177 ******************************/
178 typedef struct XXH32_state_s XXH32_state_t;   /* incomplete type */
179 typedef struct XXH64_state_s XXH64_state_t;   /* incomplete type */
180 
181 /*! Dynamic allocation of states
182     Compatible with dynamic libraries */
183 
184 XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void);
185 XXH_PUBLIC_API XXH_errorcode  XXH32_freeState(XXH32_state_t* statePtr);
186 
187 XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void);
188 XXH_PUBLIC_API XXH_errorcode  XXH64_freeState(XXH64_state_t* statePtr);
189 
190 
191 /* hash streaming */
192 
193 XXH_PUBLIC_API XXH_errorcode XXH32_reset  (XXH32_state_t* statePtr, unsigned int seed);
194 XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
195 XXH_PUBLIC_API XXH32_hash_t  XXH32_digest (const XXH32_state_t* statePtr);
196 
197 XXH_PUBLIC_API XXH_errorcode XXH64_reset  (XXH64_state_t* statePtr, unsigned long long seed);
198 XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);
199 XXH_PUBLIC_API XXH64_hash_t  XXH64_digest (const XXH64_state_t* statePtr);
200 
201 /*!
202 These functions generate the xxHash of an input provided in multiple segments,
203 as opposed to provided as a single block.
204 
205 XXH state must first be allocated, using either static or dynamic method provided above.
206 
207 Start a new hash by initializing state with a seed, using XXHnn_reset().
208 
209 Then, feed the hash state by calling XXHnn_update() as many times as necessary.
210 Obviously, input must be valid, hence allocated and read accessible.
211 The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
212 
213 Finally, a hash value can be produced anytime, by using XXHnn_digest().
214 This function returns the nn-bits hash as an int or long long.
215 
216 It's still possible to continue inserting input into the hash state after a digest,
217 and later on generate some new hashes, by calling again XXHnn_digest().
218 
219 When done, free XXH state space if it was allocated dynamically.
220 */
221 
222 
223 /* **************************
224 *  Canonical representation
225 ****************************/
226 typedef struct { unsigned char digest[4]; } XXH32_canonical_t;
227 typedef struct { unsigned char digest[8]; } XXH64_canonical_t;
228 
229 XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);
230 XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash);
231 
232 XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);
233 XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src);
234 
235 /*! Default result type for XXH functions are primitive unsigned 32 and 64 bits.
236 *   The canonical representation uses human-readable write convention, aka big-endian (large digits first).
237 *   These functions allow transformation of hash result into and from its canonical format.
238 *   This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.
239 */
240 
241 
242 #ifdef XXH_STATIC_LINKING_ONLY
243 
244 /* This part contains definition which shall only be used with static linking.
245    The prototypes / types defined here are not guaranteed to remain stable.
246    They could change in a future version, becoming incompatible with a different version of the library */
247 
248    struct XXH32_state_s {
249        unsigned long long total_len;
250        unsigned seed;
251        unsigned v1;
252        unsigned v2;
253        unsigned v3;
254        unsigned v4;
255        unsigned mem32[4];   /* buffer defined as U32 for alignment */
256        unsigned memsize;
257    };   /* typedef'd to XXH32_state_t */
258 
259    struct XXH64_state_s {
260        unsigned long long total_len;
261        unsigned long long seed;
262        unsigned long long v1;
263        unsigned long long v2;
264        unsigned long long v3;
265        unsigned long long v4;
266        unsigned long long mem64[4];   /* buffer defined as U64 for alignment */
267        unsigned memsize;
268    };   /* typedef'd to XXH64_state_t */
269 
270 
271 #endif
272 
273 
274 #if defined (__cplusplus)
275 }
276 #endif
277 
278 #endif /* XXHASH_H_5627135585666179 */
279