xxph3.h 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. /*
  6. xxHash - Extremely Fast Hash algorithm
  7. Header File
  8. Copyright (C) 2012-2016, Yann Collet.
  9. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are
  12. met:
  13. * Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above
  16. copyright notice, this list of conditions and the following disclaimer
  17. in the documentation and/or other materials provided with the
  18. distribution.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. You can contact the author at :
  31. - xxHash source repository : https://github.com/Cyan4973/xxHash
  32. */
  33. // This is a fork of a preview version of xxHash, as RocksDB depends on
  34. // this preview version of XXH3. To allow this to coexist with the
  35. // standard xxHash, including in the "unity" build where all source files
  36. // and headers go into a single translation unit, here "XXH" has been
  37. // replaced with "XXPH" for XX Preview Hash.
  38. #ifndef XXPHASH_H_5627135585666179
  39. #define XXPHASH_H_5627135585666179 1
  40. /* BEGIN RocksDB customizations */
  41. #ifndef XXPH_STATIC_LINKING_ONLY
  42. // Access experimental APIs
  43. #define XXPH_STATIC_LINKING_ONLY 1
  44. #endif
  45. #define XXPH_NAMESPACE ROCKSDB_
  46. #define XXPH_INLINE_ALL
  47. #include <cstring>
  48. /* END RocksDB customizations */
  49. // clang-format off
  50. #if defined (__cplusplus)
  51. extern "C" {
  52. #endif
  53. /* ****************************
  54. * Definitions
  55. ******************************/
  56. #include <stddef.h> /* size_t */
  57. typedef enum { XXPH_OK=0, XXPH_ERROR } XXPH_errorcode;
  58. /* ****************************
  59. * API modifier
  60. ******************************/
  61. /** XXPH_INLINE_ALL (and XXPH_PRIVATE_API)
  62. * This build macro includes xxhash functions in `static` mode
  63. * in order to inline them, and remove their symbol from the public list.
  64. * Inlining offers great performance improvement on small keys,
  65. * and dramatic ones when length is expressed as a compile-time constant.
  66. * See https://fastcompression.blogspot.com/2018/03/xxhash-for-small-keys-impressive-power.html .
  67. * Methodology :
  68. * #define XXPH_INLINE_ALL
  69. * #include "xxhash.h"
  70. * `xxhash.c` is automatically included.
  71. * It's not useful to compile and link it as a separate object.
  72. */
  73. #if defined(XXPH_INLINE_ALL) || defined(XXPH_PRIVATE_API)
  74. # ifndef XXPH_STATIC_LINKING_ONLY
  75. # define XXPH_STATIC_LINKING_ONLY
  76. # endif
  77. # if defined(__GNUC__)
  78. # define XXPH_PUBLIC_API static __inline __attribute__((unused))
  79. # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  80. # define XXPH_PUBLIC_API static inline
  81. # elif defined(_MSC_VER)
  82. # define XXPH_PUBLIC_API static __inline
  83. # else
  84. /* this version may generate warnings for unused static functions */
  85. # define XXPH_PUBLIC_API static
  86. # endif
  87. #else
  88. # if defined(WIN32) && defined(_MSC_VER) && (defined(XXPH_IMPORT) || defined(XXPH_EXPORT))
  89. # ifdef XXPH_EXPORT
  90. # define XXPH_PUBLIC_API __declspec(dllexport)
  91. # elif XXPH_IMPORT
  92. # define XXPH_PUBLIC_API __declspec(dllimport)
  93. # endif
  94. # else
  95. # define XXPH_PUBLIC_API /* do nothing */
  96. # endif
  97. #endif /* XXPH_INLINE_ALL || XXPH_PRIVATE_API */
  98. /*! XXPH_NAMESPACE, aka Namespace Emulation :
  99. *
  100. * If you want to include _and expose_ xxHash functions from within your own library,
  101. * but also want to avoid symbol collisions with other libraries which may also include xxHash,
  102. *
  103. * you can use XXPH_NAMESPACE, to automatically prefix any public symbol from xxhash library
  104. * with the value of XXPH_NAMESPACE (therefore, avoid NULL and numeric values).
  105. *
  106. * Note that no change is required within the calling program as long as it includes `xxhash.h` :
  107. * regular symbol name will be automatically translated by this header.
  108. */
  109. #ifdef XXPH_NAMESPACE
  110. # define XXPH_CAT(A,B) A##B
  111. # define XXPH_NAME2(A,B) XXPH_CAT(A,B)
  112. # define XXPH_versionNumber XXPH_NAME2(XXPH_NAMESPACE, XXPH_versionNumber)
  113. #endif
  114. /* *************************************
  115. * Version
  116. ***************************************/
  117. #define XXPH_VERSION_MAJOR 0
  118. #define XXPH_VERSION_MINOR 7
  119. #define XXPH_VERSION_RELEASE 2
  120. #define XXPH_VERSION_NUMBER (XXPH_VERSION_MAJOR *100*100 + XXPH_VERSION_MINOR *100 + XXPH_VERSION_RELEASE)
  121. XXPH_PUBLIC_API unsigned XXPH_versionNumber (void);
  122. /*-**********************************************************************
  123. * 32-bit hash
  124. ************************************************************************/
  125. #if !defined (__VMS) \
  126. && (defined (__cplusplus) \
  127. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  128. # include <stdint.h>
  129. typedef uint32_t XXPH32_hash_t;
  130. #else
  131. # include <limits.h>
  132. # if UINT_MAX == 0xFFFFFFFFUL
  133. typedef unsigned int XXPH32_hash_t;
  134. # else
  135. # if ULONG_MAX == 0xFFFFFFFFUL
  136. typedef unsigned long XXPH32_hash_t;
  137. # else
  138. # error "unsupported platform : need a 32-bit type"
  139. # endif
  140. # endif
  141. #endif
  142. #ifndef XXPH_NO_LONG_LONG
  143. /*-**********************************************************************
  144. * 64-bit hash
  145. ************************************************************************/
  146. #if !defined (__VMS) \
  147. && (defined (__cplusplus) \
  148. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  149. # include <stdint.h>
  150. typedef uint64_t XXPH64_hash_t;
  151. #else
  152. /* the following type must have a width of 64-bit */
  153. typedef unsigned long long XXPH64_hash_t;
  154. #endif
  155. #endif /* XXPH_NO_LONG_LONG */
  156. #ifdef XXPH_STATIC_LINKING_ONLY
  157. /* ================================================================================================
  158. This section contains declarations which are not guaranteed to remain stable.
  159. They may change in future versions, becoming incompatible with a different version of the library.
  160. These declarations should only be used with static linking.
  161. Never use them in association with dynamic linking !
  162. =================================================================================================== */
  163. /*-**********************************************************************
  164. * XXPH3
  165. * New experimental hash
  166. ************************************************************************/
  167. #ifndef XXPH_NO_LONG_LONG
  168. /* ============================================
  169. * XXPH3 is a new hash algorithm,
  170. * featuring improved speed performance for both small and large inputs.
  171. * See full speed analysis at : http://fastcompression.blogspot.com/2019/03/presenting-xxh3.html
  172. * In general, expect XXPH3 to run about ~2x faster on large inputs,
  173. * and >3x faster on small ones, though exact differences depend on platform.
  174. *
  175. * The algorithm is portable, will generate the same hash on all platforms.
  176. * It benefits greatly from vectorization units, but does not require it.
  177. *
  178. * XXPH3 offers 2 variants, _64bits and _128bits.
  179. * When only 64 bits are needed, prefer calling the _64bits variant :
  180. * it reduces the amount of mixing, resulting in faster speed on small inputs.
  181. * It's also generally simpler to manipulate a scalar return type than a struct.
  182. *
  183. * The XXPH3 algorithm is still considered experimental.
  184. * Produced results can still change between versions.
  185. * Results produced by v0.7.x are not comparable with results from v0.7.y .
  186. * It's nonetheless possible to use XXPH3 for ephemeral data (local sessions),
  187. * but avoid storing values in long-term storage for later reads.
  188. *
  189. * The API supports one-shot hashing, streaming mode, and custom secrets.
  190. *
  191. * There are still a number of opened questions that community can influence during the experimental period.
  192. * I'm trying to list a few of them below, though don't consider this list as complete.
  193. *
  194. * - 128-bits output type : currently defined as a structure of two 64-bits fields.
  195. * That's because 128-bit values do not exist in C standard.
  196. * Note that it means that, at byte level, result is not identical depending on endianess.
  197. * However, at field level, they are identical on all platforms.
  198. * The canonical representation solves the issue of identical byte-level representation across platforms,
  199. * which is necessary for serialization.
  200. * Q1 : Would there be a better representation for a 128-bit hash result ?
  201. * Q2 : Are the names of the inner 64-bit fields important ? Should they be changed ?
  202. *
  203. * - Prototype XXPH128() : XXPH128() uses the same arguments as XXPH64(), for consistency.
  204. * It means it maps to XXPH3_128bits_withSeed().
  205. * This variant is slightly slower than XXPH3_128bits(),
  206. * because the seed is now part of the algorithm, and can't be simplified.
  207. * Is that a good idea ?
  208. *
  209. * - Seed type for XXPH128() : currently, it's a single 64-bit value, like the 64-bit variant.
  210. * It could be argued that it's more logical to offer a 128-bit seed input parameter for a 128-bit hash.
  211. * But 128-bit seed is more difficult to use, since it requires to pass a structure instead of a scalar value.
  212. * Such a variant could either replace current one, or become an additional one.
  213. * Farmhash, for example, offers both variants (the 128-bits seed variant is called `doubleSeed`).
  214. * Follow up question : if both 64-bit and 128-bit seeds are allowed, which variant should be called XXPH128 ?
  215. *
  216. * - Result for len==0 : Currently, the result of hashing a zero-length input is always `0`.
  217. * It seems okay as a return value when using "default" secret and seed.
  218. * But is it still fine to return `0` when secret or seed are non-default ?
  219. * Are there use cases which could depend on generating a different hash result for zero-length input when the secret is different ?
  220. *
  221. * - Consistency (1) : Streaming XXPH128 uses an XXPH3 state, which is the same state as XXPH3_64bits().
  222. * It means a 128bit streaming loop must invoke the following symbols :
  223. * XXPH3_createState(), XXPH3_128bits_reset(), XXPH3_128bits_update() (loop), XXPH3_128bits_digest(), XXPH3_freeState().
  224. * Is that consistent enough ?
  225. *
  226. * - Consistency (2) : The canonical representation of `XXPH3_64bits` is provided by existing functions
  227. * XXPH64_canonicalFromHash(), and reverse operation XXPH64_hashFromCanonical().
  228. * As a mirror, canonical functions for XXPH128_hash_t results generated by `XXPH3_128bits`
  229. * are XXPH128_canonicalFromHash() and XXPH128_hashFromCanonical().
  230. * Which means, `XXPH3` doesn't appear in the names, because canonical functions operate on a type,
  231. * independently of which algorithm was used to generate that type.
  232. * Is that consistent enough ?
  233. */
  234. #ifdef XXPH_NAMESPACE
  235. # define XXPH3_64bits XXPH_NAME2(XXPH_NAMESPACE, XXPH3_64bits)
  236. # define XXPH3_64bits_withSecret XXPH_NAME2(XXPH_NAMESPACE, XXPH3_64bits_withSecret)
  237. # define XXPH3_64bits_withSeed XXPH_NAME2(XXPH_NAMESPACE, XXPH3_64bits_withSeed)
  238. #endif
  239. /* XXPH3_64bits() :
  240. * default 64-bit variant, using default secret and default seed of 0.
  241. * It's the fastest variant. */
  242. XXPH_PUBLIC_API XXPH64_hash_t XXPH3_64bits(const void* data, size_t len);
  243. /* XXPH3_64bits_withSecret() :
  244. * It's possible to provide any blob of bytes as a "secret" to generate the hash.
  245. * This makes it more difficult for an external actor to prepare an intentional collision.
  246. * The secret *must* be large enough (>= XXPH3_SECRET_SIZE_MIN).
  247. * It should consist of random bytes.
  248. * Avoid repeating same character, or sequences of bytes,
  249. * and especially avoid swathes of \0.
  250. * Failure to respect these conditions will result in a poor quality hash.
  251. */
  252. #define XXPH3_SECRET_SIZE_MIN 136
  253. XXPH_PUBLIC_API XXPH64_hash_t XXPH3_64bits_withSecret(const void* data, size_t len, const void* secret, size_t secretSize);
  254. /* XXPH3_64bits_withSeed() :
  255. * This variant generates on the fly a custom secret,
  256. * based on the default secret, altered using the `seed` value.
  257. * While this operation is decently fast, note that it's not completely free.
  258. * note : seed==0 produces same results as XXPH3_64bits() */
  259. XXPH_PUBLIC_API XXPH64_hash_t XXPH3_64bits_withSeed(const void* data, size_t len, XXPH64_hash_t seed);
  260. #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11+ */
  261. # include <stdalign.h>
  262. # define XXPH_ALIGN(n) alignas(n)
  263. #elif defined(__GNUC__)
  264. # define XXPH_ALIGN(n) __attribute__ ((aligned(n)))
  265. #elif defined(_MSC_VER)
  266. # define XXPH_ALIGN(n) __declspec(align(n))
  267. #else
  268. # define XXPH_ALIGN(n) /* disabled */
  269. #endif
  270. #define XXPH3_SECRET_DEFAULT_SIZE 192 /* minimum XXPH3_SECRET_SIZE_MIN */
  271. #endif /* XXPH_NO_LONG_LONG */
  272. /*-**********************************************************************
  273. * XXPH_INLINE_ALL
  274. ************************************************************************/
  275. #if defined(XXPH_INLINE_ALL) || defined(XXPH_PRIVATE_API)
  276. /* === RocksDB modification: was #include here but permanently inlining === */
  277. typedef struct {
  278. XXPH64_hash_t low64;
  279. XXPH64_hash_t high64;
  280. } XXPH128_hash_t;
  281. /* *************************************
  282. * Tuning parameters
  283. ***************************************/
  284. /*!XXPH_FORCE_MEMORY_ACCESS :
  285. * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
  286. * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
  287. * The below switch allow to select different access method for improved performance.
  288. * Method 0 (default) : use `memcpy()`. Safe and portable.
  289. * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
  290. * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
  291. * Method 2 : direct access. This method doesn't depend on compiler but violate C standard.
  292. * It can generate buggy code on targets which do not support unaligned memory accesses.
  293. * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
  294. * See http://stackoverflow.com/a/32095106/646947 for details.
  295. * Prefer these methods in priority order (0 > 1 > 2)
  296. */
  297. #ifndef XXPH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  298. # if !defined(__clang__) && defined(__GNUC__) && defined(__ARM_FEATURE_UNALIGNED) && defined(__ARM_ARCH) && (__ARM_ARCH == 6)
  299. # define XXPH_FORCE_MEMORY_ACCESS 2
  300. # elif !defined(__clang__) && ((defined(__INTEL_COMPILER) && !defined(_WIN32)) || \
  301. (defined(__GNUC__) && (defined(__ARM_ARCH) && __ARM_ARCH >= 7)))
  302. # define XXPH_FORCE_MEMORY_ACCESS 1
  303. # endif
  304. #endif
  305. /*!XXPH_ACCEPT_NULL_INPUT_POINTER :
  306. * If input pointer is NULL, xxHash default behavior is to dereference it, triggering a segfault.
  307. * When this macro is enabled, xxHash actively checks input for null pointer.
  308. * It it is, result for null input pointers is the same as a null-length input.
  309. */
  310. #ifndef XXPH_ACCEPT_NULL_INPUT_POINTER /* can be defined externally */
  311. # define XXPH_ACCEPT_NULL_INPUT_POINTER 0
  312. #endif
  313. /*!XXPH_FORCE_ALIGN_CHECK :
  314. * This is a minor performance trick, only useful with lots of very small keys.
  315. * It means : check for aligned/unaligned input.
  316. * The check costs one initial branch per hash;
  317. * set it to 0 when the input is guaranteed to be aligned,
  318. * or when alignment doesn't matter for performance.
  319. */
  320. #ifndef XXPH_FORCE_ALIGN_CHECK /* can be defined externally */
  321. # if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
  322. # define XXPH_FORCE_ALIGN_CHECK 0
  323. # else
  324. # define XXPH_FORCE_ALIGN_CHECK 1
  325. # endif
  326. #endif
  327. /*!XXPH_REROLL:
  328. * Whether to reroll XXPH32_finalize, and XXPH64_finalize,
  329. * instead of using an unrolled jump table/if statement loop.
  330. *
  331. * This is automatically defined on -Os/-Oz on GCC and Clang. */
  332. #ifndef XXPH_REROLL
  333. # if defined(__OPTIMIZE_SIZE__)
  334. # define XXPH_REROLL 1
  335. # else
  336. # define XXPH_REROLL 0
  337. # endif
  338. #endif
  339. #include <limits.h> /* ULLONG_MAX */
  340. #ifndef XXPH_STATIC_LINKING_ONLY
  341. #define XXPH_STATIC_LINKING_ONLY
  342. #endif
  343. /* *************************************
  344. * Compiler Specific Options
  345. ***************************************/
  346. #ifdef _MSC_VER /* Visual Studio */
  347. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  348. # define XXPH_FORCE_INLINE static __forceinline
  349. # define XXPH_NO_INLINE static __declspec(noinline)
  350. #else
  351. # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  352. # ifdef __GNUC__
  353. # define XXPH_FORCE_INLINE static inline __attribute__((always_inline))
  354. # define XXPH_NO_INLINE static __attribute__((noinline))
  355. # else
  356. # define XXPH_FORCE_INLINE static inline
  357. # define XXPH_NO_INLINE static
  358. # endif
  359. # else
  360. # define XXPH_FORCE_INLINE static
  361. # define XXPH_NO_INLINE static
  362. # endif /* __STDC_VERSION__ */
  363. #endif
  364. /* *************************************
  365. * Debug
  366. ***************************************/
  367. /* DEBUGLEVEL is expected to be defined externally,
  368. * typically through compiler command line.
  369. * Value must be a number. */
  370. #ifndef DEBUGLEVEL
  371. # define DEBUGLEVEL 0
  372. #endif
  373. #if (DEBUGLEVEL>=1)
  374. # include <assert.h> /* note : can still be disabled with NDEBUG */
  375. # define XXPH_ASSERT(c) assert(c)
  376. #else
  377. # define XXPH_ASSERT(c) ((void)0)
  378. #endif
  379. /* note : use after variable declarations */
  380. #define XXPH_STATIC_ASSERT(c) { enum { XXPH_sa = 1/(int)(!!(c)) }; }
  381. /* *************************************
  382. * Basic Types
  383. ***************************************/
  384. #if !defined (__VMS) \
  385. && (defined (__cplusplus) \
  386. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  387. # include <stdint.h>
  388. typedef uint8_t xxh_u8;
  389. #else
  390. typedef unsigned char xxh_u8;
  391. #endif
  392. typedef XXPH32_hash_t xxh_u32;
  393. /* === Memory access === */
  394. #if (defined(XXPH_FORCE_MEMORY_ACCESS) && (XXPH_FORCE_MEMORY_ACCESS==2))
  395. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  396. static xxh_u32 XXPH_read32(const void* memPtr) { return *(const xxh_u32*) memPtr; }
  397. #elif (defined(XXPH_FORCE_MEMORY_ACCESS) && (XXPH_FORCE_MEMORY_ACCESS==1))
  398. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  399. /* currently only defined for gcc and icc */
  400. typedef union { xxh_u32 u32; } __attribute__((packed)) unalign;
  401. static xxh_u32 XXPH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  402. #else
  403. /* portable and safe solution. Generally efficient.
  404. * see : http://stackoverflow.com/a/32095106/646947
  405. */
  406. static xxh_u32 XXPH_read32(const void* memPtr)
  407. {
  408. xxh_u32 val;
  409. memcpy(&val, memPtr, sizeof(val));
  410. return val;
  411. }
  412. #endif /* XXPH_FORCE_DIRECT_MEMORY_ACCESS */
  413. /* === Endianess === */
  414. /* XXPH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */
  415. #ifndef XXPH_CPU_LITTLE_ENDIAN
  416. # if defined(_WIN32) /* Windows is always little endian */ \
  417. || defined(__LITTLE_ENDIAN__) \
  418. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  419. # define XXPH_CPU_LITTLE_ENDIAN 1
  420. # elif defined(__BIG_ENDIAN__) \
  421. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  422. # define XXPH_CPU_LITTLE_ENDIAN 0
  423. # else
  424. static int XXPH_isLittleEndian(void)
  425. {
  426. const union { xxh_u32 u; xxh_u8 c[4]; } one = { 1 }; /* don't use static : performance detrimental */
  427. return one.c[0];
  428. }
  429. # define XXPH_CPU_LITTLE_ENDIAN XXPH_isLittleEndian()
  430. # endif
  431. #endif
  432. /* ****************************************
  433. * Compiler-specific Functions and Macros
  434. ******************************************/
  435. #define XXPH_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  436. #ifndef __has_builtin
  437. # define __has_builtin(x) 0
  438. #endif
  439. #if !defined(NO_CLANG_BUILTIN) && __has_builtin(__builtin_rotateleft32) && __has_builtin(__builtin_rotateleft64)
  440. # define XXPH_rotl32 __builtin_rotateleft32
  441. # define XXPH_rotl64 __builtin_rotateleft64
  442. /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */
  443. #elif defined(_MSC_VER)
  444. # define XXPH_rotl32(x,r) _rotl(x,r)
  445. # define XXPH_rotl64(x,r) _rotl64(x,r)
  446. #else
  447. # define XXPH_rotl32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))
  448. # define XXPH_rotl64(x,r) (((x) << (r)) | ((x) >> (64 - (r))))
  449. #endif
  450. #if defined(_MSC_VER) /* Visual Studio */
  451. # define XXPH_swap32 _byteswap_ulong
  452. #elif XXPH_GCC_VERSION >= 403
  453. # define XXPH_swap32 __builtin_bswap32
  454. #else
  455. static xxh_u32 XXPH_swap32 (xxh_u32 x)
  456. {
  457. return ((x << 24) & 0xff000000 ) |
  458. ((x << 8) & 0x00ff0000 ) |
  459. ((x >> 8) & 0x0000ff00 ) |
  460. ((x >> 24) & 0x000000ff );
  461. }
  462. #endif
  463. /* ***************************
  464. * Memory reads
  465. *****************************/
  466. typedef enum { XXPH_aligned, XXPH_unaligned } XXPH_alignment;
  467. XXPH_FORCE_INLINE xxh_u32 XXPH_readLE32(const void* ptr)
  468. {
  469. return XXPH_CPU_LITTLE_ENDIAN ? XXPH_read32(ptr) : XXPH_swap32(XXPH_read32(ptr));
  470. }
  471. XXPH_FORCE_INLINE xxh_u32
  472. XXPH_readLE32_align(const void* ptr, XXPH_alignment align)
  473. {
  474. if (align==XXPH_unaligned) {
  475. return XXPH_readLE32(ptr);
  476. } else {
  477. return XXPH_CPU_LITTLE_ENDIAN ? *(const xxh_u32*)ptr : XXPH_swap32(*(const xxh_u32*)ptr);
  478. }
  479. }
  480. /* *************************************
  481. * Misc
  482. ***************************************/
  483. XXPH_PUBLIC_API unsigned XXPH_versionNumber (void) { return XXPH_VERSION_NUMBER; }
  484. static const xxh_u32 PRIME32_1 = 0x9E3779B1U; /* 0b10011110001101110111100110110001 */
  485. static const xxh_u32 PRIME32_2 = 0x85EBCA77U; /* 0b10000101111010111100101001110111 */
  486. static const xxh_u32 PRIME32_3 = 0xC2B2AE3DU; /* 0b11000010101100101010111000111101 */
  487. static const xxh_u32 PRIME32_4 = 0x27D4EB2FU; /* 0b00100111110101001110101100101111 */
  488. static const xxh_u32 PRIME32_5 = 0x165667B1U; /* 0b00010110010101100110011110110001 */
  489. #ifndef XXPH_NO_LONG_LONG
  490. /* *******************************************************************
  491. * 64-bit hash functions
  492. *********************************************************************/
  493. /*====== Memory access ======*/
  494. typedef XXPH64_hash_t xxh_u64;
  495. #if (defined(XXPH_FORCE_MEMORY_ACCESS) && (XXPH_FORCE_MEMORY_ACCESS==2))
  496. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  497. static xxh_u64 XXPH_read64(const void* memPtr) { return *(const xxh_u64*) memPtr; }
  498. #elif (defined(XXPH_FORCE_MEMORY_ACCESS) && (XXPH_FORCE_MEMORY_ACCESS==1))
  499. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  500. /* currently only defined for gcc and icc */
  501. typedef union { xxh_u32 u32; xxh_u64 u64; } __attribute__((packed)) unalign64;
  502. static xxh_u64 XXPH_read64(const void* ptr) { return ((const unalign64*)ptr)->u64; }
  503. #else
  504. /* portable and safe solution. Generally efficient.
  505. * see : http://stackoverflow.com/a/32095106/646947
  506. */
  507. static xxh_u64 XXPH_read64(const void* memPtr)
  508. {
  509. xxh_u64 val;
  510. memcpy(&val, memPtr, sizeof(val));
  511. return val;
  512. }
  513. #endif /* XXPH_FORCE_DIRECT_MEMORY_ACCESS */
  514. #if defined(_MSC_VER) /* Visual Studio */
  515. # define XXPH_swap64 _byteswap_uint64
  516. #elif XXPH_GCC_VERSION >= 403
  517. # define XXPH_swap64 __builtin_bswap64
  518. #else
  519. static xxh_u64 XXPH_swap64 (xxh_u64 x)
  520. {
  521. return ((x << 56) & 0xff00000000000000ULL) |
  522. ((x << 40) & 0x00ff000000000000ULL) |
  523. ((x << 24) & 0x0000ff0000000000ULL) |
  524. ((x << 8) & 0x000000ff00000000ULL) |
  525. ((x >> 8) & 0x00000000ff000000ULL) |
  526. ((x >> 24) & 0x0000000000ff0000ULL) |
  527. ((x >> 40) & 0x000000000000ff00ULL) |
  528. ((x >> 56) & 0x00000000000000ffULL);
  529. }
  530. #endif
  531. XXPH_FORCE_INLINE xxh_u64 XXPH_readLE64(const void* ptr)
  532. {
  533. return XXPH_CPU_LITTLE_ENDIAN ? XXPH_read64(ptr) : XXPH_swap64(XXPH_read64(ptr));
  534. }
  535. XXPH_FORCE_INLINE xxh_u64
  536. XXPH_readLE64_align(const void* ptr, XXPH_alignment align)
  537. {
  538. if (align==XXPH_unaligned)
  539. return XXPH_readLE64(ptr);
  540. else
  541. return XXPH_CPU_LITTLE_ENDIAN ? *(const xxh_u64*)ptr : XXPH_swap64(*(const xxh_u64*)ptr);
  542. }
  543. /*====== xxh64 ======*/
  544. static const xxh_u64 PRIME64_1 = 0x9E3779B185EBCA87ULL; /* 0b1001111000110111011110011011000110000101111010111100101010000111 */
  545. static const xxh_u64 PRIME64_2 = 0xC2B2AE3D27D4EB4FULL; /* 0b1100001010110010101011100011110100100111110101001110101101001111 */
  546. static const xxh_u64 PRIME64_3 = 0x165667B19E3779F9ULL; /* 0b0001011001010110011001111011000110011110001101110111100111111001 */
  547. static const xxh_u64 PRIME64_4 = 0x85EBCA77C2B2AE63ULL; /* 0b1000010111101011110010100111011111000010101100101010111001100011 */
  548. static const xxh_u64 PRIME64_5 = 0x27D4EB2F165667C5ULL; /* 0b0010011111010100111010110010111100010110010101100110011111000101 */
  549. /* *********************************************************************
  550. * XXPH3
  551. * New generation hash designed for speed on small keys and vectorization
  552. ************************************************************************ */
  553. /*======== Was #include "xxh3.h", now inlined below ==========*/
  554. /*
  555. xxHash - Extremely Fast Hash algorithm
  556. Development source file for `xxh3`
  557. Copyright (C) 2019-present, Yann Collet.
  558. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  559. Redistribution and use in source and binary forms, with or without
  560. modification, are permitted provided that the following conditions are
  561. met:
  562. * Redistributions of source code must retain the above copyright
  563. notice, this list of conditions and the following disclaimer.
  564. * Redistributions in binary form must reproduce the above
  565. copyright notice, this list of conditions and the following disclaimer
  566. in the documentation and/or other materials provided with the
  567. distribution.
  568. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  569. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  570. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  571. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  572. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  573. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  574. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  575. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  576. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  577. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  578. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  579. You can contact the author at :
  580. - xxHash source repository : https://github.com/Cyan4973/xxHash
  581. */
  582. /* RocksDB Note: This file contains a preview release (xxhash repository
  583. version 0.7.2) of XXPH3 that is unlikely to be compatible with the final
  584. version of XXPH3. We have therefore renamed this XXPH3 ("preview"), for
  585. clarity so that we can continue to use this version even after
  586. integrating a newer incompatible version.
  587. */
  588. /* === Dependencies === */
  589. #undef XXPH_INLINE_ALL /* in case it's already defined */
  590. #define XXPH_INLINE_ALL
  591. /* === Compiler specifics === */
  592. #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* >= C99 */
  593. # define XXPH_RESTRICT restrict
  594. #else
  595. /* note : it might be useful to define __restrict or __restrict__ for some C++ compilers */
  596. # define XXPH_RESTRICT /* disable */
  597. #endif
  598. #if defined(__GNUC__)
  599. # if defined(__AVX2__)
  600. # include <immintrin.h>
  601. # elif defined(__SSE2__)
  602. # include <emmintrin.h>
  603. # elif defined(__ARM_NEON__) || defined(__ARM_NEON)
  604. # define inline __inline__ /* clang bug */
  605. # include <arm_neon.h>
  606. # undef inline
  607. # endif
  608. #elif defined(_MSC_VER)
  609. # include <intrin.h>
  610. #endif
  611. /*
  612. * Sanity check.
  613. *
  614. * XXPH3 only requires these features to be efficient:
  615. *
  616. * - Usable unaligned access
  617. * - A 32-bit or 64-bit ALU
  618. * - If 32-bit, a decent ADC instruction
  619. * - A 32 or 64-bit multiply with a 64-bit result
  620. *
  621. * Almost all 32-bit and 64-bit targets meet this, except for Thumb-1, the
  622. * classic 16-bit only subset of ARM's instruction set.
  623. *
  624. * First of all, Thumb-1 lacks support for the UMULL instruction which
  625. * performs the important long multiply. This means numerous __aeabi_lmul
  626. * calls.
  627. *
  628. * Second of all, the 8 functional registers are just not enough.
  629. * Setup for __aeabi_lmul, byteshift loads, pointers, and all arithmetic need
  630. * Lo registers, and this shuffling results in thousands more MOVs than A32.
  631. *
  632. * A32 and T32 don't have this limitation. They can access all 14 registers,
  633. * do a 32->64 multiply with UMULL, and the flexible operand is helpful too.
  634. *
  635. * If compiling Thumb-1 for a target which supports ARM instructions, we
  636. * will give a warning.
  637. *
  638. * Usually, if this happens, it is because of an accident and you probably
  639. * need to specify -march, as you probably meant to compileh for a newer
  640. * architecture.
  641. */
  642. #if defined(__thumb__) && !defined(__thumb2__) && defined(__ARM_ARCH_ISA_ARM)
  643. # warning "XXPH3 is highly inefficient without ARM or Thumb-2."
  644. #endif
  645. /* ==========================================
  646. * Vectorization detection
  647. * ========================================== */
  648. #define XXPH_SCALAR 0
  649. #define XXPH_SSE2 1
  650. #define XXPH_AVX2 2
  651. #define XXPH_NEON 3
  652. #define XXPH_VSX 4
  653. #ifndef XXPH_VECTOR /* can be defined on command line */
  654. # if defined(__AVX2__)
  655. # define XXPH_VECTOR XXPH_AVX2
  656. # elif defined(__SSE2__) || defined(_M_AMD64) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP == 2))
  657. # define XXPH_VECTOR XXPH_SSE2
  658. # elif defined(__GNUC__) /* msvc support maybe later */ \
  659. && (defined(__ARM_NEON__) || defined(__ARM_NEON)) \
  660. && (defined(__LITTLE_ENDIAN__) /* We only support little endian NEON */ \
  661. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  662. # define XXPH_VECTOR XXPH_NEON
  663. # elif defined(__PPC64__) && defined(__POWER8_VECTOR__) && defined(__GNUC__)
  664. # define XXPH_VECTOR XXPH_VSX
  665. # else
  666. # define XXPH_VECTOR XXPH_SCALAR
  667. # endif
  668. #endif
  669. /* control alignment of accumulator,
  670. * for compatibility with fast vector loads */
  671. #ifndef XXPH_ACC_ALIGN
  672. # if XXPH_VECTOR == 0 /* scalar */
  673. # define XXPH_ACC_ALIGN 8
  674. # elif XXPH_VECTOR == 1 /* sse2 */
  675. # define XXPH_ACC_ALIGN 16
  676. # elif XXPH_VECTOR == 2 /* avx2 */
  677. # define XXPH_ACC_ALIGN 32
  678. # elif XXPH_VECTOR == 3 /* neon */
  679. # define XXPH_ACC_ALIGN 16
  680. # elif XXPH_VECTOR == 4 /* vsx */
  681. # define XXPH_ACC_ALIGN 16
  682. # endif
  683. #endif
  684. /* xxh_u64 XXPH_mult32to64(xxh_u32 a, xxh_u64 b) { return (xxh_u64)a * (xxh_u64)b; } */
  685. #if defined(_MSC_VER) && defined(_M_IX86)
  686. # include <intrin.h>
  687. # define XXPH_mult32to64(x, y) __emulu(x, y)
  688. #else
  689. # define XXPH_mult32to64(x, y) ((xxh_u64)((x) & 0xFFFFFFFF) * (xxh_u64)((y) & 0xFFFFFFFF))
  690. #endif
  691. /* VSX stuff. It's a lot because VSX support is mediocre across compilers and
  692. * there is a lot of mischief with endianness. */
  693. #if XXPH_VECTOR == XXPH_VSX
  694. # include <altivec.h>
  695. # undef vector
  696. typedef __vector unsigned long long U64x2;
  697. typedef __vector unsigned char U8x16;
  698. typedef __vector unsigned U32x4;
  699. #ifndef XXPH_VSX_BE
  700. # if defined(__BIG_ENDIAN__) \
  701. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  702. # define XXPH_VSX_BE 1
  703. # elif defined(__VEC_ELEMENT_REG_ORDER__) && __VEC_ELEMENT_REG_ORDER__ == __ORDER_BIG_ENDIAN__
  704. # warning "-maltivec=be is not recommended. Please use native endianness."
  705. # define XXPH_VSX_BE 1
  706. # else
  707. # define XXPH_VSX_BE 0
  708. # endif
  709. #endif
  710. /* We need some helpers for big endian mode. */
  711. #if XXPH_VSX_BE
  712. /* A wrapper for POWER9's vec_revb. */
  713. # ifdef __POWER9_VECTOR__
  714. # define XXPH_vec_revb vec_revb
  715. # else
  716. XXPH_FORCE_INLINE U64x2 XXPH_vec_revb(U64x2 val)
  717. {
  718. U8x16 const vByteSwap = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
  719. 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08 };
  720. return vec_perm(val, val, vByteSwap);
  721. }
  722. # endif
  723. /* Power8 Crypto gives us vpermxor which is very handy for
  724. * PPC64EB.
  725. *
  726. * U8x16 vpermxor(U8x16 a, U8x16 b, U8x16 mask)
  727. * {
  728. * U8x16 ret;
  729. * for (int i = 0; i < 16; i++) {
  730. * ret[i] = a[mask[i] & 0xF] ^ b[mask[i] >> 4];
  731. * }
  732. * return ret;
  733. * }
  734. *
  735. * Because both of the main loops load the key, swap, and xor it with input,
  736. * we can combine the key swap into this instruction.
  737. */
  738. # ifdef vec_permxor
  739. # define XXPH_vec_permxor vec_permxor
  740. # else
  741. # define XXPH_vec_permxor __builtin_crypto_vpermxor
  742. # endif
  743. #endif /* XXPH_VSX_BE */
  744. /*
  745. * Because we reinterpret the multiply, there are endian memes: vec_mulo actually becomes
  746. * vec_mule.
  747. *
  748. * Additionally, the intrinsic wasn't added until GCC 8, despite existing for a while.
  749. * Clang has an easy way to control this, we can just use the builtin which doesn't swap.
  750. * GCC needs inline assembly. */
  751. #if __has_builtin(__builtin_altivec_vmuleuw)
  752. # define XXPH_vec_mulo __builtin_altivec_vmulouw
  753. # define XXPH_vec_mule __builtin_altivec_vmuleuw
  754. #else
  755. /* Adapted from https://github.com/google/highwayhash/blob/master/highwayhash/hh_vsx.h. */
  756. XXPH_FORCE_INLINE U64x2 XXPH_vec_mulo(U32x4 a, U32x4 b) {
  757. U64x2 result;
  758. __asm__("vmulouw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
  759. return result;
  760. }
  761. XXPH_FORCE_INLINE U64x2 XXPH_vec_mule(U32x4 a, U32x4 b) {
  762. U64x2 result;
  763. __asm__("vmuleuw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
  764. return result;
  765. }
  766. #endif /* __has_builtin(__builtin_altivec_vmuleuw) */
  767. #endif /* XXPH_VECTOR == XXPH_VSX */
  768. /* prefetch
  769. * can be disabled, by declaring XXPH_NO_PREFETCH build macro */
  770. #if defined(XXPH_NO_PREFETCH)
  771. # define XXPH_PREFETCH(ptr) (void)(ptr) /* disabled */
  772. #else
  773. #if defined(_MSC_VER) && \
  774. (defined(_M_X64) || \
  775. defined(_M_IX86)) /* _mm_prefetch() is not defined outside of x86/x64 */
  776. # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
  777. # define XXPH_PREFETCH(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
  778. # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  779. # define XXPH_PREFETCH(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  780. # else
  781. # define XXPH_PREFETCH(ptr) (void)(ptr) /* disabled */
  782. # endif
  783. #endif /* XXPH_NO_PREFETCH */
  784. /* ==========================================
  785. * XXPH3 default settings
  786. * ========================================== */
  787. #define XXPH_SECRET_DEFAULT_SIZE 192 /* minimum XXPH3_SECRET_SIZE_MIN */
  788. #if (XXPH_SECRET_DEFAULT_SIZE < XXPH3_SECRET_SIZE_MIN)
  789. # error "default keyset is not large enough"
  790. #endif
  791. XXPH_ALIGN(64) static const xxh_u8 kSecret[XXPH_SECRET_DEFAULT_SIZE] = {
  792. 0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe, 0x7c, 0x01, 0x81, 0x2c, 0xf7, 0x21, 0xad, 0x1c,
  793. 0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb, 0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f,
  794. 0xcb, 0x79, 0xe6, 0x4e, 0xcc, 0xc0, 0xe5, 0x78, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21,
  795. 0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e, 0xe0, 0x35, 0x90, 0xe6, 0x81, 0x3a, 0x26, 0x4c,
  796. 0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb, 0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3,
  797. 0x71, 0x64, 0x48, 0x97, 0xa2, 0x0d, 0xf9, 0x4e, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8,
  798. 0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f, 0xf9, 0xdc, 0xbb, 0xc7, 0xc7, 0x0b, 0x4f, 0x1d,
  799. 0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31, 0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64,
  800. 0xea, 0xc5, 0xac, 0x83, 0x34, 0xd3, 0xeb, 0xc3, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb,
  801. 0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49, 0xd3, 0x16, 0x55, 0x26, 0x29, 0xd4, 0x68, 0x9e,
  802. 0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc, 0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce,
  803. 0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e,
  804. };
  805. /*
  806. * GCC for x86 has a tendency to use SSE in this loop. While it
  807. * successfully avoids swapping (as MUL overwrites EAX and EDX), it
  808. * slows it down because instead of free register swap shifts, it
  809. * must use pshufd and punpckl/hd.
  810. *
  811. * To prevent this, we use this attribute to shut off SSE.
  812. */
  813. #if defined(__GNUC__) && !defined(__clang__) && defined(__i386__)
  814. __attribute__((__target__("no-sse")))
  815. #endif
  816. static XXPH128_hash_t
  817. XXPH_mult64to128(xxh_u64 lhs, xxh_u64 rhs)
  818. {
  819. /*
  820. * GCC/Clang __uint128_t method.
  821. *
  822. * On most 64-bit targets, GCC and Clang define a __uint128_t type.
  823. * This is usually the best way as it usually uses a native long 64-bit
  824. * multiply, such as MULQ on x86_64 or MUL + UMULH on aarch64.
  825. *
  826. * Usually.
  827. *
  828. * Despite being a 32-bit platform, Clang (and emscripten) define this
  829. * type despite not having the arithmetic for it. This results in a
  830. * laggy compiler builtin call which calculates a full 128-bit multiply.
  831. * In that case it is best to use the portable one.
  832. * https://github.com/Cyan4973/xxHash/issues/211#issuecomment-515575677
  833. */
  834. #if defined(__GNUC__) && !defined(__wasm__) \
  835. && defined(__SIZEOF_INT128__) \
  836. || (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  837. __uint128_t product = (__uint128_t)lhs * (__uint128_t)rhs;
  838. XXPH128_hash_t const r128 = { (xxh_u64)(product), (xxh_u64)(product >> 64) };
  839. return r128;
  840. /*
  841. * MSVC for x64's _umul128 method.
  842. *
  843. * xxh_u64 _umul128(xxh_u64 Multiplier, xxh_u64 Multiplicand, xxh_u64 *HighProduct);
  844. *
  845. * This compiles to single operand MUL on x64.
  846. */
  847. #elif defined(_M_X64) || defined(_M_IA64)
  848. #ifndef _MSC_VER
  849. # pragma intrinsic(_umul128)
  850. #endif
  851. xxh_u64 product_high;
  852. xxh_u64 const product_low = _umul128(lhs, rhs, &product_high);
  853. XXPH128_hash_t const r128 = { product_low, product_high };
  854. return r128;
  855. #else
  856. /*
  857. * Portable scalar method. Optimized for 32-bit and 64-bit ALUs.
  858. *
  859. * This is a fast and simple grade school multiply, which is shown
  860. * below with base 10 arithmetic instead of base 0x100000000.
  861. *
  862. * 9 3 // D2 lhs = 93
  863. * x 7 5 // D2 rhs = 75
  864. * ----------
  865. * 1 5 // D2 lo_lo = (93 % 10) * (75 % 10)
  866. * 4 5 | // D2 hi_lo = (93 / 10) * (75 % 10)
  867. * 2 1 | // D2 lo_hi = (93 % 10) * (75 / 10)
  868. * + 6 3 | | // D2 hi_hi = (93 / 10) * (75 / 10)
  869. * ---------
  870. * 2 7 | // D2 cross = (15 / 10) + (45 % 10) + 21
  871. * + 6 7 | | // D2 upper = (27 / 10) + (45 / 10) + 63
  872. * ---------
  873. * 6 9 7 5
  874. *
  875. * The reasons for adding the products like this are:
  876. * 1. It avoids manual carry tracking. Just like how
  877. * (9 * 9) + 9 + 9 = 99, the same applies with this for
  878. * UINT64_MAX. This avoids a lot of complexity.
  879. *
  880. * 2. It hints for, and on Clang, compiles to, the powerful UMAAL
  881. * instruction available in ARMv6+ A32/T32, which is shown below:
  882. *
  883. * void UMAAL(xxh_u32 *RdLo, xxh_u32 *RdHi, xxh_u32 Rn, xxh_u32 Rm)
  884. * {
  885. * xxh_u64 product = (xxh_u64)*RdLo * (xxh_u64)*RdHi + Rn + Rm;
  886. * *RdLo = (xxh_u32)(product & 0xFFFFFFFF);
  887. * *RdHi = (xxh_u32)(product >> 32);
  888. * }
  889. *
  890. * This instruction was designed for efficient long multiplication,
  891. * and allows this to be calculated in only 4 instructions which
  892. * is comparable to some 64-bit ALUs.
  893. *
  894. * 3. It isn't terrible on other platforms. Usually this will be
  895. * a couple of 32-bit ADD/ADCs.
  896. */
  897. /* First calculate all of the cross products. */
  898. xxh_u64 const lo_lo = XXPH_mult32to64(lhs & 0xFFFFFFFF, rhs & 0xFFFFFFFF);
  899. xxh_u64 const hi_lo = XXPH_mult32to64(lhs >> 32, rhs & 0xFFFFFFFF);
  900. xxh_u64 const lo_hi = XXPH_mult32to64(lhs & 0xFFFFFFFF, rhs >> 32);
  901. xxh_u64 const hi_hi = XXPH_mult32to64(lhs >> 32, rhs >> 32);
  902. /* Now add the products together. These will never overflow. */
  903. xxh_u64 const cross = (lo_lo >> 32) + (hi_lo & 0xFFFFFFFF) + lo_hi;
  904. xxh_u64 const upper = (hi_lo >> 32) + (cross >> 32) + hi_hi;
  905. xxh_u64 const lower = (cross << 32) | (lo_lo & 0xFFFFFFFF);
  906. XXPH128_hash_t r128 = { lower, upper };
  907. return r128;
  908. #endif
  909. }
  910. /*
  911. * We want to keep the attribute here because a target switch
  912. * disables inlining.
  913. *
  914. * Does a 64-bit to 128-bit multiply, then XOR folds it.
  915. * The reason for the separate function is to prevent passing
  916. * too many structs around by value. This will hopefully inline
  917. * the multiply, but we don't force it.
  918. */
  919. #if defined(__GNUC__) && !defined(__clang__) && defined(__i386__)
  920. __attribute__((__target__("no-sse")))
  921. #endif
  922. static xxh_u64
  923. XXPH3_mul128_fold64(xxh_u64 lhs, xxh_u64 rhs)
  924. {
  925. XXPH128_hash_t product = XXPH_mult64to128(lhs, rhs);
  926. return product.low64 ^ product.high64;
  927. }
  928. static XXPH64_hash_t XXPH3_avalanche(xxh_u64 h64)
  929. {
  930. h64 ^= h64 >> 37;
  931. h64 *= PRIME64_3;
  932. h64 ^= h64 >> 32;
  933. return h64;
  934. }
  935. /* ==========================================
  936. * Short keys
  937. * ========================================== */
  938. XXPH_FORCE_INLINE XXPH64_hash_t
  939. XXPH3_len_1to3_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXPH64_hash_t seed)
  940. {
  941. XXPH_ASSERT(input != NULL);
  942. XXPH_ASSERT(1 <= len && len <= 3);
  943. XXPH_ASSERT(secret != NULL);
  944. { xxh_u8 const c1 = input[0];
  945. xxh_u8 const c2 = input[len >> 1];
  946. xxh_u8 const c3 = input[len - 1];
  947. xxh_u32 const combined = ((xxh_u32)c1) | (((xxh_u32)c2) << 8) | (((xxh_u32)c3) << 16) | (((xxh_u32)len) << 24);
  948. xxh_u64 const keyed = (xxh_u64)combined ^ (XXPH_readLE32(secret) + seed);
  949. xxh_u64 const mixed = keyed * PRIME64_1;
  950. return XXPH3_avalanche(mixed);
  951. }
  952. }
  953. XXPH_FORCE_INLINE XXPH64_hash_t
  954. XXPH3_len_4to8_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXPH64_hash_t seed)
  955. {
  956. XXPH_ASSERT(input != NULL);
  957. XXPH_ASSERT(secret != NULL);
  958. XXPH_ASSERT(4 <= len && len <= 8);
  959. { xxh_u32 const input_lo = XXPH_readLE32(input);
  960. xxh_u32 const input_hi = XXPH_readLE32(input + len - 4);
  961. xxh_u64 const input_64 = input_lo | ((xxh_u64)input_hi << 32);
  962. xxh_u64 const keyed = input_64 ^ (XXPH_readLE64(secret) + seed);
  963. xxh_u64 const mix64 = len + ((keyed ^ (keyed >> 51)) * PRIME32_1);
  964. return XXPH3_avalanche((mix64 ^ (mix64 >> 47)) * PRIME64_2);
  965. }
  966. }
  967. XXPH_FORCE_INLINE XXPH64_hash_t
  968. XXPH3_len_9to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXPH64_hash_t seed)
  969. {
  970. XXPH_ASSERT(input != NULL);
  971. XXPH_ASSERT(secret != NULL);
  972. XXPH_ASSERT(9 <= len && len <= 16);
  973. { xxh_u64 const input_lo = XXPH_readLE64(input) ^ (XXPH_readLE64(secret) + seed);
  974. xxh_u64 const input_hi = XXPH_readLE64(input + len - 8) ^ (XXPH_readLE64(secret + 8) - seed);
  975. xxh_u64 const acc = len + (input_lo + input_hi) + XXPH3_mul128_fold64(input_lo, input_hi);
  976. return XXPH3_avalanche(acc);
  977. }
  978. }
  979. XXPH_FORCE_INLINE XXPH64_hash_t
  980. XXPH3_len_0to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXPH64_hash_t seed)
  981. {
  982. XXPH_ASSERT(len <= 16);
  983. { if (len > 8) return XXPH3_len_9to16_64b(input, len, secret, seed);
  984. if (len >= 4) return XXPH3_len_4to8_64b(input, len, secret, seed);
  985. if (len) return XXPH3_len_1to3_64b(input, len, secret, seed);
  986. /*
  987. * RocksDB modification from XXPH3 preview: zero result for empty
  988. * string can be problematic for multiplication-based algorithms.
  989. * Return a hash of the seed instead.
  990. */
  991. return XXPH3_mul128_fold64(seed + XXPH_readLE64(secret), PRIME64_2);
  992. }
  993. }
  994. /* === Long Keys === */
  995. #define STRIPE_LEN 64
  996. #define XXPH_SECRET_CONSUME_RATE 8 /* nb of secret bytes consumed at each accumulation */
  997. #define ACC_NB (STRIPE_LEN / sizeof(xxh_u64))
  998. typedef enum { XXPH3_acc_64bits, XXPH3_acc_128bits } XXPH3_accWidth_e;
  999. XXPH_FORCE_INLINE void
  1000. XXPH3_accumulate_512( void* XXPH_RESTRICT acc,
  1001. const void* XXPH_RESTRICT input,
  1002. const void* XXPH_RESTRICT secret,
  1003. XXPH3_accWidth_e accWidth)
  1004. {
  1005. #if (XXPH_VECTOR == XXPH_AVX2)
  1006. XXPH_ASSERT((((size_t)acc) & 31) == 0);
  1007. { XXPH_ALIGN(32) __m256i* const xacc = (__m256i *) acc;
  1008. const __m256i* const xinput = (const __m256i *) input; /* not really aligned, just for ptr arithmetic, and because _mm256_loadu_si256() requires this type */
  1009. const __m256i* const xsecret = (const __m256i *) secret; /* not really aligned, just for ptr arithmetic, and because _mm256_loadu_si256() requires this type */
  1010. size_t i;
  1011. for (i=0; i < STRIPE_LEN/sizeof(__m256i); i++) {
  1012. __m256i const data_vec = _mm256_loadu_si256 (xinput+i);
  1013. __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
  1014. __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec); /* uint32 dk[8] = {d0+k0, d1+k1, d2+k2, d3+k3, ...} */
  1015. __m256i const product = _mm256_mul_epu32 (data_key, _mm256_shuffle_epi32 (data_key, 0x31)); /* uint64 mul[4] = {dk0*dk1, dk2*dk3, ...} */
  1016. if (accWidth == XXPH3_acc_128bits) {
  1017. __m256i const data_swap = _mm256_shuffle_epi32(data_vec, _MM_SHUFFLE(1,0,3,2));
  1018. __m256i const sum = _mm256_add_epi64(xacc[i], data_swap);
  1019. xacc[i] = _mm256_add_epi64(product, sum);
  1020. } else { /* XXPH3_acc_64bits */
  1021. __m256i const sum = _mm256_add_epi64(xacc[i], data_vec);
  1022. xacc[i] = _mm256_add_epi64(product, sum);
  1023. }
  1024. } }
  1025. #elif (XXPH_VECTOR == XXPH_SSE2)
  1026. XXPH_ASSERT((((size_t)acc) & 15) == 0);
  1027. { XXPH_ALIGN(16) __m128i* const xacc = (__m128i *) acc;
  1028. const __m128i* const xinput = (const __m128i *) input; /* not really aligned, just for ptr arithmetic, and because _mm_loadu_si128() requires this type */
  1029. const __m128i* const xsecret = (const __m128i *) secret; /* not really aligned, just for ptr arithmetic, and because _mm_loadu_si128() requires this type */
  1030. size_t i;
  1031. for (i=0; i < STRIPE_LEN/sizeof(__m128i); i++) {
  1032. __m128i const data_vec = _mm_loadu_si128 (xinput+i);
  1033. __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
  1034. __m128i const data_key = _mm_xor_si128 (data_vec, key_vec); /* uint32 dk[8] = {d0+k0, d1+k1, d2+k2, d3+k3, ...} */
  1035. __m128i const product = _mm_mul_epu32 (data_key, _mm_shuffle_epi32 (data_key, 0x31)); /* uint64 mul[4] = {dk0*dk1, dk2*dk3, ...} */
  1036. if (accWidth == XXPH3_acc_128bits) {
  1037. __m128i const data_swap = _mm_shuffle_epi32(data_vec, _MM_SHUFFLE(1,0,3,2));
  1038. __m128i const sum = _mm_add_epi64(xacc[i], data_swap);
  1039. xacc[i] = _mm_add_epi64(product, sum);
  1040. } else { /* XXPH3_acc_64bits */
  1041. __m128i const sum = _mm_add_epi64(xacc[i], data_vec);
  1042. xacc[i] = _mm_add_epi64(product, sum);
  1043. }
  1044. } }
  1045. #elif (XXPH_VECTOR == XXPH_NEON)
  1046. XXPH_ASSERT((((size_t)acc) & 15) == 0);
  1047. {
  1048. XXPH_ALIGN(16) uint64x2_t* const xacc = (uint64x2_t *) acc;
  1049. /* We don't use a uint32x4_t pointer because it causes bus errors on ARMv7. */
  1050. uint8_t const* const xinput = (const uint8_t *) input;
  1051. uint8_t const* const xsecret = (const uint8_t *) secret;
  1052. size_t i;
  1053. for (i=0; i < STRIPE_LEN / sizeof(uint64x2_t); i++) {
  1054. #if !defined(__aarch64__) && !defined(__arm64__) && defined(__GNUC__) /* ARM32-specific hack */
  1055. /* vzip on ARMv7 Clang generates a lot of vmovs (technically vorrs) without this.
  1056. * vzip on 32-bit ARM NEON will overwrite the original register, and I think that Clang
  1057. * assumes I don't want to destroy it and tries to make a copy. This slows down the code
  1058. * a lot.
  1059. * aarch64 not only uses an entirely different syntax, but it requires three
  1060. * instructions...
  1061. * ext v1.16B, v0.16B, #8 // select high bits because aarch64 can't address them directly
  1062. * zip1 v3.2s, v0.2s, v1.2s // first zip
  1063. * zip2 v2.2s, v0.2s, v1.2s // second zip
  1064. * ...to do what ARM does in one:
  1065. * vzip.32 d0, d1 // Interleave high and low bits and overwrite. */
  1066. /* data_vec = xsecret[i]; */
  1067. uint8x16_t const data_vec = vld1q_u8(xinput + (i * 16));
  1068. /* key_vec = xsecret[i]; */
  1069. uint8x16_t const key_vec = vld1q_u8(xsecret + (i * 16));
  1070. /* data_key = data_vec ^ key_vec; */
  1071. uint32x4_t data_key;
  1072. if (accWidth == XXPH3_acc_64bits) {
  1073. /* Add first to prevent register swaps */
  1074. /* xacc[i] += data_vec; */
  1075. xacc[i] = vaddq_u64 (xacc[i], vreinterpretq_u64_u8(data_vec));
  1076. } else { /* XXPH3_acc_128bits */
  1077. /* xacc[i] += swap(data_vec); */
  1078. /* can probably be optimized better */
  1079. uint64x2_t const data64 = vreinterpretq_u64_u8(data_vec);
  1080. uint64x2_t const swapped= vextq_u64(data64, data64, 1);
  1081. xacc[i] = vaddq_u64 (xacc[i], swapped);
  1082. }
  1083. data_key = vreinterpretq_u32_u8(veorq_u8(data_vec, key_vec));
  1084. /* Here's the magic. We use the quirkiness of vzip to shuffle data_key in place.
  1085. * shuffle: data_key[0, 1, 2, 3] = data_key[0, 2, 1, 3] */
  1086. __asm__("vzip.32 %e0, %f0" : "+w" (data_key));
  1087. /* xacc[i] += (uint64x2_t) data_key[0, 1] * (uint64x2_t) data_key[2, 3]; */
  1088. xacc[i] = vmlal_u32(xacc[i], vget_low_u32(data_key), vget_high_u32(data_key));
  1089. #else
  1090. /* On aarch64, vshrn/vmovn seems to be equivalent to, if not faster than, the vzip method. */
  1091. /* data_vec = xsecret[i]; */
  1092. uint8x16_t const data_vec = vld1q_u8(xinput + (i * 16));
  1093. /* key_vec = xsecret[i]; */
  1094. uint8x16_t const key_vec = vld1q_u8(xsecret + (i * 16));
  1095. /* data_key = data_vec ^ key_vec; */
  1096. uint64x2_t const data_key = vreinterpretq_u64_u8(veorq_u8(data_vec, key_vec));
  1097. /* data_key_lo = (uint32x2_t) (data_key & 0xFFFFFFFF); */
  1098. uint32x2_t const data_key_lo = vmovn_u64 (data_key);
  1099. /* data_key_hi = (uint32x2_t) (data_key >> 32); */
  1100. uint32x2_t const data_key_hi = vshrn_n_u64 (data_key, 32);
  1101. if (accWidth == XXPH3_acc_64bits) {
  1102. /* xacc[i] += data_vec; */
  1103. xacc[i] = vaddq_u64 (xacc[i], vreinterpretq_u64_u8(data_vec));
  1104. } else { /* XXPH3_acc_128bits */
  1105. /* xacc[i] += swap(data_vec); */
  1106. uint64x2_t const data64 = vreinterpretq_u64_u8(data_vec);
  1107. uint64x2_t const swapped= vextq_u64(data64, data64, 1);
  1108. xacc[i] = vaddq_u64 (xacc[i], swapped);
  1109. }
  1110. /* xacc[i] += (uint64x2_t) data_key_lo * (uint64x2_t) data_key_hi; */
  1111. xacc[i] = vmlal_u32 (xacc[i], data_key_lo, data_key_hi);
  1112. #endif
  1113. }
  1114. }
  1115. #elif (XXPH_VECTOR == XXPH_VSX) && /* work around a compiler bug */ (__GNUC__ > 5)
  1116. U64x2* const xacc = (U64x2*) acc; /* presumed aligned */
  1117. U64x2 const* const xinput = (U64x2 const*) input; /* no alignment restriction */
  1118. U64x2 const* const xsecret = (U64x2 const*) secret; /* no alignment restriction */
  1119. U64x2 const v32 = { 32, 32 };
  1120. #if XXPH_VSX_BE
  1121. U8x16 const vXorSwap = { 0x07, 0x16, 0x25, 0x34, 0x43, 0x52, 0x61, 0x70,
  1122. 0x8F, 0x9E, 0xAD, 0xBC, 0xCB, 0xDA, 0xE9, 0xF8 };
  1123. #endif
  1124. size_t i;
  1125. for (i = 0; i < STRIPE_LEN / sizeof(U64x2); i++) {
  1126. /* data_vec = xinput[i]; */
  1127. /* key_vec = xsecret[i]; */
  1128. #if XXPH_VSX_BE
  1129. /* byteswap */
  1130. U64x2 const data_vec = XXPH_vec_revb(vec_vsx_ld(0, xinput + i));
  1131. U64x2 const key_raw = vec_vsx_ld(0, xsecret + i);
  1132. /* See comment above. data_key = data_vec ^ swap(xsecret[i]); */
  1133. U64x2 const data_key = (U64x2)XXPH_vec_permxor((U8x16)data_vec, (U8x16)key_raw, vXorSwap);
  1134. #else
  1135. U64x2 const data_vec = vec_vsx_ld(0, xinput + i);
  1136. U64x2 const key_vec = vec_vsx_ld(0, xsecret + i);
  1137. U64x2 const data_key = data_vec ^ key_vec;
  1138. #endif
  1139. /* shuffled = (data_key << 32) | (data_key >> 32); */
  1140. U32x4 const shuffled = (U32x4)vec_rl(data_key, v32);
  1141. /* product = ((U64x2)data_key & 0xFFFFFFFF) * ((U64x2)shuffled & 0xFFFFFFFF); */
  1142. U64x2 const product = XXPH_vec_mulo((U32x4)data_key, shuffled);
  1143. xacc[i] += product;
  1144. if (accWidth == XXPH3_acc_64bits) {
  1145. xacc[i] += data_vec;
  1146. } else { /* XXPH3_acc_128bits */
  1147. /* swap high and low halves */
  1148. U64x2 const data_swapped = vec_xxpermdi(data_vec, data_vec, 2);
  1149. xacc[i] += data_swapped;
  1150. }
  1151. }
  1152. #else /* scalar variant of Accumulator - universal */
  1153. XXPH_ALIGN(XXPH_ACC_ALIGN) xxh_u64* const xacc = (xxh_u64*) acc; /* presumed aligned on 32-bytes boundaries, little hint for the auto-vectorizer */
  1154. const xxh_u8* const xinput = (const xxh_u8*) input; /* no alignment restriction */
  1155. const xxh_u8* const xsecret = (const xxh_u8*) secret; /* no alignment restriction */
  1156. size_t i;
  1157. XXPH_ASSERT(((size_t)acc & (XXPH_ACC_ALIGN-1)) == 0);
  1158. for (i=0; i < ACC_NB; i++) {
  1159. xxh_u64 const data_val = XXPH_readLE64(xinput + 8*i);
  1160. xxh_u64 const data_key = data_val ^ XXPH_readLE64(xsecret + i*8);
  1161. if (accWidth == XXPH3_acc_64bits) {
  1162. xacc[i] += data_val;
  1163. } else {
  1164. xacc[i ^ 1] += data_val; /* swap adjacent lanes */
  1165. }
  1166. xacc[i] += XXPH_mult32to64(data_key & 0xFFFFFFFF, data_key >> 32);
  1167. }
  1168. #endif
  1169. }
  1170. XXPH_FORCE_INLINE void
  1171. XXPH3_scrambleAcc(void* XXPH_RESTRICT acc, const void* XXPH_RESTRICT secret)
  1172. {
  1173. #if (XXPH_VECTOR == XXPH_AVX2)
  1174. XXPH_ASSERT((((size_t)acc) & 31) == 0);
  1175. { XXPH_ALIGN(32) __m256i* const xacc = (__m256i*) acc;
  1176. const __m256i* const xsecret = (const __m256i *) secret; /* not really aligned, just for ptr arithmetic, and because _mm256_loadu_si256() requires this argument type */
  1177. const __m256i prime32 = _mm256_set1_epi32((int)PRIME32_1);
  1178. size_t i;
  1179. for (i=0; i < STRIPE_LEN/sizeof(__m256i); i++) {
  1180. /* xacc[i] ^= (xacc[i] >> 47) */
  1181. __m256i const acc_vec = xacc[i];
  1182. __m256i const shifted = _mm256_srli_epi64 (acc_vec, 47);
  1183. __m256i const data_vec = _mm256_xor_si256 (acc_vec, shifted);
  1184. /* xacc[i] ^= xsecret; */
  1185. __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
  1186. __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec);
  1187. /* xacc[i] *= PRIME32_1; */
  1188. __m256i const data_key_hi = _mm256_shuffle_epi32 (data_key, 0x31);
  1189. __m256i const prod_lo = _mm256_mul_epu32 (data_key, prime32);
  1190. __m256i const prod_hi = _mm256_mul_epu32 (data_key_hi, prime32);
  1191. xacc[i] = _mm256_add_epi64(prod_lo, _mm256_slli_epi64(prod_hi, 32));
  1192. }
  1193. }
  1194. #elif (XXPH_VECTOR == XXPH_SSE2)
  1195. XXPH_ASSERT((((size_t)acc) & 15) == 0);
  1196. { XXPH_ALIGN(16) __m128i* const xacc = (__m128i*) acc;
  1197. const __m128i* const xsecret = (const __m128i *) secret; /* not really aligned, just for ptr arithmetic, and because _mm_loadu_si128() requires this argument type */
  1198. const __m128i prime32 = _mm_set1_epi32((int)PRIME32_1);
  1199. size_t i;
  1200. for (i=0; i < STRIPE_LEN/sizeof(__m128i); i++) {
  1201. /* xacc[i] ^= (xacc[i] >> 47) */
  1202. __m128i const acc_vec = xacc[i];
  1203. __m128i const shifted = _mm_srli_epi64 (acc_vec, 47);
  1204. __m128i const data_vec = _mm_xor_si128 (acc_vec, shifted);
  1205. /* xacc[i] ^= xsecret; */
  1206. __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
  1207. __m128i const data_key = _mm_xor_si128 (data_vec, key_vec);
  1208. /* xacc[i] *= PRIME32_1; */
  1209. __m128i const data_key_hi = _mm_shuffle_epi32 (data_key, 0x31);
  1210. __m128i const prod_lo = _mm_mul_epu32 (data_key, prime32);
  1211. __m128i const prod_hi = _mm_mul_epu32 (data_key_hi, prime32);
  1212. xacc[i] = _mm_add_epi64(prod_lo, _mm_slli_epi64(prod_hi, 32));
  1213. }
  1214. }
  1215. #elif (XXPH_VECTOR == XXPH_NEON)
  1216. XXPH_ASSERT((((size_t)acc) & 15) == 0);
  1217. { uint64x2_t* const xacc = (uint64x2_t*) acc;
  1218. uint8_t const* const xsecret = (uint8_t const*) secret;
  1219. uint32x2_t const prime = vdup_n_u32 (PRIME32_1);
  1220. size_t i;
  1221. for (i=0; i < STRIPE_LEN/sizeof(uint64x2_t); i++) {
  1222. /* data_vec = xacc[i] ^ (xacc[i] >> 47); */
  1223. uint64x2_t const acc_vec = xacc[i];
  1224. uint64x2_t const shifted = vshrq_n_u64 (acc_vec, 47);
  1225. uint64x2_t const data_vec = veorq_u64 (acc_vec, shifted);
  1226. /* key_vec = xsecret[i]; */
  1227. uint32x4_t const key_vec = vreinterpretq_u32_u8(vld1q_u8(xsecret + (i * 16)));
  1228. /* data_key = data_vec ^ key_vec; */
  1229. uint32x4_t const data_key = veorq_u32 (vreinterpretq_u32_u64(data_vec), key_vec);
  1230. /* shuffled = { data_key[0, 2], data_key[1, 3] }; */
  1231. uint32x2x2_t const shuffled = vzip_u32 (vget_low_u32(data_key), vget_high_u32(data_key));
  1232. /* data_key *= PRIME32_1 */
  1233. /* prod_hi = (data_key >> 32) * PRIME32_1; */
  1234. uint64x2_t const prod_hi = vmull_u32 (shuffled.val[1], prime);
  1235. /* xacc[i] = prod_hi << 32; */
  1236. xacc[i] = vshlq_n_u64(prod_hi, 32);
  1237. /* xacc[i] += (prod_hi & 0xFFFFFFFF) * PRIME32_1; */
  1238. xacc[i] = vmlal_u32(xacc[i], shuffled.val[0], prime);
  1239. } }
  1240. #elif (XXPH_VECTOR == XXPH_VSX) && /* work around a compiler bug */ (__GNUC__ > 5)
  1241. U64x2* const xacc = (U64x2*) acc;
  1242. const U64x2* const xsecret = (const U64x2*) secret;
  1243. /* constants */
  1244. U64x2 const v32 = { 32, 32 };
  1245. U64x2 const v47 = { 47, 47 };
  1246. U32x4 const prime = { PRIME32_1, PRIME32_1, PRIME32_1, PRIME32_1 };
  1247. size_t i;
  1248. #if XXPH_VSX_BE
  1249. /* endian swap */
  1250. U8x16 const vXorSwap = { 0x07, 0x16, 0x25, 0x34, 0x43, 0x52, 0x61, 0x70,
  1251. 0x8F, 0x9E, 0xAD, 0xBC, 0xCB, 0xDA, 0xE9, 0xF8 };
  1252. #endif
  1253. for (i = 0; i < STRIPE_LEN / sizeof(U64x2); i++) {
  1254. U64x2 const acc_vec = xacc[i];
  1255. U64x2 const data_vec = acc_vec ^ (acc_vec >> v47);
  1256. /* key_vec = xsecret[i]; */
  1257. #if XXPH_VSX_BE
  1258. /* swap bytes words */
  1259. U64x2 const key_raw = vec_vsx_ld(0, xsecret + i);
  1260. U64x2 const data_key = (U64x2)XXPH_vec_permxor((U8x16)data_vec, (U8x16)key_raw, vXorSwap);
  1261. #else
  1262. U64x2 const key_vec = vec_vsx_ld(0, xsecret + i);
  1263. U64x2 const data_key = data_vec ^ key_vec;
  1264. #endif
  1265. /* data_key *= PRIME32_1 */
  1266. /* prod_lo = ((U64x2)data_key & 0xFFFFFFFF) * ((U64x2)prime & 0xFFFFFFFF); */
  1267. U64x2 const prod_even = XXPH_vec_mule((U32x4)data_key, prime);
  1268. /* prod_hi = ((U64x2)data_key >> 32) * ((U64x2)prime >> 32); */
  1269. U64x2 const prod_odd = XXPH_vec_mulo((U32x4)data_key, prime);
  1270. xacc[i] = prod_odd + (prod_even << v32);
  1271. }
  1272. #else /* scalar variant of Scrambler - universal */
  1273. XXPH_ALIGN(XXPH_ACC_ALIGN) xxh_u64* const xacc = (xxh_u64*) acc; /* presumed aligned on 32-bytes boundaries, little hint for the auto-vectorizer */
  1274. const xxh_u8* const xsecret = (const xxh_u8*) secret; /* no alignment restriction */
  1275. size_t i;
  1276. XXPH_ASSERT((((size_t)acc) & (XXPH_ACC_ALIGN-1)) == 0);
  1277. for (i=0; i < ACC_NB; i++) {
  1278. xxh_u64 const key64 = XXPH_readLE64(xsecret + 8*i);
  1279. xxh_u64 acc64 = xacc[i];
  1280. acc64 ^= acc64 >> 47;
  1281. acc64 ^= key64;
  1282. acc64 *= PRIME32_1;
  1283. xacc[i] = acc64;
  1284. }
  1285. #endif
  1286. }
  1287. #define XXPH_PREFETCH_DIST 384
  1288. /* assumption : nbStripes will not overflow secret size */
  1289. XXPH_FORCE_INLINE void
  1290. XXPH3_accumulate( xxh_u64* XXPH_RESTRICT acc,
  1291. const xxh_u8* XXPH_RESTRICT input,
  1292. const xxh_u8* XXPH_RESTRICT secret,
  1293. size_t nbStripes,
  1294. XXPH3_accWidth_e accWidth)
  1295. {
  1296. size_t n;
  1297. for (n = 0; n < nbStripes; n++ ) {
  1298. const xxh_u8* const in = input + n*STRIPE_LEN;
  1299. XXPH_PREFETCH(in + XXPH_PREFETCH_DIST);
  1300. XXPH3_accumulate_512(acc,
  1301. in,
  1302. secret + n*XXPH_SECRET_CONSUME_RATE,
  1303. accWidth);
  1304. }
  1305. }
  1306. /* note : clang auto-vectorizes well in SS2 mode _if_ this function is `static`,
  1307. * and doesn't auto-vectorize it at all if it is `FORCE_INLINE`.
  1308. * However, it auto-vectorizes better AVX2 if it is `FORCE_INLINE`
  1309. * Pretty much every other modes and compilers prefer `FORCE_INLINE`.
  1310. */
  1311. #if defined(__clang__) && (XXPH_VECTOR==0) && !defined(__AVX2__) && !defined(__arm__) && !defined(__thumb__)
  1312. static void
  1313. #else
  1314. XXPH_FORCE_INLINE void
  1315. #endif
  1316. XXPH3_hashLong_internal_loop( xxh_u64* XXPH_RESTRICT acc,
  1317. const xxh_u8* XXPH_RESTRICT input, size_t len,
  1318. const xxh_u8* XXPH_RESTRICT secret, size_t secretSize,
  1319. XXPH3_accWidth_e accWidth)
  1320. {
  1321. size_t const nb_rounds = (secretSize - STRIPE_LEN) / XXPH_SECRET_CONSUME_RATE;
  1322. size_t const block_len = STRIPE_LEN * nb_rounds;
  1323. size_t const nb_blocks = len / block_len;
  1324. size_t n;
  1325. XXPH_ASSERT(secretSize >= XXPH3_SECRET_SIZE_MIN);
  1326. for (n = 0; n < nb_blocks; n++) {
  1327. XXPH3_accumulate(acc, input + n*block_len, secret, nb_rounds, accWidth);
  1328. XXPH3_scrambleAcc(acc, secret + secretSize - STRIPE_LEN);
  1329. }
  1330. /* last partial block */
  1331. XXPH_ASSERT(len > STRIPE_LEN);
  1332. { size_t const nbStripes = (len - (block_len * nb_blocks)) / STRIPE_LEN;
  1333. XXPH_ASSERT(nbStripes <= (secretSize / XXPH_SECRET_CONSUME_RATE));
  1334. XXPH3_accumulate(acc, input + nb_blocks*block_len, secret, nbStripes, accWidth);
  1335. /* last stripe */
  1336. if (len & (STRIPE_LEN - 1)) {
  1337. const xxh_u8* const p = input + len - STRIPE_LEN;
  1338. #define XXPH_SECRET_LASTACC_START 7 /* do not align on 8, so that secret is different from scrambler */
  1339. XXPH3_accumulate_512(acc, p, secret + secretSize - STRIPE_LEN - XXPH_SECRET_LASTACC_START, accWidth);
  1340. } }
  1341. }
  1342. XXPH_FORCE_INLINE xxh_u64
  1343. XXPH3_mix2Accs(const xxh_u64* XXPH_RESTRICT acc, const xxh_u8* XXPH_RESTRICT secret)
  1344. {
  1345. return XXPH3_mul128_fold64(
  1346. acc[0] ^ XXPH_readLE64(secret),
  1347. acc[1] ^ XXPH_readLE64(secret+8) );
  1348. }
  1349. static XXPH64_hash_t
  1350. XXPH3_mergeAccs(const xxh_u64* XXPH_RESTRICT acc, const xxh_u8* XXPH_RESTRICT secret, xxh_u64 start)
  1351. {
  1352. xxh_u64 result64 = start;
  1353. result64 += XXPH3_mix2Accs(acc+0, secret + 0);
  1354. result64 += XXPH3_mix2Accs(acc+2, secret + 16);
  1355. result64 += XXPH3_mix2Accs(acc+4, secret + 32);
  1356. result64 += XXPH3_mix2Accs(acc+6, secret + 48);
  1357. return XXPH3_avalanche(result64);
  1358. }
  1359. #define XXPH3_INIT_ACC { PRIME32_3, PRIME64_1, PRIME64_2, PRIME64_3, \
  1360. PRIME64_4, PRIME32_2, PRIME64_5, PRIME32_1 };
  1361. XXPH_FORCE_INLINE XXPH64_hash_t
  1362. XXPH3_hashLong_internal(const xxh_u8* XXPH_RESTRICT input, size_t len,
  1363. const xxh_u8* XXPH_RESTRICT secret, size_t secretSize)
  1364. {
  1365. XXPH_ALIGN(XXPH_ACC_ALIGN) xxh_u64 acc[ACC_NB] = XXPH3_INIT_ACC;
  1366. XXPH3_hashLong_internal_loop(acc, input, len, secret, secretSize, XXPH3_acc_64bits);
  1367. /* converge into final hash */
  1368. XXPH_STATIC_ASSERT(sizeof(acc) == 64);
  1369. #define XXPH_SECRET_MERGEACCS_START 11 /* do not align on 8, so that secret is different from accumulator */
  1370. XXPH_ASSERT(secretSize >= sizeof(acc) + XXPH_SECRET_MERGEACCS_START);
  1371. return XXPH3_mergeAccs(acc, secret + XXPH_SECRET_MERGEACCS_START, (xxh_u64)len * PRIME64_1);
  1372. }
  1373. XXPH_NO_INLINE XXPH64_hash_t /* It's important for performance that XXPH3_hashLong is not inlined. Not sure why (uop cache maybe ?), but difference is large and easily measurable */
  1374. XXPH3_hashLong_64b_defaultSecret(const xxh_u8* XXPH_RESTRICT input, size_t len)
  1375. {
  1376. return XXPH3_hashLong_internal(input, len, kSecret, sizeof(kSecret));
  1377. }
  1378. XXPH_NO_INLINE XXPH64_hash_t /* It's important for performance that XXPH3_hashLong is not inlined. Not sure why (uop cache maybe ?), but difference is large and easily measurable */
  1379. XXPH3_hashLong_64b_withSecret(const xxh_u8* XXPH_RESTRICT input, size_t len,
  1380. const xxh_u8* XXPH_RESTRICT secret, size_t secretSize)
  1381. {
  1382. return XXPH3_hashLong_internal(input, len, secret, secretSize);
  1383. }
  1384. XXPH_FORCE_INLINE void XXPH_writeLE64(void* dst, xxh_u64 v64)
  1385. {
  1386. if (!XXPH_CPU_LITTLE_ENDIAN) v64 = XXPH_swap64(v64);
  1387. memcpy(dst, &v64, sizeof(v64));
  1388. }
  1389. /* XXPH3_initCustomSecret() :
  1390. * destination `customSecret` is presumed allocated and same size as `kSecret`.
  1391. */
  1392. XXPH_FORCE_INLINE void XXPH3_initCustomSecret(xxh_u8* customSecret, xxh_u64 seed64)
  1393. {
  1394. int const nbRounds = XXPH_SECRET_DEFAULT_SIZE / 16;
  1395. int i;
  1396. XXPH_STATIC_ASSERT((XXPH_SECRET_DEFAULT_SIZE & 15) == 0);
  1397. for (i=0; i < nbRounds; i++) {
  1398. XXPH_writeLE64(customSecret + 16*i, XXPH_readLE64(kSecret + 16*i) + seed64);
  1399. XXPH_writeLE64(customSecret + 16*i + 8, XXPH_readLE64(kSecret + 16*i + 8) - seed64);
  1400. }
  1401. }
  1402. /* XXPH3_hashLong_64b_withSeed() :
  1403. * Generate a custom key,
  1404. * based on alteration of default kSecret with the seed,
  1405. * and then use this key for long mode hashing.
  1406. * This operation is decently fast but nonetheless costs a little bit of time.
  1407. * Try to avoid it whenever possible (typically when seed==0).
  1408. */
  1409. XXPH_NO_INLINE XXPH64_hash_t /* It's important for performance that XXPH3_hashLong is not inlined. Not sure why (uop cache maybe ?), but difference is large and easily measurable */
  1410. XXPH3_hashLong_64b_withSeed(const xxh_u8* input, size_t len, XXPH64_hash_t seed)
  1411. {
  1412. XXPH_ALIGN(8) xxh_u8 secret[XXPH_SECRET_DEFAULT_SIZE];
  1413. if (seed==0) return XXPH3_hashLong_64b_defaultSecret(input, len);
  1414. XXPH3_initCustomSecret(secret, seed);
  1415. return XXPH3_hashLong_internal(input, len, secret, sizeof(secret));
  1416. }
  1417. XXPH_FORCE_INLINE xxh_u64 XXPH3_mix16B(const xxh_u8* XXPH_RESTRICT input,
  1418. const xxh_u8* XXPH_RESTRICT secret, xxh_u64 seed64)
  1419. {
  1420. xxh_u64 const input_lo = XXPH_readLE64(input);
  1421. xxh_u64 const input_hi = XXPH_readLE64(input+8);
  1422. return XXPH3_mul128_fold64(
  1423. input_lo ^ (XXPH_readLE64(secret) + seed64),
  1424. input_hi ^ (XXPH_readLE64(secret+8) - seed64) );
  1425. }
  1426. XXPH_FORCE_INLINE XXPH64_hash_t
  1427. XXPH3_len_17to128_64b(const xxh_u8* XXPH_RESTRICT input, size_t len,
  1428. const xxh_u8* XXPH_RESTRICT secret, size_t secretSize,
  1429. XXPH64_hash_t seed)
  1430. {
  1431. XXPH_ASSERT(secretSize >= XXPH3_SECRET_SIZE_MIN); (void)secretSize;
  1432. XXPH_ASSERT(16 < len && len <= 128);
  1433. { xxh_u64 acc = len * PRIME64_1;
  1434. if (len > 32) {
  1435. if (len > 64) {
  1436. if (len > 96) {
  1437. acc += XXPH3_mix16B(input+48, secret+96, seed);
  1438. acc += XXPH3_mix16B(input+len-64, secret+112, seed);
  1439. }
  1440. acc += XXPH3_mix16B(input+32, secret+64, seed);
  1441. acc += XXPH3_mix16B(input+len-48, secret+80, seed);
  1442. }
  1443. acc += XXPH3_mix16B(input+16, secret+32, seed);
  1444. acc += XXPH3_mix16B(input+len-32, secret+48, seed);
  1445. }
  1446. acc += XXPH3_mix16B(input+0, secret+0, seed);
  1447. acc += XXPH3_mix16B(input+len-16, secret+16, seed);
  1448. return XXPH3_avalanche(acc);
  1449. }
  1450. }
  1451. #define XXPH3_MIDSIZE_MAX 240
  1452. XXPH_NO_INLINE XXPH64_hash_t
  1453. XXPH3_len_129to240_64b(const xxh_u8* XXPH_RESTRICT input, size_t len,
  1454. const xxh_u8* XXPH_RESTRICT secret, size_t secretSize,
  1455. XXPH64_hash_t seed)
  1456. {
  1457. XXPH_ASSERT(secretSize >= XXPH3_SECRET_SIZE_MIN); (void)secretSize;
  1458. XXPH_ASSERT(128 < len && len <= XXPH3_MIDSIZE_MAX);
  1459. #define XXPH3_MIDSIZE_STARTOFFSET 3
  1460. #define XXPH3_MIDSIZE_LASTOFFSET 17
  1461. { xxh_u64 acc = len * PRIME64_1;
  1462. int const nbRounds = (int)len / 16;
  1463. int i;
  1464. for (i=0; i<8; i++) {
  1465. acc += XXPH3_mix16B(input+(16*i), secret+(16*i), seed);
  1466. }
  1467. acc = XXPH3_avalanche(acc);
  1468. XXPH_ASSERT(nbRounds >= 8);
  1469. for (i=8 ; i < nbRounds; i++) {
  1470. acc += XXPH3_mix16B(input+(16*i), secret+(16*(i-8)) + XXPH3_MIDSIZE_STARTOFFSET, seed);
  1471. }
  1472. /* last bytes */
  1473. acc += XXPH3_mix16B(input + len - 16, secret + XXPH3_SECRET_SIZE_MIN - XXPH3_MIDSIZE_LASTOFFSET, seed);
  1474. return XXPH3_avalanche(acc);
  1475. }
  1476. }
  1477. /* === Public entry point === */
  1478. XXPH_PUBLIC_API XXPH64_hash_t XXPH3_64bits(const void* input, size_t len)
  1479. {
  1480. if (len <= 16) return XXPH3_len_0to16_64b((const xxh_u8*)input, len, kSecret, 0);
  1481. if (len <= 128) return XXPH3_len_17to128_64b((const xxh_u8*)input, len, kSecret, sizeof(kSecret), 0);
  1482. if (len <= XXPH3_MIDSIZE_MAX) return XXPH3_len_129to240_64b((const xxh_u8*)input, len, kSecret, sizeof(kSecret), 0);
  1483. return XXPH3_hashLong_64b_defaultSecret((const xxh_u8*)input, len);
  1484. }
  1485. XXPH_PUBLIC_API XXPH64_hash_t
  1486. XXPH3_64bits_withSecret(const void* input, size_t len, const void* secret, size_t secretSize)
  1487. {
  1488. XXPH_ASSERT(secretSize >= XXPH3_SECRET_SIZE_MIN);
  1489. /* if an action must be taken should `secret` conditions not be respected,
  1490. * it should be done here.
  1491. * For now, it's a contract pre-condition.
  1492. * Adding a check and a branch here would cost performance at every hash */
  1493. if (len <= 16) return XXPH3_len_0to16_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, 0);
  1494. if (len <= 128) return XXPH3_len_17to128_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretSize, 0);
  1495. if (len <= XXPH3_MIDSIZE_MAX) return XXPH3_len_129to240_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretSize, 0);
  1496. return XXPH3_hashLong_64b_withSecret((const xxh_u8*)input, len, (const xxh_u8*)secret, secretSize);
  1497. }
  1498. XXPH_PUBLIC_API XXPH64_hash_t
  1499. XXPH3_64bits_withSeed(const void* input, size_t len, XXPH64_hash_t seed)
  1500. {
  1501. if (len <= 16) return XXPH3_len_0to16_64b((const xxh_u8*)input, len, kSecret, seed);
  1502. if (len <= 128) return XXPH3_len_17to128_64b((const xxh_u8*)input, len, kSecret, sizeof(kSecret), seed);
  1503. if (len <= XXPH3_MIDSIZE_MAX) return XXPH3_len_129to240_64b((const xxh_u8*)input, len, kSecret, sizeof(kSecret), seed);
  1504. return XXPH3_hashLong_64b_withSeed((const xxh_u8*)input, len, seed);
  1505. }
  1506. /* === XXPH3 streaming === */
  1507. /* RocksDB Note: unused & removed due to bug in preview version */
  1508. /*======== END #include "xxh3.h", now inlined above ==========*/
  1509. #endif /* XXPH_NO_LONG_LONG */
  1510. /* === END RocksDB modification of permanently inlining === */
  1511. #endif /* defined(XXPH_INLINE_ALL) || defined(XXPH_PRIVATE_API) */
  1512. #endif /* XXPH_STATIC_LINKING_ONLY */
  1513. #if defined (__cplusplus)
  1514. }
  1515. #endif
  1516. #endif /* XXPHASH_H_5627135585666179 */