4c7aafebccef07a32fecd0d7a24150f3d0f79ee6
[fio.git] / crc / xxhash.h
1 /*\r
2    xxHash - Fast Hash algorithm\r
3    Header File\r
4    Copyright (C) 2012-2014, Yann Collet.\r
5    BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)\r
6 \r
7    Redistribution and use in source and binary forms, with or without\r
8    modification, are permitted provided that the following conditions are\r
9    met:\r
10   \r
11        * Redistributions of source code must retain the above copyright\r
12    notice, this list of conditions and the following disclaimer.\r
13        * Redistributions in binary form must reproduce the above\r
14    copyright notice, this list of conditions and the following disclaimer\r
15    in the documentation and/or other materials provided with the\r
16    distribution.\r
17   \r
18    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
19    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
20    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
21    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
22    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
23    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
24    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
25    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
26    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
27    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
28    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
29 \r
30    You can contact the author at :\r
31    - xxHash source repository : http://code.google.com/p/xxhash/\r
32 */\r
33 \r
34 /* Notice extracted from xxHash homepage :\r
35 \r
36 xxHash is an extremely fast Hash algorithm, running at RAM speed limits.\r
37 It also successfully passes all tests from the SMHasher suite.\r
38 \r
39 Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)\r
40 \r
41 Name            Speed       Q.Score   Author\r
42 xxHash          5.4 GB/s     10\r
43 CrapWow         3.2 GB/s      2       Andrew\r
44 MumurHash 3a    2.7 GB/s     10       Austin Appleby\r
45 SpookyHash      2.0 GB/s     10       Bob Jenkins\r
46 SBox            1.4 GB/s      9       Bret Mulvey\r
47 Lookup3         1.2 GB/s      9       Bob Jenkins\r
48 SuperFastHash   1.2 GB/s      1       Paul Hsieh\r
49 CityHash64      1.05 GB/s    10       Pike & Alakuijala\r
50 FNV             0.55 GB/s     5       Fowler, Noll, Vo\r
51 CRC32           0.43 GB/s     9\r
52 MD5-32          0.33 GB/s    10       Ronald L. Rivest\r
53 SHA1-32         0.28 GB/s    10\r
54 \r
55 Q.Score is a measure of quality of the hash function. \r
56 It depends on successfully passing SMHasher test set. \r
57 10 is a perfect score.\r
58 */\r
59 \r
60 #pragma once\r
61 \r
62 #if defined (__cplusplus)\r
63 extern "C" {\r
64 #endif\r
65 \r
66 #include <inttypes.h>\r
67 \r
68 struct XXH_state32_t\r
69 {\r
70     uint64_t total_len;\r
71     uint32_t seed;\r
72     uint32_t v1;\r
73     uint32_t v2;\r
74     uint32_t v3;\r
75     uint32_t v4;\r
76     int memsize;\r
77     char memory[16];\r
78 };\r
79 \r
80 //****************************\r
81 // Type\r
82 //****************************\r
83 typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;\r
84 \r
85 \r
86 \r
87 //****************************\r
88 // Simple Hash Functions\r
89 //****************************\r
90 \r
91 unsigned int XXH32 (const void* input, int len, unsigned int seed);\r
92 \r
93 /*\r
94 XXH32() :\r
95     Calculate the 32-bits hash of sequence of length "len" stored at memory address "input".\r
96     The memory between input & input+len must be valid (allocated and read-accessible).\r
97     "seed" can be used to alter the result predictably.\r
98     This function successfully passes all SMHasher tests.\r
99     Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s\r
100     Note that "len" is type "int", which means it is limited to 2^31-1.\r
101     If your data is larger, use the advanced functions below.\r
102 */\r
103 \r
104 \r
105 \r
106 //****************************\r
107 // Advanced Hash Functions\r
108 //****************************\r
109 \r
110 void*         XXH32_init   (unsigned int seed);\r
111 XXH_errorcode XXH32_update (void* state, const void* input, int len);\r
112 unsigned int  XXH32_digest (void* state);\r
113 \r
114 /*\r
115 These functions calculate the xxhash of an input provided in several small packets,\r
116 as opposed to an input provided as a single block.\r
117 \r
118 It must be started with :\r
119 void* XXH32_init()\r
120 The function returns a pointer which holds the state of calculation.\r
121 \r
122 This pointer must be provided as "void* state" parameter for XXH32_update().\r
123 XXH32_update() can be called as many times as necessary.\r
124 The user must provide a valid (allocated) input.\r
125 The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.\r
126 Note that "len" is type "int", which means it is limited to 2^31-1. \r
127 If your data is larger, it is recommended to chunk your data into blocks \r
128 of size for example 2^30 (1GB) to avoid any "int" overflow issue.\r
129 \r
130 Finally, you can end the calculation anytime, by using XXH32_digest().\r
131 This function returns the final 32-bits hash.\r
132 You must provide the same "void* state" parameter created by XXH32_init().\r
133 Memory will be freed by XXH32_digest().\r
134 */\r
135 \r
136 \r
137 int           XXH32_sizeofState();\r
138 XXH_errorcode XXH32_resetState(void* state, unsigned int seed);\r
139 \r
140 #define       XXH32_SIZEOFSTATE 48\r
141 typedef struct { long long ll[(XXH32_SIZEOFSTATE+(sizeof(long long)-1))/sizeof(long long)]; } XXH32_stateSpace_t;\r
142 /*\r
143 These functions allow user application to make its own allocation for state.\r
144 \r
145 XXH32_sizeofState() is used to know how much space must be allocated for the xxHash 32-bits state.\r
146 Note that the state must be aligned to access 'long long' fields. Memory must be allocated and referenced by a pointer.\r
147 This pointer must then be provided as 'state' into XXH32_resetState(), which initializes the state.\r
148 \r
149 For static allocation purposes (such as allocation on stack, or freestanding systems without malloc()),\r
150 use the structure XXH32_stateSpace_t, which will ensure that memory space is large enough and correctly aligned to access 'long long' fields.\r
151 */\r
152 \r
153 \r
154 unsigned int XXH32_intermediateDigest (void* state);\r
155 /*\r
156 This function does the same as XXH32_digest(), generating a 32-bit hash,\r
157 but preserve memory context.\r
158 This way, it becomes possible to generate intermediate hashes, and then continue feeding data with XXH32_update().\r
159 To free memory context, use XXH32_digest(), or free().\r
160 */\r
161 \r
162 \r
163 \r
164 //****************************\r
165 // Deprecated function names\r
166 //****************************\r
167 // The following translations are provided to ease code transition\r
168 // You are encouraged to no longer this function names\r
169 #define XXH32_feed   XXH32_update\r
170 #define XXH32_result XXH32_digest\r
171 #define XXH32_getIntermediateResult XXH32_intermediateDigest\r
172 \r
173 \r
174 \r
175 #if defined (__cplusplus)\r
176 }\r
177 #endif\r