t1ha.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Copyright (c) 2016-2020 Positive Technologies, https://www.ptsecurity.com,
  3. * Fast Positive Hash.
  4. *
  5. * Portions Copyright (c) 2010-2020 Leonid Yuriev <leo@yuriev.ru>,
  6. * The 1Hippeus project (t1h).
  7. *
  8. * This software is provided 'as-is', without any express or implied
  9. * warranty. In no event will the authors be held liable for any damages
  10. * arising from the use of this software.
  11. *
  12. * Permission is granted to anyone to use this software for any purpose,
  13. * including commercial applications, and to alter it and redistribute it
  14. * freely, subject to the following restrictions:
  15. *
  16. * 1. The origin of this software must not be misrepresented; you must not
  17. * claim that you wrote the original software. If you use this software
  18. * in a product, an acknowledgement in the product documentation would be
  19. * appreciated but is not required.
  20. * 2. Altered source versions must be plainly marked as such, and must not be
  21. * misrepresented as being the original software.
  22. * 3. This notice may not be removed or altered from any source distribution.
  23. */
  24. /*
  25. * t1ha = { Fast Positive Hash, aka "Позитивный Хэш" }
  26. * by [Positive Technologies](https://www.ptsecurity.ru)
  27. *
  28. * Briefly, it is a 64-bit Hash Function:
  29. * 1. Created for 64-bit little-endian platforms, in predominantly for x86_64,
  30. * but portable and without penalties it can run on any 64-bit CPU.
  31. * 2. In most cases up to 15% faster than City64, xxHash, mum-hash, metro-hash
  32. * and all others portable hash-functions (which do not use specific
  33. * hardware tricks).
  34. * 3. Not suitable for cryptography.
  35. *
  36. * The Future will (be) Positive. Всё будет хорошо.
  37. *
  38. * ACKNOWLEDGEMENT:
  39. * The t1ha was originally developed by Leonid Yuriev (Леонид Юрьев)
  40. * for The 1Hippeus project - zerocopy messaging in the spirit of Sparta!
  41. */
  42. #pragma once
  43. /*****************************************************************************
  44. *
  45. * PLEASE PAY ATTENTION TO THE FOLLOWING NOTES
  46. * about macros definitions which controls t1ha behaviour and/or performance.
  47. *
  48. *
  49. * 1) T1HA_SYS_UNALIGNED_ACCESS = Defines the system/platform/CPU/architecture
  50. * abilities for unaligned data access.
  51. *
  52. * By default, when the T1HA_SYS_UNALIGNED_ACCESS not defined,
  53. * it will defined on the basis hardcoded knowledge about of capabilities
  54. * of most common CPU architectures. But you could override this
  55. * default behavior when build t1ha library itself:
  56. *
  57. * // To disable unaligned access at all.
  58. * #define T1HA_SYS_UNALIGNED_ACCESS 0
  59. *
  60. * // To enable unaligned access, but indicate that it significantly slow.
  61. * #define T1HA_SYS_UNALIGNED_ACCESS 1
  62. *
  63. * // To enable unaligned access, and indicate that it effecient.
  64. * #define T1HA_SYS_UNALIGNED_ACCESS 2
  65. *
  66. *
  67. * 2) T1HA_USE_FAST_ONESHOT_READ = Controls the data reads at the end of buffer.
  68. *
  69. * When defined to non-zero, t1ha will use 'one shot' method for reading
  70. * up to 8 bytes at the end of data. In this case just the one 64-bit read
  71. * will be performed even when the available less than 8 bytes.
  72. *
  73. * This is little bit faster that switching by length of data tail.
  74. * Unfortunately this will triggering a false-positive alarms from Valgrind,
  75. * AddressSanitizer and other similar tool.
  76. *
  77. * By default, t1ha defines it to 1, but you could override this
  78. * default behavior when build t1ha library itself:
  79. *
  80. * // For little bit faster and small code.
  81. * #define T1HA_USE_FAST_ONESHOT_READ 1
  82. *
  83. * // For calmness if doubt.
  84. * #define T1HA_USE_FAST_ONESHOT_READ 0
  85. *
  86. *
  87. * 3) T1HA0_RUNTIME_SELECT = Controls choice fastest function in runtime.
  88. *
  89. * t1ha library offers the t1ha0() function as the fastest for current CPU.
  90. * But actual CPU's features/capabilities and may be significantly different,
  91. * especially on x86 platform. Therefore, internally, t1ha0() may require
  92. * dynamic dispatching for choice best implementation.
  93. *
  94. * By default, t1ha enables such runtime choice and (may be) corresponding
  95. * indirect calls if it reasonable, but you could override this default
  96. * behavior when build t1ha library itself:
  97. *
  98. * // To enable runtime choice of fastest implementation.
  99. * #define T1HA0_RUNTIME_SELECT 1
  100. *
  101. * // To disable runtime choice of fastest implementation.
  102. * #define T1HA0_RUNTIME_SELECT 0
  103. *
  104. * When T1HA0_RUNTIME_SELECT is nonzero the t1ha0_resolve() function could
  105. * be used to get actual t1ha0() implementation address at runtime. This is
  106. * useful for two cases:
  107. * - calling by local pointer-to-function usually is little
  108. * bit faster (less overhead) than via a PLT thru the DSO boundary.
  109. * - GNU Indirect functions (see below) don't supported by environment
  110. * and calling by t1ha0_funcptr is not available and/or expensive.
  111. *
  112. * 4) T1HA_USE_INDIRECT_FUNCTIONS = Controls usage of GNU Indirect functions.
  113. *
  114. * In continue of T1HA0_RUNTIME_SELECT the T1HA_USE_INDIRECT_FUNCTIONS
  115. * controls usage of ELF indirect functions feature. In general, when
  116. * available, this reduces overhead of indirect function's calls though
  117. * a DSO-bundary (https://sourceware.org/glibc/wiki/GNU_IFUNC).
  118. *
  119. * By default, t1ha engage GNU Indirect functions when it available
  120. * and useful, but you could override this default behavior when build
  121. * t1ha library itself:
  122. *
  123. * // To enable use of GNU ELF Indirect functions.
  124. * #define T1HA_USE_INDIRECT_FUNCTIONS 1
  125. *
  126. * // To disable use of GNU ELF Indirect functions. This may be useful
  127. * // if the actual toolchain or the system's loader don't support ones.
  128. * #define T1HA_USE_INDIRECT_FUNCTIONS 0
  129. *
  130. * 5) T1HA0_AESNI_AVAILABLE = Controls AES-NI detection and dispatching on x86.
  131. *
  132. * In continue of T1HA0_RUNTIME_SELECT the T1HA0_AESNI_AVAILABLE controls
  133. * detection and usage of AES-NI CPU's feature. On the other hand, this
  134. * requires compiling parts of t1ha library with certain properly options,
  135. * and could be difficult or inconvenient in some cases.
  136. *
  137. * By default, t1ha engade AES-NI for t1ha0() on the x86 platform, but
  138. * you could override this default behavior when build t1ha library itself:
  139. *
  140. * // To disable detection and usage of AES-NI instructions for t1ha0().
  141. * // This may be useful when you unable to build t1ha library properly
  142. * // or known that AES-NI will be unavailable at the deploy.
  143. * #define T1HA0_AESNI_AVAILABLE 0
  144. *
  145. * // To force detection and usage of AES-NI instructions for t1ha0(),
  146. * // but I don't known reasons to anybody would need this.
  147. * #define T1HA0_AESNI_AVAILABLE 1
  148. *
  149. * 6) T1HA0_DISABLED, T1HA1_DISABLED, T1HA2_DISABLED = Controls availability of
  150. * t1ha functions.
  151. *
  152. * In some cases could be useful to import/use only few of t1ha functions
  153. * or just the one. So, this definitions allows disable corresponding parts
  154. * of t1ha library.
  155. *
  156. * // To disable t1ha0(), t1ha0_32le(), t1ha0_32be() and all AES-NI.
  157. * #define T1HA0_DISABLED
  158. *
  159. * // To disable t1ha1_le() and t1ha1_be().
  160. * #define T1HA1_DISABLED
  161. *
  162. * // To disable t1ha2_atonce(), t1ha2_atonce128() and so on.
  163. * #define T1HA2_DISABLED
  164. *
  165. *****************************************************************************/
  166. #define T1HA_VERSION_MAJOR 2
  167. #define T1HA_VERSION_MINOR 1
  168. #define T1HA_VERSION_RELEASE 1
  169. #ifndef __has_attribute
  170. #define __has_attribute(x) (0)
  171. #endif
  172. #ifndef __has_include
  173. #define __has_include(x) (0)
  174. #endif
  175. #ifndef __GNUC_PREREQ
  176. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  177. #define __GNUC_PREREQ(maj, min) \
  178. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  179. #else
  180. #define __GNUC_PREREQ(maj, min) 0
  181. #endif
  182. #endif /* __GNUC_PREREQ */
  183. #ifndef __CLANG_PREREQ
  184. #ifdef __clang__
  185. #define __CLANG_PREREQ(maj, min) \
  186. ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
  187. #else
  188. #define __CLANG_PREREQ(maj, min) (0)
  189. #endif
  190. #endif /* __CLANG_PREREQ */
  191. #ifndef __LCC_PREREQ
  192. #ifdef __LCC__
  193. #define __LCC_PREREQ(maj, min) \
  194. ((__LCC__ << 16) + __LCC_MINOR__ >= ((maj) << 16) + (min))
  195. #else
  196. #define __LCC_PREREQ(maj, min) (0)
  197. #endif
  198. #endif /* __LCC_PREREQ */
  199. /*****************************************************************************/
  200. #ifdef _MSC_VER
  201. /* Avoid '16' bytes padding added after data member 't1ha_context::total'
  202. * and other warnings from std-headers if warning-level > 3. */
  203. #pragma warning(push, 3)
  204. #endif
  205. #if defined(__cplusplus) && __cplusplus >= 201103L
  206. #include <climits>
  207. #include <cstddef>
  208. #include <cstdint>
  209. #else
  210. #include <limits.h>
  211. #include <stddef.h>
  212. #include <stdint.h>
  213. #endif
  214. /*****************************************************************************/
  215. #if defined(i386) || defined(__386) || defined(__i386) || defined(__i386__) || \
  216. defined(i486) || defined(__i486) || defined(__i486__) || \
  217. defined(i586) | defined(__i586) || defined(__i586__) || defined(i686) || \
  218. defined(__i686) || defined(__i686__) || defined(_M_IX86) || \
  219. defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) || \
  220. defined(__INTEL__) || defined(__x86_64) || defined(__x86_64__) || \
  221. defined(__amd64__) || defined(__amd64) || defined(_M_X64) || \
  222. defined(_M_AMD64) || defined(__IA32__) || defined(__INTEL__)
  223. #ifndef __ia32__
  224. /* LY: define neutral __ia32__ for x86 and x86-64 archs */
  225. #define __ia32__ 1
  226. #endif /* __ia32__ */
  227. #if !defined(__amd64__) && (defined(__x86_64) || defined(__x86_64__) || \
  228. defined(__amd64) || defined(_M_X64))
  229. /* LY: define trusty __amd64__ for all AMD64/x86-64 arch */
  230. #define __amd64__ 1
  231. #endif /* __amd64__ */
  232. #endif /* all x86 */
  233. #if !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__) || \
  234. !defined(__ORDER_BIG_ENDIAN__)
  235. /* *INDENT-OFF* */
  236. /* clang-format off */
  237. #if defined(__GLIBC__) || defined(__GNU_LIBRARY__) || defined(__ANDROID__) || \
  238. defined(HAVE_ENDIAN_H) || __has_include(<endian.h>)
  239. #include <endian.h>
  240. #elif defined(__APPLE__) || defined(__MACH__) || defined(__OpenBSD__) || \
  241. defined(HAVE_MACHINE_ENDIAN_H) || __has_include(<machine/endian.h>)
  242. #include <machine/endian.h>
  243. #elif defined(HAVE_SYS_ISA_DEFS_H) || __has_include(<sys/isa_defs.h>)
  244. #error #include <sys/isa_defs.h>
  245. #elif (defined(HAVE_SYS_TYPES_H) && defined(HAVE_SYS_ENDIAN_H)) || \
  246. (__has_include(<sys/types.h>) && __has_include(<sys/endian.h>))
  247. #include <sys/endian.h>
  248. #include <sys/types.h>
  249. #elif defined(__bsdi__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
  250. defined(__NETBSD__) || defined(__NetBSD__) || \
  251. defined(HAVE_SYS_PARAM_H) || __has_include(<sys/param.h>)
  252. #include <sys/param.h>
  253. #endif /* OS */
  254. /* *INDENT-ON* */
  255. /* clang-format on */
  256. #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
  257. #define __ORDER_LITTLE_ENDIAN__ __LITTLE_ENDIAN
  258. #define __ORDER_BIG_ENDIAN__ __BIG_ENDIAN
  259. #define __BYTE_ORDER__ __BYTE_ORDER
  260. #elif defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
  261. #define __ORDER_LITTLE_ENDIAN__ _LITTLE_ENDIAN
  262. #define __ORDER_BIG_ENDIAN__ _BIG_ENDIAN
  263. #define __BYTE_ORDER__ _BYTE_ORDER
  264. #else
  265. #define __ORDER_LITTLE_ENDIAN__ 1234
  266. #define __ORDER_BIG_ENDIAN__ 4321
  267. #if defined(__LITTLE_ENDIAN__) || \
  268. (defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)) || \
  269. defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
  270. defined(__MIPSEL__) || defined(_MIPSEL) || defined(__MIPSEL) || \
  271. defined(_M_ARM) || defined(_M_ARM64) || defined(__e2k__) || \
  272. defined(__elbrus_4c__) || defined(__elbrus_8c__) || defined(__bfin__) || \
  273. defined(__BFIN__) || defined(__ia64__) || defined(_IA64) || \
  274. defined(__IA64__) || defined(__ia64) || defined(_M_IA64) || \
  275. defined(__itanium__) || defined(__ia32__) || defined(__CYGWIN__) || \
  276. defined(_WIN64) || defined(_WIN32) || defined(__TOS_WIN__) || \
  277. defined(__WINDOWS__)
  278. #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
  279. #elif defined(__BIG_ENDIAN__) || \
  280. (defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) || \
  281. defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
  282. defined(__MIPSEB__) || defined(_MIPSEB) || defined(__MIPSEB) || \
  283. defined(__m68k__) || defined(M68000) || defined(__hppa__) || \
  284. defined(__hppa) || defined(__HPPA__) || defined(__sparc__) || \
  285. defined(__sparc) || defined(__370__) || defined(__THW_370__) || \
  286. defined(__s390__) || defined(__s390x__) || defined(__SYSC_ZARCH__)
  287. #define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
  288. #else
  289. #error __BYTE_ORDER__ should be defined.
  290. #endif /* Arch */
  291. #endif
  292. #endif /* __BYTE_ORDER__ || __ORDER_LITTLE_ENDIAN__ || __ORDER_BIG_ENDIAN__ */
  293. /*****************************************************************************/
  294. #ifndef __dll_export
  295. #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
  296. #if defined(__GNUC__) || __has_attribute(dllexport)
  297. #define __dll_export __attribute__((dllexport))
  298. #else
  299. #define __dll_export __declspec(dllexport)
  300. #endif
  301. #elif defined(__GNUC__) || __has_attribute(__visibility__)
  302. #define __dll_export __attribute__((__visibility__("default")))
  303. #else
  304. #define __dll_export
  305. #endif
  306. #endif /* __dll_export */
  307. #ifndef __dll_import
  308. #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
  309. #if defined(__GNUC__) || __has_attribute(dllimport)
  310. #define __dll_import __attribute__((dllimport))
  311. #else
  312. #define __dll_import __declspec(dllimport)
  313. #endif
  314. #elif defined(__GNUC__) || __has_attribute(__visibility__)
  315. #define __dll_import __attribute__((__visibility__("default")))
  316. #else
  317. #define __dll_import
  318. #endif
  319. #endif /* __dll_import */
  320. #ifndef __force_inline
  321. #ifdef _MSC_VER
  322. #define __force_inline __forceinline
  323. #elif __GNUC_PREREQ(3, 2) || __has_attribute(__always_inline__)
  324. #define __force_inline __inline __attribute__((__always_inline__))
  325. #else
  326. #define __force_inline __inline
  327. #endif
  328. #endif /* __force_inline */
  329. #ifndef T1HA_API
  330. #if defined(t1ha_EXPORTS)
  331. #define T1HA_API __dll_export
  332. #elif defined(t1ha_IMPORTS)
  333. #define T1HA_API __dll_import
  334. #else
  335. #define T1HA_API
  336. #endif
  337. #endif /* T1HA_API */
  338. #if defined(_MSC_VER) && defined(__ia32__)
  339. #define T1HA_ALIGN_PREFIX __declspec(align(32)) /* required only for SIMD */
  340. #else
  341. #define T1HA_ALIGN_PREFIX
  342. #endif /* _MSC_VER */
  343. #if defined(__GNUC__) && defined(__ia32__)
  344. #define T1HA_ALIGN_SUFFIX \
  345. __attribute__((__aligned__(32))) /* required only for SIMD */
  346. #else
  347. #define T1HA_ALIGN_SUFFIX
  348. #endif /* GCC x86 */
  349. #include <util/system/compiler.h>
  350. #ifndef T1HA_USE_INDIRECT_FUNCTIONS
  351. /* GNU ELF indirect functions usage control. For more info please see
  352. * https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
  353. * and https://sourceware.org/glibc/wiki/GNU_IFUNC */
  354. #if defined(__ELF__) && defined(__amd64__) && \
  355. (__has_attribute(__ifunc__) || \
  356. (!defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && \
  357. !defined(__SANITIZE_ADDRESS__) && !defined(__SSP_ALL__)))
  358. /* Enable gnu_indirect_function by default if :
  359. * - ELF AND x86_64
  360. * - attribute(__ifunc__) is available OR
  361. * GCC >= 4 WITHOUT -fsanitize=address NOR -fstack-protector-all */
  362. #define T1HA_USE_INDIRECT_FUNCTIONS 1
  363. #else
  364. #define T1HA_USE_INDIRECT_FUNCTIONS 0
  365. #endif
  366. #endif /* T1HA_USE_INDIRECT_FUNCTIONS */
  367. #if __GNUC_PREREQ(4, 0)
  368. #pragma GCC visibility push(hidden)
  369. #endif /* __GNUC_PREREQ(4,0) */
  370. #ifdef __cplusplus
  371. extern "C" {
  372. #endif
  373. typedef union T1HA_ALIGN_PREFIX t1ha_state256 {
  374. uint8_t bytes[32];
  375. uint32_t u32[8];
  376. uint64_t u64[4];
  377. struct {
  378. uint64_t a, b, c, d;
  379. } n;
  380. } t1ha_state256_t T1HA_ALIGN_SUFFIX;
  381. typedef struct t1ha_context {
  382. t1ha_state256_t state;
  383. t1ha_state256_t buffer;
  384. size_t partial;
  385. uint64_t total;
  386. } t1ha_context_t;
  387. #ifdef _MSC_VER
  388. #pragma warning(pop)
  389. #endif
  390. /******************************************************************************
  391. *
  392. * Self-testing API.
  393. *
  394. * Unfortunately, some compilers (exactly only Microsoft Visual C/C++) has
  395. * a bugs which leads t1ha-functions to produce wrong results. This API allows
  396. * check the correctness of the actual code in runtime.
  397. *
  398. * All check-functions returns 0 on success, or -1 in case the corresponding
  399. * hash-function failed verification. PLEASE, always perform such checking at
  400. * initialization of your code, if you using MSVC or other troubleful compilers.
  401. */
  402. T1HA_API int t1ha_selfcheck__all_enabled(void);
  403. #ifndef T1HA2_DISABLED
  404. T1HA_API int t1ha_selfcheck__t1ha2_atonce(void);
  405. T1HA_API int t1ha_selfcheck__t1ha2_atonce128(void);
  406. T1HA_API int t1ha_selfcheck__t1ha2_stream(void);
  407. T1HA_API int t1ha_selfcheck__t1ha2(void);
  408. #endif /* T1HA2_DISABLED */
  409. #ifndef T1HA1_DISABLED
  410. T1HA_API int t1ha_selfcheck__t1ha1_le(void);
  411. T1HA_API int t1ha_selfcheck__t1ha1_be(void);
  412. T1HA_API int t1ha_selfcheck__t1ha1(void);
  413. #endif /* T1HA1_DISABLED */
  414. #ifndef T1HA0_DISABLED
  415. T1HA_API int t1ha_selfcheck__t1ha0_32le(void);
  416. T1HA_API int t1ha_selfcheck__t1ha0_32be(void);
  417. T1HA_API int t1ha_selfcheck__t1ha0(void);
  418. /* Define T1HA0_AESNI_AVAILABLE to 0 for disable AES-NI support. */
  419. #ifndef T1HA0_AESNI_AVAILABLE
  420. #if defined(__e2k__) || \
  421. (defined(__ia32__) && (!defined(_M_IX86) || _MSC_VER > 1800))
  422. #define T1HA0_AESNI_AVAILABLE 1
  423. #else
  424. #define T1HA0_AESNI_AVAILABLE 0
  425. #endif
  426. #endif /* ifndef T1HA0_AESNI_AVAILABLE */
  427. #if T1HA0_AESNI_AVAILABLE
  428. T1HA_API int t1ha_selfcheck__t1ha0_ia32aes_noavx(void);
  429. T1HA_API int t1ha_selfcheck__t1ha0_ia32aes_avx(void);
  430. #ifndef __e2k__
  431. T1HA_API int t1ha_selfcheck__t1ha0_ia32aes_avx2(void);
  432. #endif
  433. #endif /* if T1HA0_AESNI_AVAILABLE */
  434. #endif /* T1HA0_DISABLED */
  435. /******************************************************************************
  436. *
  437. * t1ha2 = 64 and 128-bit, SLIGHTLY MORE ATTENTION FOR QUALITY AND STRENGTH.
  438. *
  439. * - The recommended version of "Fast Positive Hash" with good quality
  440. * for checksum, hash tables and fingerprinting.
  441. * - Portable and extremely efficiency on modern 64-bit CPUs.
  442. * Designed for 64-bit little-endian platforms,
  443. * in other cases will runs slowly.
  444. * - Great quality of hashing and still faster than other non-t1ha hashes.
  445. * Provides streaming mode and 128-bit result.
  446. *
  447. * Note: Due performance reason 64- and 128-bit results are completely
  448. * different each other, i.e. 64-bit result is NOT any part of 128-bit.
  449. */
  450. #ifndef T1HA2_DISABLED
  451. /* The at-once variant with 64-bit result */
  452. T1HA_API uint64_t t1ha2_atonce(const void *data, size_t length, uint64_t seed);
  453. /* The at-once variant with 128-bit result.
  454. * Argument `extra_result` is NOT optional and MUST be valid.
  455. * The high 64-bit part of 128-bit hash will be always unconditionally
  456. * stored to the address given by `extra_result` argument. */
  457. T1HA_API uint64_t t1ha2_atonce128(uint64_t *__restrict extra_result,
  458. const void *__restrict data, size_t length,
  459. uint64_t seed);
  460. /* The init/update/final trinity for streaming.
  461. * Return 64 or 128-bit result depentently from `extra_result` argument. */
  462. T1HA_API void t1ha2_init(t1ha_context_t *ctx, uint64_t seed_x, uint64_t seed_y);
  463. T1HA_API void t1ha2_update(t1ha_context_t *__restrict ctx,
  464. const void *__restrict data, size_t length);
  465. /* Argument `extra_result` is optional and MAY be NULL.
  466. * - If `extra_result` is NOT NULL then the 128-bit hash will be calculated,
  467. * and high 64-bit part of it will be stored to the address given
  468. * by `extra_result` argument.
  469. * - Otherwise the 64-bit hash will be calculated
  470. * and returned from function directly.
  471. *
  472. * Note: Due performance reason 64- and 128-bit results are completely
  473. * different each other, i.e. 64-bit result is NOT any part of 128-bit. */
  474. T1HA_API uint64_t t1ha2_final(t1ha_context_t *__restrict ctx,
  475. uint64_t *__restrict extra_result /* optional */);
  476. #endif /* T1HA2_DISABLED */
  477. /******************************************************************************
  478. *
  479. * t1ha1 = 64-bit, BASELINE FAST PORTABLE HASH:
  480. *
  481. * - Runs faster on 64-bit platforms in other cases may runs slowly.
  482. * - Portable and stable, returns same 64-bit result
  483. * on all architectures and CPUs.
  484. * - Unfortunately it fails the "strict avalanche criteria",
  485. * see test results at https://github.com/demerphq/smhasher.
  486. *
  487. * This flaw is insignificant for the t1ha1() purposes and imperceptible
  488. * from a practical point of view.
  489. * However, nowadays this issue has resolved in the next t1ha2(),
  490. * that was initially planned to providing a bit more quality.
  491. */
  492. #ifndef T1HA1_DISABLED
  493. /* The little-endian variant. */
  494. T1HA_API uint64_t t1ha1_le(const void *data, size_t length, uint64_t seed);
  495. /* The big-endian variant. */
  496. T1HA_API uint64_t t1ha1_be(const void *data, size_t length, uint64_t seed);
  497. #endif /* T1HA1_DISABLED */
  498. /******************************************************************************
  499. *
  500. * t1ha0 = 64-bit, JUST ONLY FASTER:
  501. *
  502. * - Provides fast-as-possible hashing for current CPU, including
  503. * 32-bit systems and engaging the available hardware acceleration.
  504. * - It is a facade that selects most quick-and-dirty hash
  505. * for the current processor. For instance, on IA32 (x86) actual function
  506. * will be selected in runtime, depending on current CPU capabilities
  507. *
  508. * BE CAREFUL!!! THIS IS MEANS:
  509. *
  510. * 1. The quality of hash is a subject for tradeoffs with performance.
  511. * So, the quality and strength of t1ha0() may be lower than t1ha1(),
  512. * especially on 32-bit targets, but then much faster.
  513. * However, guaranteed that it passes all SMHasher tests.
  514. *
  515. * 2. No warranty that the hash result will be same for particular
  516. * key on another machine or another version of libt1ha.
  517. *
  518. * Briefly, such hash-results and their derivatives, should be
  519. * used only in runtime, but should not be persist or transferred
  520. * over a network.
  521. *
  522. *
  523. * When T1HA0_RUNTIME_SELECT is nonzero the t1ha0_resolve() function could
  524. * be used to get actual t1ha0() implementation address at runtime. This is
  525. * useful for two cases:
  526. * - calling by local pointer-to-function usually is little
  527. * bit faster (less overhead) than via a PLT thru the DSO boundary.
  528. * - GNU Indirect functions (see below) don't supported by environment
  529. * and calling by t1ha0_funcptr is not available and/or expensive.
  530. */
  531. #ifndef T1HA0_DISABLED
  532. /* The little-endian variant for 32-bit CPU. */
  533. uint64_t t1ha0_32le(const void *data, size_t length, uint64_t seed);
  534. /* The big-endian variant for 32-bit CPU. */
  535. uint64_t t1ha0_32be(const void *data, size_t length, uint64_t seed);
  536. /* Define T1HA0_AESNI_AVAILABLE to 0 for disable AES-NI support. */
  537. #ifndef T1HA0_AESNI_AVAILABLE
  538. #if defined(__e2k__) || \
  539. (defined(__ia32__) && (!defined(_M_IX86) || _MSC_VER > 1800))
  540. #define T1HA0_AESNI_AVAILABLE 1
  541. #else
  542. #define T1HA0_AESNI_AVAILABLE 0
  543. #endif
  544. #endif /* T1HA0_AESNI_AVAILABLE */
  545. /* Define T1HA0_RUNTIME_SELECT to 0 for disable dispatching t1ha0 at runtime. */
  546. #ifndef T1HA0_RUNTIME_SELECT
  547. #if T1HA0_AESNI_AVAILABLE && !defined(__e2k__)
  548. #define T1HA0_RUNTIME_SELECT 1
  549. #else
  550. #define T1HA0_RUNTIME_SELECT 0
  551. #endif
  552. #endif /* T1HA0_RUNTIME_SELECT */
  553. #if !T1HA0_RUNTIME_SELECT && !defined(T1HA0_USE_DEFINE)
  554. #if defined(__LCC__)
  555. #define T1HA0_USE_DEFINE 1
  556. #else
  557. #define T1HA0_USE_DEFINE 0
  558. #endif
  559. #endif /* T1HA0_USE_DEFINE */
  560. #if T1HA0_AESNI_AVAILABLE
  561. uint64_t t1ha0_ia32aes_noavx(const void *data, size_t length, uint64_t seed);
  562. uint64_t t1ha0_ia32aes_avx(const void *data, size_t length, uint64_t seed);
  563. #ifndef __e2k__
  564. uint64_t t1ha0_ia32aes_avx2(const void *data, size_t length, uint64_t seed);
  565. #endif
  566. #endif /* T1HA0_AESNI_AVAILABLE */
  567. #if T1HA0_RUNTIME_SELECT
  568. typedef uint64_t (*t1ha0_function_t)(const void *, size_t, uint64_t);
  569. T1HA_API t1ha0_function_t t1ha0_resolve(void);
  570. #if T1HA_USE_INDIRECT_FUNCTIONS
  571. T1HA_API uint64_t t1ha0(const void *data, size_t length, uint64_t seed);
  572. #else
  573. /* Otherwise function pointer will be used.
  574. * Unfortunately this may cause some overhead calling. */
  575. T1HA_API extern uint64_t (*t1ha0_funcptr)(const void *data, size_t length,
  576. uint64_t seed);
  577. static __force_inline uint64_t t1ha0(const void *data, size_t length,
  578. uint64_t seed) {
  579. return t1ha0_funcptr(data, length, seed);
  580. }
  581. #endif /* T1HA_USE_INDIRECT_FUNCTIONS */
  582. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  583. #if T1HA0_USE_DEFINE
  584. #if (UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul) && \
  585. (!defined(T1HA1_DISABLED) || !defined(T1HA2_DISABLED))
  586. #if defined(T1HA1_DISABLED)
  587. #define t1ha0 t1ha2_atonce
  588. #else
  589. #define t1ha0 t1ha1_be
  590. #endif /* T1HA1_DISABLED */
  591. #else /* 32/64 */
  592. #define t1ha0 t1ha0_32be
  593. #endif /* 32/64 */
  594. #else /* T1HA0_USE_DEFINE */
  595. static __force_inline uint64_t t1ha0(const void *data, size_t length,
  596. uint64_t seed) {
  597. #if (UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul) && \
  598. (!defined(T1HA1_DISABLED) || !defined(T1HA2_DISABLED))
  599. #if defined(T1HA1_DISABLED)
  600. return t1ha2_atonce(data, length, seed);
  601. #else
  602. return t1ha1_be(data, length, seed);
  603. #endif /* T1HA1_DISABLED */
  604. #else /* 32/64 */
  605. return t1ha0_32be(data, length, seed);
  606. #endif /* 32/64 */
  607. }
  608. #endif /* !T1HA0_USE_DEFINE */
  609. #else /* !T1HA0_RUNTIME_SELECT && __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__ */
  610. #if T1HA0_USE_DEFINE
  611. #if (UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul) && \
  612. (!defined(T1HA1_DISABLED) || !defined(T1HA2_DISABLED))
  613. #if defined(T1HA1_DISABLED)
  614. #define t1ha0 t1ha2_atonce
  615. #else
  616. #define t1ha0 t1ha1_le
  617. #endif /* T1HA1_DISABLED */
  618. #else /* 32/64 */
  619. #define t1ha0 t1ha0_32le
  620. #endif /* 32/64 */
  621. #else
  622. static __force_inline uint64_t t1ha0(const void *data, size_t length,
  623. uint64_t seed) {
  624. #if (UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul) && \
  625. (!defined(T1HA1_DISABLED) || !defined(T1HA2_DISABLED))
  626. #if defined(T1HA1_DISABLED)
  627. return t1ha2_atonce(data, length, seed);
  628. #else
  629. return t1ha1_le(data, length, seed);
  630. #endif /* T1HA1_DISABLED */
  631. #else /* 32/64 */
  632. return t1ha0_32le(data, length, seed);
  633. #endif /* 32/64 */
  634. }
  635. #endif /* !T1HA0_USE_DEFINE */
  636. #endif /* !T1HA0_RUNTIME_SELECT */
  637. #endif /* T1HA0_DISABLED */
  638. #ifdef __cplusplus
  639. }
  640. #endif
  641. #if __GNUC_PREREQ(4, 0)
  642. #pragma GCC visibility pop
  643. #endif /* __GNUC_PREREQ(4,0) */