xxhash.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. #define XXH_API static inline __attribute__((unused))
  77. /*-****************************
  78. * Simple Hash Functions
  79. *****************************/
  80. /**
  81. * xxh32() - calculate the 32-bit hash of the input with a given seed.
  82. *
  83. * @input: The data to hash.
  84. * @length: The length of the data to hash.
  85. * @seed: The seed can be used to alter the result predictably.
  86. *
  87. * Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
  88. *
  89. * Return: The 32-bit hash of the data.
  90. */
  91. XXH_API uint32_t xxh32(const void *input, size_t length, uint32_t seed);
  92. /**
  93. * xxh64() - calculate the 64-bit hash of the input with a given seed.
  94. *
  95. * @input: The data to hash.
  96. * @length: The length of the data to hash.
  97. * @seed: The seed can be used to alter the result predictably.
  98. *
  99. * This function runs 2x faster on 64-bit systems, but slower on 32-bit systems.
  100. *
  101. * Return: The 64-bit hash of the data.
  102. */
  103. XXH_API uint64_t xxh64(const void *input, size_t length, uint64_t seed);
  104. /**
  105. * xxhash() - calculate wordsize hash of the input with a given seed
  106. * @input: The data to hash.
  107. * @length: The length of the data to hash.
  108. * @seed: The seed can be used to alter the result predictably.
  109. *
  110. * If the hash does not need to be comparable between machines with
  111. * different word sizes, this function will call whichever of xxh32()
  112. * or xxh64() is faster.
  113. *
  114. * Return: wordsize hash of the data.
  115. */
  116. static inline unsigned long xxhash(const void *input, size_t length,
  117. uint64_t seed)
  118. {
  119. if (sizeof(size_t) == 8)
  120. return xxh64(input, length, seed);
  121. else
  122. return xxh32(input, length, seed);
  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. XXH_API 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. XXH_API 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. XXH_API 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. XXH_API 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. XXH_API 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. XXH_API 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. XXH_API 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. XXH_API void xxh64_copy_state(struct xxh64_state *dst, const struct xxh64_state *src);
  237. /*
  238. * xxHash - Extremely Fast Hash algorithm
  239. * Copyright (C) 2012-2016, Yann Collet.
  240. *
  241. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  242. *
  243. * Redistribution and use in source and binary forms, with or without
  244. * modification, are permitted provided that the following conditions are
  245. * met:
  246. *
  247. * * Redistributions of source code must retain the above copyright
  248. * notice, this list of conditions and the following disclaimer.
  249. * * Redistributions in binary form must reproduce the above
  250. * copyright notice, this list of conditions and the following disclaimer
  251. * in the documentation and/or other materials provided with the
  252. * distribution.
  253. *
  254. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  255. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  256. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  257. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  258. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  259. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  260. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  261. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  262. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  263. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  264. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  265. *
  266. * This program is free software; you can redistribute it and/or modify it under
  267. * the terms of the GNU General Public License version 2 as published by the
  268. * Free Software Foundation. This program is dual-licensed; you may select
  269. * either version 2 of the GNU General Public License ("GPL") or BSD license
  270. * ("BSD").
  271. *
  272. * You can contact the author at:
  273. * - xxHash homepage: https://cyan4973.github.io/xxHash/
  274. * - xxHash source repository: https://github.com/Cyan4973/xxHash
  275. */
  276. #include <asm/unaligned.h>
  277. #include <linux/errno.h>
  278. #include <linux/kernel.h>
  279. #include <linux/module.h>
  280. #include <linux/xxhash.h>
  281. /*-*************************************
  282. * Macros
  283. **************************************/
  284. #define xxh_rotl32(x, r) ((x << r) | (x >> (32 - r)))
  285. #define xxh_rotl64(x, r) ((x << r) | (x >> (64 - r)))
  286. #ifdef __LITTLE_ENDIAN
  287. # define XXH_CPU_LITTLE_ENDIAN 1
  288. #else
  289. # define XXH_CPU_LITTLE_ENDIAN 0
  290. #endif
  291. /*-*************************************
  292. * Constants
  293. **************************************/
  294. static const uint32_t PRIME32_1 = 2654435761U;
  295. static const uint32_t PRIME32_2 = 2246822519U;
  296. static const uint32_t PRIME32_3 = 3266489917U;
  297. static const uint32_t PRIME32_4 = 668265263U;
  298. static const uint32_t PRIME32_5 = 374761393U;
  299. static const uint64_t PRIME64_1 = 11400714785074694791ULL;
  300. static const uint64_t PRIME64_2 = 14029467366897019727ULL;
  301. static const uint64_t PRIME64_3 = 1609587929392839161ULL;
  302. static const uint64_t PRIME64_4 = 9650029242287828579ULL;
  303. static const uint64_t PRIME64_5 = 2870177450012600261ULL;
  304. /*-**************************
  305. * Utils
  306. ***************************/
  307. XXH_API void xxh32_copy_state(struct xxh32_state *dst, const struct xxh32_state *src)
  308. {
  309. __builtin_memcpy(dst, src, sizeof(*dst));
  310. }
  311. XXH_API void xxh64_copy_state(struct xxh64_state *dst, const struct xxh64_state *src)
  312. {
  313. __builtin_memcpy(dst, src, sizeof(*dst));
  314. }
  315. /*-***************************
  316. * Simple Hash Functions
  317. ****************************/
  318. static uint32_t xxh32_round(uint32_t seed, const uint32_t input)
  319. {
  320. seed += input * PRIME32_2;
  321. seed = xxh_rotl32(seed, 13);
  322. seed *= PRIME32_1;
  323. return seed;
  324. }
  325. XXH_API uint32_t xxh32(const void *input, const size_t len, const uint32_t seed)
  326. {
  327. const uint8_t *p = (const uint8_t *)input;
  328. const uint8_t *b_end = p + len;
  329. uint32_t h32;
  330. if (len >= 16) {
  331. const uint8_t *const limit = b_end - 16;
  332. uint32_t v1 = seed + PRIME32_1 + PRIME32_2;
  333. uint32_t v2 = seed + PRIME32_2;
  334. uint32_t v3 = seed + 0;
  335. uint32_t v4 = seed - PRIME32_1;
  336. do {
  337. v1 = xxh32_round(v1, get_unaligned_le32(p));
  338. p += 4;
  339. v2 = xxh32_round(v2, get_unaligned_le32(p));
  340. p += 4;
  341. v3 = xxh32_round(v3, get_unaligned_le32(p));
  342. p += 4;
  343. v4 = xxh32_round(v4, get_unaligned_le32(p));
  344. p += 4;
  345. } while (p <= limit);
  346. h32 = xxh_rotl32(v1, 1) + xxh_rotl32(v2, 7) +
  347. xxh_rotl32(v3, 12) + xxh_rotl32(v4, 18);
  348. } else {
  349. h32 = seed + PRIME32_5;
  350. }
  351. h32 += (uint32_t)len;
  352. while (p + 4 <= b_end) {
  353. h32 += get_unaligned_le32(p) * PRIME32_3;
  354. h32 = xxh_rotl32(h32, 17) * PRIME32_4;
  355. p += 4;
  356. }
  357. while (p < b_end) {
  358. h32 += (*p) * PRIME32_5;
  359. h32 = xxh_rotl32(h32, 11) * PRIME32_1;
  360. p++;
  361. }
  362. h32 ^= h32 >> 15;
  363. h32 *= PRIME32_2;
  364. h32 ^= h32 >> 13;
  365. h32 *= PRIME32_3;
  366. h32 ^= h32 >> 16;
  367. return h32;
  368. }
  369. static uint64_t xxh64_round(uint64_t acc, const uint64_t input)
  370. {
  371. acc += input * PRIME64_2;
  372. acc = xxh_rotl64(acc, 31);
  373. acc *= PRIME64_1;
  374. return acc;
  375. }
  376. static uint64_t xxh64_merge_round(uint64_t acc, uint64_t val)
  377. {
  378. val = xxh64_round(0, val);
  379. acc ^= val;
  380. acc = acc * PRIME64_1 + PRIME64_4;
  381. return acc;
  382. }
  383. XXH_API uint64_t xxh64(const void *input, const size_t len, const uint64_t seed)
  384. {
  385. const uint8_t *p = (const uint8_t *)input;
  386. const uint8_t *const b_end = p + len;
  387. uint64_t h64;
  388. if (len >= 32) {
  389. const uint8_t *const limit = b_end - 32;
  390. uint64_t v1 = seed + PRIME64_1 + PRIME64_2;
  391. uint64_t v2 = seed + PRIME64_2;
  392. uint64_t v3 = seed + 0;
  393. uint64_t v4 = seed - PRIME64_1;
  394. do {
  395. v1 = xxh64_round(v1, get_unaligned_le64(p));
  396. p += 8;
  397. v2 = xxh64_round(v2, get_unaligned_le64(p));
  398. p += 8;
  399. v3 = xxh64_round(v3, get_unaligned_le64(p));
  400. p += 8;
  401. v4 = xxh64_round(v4, get_unaligned_le64(p));
  402. p += 8;
  403. } while (p <= limit);
  404. h64 = xxh_rotl64(v1, 1) + xxh_rotl64(v2, 7) +
  405. xxh_rotl64(v3, 12) + xxh_rotl64(v4, 18);
  406. h64 = xxh64_merge_round(h64, v1);
  407. h64 = xxh64_merge_round(h64, v2);
  408. h64 = xxh64_merge_round(h64, v3);
  409. h64 = xxh64_merge_round(h64, v4);
  410. } else {
  411. h64 = seed + PRIME64_5;
  412. }
  413. h64 += (uint64_t)len;
  414. while (p + 8 <= b_end) {
  415. const uint64_t k1 = xxh64_round(0, get_unaligned_le64(p));
  416. h64 ^= k1;
  417. h64 = xxh_rotl64(h64, 27) * PRIME64_1 + PRIME64_4;
  418. p += 8;
  419. }
  420. if (p + 4 <= b_end) {
  421. h64 ^= (uint64_t)(get_unaligned_le32(p)) * PRIME64_1;
  422. h64 = xxh_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  423. p += 4;
  424. }
  425. while (p < b_end) {
  426. h64 ^= (*p) * PRIME64_5;
  427. h64 = xxh_rotl64(h64, 11) * PRIME64_1;
  428. p++;
  429. }
  430. h64 ^= h64 >> 33;
  431. h64 *= PRIME64_2;
  432. h64 ^= h64 >> 29;
  433. h64 *= PRIME64_3;
  434. h64 ^= h64 >> 32;
  435. return h64;
  436. }
  437. /*-**************************************************
  438. * Advanced Hash Functions
  439. ***************************************************/
  440. XXH_API void xxh32_reset(struct xxh32_state *statePtr, const uint32_t seed)
  441. {
  442. /* use a local state for memcpy() to avoid strict-aliasing warnings */
  443. struct xxh32_state state;
  444. __builtin_memset(&state, 0, sizeof(state));
  445. state.v1 = seed + PRIME32_1 + PRIME32_2;
  446. state.v2 = seed + PRIME32_2;
  447. state.v3 = seed + 0;
  448. state.v4 = seed - PRIME32_1;
  449. __builtin_memcpy(statePtr, &state, sizeof(state));
  450. }
  451. XXH_API void xxh64_reset(struct xxh64_state *statePtr, const uint64_t seed)
  452. {
  453. /* use a local state for memcpy() to avoid strict-aliasing warnings */
  454. struct xxh64_state state;
  455. __builtin_memset(&state, 0, sizeof(state));
  456. state.v1 = seed + PRIME64_1 + PRIME64_2;
  457. state.v2 = seed + PRIME64_2;
  458. state.v3 = seed + 0;
  459. state.v4 = seed - PRIME64_1;
  460. __builtin_memcpy(statePtr, &state, sizeof(state));
  461. }
  462. XXH_API int xxh32_update(struct xxh32_state *state, const void *input, const size_t len)
  463. {
  464. const uint8_t *p = (const uint8_t *)input;
  465. const uint8_t *const b_end = p + len;
  466. if (input == NULL)
  467. return -EINVAL;
  468. state->total_len_32 += (uint32_t)len;
  469. state->large_len |= (len >= 16) | (state->total_len_32 >= 16);
  470. if (state->memsize + len < 16) { /* fill in tmp buffer */
  471. __builtin_memcpy((uint8_t *)(state->mem32) + state->memsize, input, len);
  472. state->memsize += (uint32_t)len;
  473. return 0;
  474. }
  475. if (state->memsize) { /* some data left from previous update */
  476. const uint32_t *p32 = state->mem32;
  477. __builtin_memcpy((uint8_t *)(state->mem32) + state->memsize, input,
  478. 16 - state->memsize);
  479. state->v1 = xxh32_round(state->v1, get_unaligned_le32(p32));
  480. p32++;
  481. state->v2 = xxh32_round(state->v2, get_unaligned_le32(p32));
  482. p32++;
  483. state->v3 = xxh32_round(state->v3, get_unaligned_le32(p32));
  484. p32++;
  485. state->v4 = xxh32_round(state->v4, get_unaligned_le32(p32));
  486. p32++;
  487. p += 16-state->memsize;
  488. state->memsize = 0;
  489. }
  490. if (p <= b_end - 16) {
  491. const uint8_t *const limit = b_end - 16;
  492. uint32_t v1 = state->v1;
  493. uint32_t v2 = state->v2;
  494. uint32_t v3 = state->v3;
  495. uint32_t v4 = state->v4;
  496. do {
  497. v1 = xxh32_round(v1, get_unaligned_le32(p));
  498. p += 4;
  499. v2 = xxh32_round(v2, get_unaligned_le32(p));
  500. p += 4;
  501. v3 = xxh32_round(v3, get_unaligned_le32(p));
  502. p += 4;
  503. v4 = xxh32_round(v4, get_unaligned_le32(p));
  504. p += 4;
  505. } while (p <= limit);
  506. state->v1 = v1;
  507. state->v2 = v2;
  508. state->v3 = v3;
  509. state->v4 = v4;
  510. }
  511. if (p < b_end) {
  512. __builtin_memcpy(state->mem32, p, (size_t)(b_end-p));
  513. state->memsize = (uint32_t)(b_end-p);
  514. }
  515. return 0;
  516. }
  517. XXH_API uint32_t xxh32_digest(const struct xxh32_state *state)
  518. {
  519. const uint8_t *p = (const uint8_t *)state->mem32;
  520. const uint8_t *const b_end = (const uint8_t *)(state->mem32) +
  521. state->memsize;
  522. uint32_t h32;
  523. if (state->large_len) {
  524. h32 = xxh_rotl32(state->v1, 1) + xxh_rotl32(state->v2, 7) +
  525. xxh_rotl32(state->v3, 12) + xxh_rotl32(state->v4, 18);
  526. } else {
  527. h32 = state->v3 /* == seed */ + PRIME32_5;
  528. }
  529. h32 += state->total_len_32;
  530. while (p + 4 <= b_end) {
  531. h32 += get_unaligned_le32(p) * PRIME32_3;
  532. h32 = xxh_rotl32(h32, 17) * PRIME32_4;
  533. p += 4;
  534. }
  535. while (p < b_end) {
  536. h32 += (*p) * PRIME32_5;
  537. h32 = xxh_rotl32(h32, 11) * PRIME32_1;
  538. p++;
  539. }
  540. h32 ^= h32 >> 15;
  541. h32 *= PRIME32_2;
  542. h32 ^= h32 >> 13;
  543. h32 *= PRIME32_3;
  544. h32 ^= h32 >> 16;
  545. return h32;
  546. }
  547. XXH_API int xxh64_update(struct xxh64_state *state, const void *input, const size_t len)
  548. {
  549. const uint8_t *p = (const uint8_t *)input;
  550. const uint8_t *const b_end = p + len;
  551. if (input == NULL)
  552. return -EINVAL;
  553. state->total_len += len;
  554. if (state->memsize + len < 32) { /* fill in tmp buffer */
  555. __builtin_memcpy(((uint8_t *)state->mem64) + state->memsize, input, len);
  556. state->memsize += (uint32_t)len;
  557. return 0;
  558. }
  559. if (state->memsize) { /* tmp buffer is full */
  560. uint64_t *p64 = state->mem64;
  561. __builtin_memcpy(((uint8_t *)p64) + state->memsize, input,
  562. 32 - state->memsize);
  563. state->v1 = xxh64_round(state->v1, get_unaligned_le64(p64));
  564. p64++;
  565. state->v2 = xxh64_round(state->v2, get_unaligned_le64(p64));
  566. p64++;
  567. state->v3 = xxh64_round(state->v3, get_unaligned_le64(p64));
  568. p64++;
  569. state->v4 = xxh64_round(state->v4, get_unaligned_le64(p64));
  570. p += 32 - state->memsize;
  571. state->memsize = 0;
  572. }
  573. if (p + 32 <= b_end) {
  574. const uint8_t *const limit = b_end - 32;
  575. uint64_t v1 = state->v1;
  576. uint64_t v2 = state->v2;
  577. uint64_t v3 = state->v3;
  578. uint64_t v4 = state->v4;
  579. do {
  580. v1 = xxh64_round(v1, get_unaligned_le64(p));
  581. p += 8;
  582. v2 = xxh64_round(v2, get_unaligned_le64(p));
  583. p += 8;
  584. v3 = xxh64_round(v3, get_unaligned_le64(p));
  585. p += 8;
  586. v4 = xxh64_round(v4, get_unaligned_le64(p));
  587. p += 8;
  588. } while (p <= limit);
  589. state->v1 = v1;
  590. state->v2 = v2;
  591. state->v3 = v3;
  592. state->v4 = v4;
  593. }
  594. if (p < b_end) {
  595. __builtin_memcpy(state->mem64, p, (size_t)(b_end-p));
  596. state->memsize = (uint32_t)(b_end - p);
  597. }
  598. return 0;
  599. }
  600. XXH_API uint64_t xxh64_digest(const struct xxh64_state *state)
  601. {
  602. const uint8_t *p = (const uint8_t *)state->mem64;
  603. const uint8_t *const b_end = (const uint8_t *)state->mem64 +
  604. state->memsize;
  605. uint64_t h64;
  606. if (state->total_len >= 32) {
  607. const uint64_t v1 = state->v1;
  608. const uint64_t v2 = state->v2;
  609. const uint64_t v3 = state->v3;
  610. const uint64_t v4 = state->v4;
  611. h64 = xxh_rotl64(v1, 1) + xxh_rotl64(v2, 7) +
  612. xxh_rotl64(v3, 12) + xxh_rotl64(v4, 18);
  613. h64 = xxh64_merge_round(h64, v1);
  614. h64 = xxh64_merge_round(h64, v2);
  615. h64 = xxh64_merge_round(h64, v3);
  616. h64 = xxh64_merge_round(h64, v4);
  617. } else {
  618. h64 = state->v3 + PRIME64_5;
  619. }
  620. h64 += (uint64_t)state->total_len;
  621. while (p + 8 <= b_end) {
  622. const uint64_t k1 = xxh64_round(0, get_unaligned_le64(p));
  623. h64 ^= k1;
  624. h64 = xxh_rotl64(h64, 27) * PRIME64_1 + PRIME64_4;
  625. p += 8;
  626. }
  627. if (p + 4 <= b_end) {
  628. h64 ^= (uint64_t)(get_unaligned_le32(p)) * PRIME64_1;
  629. h64 = xxh_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  630. p += 4;
  631. }
  632. while (p < b_end) {
  633. h64 ^= (*p) * PRIME64_5;
  634. h64 = xxh_rotl64(h64, 11) * PRIME64_1;
  635. p++;
  636. }
  637. h64 ^= h64 >> 33;
  638. h64 *= PRIME64_2;
  639. h64 ^= h64 >> 29;
  640. h64 *= PRIME64_3;
  641. h64 ^= h64 >> 32;
  642. return h64;
  643. }
  644. #endif /* XXHASH_H */