xxhash.h 8.3 KB

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