xxhash.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #include <linux/unaligned.h>
  41. #include <linux/errno.h>
  42. #include <linux/compiler.h>
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/string.h>
  46. #include <linux/xxhash.h>
  47. /*-*************************************
  48. * Macros
  49. **************************************/
  50. #define xxh_rotl32(x, r) ((x << r) | (x >> (32 - r)))
  51. #define xxh_rotl64(x, r) ((x << r) | (x >> (64 - r)))
  52. #ifdef __LITTLE_ENDIAN
  53. # define XXH_CPU_LITTLE_ENDIAN 1
  54. #else
  55. # define XXH_CPU_LITTLE_ENDIAN 0
  56. #endif
  57. /*-*************************************
  58. * Constants
  59. **************************************/
  60. static const uint32_t PRIME32_1 = 2654435761U;
  61. static const uint32_t PRIME32_2 = 2246822519U;
  62. static const uint32_t PRIME32_3 = 3266489917U;
  63. static const uint32_t PRIME32_4 = 668265263U;
  64. static const uint32_t PRIME32_5 = 374761393U;
  65. static const uint64_t PRIME64_1 = 11400714785074694791ULL;
  66. static const uint64_t PRIME64_2 = 14029467366897019727ULL;
  67. static const uint64_t PRIME64_3 = 1609587929392839161ULL;
  68. static const uint64_t PRIME64_4 = 9650029242287828579ULL;
  69. static const uint64_t PRIME64_5 = 2870177450012600261ULL;
  70. /*-***************************
  71. * Simple Hash Functions
  72. ****************************/
  73. static uint32_t xxh32_round(uint32_t seed, const uint32_t input)
  74. {
  75. seed += input * PRIME32_2;
  76. seed = xxh_rotl32(seed, 13);
  77. seed *= PRIME32_1;
  78. return seed;
  79. }
  80. uint32_t xxh32(const void *input, const size_t len, const uint32_t seed)
  81. {
  82. const uint8_t *p = (const uint8_t *)input;
  83. const uint8_t *b_end = p + len;
  84. uint32_t h32;
  85. if (len >= 16) {
  86. const uint8_t *const limit = b_end - 16;
  87. uint32_t v1 = seed + PRIME32_1 + PRIME32_2;
  88. uint32_t v2 = seed + PRIME32_2;
  89. uint32_t v3 = seed + 0;
  90. uint32_t v4 = seed - PRIME32_1;
  91. do {
  92. v1 = xxh32_round(v1, get_unaligned_le32(p));
  93. p += 4;
  94. v2 = xxh32_round(v2, get_unaligned_le32(p));
  95. p += 4;
  96. v3 = xxh32_round(v3, get_unaligned_le32(p));
  97. p += 4;
  98. v4 = xxh32_round(v4, get_unaligned_le32(p));
  99. p += 4;
  100. } while (p <= limit);
  101. h32 = xxh_rotl32(v1, 1) + xxh_rotl32(v2, 7) +
  102. xxh_rotl32(v3, 12) + xxh_rotl32(v4, 18);
  103. } else {
  104. h32 = seed + PRIME32_5;
  105. }
  106. h32 += (uint32_t)len;
  107. while (p + 4 <= b_end) {
  108. h32 += get_unaligned_le32(p) * PRIME32_3;
  109. h32 = xxh_rotl32(h32, 17) * PRIME32_4;
  110. p += 4;
  111. }
  112. while (p < b_end) {
  113. h32 += (*p) * PRIME32_5;
  114. h32 = xxh_rotl32(h32, 11) * PRIME32_1;
  115. p++;
  116. }
  117. h32 ^= h32 >> 15;
  118. h32 *= PRIME32_2;
  119. h32 ^= h32 >> 13;
  120. h32 *= PRIME32_3;
  121. h32 ^= h32 >> 16;
  122. return h32;
  123. }
  124. EXPORT_SYMBOL(xxh32);
  125. static uint64_t xxh64_round(uint64_t acc, const uint64_t input)
  126. {
  127. acc += input * PRIME64_2;
  128. acc = xxh_rotl64(acc, 31);
  129. acc *= PRIME64_1;
  130. return acc;
  131. }
  132. static uint64_t xxh64_merge_round(uint64_t acc, uint64_t val)
  133. {
  134. val = xxh64_round(0, val);
  135. acc ^= val;
  136. acc = acc * PRIME64_1 + PRIME64_4;
  137. return acc;
  138. }
  139. uint64_t xxh64(const void *input, const size_t len, const uint64_t seed)
  140. {
  141. const uint8_t *p = (const uint8_t *)input;
  142. const uint8_t *const b_end = p + len;
  143. uint64_t h64;
  144. if (len >= 32) {
  145. const uint8_t *const limit = b_end - 32;
  146. uint64_t v1 = seed + PRIME64_1 + PRIME64_2;
  147. uint64_t v2 = seed + PRIME64_2;
  148. uint64_t v3 = seed + 0;
  149. uint64_t v4 = seed - PRIME64_1;
  150. do {
  151. v1 = xxh64_round(v1, get_unaligned_le64(p));
  152. p += 8;
  153. v2 = xxh64_round(v2, get_unaligned_le64(p));
  154. p += 8;
  155. v3 = xxh64_round(v3, get_unaligned_le64(p));
  156. p += 8;
  157. v4 = xxh64_round(v4, get_unaligned_le64(p));
  158. p += 8;
  159. } while (p <= limit);
  160. h64 = xxh_rotl64(v1, 1) + xxh_rotl64(v2, 7) +
  161. xxh_rotl64(v3, 12) + xxh_rotl64(v4, 18);
  162. h64 = xxh64_merge_round(h64, v1);
  163. h64 = xxh64_merge_round(h64, v2);
  164. h64 = xxh64_merge_round(h64, v3);
  165. h64 = xxh64_merge_round(h64, v4);
  166. } else {
  167. h64 = seed + PRIME64_5;
  168. }
  169. h64 += (uint64_t)len;
  170. while (p + 8 <= b_end) {
  171. const uint64_t k1 = xxh64_round(0, get_unaligned_le64(p));
  172. h64 ^= k1;
  173. h64 = xxh_rotl64(h64, 27) * PRIME64_1 + PRIME64_4;
  174. p += 8;
  175. }
  176. if (p + 4 <= b_end) {
  177. h64 ^= (uint64_t)(get_unaligned_le32(p)) * PRIME64_1;
  178. h64 = xxh_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  179. p += 4;
  180. }
  181. while (p < b_end) {
  182. h64 ^= (*p) * PRIME64_5;
  183. h64 = xxh_rotl64(h64, 11) * PRIME64_1;
  184. p++;
  185. }
  186. h64 ^= h64 >> 33;
  187. h64 *= PRIME64_2;
  188. h64 ^= h64 >> 29;
  189. h64 *= PRIME64_3;
  190. h64 ^= h64 >> 32;
  191. return h64;
  192. }
  193. EXPORT_SYMBOL(xxh64);
  194. /*-**************************************************
  195. * Advanced Hash Functions
  196. ***************************************************/
  197. void xxh64_reset(struct xxh64_state *statePtr, const uint64_t seed)
  198. {
  199. /* use a local state for memcpy() to avoid strict-aliasing warnings */
  200. struct xxh64_state state;
  201. memset(&state, 0, sizeof(state));
  202. state.v1 = seed + PRIME64_1 + PRIME64_2;
  203. state.v2 = seed + PRIME64_2;
  204. state.v3 = seed + 0;
  205. state.v4 = seed - PRIME64_1;
  206. memcpy(statePtr, &state, sizeof(state));
  207. }
  208. EXPORT_SYMBOL(xxh64_reset);
  209. int xxh64_update(struct xxh64_state *state, const void *input, const size_t len)
  210. {
  211. const uint8_t *p = (const uint8_t *)input;
  212. const uint8_t *const b_end = p + len;
  213. if (input == NULL)
  214. return -EINVAL;
  215. state->total_len += len;
  216. if (state->memsize + len < 32) { /* fill in tmp buffer */
  217. memcpy(((uint8_t *)state->mem64) + state->memsize, input, len);
  218. state->memsize += (uint32_t)len;
  219. return 0;
  220. }
  221. if (state->memsize) { /* tmp buffer is full */
  222. uint64_t *p64 = state->mem64;
  223. memcpy(((uint8_t *)p64) + state->memsize, input,
  224. 32 - state->memsize);
  225. state->v1 = xxh64_round(state->v1, get_unaligned_le64(p64));
  226. p64++;
  227. state->v2 = xxh64_round(state->v2, get_unaligned_le64(p64));
  228. p64++;
  229. state->v3 = xxh64_round(state->v3, get_unaligned_le64(p64));
  230. p64++;
  231. state->v4 = xxh64_round(state->v4, get_unaligned_le64(p64));
  232. p += 32 - state->memsize;
  233. state->memsize = 0;
  234. }
  235. if (p + 32 <= b_end) {
  236. const uint8_t *const limit = b_end - 32;
  237. uint64_t v1 = state->v1;
  238. uint64_t v2 = state->v2;
  239. uint64_t v3 = state->v3;
  240. uint64_t v4 = state->v4;
  241. do {
  242. v1 = xxh64_round(v1, get_unaligned_le64(p));
  243. p += 8;
  244. v2 = xxh64_round(v2, get_unaligned_le64(p));
  245. p += 8;
  246. v3 = xxh64_round(v3, get_unaligned_le64(p));
  247. p += 8;
  248. v4 = xxh64_round(v4, get_unaligned_le64(p));
  249. p += 8;
  250. } while (p <= limit);
  251. state->v1 = v1;
  252. state->v2 = v2;
  253. state->v3 = v3;
  254. state->v4 = v4;
  255. }
  256. if (p < b_end) {
  257. memcpy(state->mem64, p, (size_t)(b_end-p));
  258. state->memsize = (uint32_t)(b_end - p);
  259. }
  260. return 0;
  261. }
  262. EXPORT_SYMBOL(xxh64_update);
  263. uint64_t xxh64_digest(const struct xxh64_state *state)
  264. {
  265. const uint8_t *p = (const uint8_t *)state->mem64;
  266. const uint8_t *const b_end = (const uint8_t *)state->mem64 +
  267. state->memsize;
  268. uint64_t h64;
  269. if (state->total_len >= 32) {
  270. const uint64_t v1 = state->v1;
  271. const uint64_t v2 = state->v2;
  272. const uint64_t v3 = state->v3;
  273. const uint64_t v4 = state->v4;
  274. h64 = xxh_rotl64(v1, 1) + xxh_rotl64(v2, 7) +
  275. xxh_rotl64(v3, 12) + xxh_rotl64(v4, 18);
  276. h64 = xxh64_merge_round(h64, v1);
  277. h64 = xxh64_merge_round(h64, v2);
  278. h64 = xxh64_merge_round(h64, v3);
  279. h64 = xxh64_merge_round(h64, v4);
  280. } else {
  281. h64 = state->v3 + PRIME64_5;
  282. }
  283. h64 += (uint64_t)state->total_len;
  284. while (p + 8 <= b_end) {
  285. const uint64_t k1 = xxh64_round(0, get_unaligned_le64(p));
  286. h64 ^= k1;
  287. h64 = xxh_rotl64(h64, 27) * PRIME64_1 + PRIME64_4;
  288. p += 8;
  289. }
  290. if (p + 4 <= b_end) {
  291. h64 ^= (uint64_t)(get_unaligned_le32(p)) * PRIME64_1;
  292. h64 = xxh_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  293. p += 4;
  294. }
  295. while (p < b_end) {
  296. h64 ^= (*p) * PRIME64_5;
  297. h64 = xxh_rotl64(h64, 11) * PRIME64_1;
  298. p++;
  299. }
  300. h64 ^= h64 >> 33;
  301. h64 *= PRIME64_2;
  302. h64 ^= h64 >> 29;
  303. h64 *= PRIME64_3;
  304. h64 ^= h64 >> 32;
  305. return h64;
  306. }
  307. EXPORT_SYMBOL(xxh64_digest);
  308. MODULE_LICENSE("Dual BSD/GPL");
  309. MODULE_DESCRIPTION("xxHash");