xxhash.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* SPDX-License-Identifier: (GPL-2.0 or BSD-2-Clause) */
  2. /*
  3. * xxHash - Extremely Fast Hash algorithm
  4. * Copyright (C) 2012-2016, Yann Collet.
  5. *
  6. * You can contact the author at:
  7. * - xxHash homepage: http://cyan4973.github.io/xxHash/
  8. * - xxHash source repository: https://github.com/Cyan4973/xxHash
  9. */
  10. /*
  11. * Notice extracted from xxHash homepage:
  12. *
  13. * xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
  14. * It also successfully passes all tests from the SMHasher suite.
  15. *
  16. * Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2
  17. * Duo @3GHz)
  18. *
  19. * Name Speed Q.Score Author
  20. * xxHash 5.4 GB/s 10
  21. * CrapWow 3.2 GB/s 2 Andrew
  22. * MumurHash 3a 2.7 GB/s 10 Austin Appleby
  23. * SpookyHash 2.0 GB/s 10 Bob Jenkins
  24. * SBox 1.4 GB/s 9 Bret Mulvey
  25. * Lookup3 1.2 GB/s 9 Bob Jenkins
  26. * SuperFastHash 1.2 GB/s 1 Paul Hsieh
  27. * CityHash64 1.05 GB/s 10 Pike & Alakuijala
  28. * FNV 0.55 GB/s 5 Fowler, Noll, Vo
  29. * CRC32 0.43 GB/s 9
  30. * MD5-32 0.33 GB/s 10 Ronald L. Rivest
  31. * SHA1-32 0.28 GB/s 10
  32. *
  33. * Q.Score is a measure of quality of the hash function.
  34. * It depends on successfully passing SMHasher test set.
  35. * 10 is a perfect score.
  36. *
  37. * A 64-bits version, named xxh64 offers much better speed,
  38. * but for 64-bits applications only.
  39. * Name Speed on 64 bits Speed on 32 bits
  40. * xxh64 13.8 GB/s 1.9 GB/s
  41. * xxh32 6.8 GB/s 6.0 GB/s
  42. */
  43. #ifndef XXHASH_H
  44. #define XXHASH_H
  45. #include <linux/types.h>
  46. /*-****************************
  47. * Simple Hash Functions
  48. *****************************/
  49. /**
  50. * xxh32() - calculate the 32-bit hash of the input with a given seed.
  51. *
  52. * @input: The data to hash.
  53. * @length: The length of the data to hash.
  54. * @seed: The seed can be used to alter the result predictably.
  55. *
  56. * Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
  57. *
  58. * Return: The 32-bit hash of the data.
  59. */
  60. uint32_t xxh32(const void *input, size_t length, uint32_t seed);
  61. /**
  62. * xxh64() - calculate the 64-bit hash of the input with a given seed.
  63. *
  64. * @input: The data to hash.
  65. * @length: The length of the data to hash.
  66. * @seed: The seed can be used to alter the result predictably.
  67. *
  68. * This function runs 2x faster on 64-bit systems, but slower on 32-bit systems.
  69. *
  70. * Return: The 64-bit hash of the data.
  71. */
  72. uint64_t xxh64(const void *input, size_t length, uint64_t seed);
  73. /**
  74. * xxhash() - calculate wordsize hash of the input with a given seed
  75. * @input: The data to hash.
  76. * @length: The length of the data to hash.
  77. * @seed: The seed can be used to alter the result predictably.
  78. *
  79. * If the hash does not need to be comparable between machines with
  80. * different word sizes, this function will call whichever of xxh32()
  81. * or xxh64() is faster.
  82. *
  83. * Return: wordsize hash of the data.
  84. */
  85. static inline unsigned long xxhash(const void *input, size_t length,
  86. uint64_t seed)
  87. {
  88. #if BITS_PER_LONG == 64
  89. return xxh64(input, length, seed);
  90. #else
  91. return xxh32(input, length, seed);
  92. #endif
  93. }
  94. /*-****************************
  95. * Streaming Hash Functions
  96. *****************************/
  97. /*
  98. * These definitions are only meant to allow allocation of XXH state
  99. * statically, on stack, or in a struct for example.
  100. * Do not use members directly.
  101. */
  102. /**
  103. * struct xxh32_state - private xxh32 state, do not use members directly
  104. */
  105. struct xxh32_state {
  106. uint32_t total_len_32;
  107. uint32_t large_len;
  108. uint32_t v1;
  109. uint32_t v2;
  110. uint32_t v3;
  111. uint32_t v4;
  112. uint32_t mem32[4];
  113. uint32_t memsize;
  114. };
  115. /**
  116. * struct xxh32_state - private xxh64 state, do not use members directly
  117. */
  118. struct xxh64_state {
  119. uint64_t total_len;
  120. uint64_t v1;
  121. uint64_t v2;
  122. uint64_t v3;
  123. uint64_t v4;
  124. uint64_t mem64[4];
  125. uint32_t memsize;
  126. };
  127. /**
  128. * xxh32_reset() - reset the xxh32 state to start a new hashing operation
  129. *
  130. * @state: The xxh32 state to reset.
  131. * @seed: Initialize the hash state with this seed.
  132. *
  133. * Call this function on any xxh32_state to prepare for a new hashing operation.
  134. */
  135. void xxh32_reset(struct xxh32_state *state, uint32_t seed);
  136. /**
  137. * xxh32_update() - hash the data given and update the xxh32 state
  138. *
  139. * @state: The xxh32 state to update.
  140. * @input: The data to hash.
  141. * @length: The length of the data to hash.
  142. *
  143. * After calling xxh32_reset() call xxh32_update() as many times as necessary.
  144. *
  145. * Return: Zero on success, otherwise an error code.
  146. */
  147. int xxh32_update(struct xxh32_state *state, const void *input, size_t length);
  148. /**
  149. * xxh32_digest() - produce the current xxh32 hash
  150. *
  151. * @state: Produce the current xxh32 hash of this state.
  152. *
  153. * A hash value can be produced at any time. It is still possible to continue
  154. * inserting input into the hash state after a call to xxh32_digest(), and
  155. * generate new hashes later on, by calling xxh32_digest() again.
  156. *
  157. * Return: The xxh32 hash stored in the state.
  158. */
  159. uint32_t xxh32_digest(const struct xxh32_state *state);
  160. /**
  161. * xxh64_reset() - reset the xxh64 state to start a new hashing operation
  162. *
  163. * @state: The xxh64 state to reset.
  164. * @seed: Initialize the hash state with this seed.
  165. */
  166. void xxh64_reset(struct xxh64_state *state, uint64_t seed);
  167. /**
  168. * xxh64_update() - hash the data given and update the xxh64 state
  169. * @state: The xxh64 state to update.
  170. * @input: The data to hash.
  171. * @length: The length of the data to hash.
  172. *
  173. * After calling xxh64_reset() call xxh64_update() as many times as necessary.
  174. *
  175. * Return: Zero on success, otherwise an error code.
  176. */
  177. int xxh64_update(struct xxh64_state *state, const void *input, size_t length);
  178. /**
  179. * xxh64_digest() - produce the current xxh64 hash
  180. *
  181. * @state: Produce the current xxh64 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 xxh64_digest(), and
  185. * generate new hashes later on, by calling xxh64_digest() again.
  186. *
  187. * Return: The xxh64 hash stored in the state.
  188. */
  189. uint64_t xxh64_digest(const struct xxh64_state *state);
  190. /*-**************************
  191. * Utils
  192. ***************************/
  193. /**
  194. * xxh32_copy_state() - copy the source state into the destination state
  195. *
  196. * @src: The source xxh32 state.
  197. * @dst: The destination xxh32 state.
  198. */
  199. void xxh32_copy_state(struct xxh32_state *dst, const struct xxh32_state *src);
  200. /**
  201. * xxh64_copy_state() - copy the source state into the destination state
  202. *
  203. * @src: The source xxh64 state.
  204. * @dst: The destination xxh64 state.
  205. */
  206. void xxh64_copy_state(struct xxh64_state *dst, const struct xxh64_state *src);
  207. #endif /* XXHASH_H */