You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

t1ha_bits.h 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /*
  2. * Copyright (c) 2016-2018 Positive Technologies, https://www.ptsecurity.com,
  3. * Fast Positive Hash.
  4. *
  5. * Portions Copyright (c) 2010-2018 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 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. #if defined(_MSC_VER)
  44. #pragma warning(disable : 4201) /* nameless struct/union */
  45. #if _MSC_VER > 1800
  46. #pragma warning(disable : 4464) /* relative include path contains '..' */
  47. #endif /* 1800 */
  48. #endif /* MSVC */
  49. #include "config.h"
  50. #include "t1ha.h"
  51. #ifndef T1HA_USE_FAST_ONESHOT_READ
  52. /* Define it to 1 for little bit faster code.
  53. * Unfortunately this may triggering a false-positive alarms from Valgrind,
  54. * AddressSanitizer and other similar tool.
  55. * So, define it to 0 for calmness if doubt. */
  56. #define T1HA_USE_FAST_ONESHOT_READ 1
  57. #endif /* T1HA_USE_FAST_ONESHOT_READ */
  58. /*****************************************************************************/
  59. #include <assert.h> /* for assert() */
  60. #include <stdbool.h> /* for bool */
  61. #include <string.h> /* for memcpy() */
  62. #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ && \
  63. __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
  64. #error Unsupported byte order.
  65. #endif
  66. #define T1HA_CONFIG_UNALIGNED_ACCESS__UNABLE 0
  67. #define T1HA_CONFIG_UNALIGNED_ACCESS__SLOW 1
  68. #define T1HA_CONFIG_UNALIGNED_ACCESS__EFFICIENT 2
  69. #ifndef T1HA_CONFIG_UNALIGNED_ACCESS
  70. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  71. #define T1HA_CONFIG_UNALIGNED_ACCESS T1HA_CONFIG_UNALIGNED_ACCESS__EFFICIENT
  72. #elif defined(__ia32__)
  73. #define T1HA_CONFIG_UNALIGNED_ACCESS T1HA_CONFIG_UNALIGNED_ACCESS__EFFICIENT
  74. #elif defined(__e2k__)
  75. #define T1HA_CONFIG_UNALIGNED_ACCESS T1HA_CONFIG_UNALIGNED_ACCESS__SLOW
  76. #elif defined(__ARM_FEATURE_UNALIGNED)
  77. #define T1HA_CONFIG_UNALIGNED_ACCESS T1HA_CONFIG_UNALIGNED_ACCESS__EFFICIENT
  78. #else
  79. #define T1HA_CONFIG_UNALIGNED_ACCESS T1HA_CONFIG_UNALIGNED_ACCESS__UNABLE
  80. #endif
  81. #endif /* T1HA_CONFIG_UNALIGNED_ACCESS */
  82. #define ALIGNMENT_16 2
  83. #define ALIGNMENT_32 4
  84. #if UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul
  85. #define ALIGNMENT_64 8
  86. #else
  87. #define ALIGNMENT_64 4
  88. #endif
  89. #ifndef PAGESIZE
  90. #define PAGESIZE 4096
  91. #endif /* PAGESIZE */
  92. /***************************************************************************/
  93. #ifndef __has_builtin
  94. #define __has_builtin(x) (0)
  95. #endif
  96. #ifndef __has_warning
  97. #define __has_warning(x) (0)
  98. #endif
  99. #ifndef __has_feature
  100. #define __has_feature(x) (0)
  101. #endif
  102. #ifndef __has_extension
  103. #define __has_extension(x) (0)
  104. #endif
  105. #if __GNUC_PREREQ(4, 4) || defined(__clang__)
  106. #if defined(__ia32__) || defined(__e2k__)
  107. #include <x86intrin.h>
  108. #endif
  109. #if defined(__ia32__) && !defined(__cpuid_count)
  110. #include <cpuid.h>
  111. #endif
  112. #if defined(__e2k__)
  113. #include <e2kbuiltin.h>
  114. #endif
  115. #ifndef likely
  116. #define likely(cond) __builtin_expect(!!(cond), 1)
  117. #endif
  118. #ifndef unlikely
  119. #define unlikely(cond) __builtin_expect(!!(cond), 0)
  120. #endif
  121. #if __GNUC_PREREQ(4, 5) || __has_builtin(__builtin_unreachable)
  122. #define unreachable() __builtin_unreachable()
  123. #endif
  124. #define bswap64(v) __builtin_bswap64(v)
  125. #define bswap32(v) __builtin_bswap32(v)
  126. #if __GNUC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
  127. #define bswap16(v) __builtin_bswap16(v)
  128. #endif
  129. #if !defined(__maybe_unused) && (__GNUC_PREREQ(4, 3) || __has_attribute(unused))
  130. #define __maybe_unused __attribute__((unused))
  131. #endif
  132. #if !defined(__always_inline) && \
  133. (__GNUC_PREREQ(3, 2) || __has_attribute(always_inline))
  134. #define __always_inline __inline __attribute__((always_inline))
  135. #endif
  136. #if defined(__e2k__)
  137. #if __iset__ >= 3
  138. #define mul_64x64_high(a, b) __builtin_e2k_umulhd(a, b)
  139. #endif /* __iset__ >= 3 */
  140. #if __iset__ >= 5
  141. static __maybe_unused __always_inline unsigned
  142. e2k_add64carry_first(uint64_t base, uint64_t addend, uint64_t *sum) {
  143. *sum = base + addend;
  144. return (unsigned)__builtin_e2k_addcd_c(base, addend, 0);
  145. }
  146. #define add64carry_first(base, addend, sum) \
  147. e2k_add64carry_first(base, addend, sum)
  148. static __maybe_unused __always_inline unsigned
  149. e2k_add64carry_next(unsigned carry, uint64_t base, uint64_t addend,
  150. uint64_t *sum) {
  151. *sum = __builtin_e2k_addcd(base, addend, carry);
  152. return (unsigned)__builtin_e2k_addcd_c(base, addend, carry);
  153. }
  154. #define add64carry_next(carry, base, addend, sum) \
  155. e2k_add64carry_next(carry, base, addend, sum)
  156. static __maybe_unused __always_inline void e2k_add64carry_last(unsigned carry,
  157. uint64_t base,
  158. uint64_t addend,
  159. uint64_t *sum) {
  160. *sum = __builtin_e2k_addcd(base, addend, carry);
  161. }
  162. #define add64carry_last(carry, base, addend, sum) \
  163. e2k_add64carry_last(carry, base, addend, sum)
  164. #endif /* __iset__ >= 5 */
  165. #define fetch64_be_aligned(ptr) ((uint64_t)__builtin_e2k_ld_64s_be(ptr))
  166. #define fetch32_be_aligned(ptr) ((uint32_t)__builtin_e2k_ld_32u_be(ptr))
  167. #endif /* __e2k__ Elbrus */
  168. #elif defined(_MSC_VER)
  169. #if _MSC_FULL_VER < 190024218 && defined(_M_IX86)
  170. #pragma message( \
  171. "For AES-NI at least \"Microsoft C/C++ Compiler\" version 19.00.24218 (Visual Studio 2015 Update 5) is required.")
  172. #endif
  173. #if _MSC_FULL_VER < 191025019
  174. #pragma message( \
  175. "It is recommended to use \"Microsoft C/C++ Compiler\" version 19.10.25019 (Visual Studio 2017) or newer.")
  176. #endif
  177. #if _MSC_FULL_VER < 180040629
  178. #error At least "Microsoft C/C++ Compiler" version 18.00.40629 (Visual Studio 2013 Update 5) is required.
  179. #endif
  180. #pragma warning(push, 1)
  181. #include <intrin.h>
  182. #include <stdlib.h>
  183. #define likely(cond) (cond)
  184. #define unlikely(cond) (cond)
  185. #define unreachable() __assume(0)
  186. #define bswap64(v) _byteswap_uint64(v)
  187. #define bswap32(v) _byteswap_ulong(v)
  188. #define bswap16(v) _byteswap_ushort(v)
  189. #define rot64(v, s) _rotr64(v, s)
  190. #define rot32(v, s) _rotr(v, s)
  191. #define __always_inline __forceinline
  192. #if defined(_M_X64) || defined(_M_IA64)
  193. #pragma intrinsic(_umul128)
  194. #define mul_64x64_128(a, b, ph) _umul128(a, b, ph)
  195. #pragma intrinsic(_addcarry_u64)
  196. #define add64carry_first(base, addend, sum) _addcarry_u64(0, base, addend, sum)
  197. #define add64carry_next(carry, base, addend, sum) \
  198. _addcarry_u64(carry, base, addend, sum)
  199. #define add64carry_last(carry, base, addend, sum) \
  200. (void)_addcarry_u64(carry, base, addend, sum)
  201. #endif
  202. #if defined(_M_ARM64) || defined(_M_X64) || defined(_M_IA64)
  203. #pragma intrinsic(__umulh)
  204. #define mul_64x64_high(a, b) __umulh(a, b)
  205. #endif
  206. #if defined(_M_IX86)
  207. #pragma intrinsic(__emulu)
  208. #define mul_32x32_64(a, b) __emulu(a, b)
  209. #if _MSC_FULL_VER >= 190024231 /* LY: workaround for optimizer bug */
  210. #pragma intrinsic(_addcarry_u32)
  211. #define add32carry_first(base, addend, sum) _addcarry_u32(0, base, addend, sum)
  212. #define add32carry_next(carry, base, addend, sum) \
  213. _addcarry_u32(carry, base, addend, sum)
  214. #define add32carry_last(carry, base, addend, sum) \
  215. (void)_addcarry_u32(carry, base, addend, sum)
  216. static __forceinline char
  217. msvc32_add64carry_first(uint64_t base, uint64_t addend, uint64_t *sum) {
  218. uint32_t *const sum32 = (uint32_t *)sum;
  219. const uint32_t base_32l = (uint32_t)base;
  220. const uint32_t base_32h = (uint32_t)(base >> 32);
  221. const uint32_t addend_32l = (uint32_t)addend;
  222. const uint32_t addend_32h = (uint32_t)(addend >> 32);
  223. return add32carry_next(add32carry_first(base_32l, addend_32l, sum32),
  224. base_32h, addend_32h, sum32 + 1);
  225. }
  226. #define add64carry_first(base, addend, sum) \
  227. msvc32_add64carry_first(base, addend, sum)
  228. static __forceinline char msvc32_add64carry_next(char carry, uint64_t base,
  229. uint64_t addend,
  230. uint64_t *sum) {
  231. uint32_t *const sum32 = (uint32_t *)sum;
  232. const uint32_t base_32l = (uint32_t)base;
  233. const uint32_t base_32h = (uint32_t)(base >> 32);
  234. const uint32_t addend_32l = (uint32_t)addend;
  235. const uint32_t addend_32h = (uint32_t)(addend >> 32);
  236. return add32carry_next(add32carry_next(carry, base_32l, addend_32l, sum32),
  237. base_32h, addend_32h, sum32 + 1);
  238. }
  239. #define add64carry_next(carry, base, addend, sum) \
  240. msvc32_add64carry_next(carry, base, addend, sum)
  241. static __forceinline void msvc32_add64carry_last(char carry, uint64_t base,
  242. uint64_t addend,
  243. uint64_t *sum) {
  244. uint32_t *const sum32 = (uint32_t *)sum;
  245. const uint32_t base_32l = (uint32_t)base;
  246. const uint32_t base_32h = (uint32_t)(base >> 32);
  247. const uint32_t addend_32l = (uint32_t)addend;
  248. const uint32_t addend_32h = (uint32_t)(addend >> 32);
  249. add32carry_last(add32carry_next(carry, base_32l, addend_32l, sum32), base_32h,
  250. addend_32h, sum32 + 1);
  251. }
  252. #define add64carry_last(carry, base, addend, sum) \
  253. msvc32_add64carry_last(carry, base, addend, sum)
  254. #endif /* _MSC_FULL_VER >= 190024231 */
  255. #elif defined(_M_ARM)
  256. #define mul_32x32_64(a, b) _arm_umull(a, b)
  257. #endif
  258. #pragma warning(pop)
  259. #pragma warning(disable : 4514) /* 'xyz': unreferenced inline function \
  260. has been removed */
  261. #pragma warning(disable : 4710) /* 'xyz': function not inlined */
  262. #pragma warning(disable : 4711) /* function 'xyz' selected for \
  263. automatic inline expansion */
  264. #pragma warning(disable : 4127) /* conditional expression is constant */
  265. #pragma warning(disable : 4702) /* unreachable code */
  266. #endif /* Compiler */
  267. #ifndef likely
  268. #define likely(cond) (cond)
  269. #endif
  270. #ifndef unlikely
  271. #define unlikely(cond) (cond)
  272. #endif
  273. #ifndef __maybe_unused
  274. #define __maybe_unused
  275. #endif
  276. #ifndef __always_inline
  277. #define __always_inline __inline
  278. #endif
  279. #ifndef unreachable
  280. #define unreachable() \
  281. do { \
  282. } while (1)
  283. #endif
  284. #ifndef bswap64
  285. #if defined(bswap_64)
  286. #define bswap64 bswap_64
  287. #elif defined(__bswap_64)
  288. #define bswap64 __bswap_64
  289. #else
  290. static __always_inline uint64_t bswap64(uint64_t v) {
  291. return v << 56 | v >> 56 | ((v << 40) & UINT64_C(0x00ff000000000000)) |
  292. ((v << 24) & UINT64_C(0x0000ff0000000000)) |
  293. ((v << 8) & UINT64_C(0x000000ff00000000)) |
  294. ((v >> 8) & UINT64_C(0x00000000ff000000)) |
  295. ((v >> 24) & UINT64_C(0x0000000000ff0000)) |
  296. ((v >> 40) & UINT64_C(0x000000000000ff00));
  297. }
  298. #endif
  299. #endif /* bswap64 */
  300. #ifndef bswap32
  301. #if defined(bswap_32)
  302. #define bswap32 bswap_32
  303. #elif defined(__bswap_32)
  304. #define bswap32 __bswap_32
  305. #else
  306. static __always_inline uint32_t bswap32(uint32_t v) {
  307. return v << 24 | v >> 24 | ((v << 8) & UINT32_C(0x00ff0000)) |
  308. ((v >> 8) & UINT32_C(0x0000ff00));
  309. }
  310. #endif
  311. #endif /* bswap32 */
  312. #ifndef bswap16
  313. #if defined(bswap_16)
  314. #define bswap16 bswap_16
  315. #elif defined(__bswap_16)
  316. #define bswap16 __bswap_16
  317. #else
  318. static __always_inline uint16_t bswap16(uint16_t v) { return v << 8 | v >> 8; }
  319. #endif
  320. #endif /* bswap16 */
  321. #ifndef read_unaligned
  322. #if defined(__GNUC__) || __has_attribute(packed)
  323. typedef struct {
  324. uint8_t unaligned_8;
  325. uint16_t unaligned_16;
  326. uint32_t unaligned_32;
  327. uint64_t unaligned_64;
  328. } __attribute__((packed)) t1ha_unaligned_proxy;
  329. #define read_unaligned(ptr, bits) \
  330. (((const t1ha_unaligned_proxy *)((const uint8_t *)(ptr)-offsetof( \
  331. t1ha_unaligned_proxy, unaligned_##bits))) \
  332. ->unaligned_##bits)
  333. #elif defined(_MSC_VER)
  334. #pragma warning( \
  335. disable : 4235) /* nonstandard extension used: '__unaligned' \
  336. * keyword not supported on this architecture */
  337. #define read_unaligned(ptr, bits) (*(const __unaligned uint##bits##_t *)(ptr))
  338. #else
  339. #pragma pack(push, 1)
  340. typedef struct {
  341. uint8_t unaligned_8;
  342. uint16_t unaligned_16;
  343. uint32_t unaligned_32;
  344. uint64_t unaligned_64;
  345. } t1ha_unaligned_proxy;
  346. #pragma pack(pop)
  347. #define read_unaligned(ptr, bits) \
  348. (((const t1ha_unaligned_proxy *)((const uint8_t *)(ptr)-offsetof( \
  349. t1ha_unaligned_proxy, unaligned_##bits))) \
  350. ->unaligned_##bits)
  351. #endif
  352. #endif /* read_unaligned */
  353. #ifndef read_aligned
  354. #if __GNUC_PREREQ(4, 8) || __has_builtin(__builtin_assume_aligned)
  355. #define read_aligned(ptr, bits) \
  356. (*(const uint##bits##_t *)__builtin_assume_aligned(ptr, ALIGNMENT_##bits))
  357. #elif (__GNUC_PREREQ(3, 3) || __has_attribute(aligned)) && !defined(__clang__)
  358. #define read_aligned(ptr, bits) \
  359. (*(const uint##bits##_t __attribute__((aligned(ALIGNMENT_##bits))) *)(ptr))
  360. #elif __has_attribute(assume_aligned)
  361. static __always_inline const
  362. uint16_t *__attribute__((assume_aligned(ALIGNMENT_16)))
  363. cast_aligned_16(const void *ptr) {
  364. return (const uint16_t *)ptr;
  365. }
  366. static __always_inline const
  367. uint32_t *__attribute__((assume_aligned(ALIGNMENT_32)))
  368. cast_aligned_32(const void *ptr) {
  369. return (const uint32_t *)ptr;
  370. }
  371. static __always_inline const
  372. uint64_t *__attribute__((assume_aligned(ALIGNMENT_64)))
  373. cast_aligned_64(const void *ptr) {
  374. return (const uint64_t *)ptr;
  375. }
  376. #define read_aligned(ptr, bits) (*cast_aligned_##bits(ptr))
  377. #elif defined(_MSC_VER)
  378. #define read_aligned(ptr, bits) \
  379. (*(const __declspec(align(ALIGNMENT_##bits)) uint##bits##_t *)(ptr))
  380. #else
  381. #define read_aligned(ptr, bits) (*(const uint##bits##_t *)(ptr))
  382. #endif
  383. #endif /* read_aligned */
  384. #ifndef prefetch
  385. #if (__GNUC_PREREQ(4, 0) || __has_builtin(__builtin_prefetch)) && \
  386. !defined(__ia32__)
  387. #define prefetch(ptr) __builtin_prefetch(ptr)
  388. #elif defined(_M_ARM64) || defined(_M_ARM)
  389. #define prefetch(ptr) __prefetch(ptr)
  390. #else
  391. #define prefetch(ptr) \
  392. do { \
  393. (void)(ptr); \
  394. } while (0)
  395. #endif
  396. #endif /* prefetch */
  397. #if __has_warning("-Wconstant-logical-operand")
  398. #if defined(__clang__)
  399. #pragma clang diagnostic ignored "-Wconstant-logical-operand"
  400. #elif defined(__GNUC__)
  401. #pragma GCC diagnostic ignored "-Wconstant-logical-operand"
  402. #else
  403. #pragma warning disable "constant-logical-operand"
  404. #endif
  405. #endif /* -Wconstant-logical-operand */
  406. #if __has_warning("-Wtautological-pointer-compare")
  407. #if defined(__clang__)
  408. #pragma clang diagnostic ignored "-Wtautological-pointer-compare"
  409. #elif defined(__GNUC__)
  410. #pragma GCC diagnostic ignored "-Wtautological-pointer-compare"
  411. #else
  412. #pragma warning disable "tautological-pointer-compare"
  413. #endif
  414. #endif /* -Wtautological-pointer-compare */
  415. /***************************************************************************/
  416. /*---------------------------------------------------------- Little Endian */
  417. #ifndef fetch16_le_aligned
  418. static __always_inline uint16_t fetch16_le_aligned(const void *v) {
  419. assert(((uintptr_t)v) % ALIGNMENT_16 == 0);
  420. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  421. return read_aligned(v, 16);
  422. #else
  423. return bswap16(read_aligned(v, 16));
  424. #endif
  425. }
  426. #endif /* fetch16_le_aligned */
  427. #ifndef fetch16_le_unaligned
  428. static __always_inline uint16_t fetch16_le_unaligned(const void *v) {
  429. #if T1HA_CONFIG_UNALIGNED_ACCESS == T1HA_CONFIG_UNALIGNED_ACCESS__UNABLE
  430. const uint8_t *p = (const uint8_t *)v;
  431. return p[0] | (uint16_t)p[1] << 8;
  432. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  433. return read_unaligned(v, 16);
  434. #else
  435. return bswap16(read_unaligned(v, 16));
  436. #endif
  437. }
  438. #endif /* fetch16_le_unaligned */
  439. #ifndef fetch32_le_aligned
  440. static __always_inline uint32_t fetch32_le_aligned(const void *v) {
  441. assert(((uintptr_t)v) % ALIGNMENT_32 == 0);
  442. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  443. return read_aligned(v, 32);
  444. #else
  445. return bswap32(read_aligned(v, 32));
  446. #endif
  447. }
  448. #endif /* fetch32_le_aligned */
  449. #ifndef fetch32_le_unaligned
  450. static __always_inline uint32_t fetch32_le_unaligned(const void *v) {
  451. #if T1HA_CONFIG_UNALIGNED_ACCESS == T1HA_CONFIG_UNALIGNED_ACCESS__UNABLE
  452. return fetch16_le_unaligned(v) |
  453. (uint32_t)fetch16_le_unaligned((const uint8_t *)v + 2) << 16;
  454. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  455. return read_unaligned(v, 32);
  456. #else
  457. return bswap32(read_unaligned(v, 32));
  458. #endif
  459. }
  460. #endif /* fetch32_le_unaligned */
  461. #ifndef fetch64_le_aligned
  462. static __always_inline uint64_t fetch64_le_aligned(const void *v) {
  463. assert(((uintptr_t)v) % ALIGNMENT_64 == 0);
  464. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  465. return read_aligned(v, 64);
  466. #else
  467. return bswap64(read_aligned(v, 64));
  468. #endif
  469. }
  470. #endif /* fetch64_le_aligned */
  471. #ifndef fetch64_le_unaligned
  472. static __always_inline uint64_t fetch64_le_unaligned(const void *v) {
  473. #if T1HA_CONFIG_UNALIGNED_ACCESS == T1HA_CONFIG_UNALIGNED_ACCESS__UNABLE
  474. return fetch32_le_unaligned(v) |
  475. (uint64_t)fetch32_le_unaligned((const uint8_t *)v + 4) << 32;
  476. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  477. return read_unaligned(v, 64);
  478. #else
  479. return bswap64(read_unaligned(v, 64));
  480. #endif
  481. }
  482. #endif /* fetch64_le_unaligned */
  483. static __always_inline uint64_t tail64_le_aligned(const void *v, size_t tail) {
  484. const uint8_t *const p = (const uint8_t *)v;
  485. #if T1HA_USE_FAST_ONESHOT_READ && !defined(__SANITIZE_ADDRESS__)
  486. /* We can perform a 'oneshot' read, which is little bit faster. */
  487. const unsigned shift = ((8 - tail) & 7) << 3;
  488. return fetch64_le_aligned(p) & ((~UINT64_C(0)) >> shift);
  489. #else
  490. uint64_t r = 0;
  491. switch (tail & 7) {
  492. default:
  493. unreachable();
  494. /* fall through */
  495. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  496. /* For most CPUs this code is better when not needed byte reordering. */
  497. case 0:
  498. return fetch64_le_aligned(p);
  499. case 7:
  500. r = (uint64_t)p[6] << 8;
  501. /* fall through */
  502. case 6:
  503. r += p[5];
  504. r <<= 8;
  505. /* fall through */
  506. case 5:
  507. r += p[4];
  508. r <<= 32;
  509. /* fall through */
  510. case 4:
  511. return r + fetch32_le_aligned(p);
  512. case 3:
  513. r = (uint64_t)p[2] << 16;
  514. /* fall through */
  515. case 2:
  516. return r + fetch16_le_aligned(p);
  517. case 1:
  518. return p[0];
  519. #else
  520. case 0:
  521. r = p[7] << 8;
  522. /* fall through */
  523. case 7:
  524. r += p[6];
  525. r <<= 8;
  526. /* fall through */
  527. case 6:
  528. r += p[5];
  529. r <<= 8;
  530. /* fall through */
  531. case 5:
  532. r += p[4];
  533. r <<= 8;
  534. /* fall through */
  535. case 4:
  536. r += p[3];
  537. r <<= 8;
  538. /* fall through */
  539. case 3:
  540. r += p[2];
  541. r <<= 8;
  542. /* fall through */
  543. case 2:
  544. r += p[1];
  545. r <<= 8;
  546. /* fall through */
  547. case 1:
  548. return r + p[0];
  549. #endif
  550. }
  551. #endif /* T1HA_USE_FAST_ONESHOT_READ */
  552. }
  553. #if T1HA_USE_FAST_ONESHOT_READ && \
  554. T1HA_CONFIG_UNALIGNED_ACCESS != T1HA_CONFIG_UNALIGNED_ACCESS__UNABLE && \
  555. defined(PAGESIZE) && !defined(__sun) && !defined(__SANITIZE_ADDRESS__)
  556. #define can_read_underside(ptr, size) \
  557. ((size) <= sizeof(uintptr_t) && ((PAGESIZE - (size)) & (uintptr_t)(ptr)) != 0)
  558. #endif /* T1HA_USE_FAST_ONESHOT_READ */
  559. static __always_inline uint64_t tail64_le_unaligned(const void *v,
  560. size_t tail) {
  561. const uint8_t *p = (const uint8_t *)v;
  562. #ifdef can_read_underside
  563. /* On some systems (e.g. x86) we can perform a 'oneshot' read, which
  564. * is little bit faster. Thanks Marcin Żukowski <marcin.zukowski@gmail.com>
  565. * for the reminder. */
  566. const unsigned offset = (8 - tail) & 7;
  567. const unsigned shift = offset << 3;
  568. if (likely(can_read_underside(p, 8))) {
  569. p -= offset;
  570. return fetch64_le_unaligned(p) >> shift;
  571. }
  572. return fetch64_le_unaligned(p) & ((~UINT64_C(0)) >> shift);
  573. #else
  574. uint64_t r = 0;
  575. switch (tail & 7) {
  576. default:
  577. unreachable();
  578. /* fall through */
  579. #if T1HA_CONFIG_UNALIGNED_ACCESS == T1HA_CONFIG_UNALIGNED_ACCESS__EFFICIENT && \
  580. __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  581. /* For most CPUs this code is better when not needed
  582. * copying for alignment or byte reordering. */
  583. case 0:
  584. return fetch64_le_unaligned(p);
  585. case 7:
  586. r = (uint64_t)p[6] << 8;
  587. /* fall through */
  588. case 6:
  589. r += p[5];
  590. r <<= 8;
  591. /* fall through */
  592. case 5:
  593. r += p[4];
  594. r <<= 32;
  595. /* fall through */
  596. case 4:
  597. return r + fetch32_le_unaligned(p);
  598. case 3:
  599. r = (uint64_t)p[2] << 16;
  600. /* fall through */
  601. case 2:
  602. return r + fetch16_le_unaligned(p);
  603. case 1:
  604. return p[0];
  605. #else
  606. /* For most CPUs this code is better than a
  607. * copying for alignment and/or byte reordering. */
  608. case 0:
  609. r = p[7] << 8;
  610. /* fall through */
  611. case 7:
  612. r += p[6];
  613. r <<= 8;
  614. /* fall through */
  615. case 6:
  616. r += p[5];
  617. r <<= 8;
  618. /* fall through */
  619. case 5:
  620. r += p[4];
  621. r <<= 8;
  622. /* fall through */
  623. case 4:
  624. r += p[3];
  625. r <<= 8;
  626. /* fall through */
  627. case 3:
  628. r += p[2];
  629. r <<= 8;
  630. /* fall through */
  631. case 2:
  632. r += p[1];
  633. r <<= 8;
  634. /* fall through */
  635. case 1:
  636. return r + p[0];
  637. #endif
  638. }
  639. #endif /* can_read_underside */
  640. }
  641. /*------------------------------------------------------------- Big Endian */
  642. #ifndef fetch16_be_aligned
  643. static __maybe_unused __always_inline uint16_t
  644. fetch16_be_aligned(const void *v) {
  645. assert(((uintptr_t)v) % ALIGNMENT_16 == 0);
  646. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  647. return read_aligned(v, 16);
  648. #else
  649. return bswap16(read_aligned(v, 16));
  650. #endif
  651. }
  652. #endif /* fetch16_be_aligned */
  653. #ifndef fetch16_be_unaligned
  654. static __maybe_unused __always_inline uint16_t
  655. fetch16_be_unaligned(const void *v) {
  656. #if T1HA_CONFIG_UNALIGNED_ACCESS == T1HA_CONFIG_UNALIGNED_ACCESS__UNABLE
  657. const uint8_t *p = (const uint8_t *)v;
  658. return (uint16_t)p[0] << 8 | p[1];
  659. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  660. return read_unaligned(v, 16);
  661. #else
  662. return bswap16(read_unaligned(v, 16));
  663. #endif
  664. }
  665. #endif /* fetch16_be_unaligned */
  666. #ifndef fetch32_be_aligned
  667. static __maybe_unused __always_inline uint32_t
  668. fetch32_be_aligned(const void *v) {
  669. assert(((uintptr_t)v) % ALIGNMENT_32 == 0);
  670. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  671. return read_aligned(v, 32);
  672. #else
  673. return bswap32(read_aligned(v, 32));
  674. #endif
  675. }
  676. #endif /* fetch32_be_aligned */
  677. #ifndef fetch32_be_unaligned
  678. static __maybe_unused __always_inline uint32_t
  679. fetch32_be_unaligned(const void *v) {
  680. #if T1HA_CONFIG_UNALIGNED_ACCESS == T1HA_CONFIG_UNALIGNED_ACCESS__UNABLE
  681. return (uint32_t)fetch16_be_unaligned(v) << 16 |
  682. fetch16_be_unaligned((const uint8_t *)v + 2);
  683. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  684. return read_unaligned(v, 32);
  685. #else
  686. return bswap32(read_unaligned(v, 32));
  687. #endif
  688. }
  689. #endif /* fetch32_be_unaligned */
  690. #ifndef fetch64_be_aligned
  691. static __maybe_unused __always_inline uint64_t
  692. fetch64_be_aligned(const void *v) {
  693. assert(((uintptr_t)v) % ALIGNMENT_64 == 0);
  694. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  695. return read_aligned(v, 64);
  696. #else
  697. return bswap64(read_aligned(v, 64));
  698. #endif
  699. }
  700. #endif /* fetch64_be_aligned */
  701. #ifndef fetch64_be_unaligned
  702. static __maybe_unused __always_inline uint64_t
  703. fetch64_be_unaligned(const void *v) {
  704. #if T1HA_CONFIG_UNALIGNED_ACCESS == T1HA_CONFIG_UNALIGNED_ACCESS__UNABLE
  705. return (uint64_t)fetch32_be_unaligned(v) << 32 |
  706. fetch32_be_unaligned((const uint8_t *)v + 4);
  707. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  708. return read_unaligned(v, 64);
  709. #else
  710. return bswap64(read_unaligned(v, 64));
  711. #endif
  712. }
  713. #endif /* fetch64_be_unaligned */
  714. static __maybe_unused __always_inline uint64_t tail64_be_aligned(const void *v,
  715. size_t tail) {
  716. const uint8_t *const p = (const uint8_t *)v;
  717. #if T1HA_USE_FAST_ONESHOT_READ && !defined(__SANITIZE_ADDRESS__)
  718. /* We can perform a 'oneshot' read, which is little bit faster. */
  719. const unsigned shift = ((8 - tail) & 7) << 3;
  720. return fetch64_be_aligned(p) >> shift;
  721. #else
  722. switch (tail & 7) {
  723. default:
  724. unreachable();
  725. /* fall through */
  726. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  727. /* For most CPUs this code is better when not byte reordering. */
  728. case 1:
  729. return p[0];
  730. case 2:
  731. return fetch16_be_aligned(p);
  732. case 3:
  733. return (uint32_t)fetch16_be_aligned(p) << 8 | p[2];
  734. case 4:
  735. return fetch32_be_aligned(p);
  736. case 5:
  737. return (uint64_t)fetch32_be_aligned(p) << 8 | p[4];
  738. case 6:
  739. return (uint64_t)fetch32_be_aligned(p) << 16 | fetch16_be_aligned(p + 4);
  740. case 7:
  741. return (uint64_t)fetch32_be_aligned(p) << 24 |
  742. (uint32_t)fetch16_be_aligned(p + 4) << 8 | p[6];
  743. case 0:
  744. return fetch64_be_aligned(p);
  745. #else
  746. case 1:
  747. return p[0];
  748. case 2:
  749. return p[1] | (uint32_t)p[0] << 8;
  750. case 3:
  751. return p[2] | (uint32_t)p[1] << 8 | (uint32_t)p[0] << 16;
  752. case 4:
  753. return p[3] | (uint32_t)p[2] << 8 | (uint32_t)p[1] << 16 |
  754. (uint32_t)p[0] << 24;
  755. case 5:
  756. return p[4] | (uint32_t)p[3] << 8 | (uint32_t)p[2] << 16 |
  757. (uint32_t)p[1] << 24 | (uint64_t)p[0] << 32;
  758. case 6:
  759. return p[5] | (uint32_t)p[4] << 8 | (uint32_t)p[3] << 16 |
  760. (uint32_t)p[2] << 24 | (uint64_t)p[1] << 32 | (uint64_t)p[0] << 40;
  761. case 7:
  762. return p[6] | (uint32_t)p[5] << 8 | (uint32_t)p[4] << 16 |
  763. (uint32_t)p[3] << 24 | (uint64_t)p[2] << 32 | (uint64_t)p[1] << 40 |
  764. (uint64_t)p[0] << 48;
  765. case 0:
  766. return p[7] | (uint32_t)p[6] << 8 | (uint32_t)p[5] << 16 |
  767. (uint32_t)p[4] << 24 | (uint64_t)p[3] << 32 | (uint64_t)p[2] << 40 |
  768. (uint64_t)p[1] << 48 | (uint64_t)p[0] << 56;
  769. #endif
  770. }
  771. #endif /* T1HA_USE_FAST_ONESHOT_READ */
  772. }
  773. static __maybe_unused __always_inline uint64_t
  774. tail64_be_unaligned(const void *v, size_t tail) {
  775. const uint8_t *p = (const uint8_t *)v;
  776. #ifdef can_read_underside
  777. /* On some systems we can perform a 'oneshot' read, which is little bit
  778. * faster. Thanks Marcin Żukowski <marcin.zukowski@gmail.com> for the
  779. * reminder. */
  780. const unsigned offset = (8 - tail) & 7;
  781. const unsigned shift = offset << 3;
  782. if (likely(can_read_underside(p, 8))) {
  783. p -= offset;
  784. return fetch64_be_unaligned(p) & ((~UINT64_C(0)) >> shift);
  785. }
  786. return fetch64_be_unaligned(p) >> shift;
  787. #else
  788. switch (tail & 7) {
  789. default:
  790. unreachable();
  791. /* fall through */
  792. #if T1HA_CONFIG_UNALIGNED_ACCESS == T1HA_CONFIG_UNALIGNED_ACCESS__EFFICIENT && \
  793. __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  794. /* For most CPUs this code is better when not needed
  795. * copying for alignment or byte reordering. */
  796. case 1:
  797. return p[0];
  798. case 2:
  799. return fetch16_be_unaligned(p);
  800. case 3:
  801. return (uint32_t)fetch16_be_unaligned(p) << 8 | p[2];
  802. case 4:
  803. return fetch32_be(p);
  804. case 5:
  805. return (uint64_t)fetch32_be_unaligned(p) << 8 | p[4];
  806. case 6:
  807. return (uint64_t)fetch32_be_unaligned(p) << 16 |
  808. fetch16_be_unaligned(p + 4);
  809. case 7:
  810. return (uint64_t)fetch32_be_unaligned(p) << 24 |
  811. (uint32_t)fetch16_be_unaligned(p + 4) << 8 | p[6];
  812. case 0:
  813. return fetch64_be_unaligned(p);
  814. #else
  815. /* For most CPUs this code is better than a
  816. * copying for alignment and/or byte reordering. */
  817. case 1:
  818. return p[0];
  819. case 2:
  820. return p[1] | (uint32_t)p[0] << 8;
  821. case 3:
  822. return p[2] | (uint32_t)p[1] << 8 | (uint32_t)p[0] << 16;
  823. case 4:
  824. return p[3] | (uint32_t)p[2] << 8 | (uint32_t)p[1] << 16 |
  825. (uint32_t)p[0] << 24;
  826. case 5:
  827. return p[4] | (uint32_t)p[3] << 8 | (uint32_t)p[2] << 16 |
  828. (uint32_t)p[1] << 24 | (uint64_t)p[0] << 32;
  829. case 6:
  830. return p[5] | (uint32_t)p[4] << 8 | (uint32_t)p[3] << 16 |
  831. (uint32_t)p[2] << 24 | (uint64_t)p[1] << 32 | (uint64_t)p[0] << 40;
  832. case 7:
  833. return p[6] | (uint32_t)p[5] << 8 | (uint32_t)p[4] << 16 |
  834. (uint32_t)p[3] << 24 | (uint64_t)p[2] << 32 | (uint64_t)p[1] << 40 |
  835. (uint64_t)p[0] << 48;
  836. case 0:
  837. return p[7] | (uint32_t)p[6] << 8 | (uint32_t)p[5] << 16 |
  838. (uint32_t)p[4] << 24 | (uint64_t)p[3] << 32 | (uint64_t)p[2] << 40 |
  839. (uint64_t)p[1] << 48 | (uint64_t)p[0] << 56;
  840. #endif
  841. }
  842. #endif /* can_read_underside */
  843. }
  844. /***************************************************************************/
  845. #ifndef rot64
  846. static __always_inline uint64_t rot64(uint64_t v, unsigned s) {
  847. return (v >> s) | (v << (64 - s));
  848. }
  849. #endif /* rot64 */
  850. #ifndef mul_32x32_64
  851. static __always_inline uint64_t mul_32x32_64(uint32_t a, uint32_t b) {
  852. return a * (uint64_t)b;
  853. }
  854. #endif /* mul_32x32_64 */
  855. #ifndef add64carry_first
  856. static __maybe_unused __always_inline unsigned
  857. add64carry_first(uint64_t base, uint64_t addend, uint64_t *sum) {
  858. #if __has_builtin(__builtin_addcll)
  859. unsigned long long carryout;
  860. *sum = __builtin_addcll(base, addend, 0, &carryout);
  861. return (unsigned)carryout;
  862. #else
  863. *sum = base + addend;
  864. return *sum < addend;
  865. #endif /* __has_builtin(__builtin_addcll) */
  866. }
  867. #endif /* add64carry_fist */
  868. #ifndef add64carry_next
  869. static __maybe_unused __always_inline unsigned
  870. add64carry_next(unsigned carry, uint64_t base, uint64_t addend, uint64_t *sum) {
  871. #if __has_builtin(__builtin_addcll)
  872. unsigned long long carryout;
  873. *sum = __builtin_addcll(base, addend, carry, &carryout);
  874. return (unsigned)carryout;
  875. #else
  876. *sum = base + addend + carry;
  877. return *sum < addend || (carry && *sum == addend);
  878. #endif /* __has_builtin(__builtin_addcll) */
  879. }
  880. #endif /* add64carry_next */
  881. #ifndef add64carry_last
  882. static __maybe_unused __always_inline void
  883. add64carry_last(unsigned carry, uint64_t base, uint64_t addend, uint64_t *sum) {
  884. #if __has_builtin(__builtin_addcll)
  885. unsigned long long carryout;
  886. *sum = __builtin_addcll(base, addend, carry, &carryout);
  887. (void)carryout;
  888. #else
  889. *sum = base + addend + carry;
  890. #endif /* __has_builtin(__builtin_addcll) */
  891. }
  892. #endif /* add64carry_last */
  893. #ifndef mul_64x64_128
  894. static __maybe_unused __always_inline uint64_t mul_64x64_128(uint64_t a,
  895. uint64_t b,
  896. uint64_t *h) {
  897. #if defined(__SIZEOF_INT128__) || \
  898. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  899. __uint128_t r = (__uint128_t)a * (__uint128_t)b;
  900. /* modern GCC could nicely optimize this */
  901. *h = (uint64_t)(r >> 64);
  902. return (uint64_t)r;
  903. #elif defined(mul_64x64_high)
  904. *h = mul_64x64_high(a, b);
  905. return a * b;
  906. #else
  907. /* performs 64x64 to 128 bit multiplication */
  908. const uint64_t ll = mul_32x32_64((uint32_t)a, (uint32_t)b);
  909. const uint64_t lh = mul_32x32_64(a >> 32, (uint32_t)b);
  910. const uint64_t hl = mul_32x32_64((uint32_t)a, b >> 32);
  911. const uint64_t hh = mul_32x32_64(a >> 32, b >> 32);
  912. /* Few simplification are possible here for 32-bit architectures,
  913. * but thus we would lost compatibility with the original 64-bit
  914. * version. Think is very bad idea, because then 32-bit t1ha will
  915. * still (relatively) very slowly and well yet not compatible. */
  916. uint64_t l;
  917. add64carry_last(add64carry_first(ll, lh << 32, &l), hh, lh >> 32, h);
  918. add64carry_last(add64carry_first(l, hl << 32, &l), *h, hl >> 32, h);
  919. return l;
  920. #endif
  921. }
  922. #endif /* mul_64x64_128() */
  923. #ifndef mul_64x64_high
  924. static __maybe_unused __always_inline uint64_t mul_64x64_high(uint64_t a,
  925. uint64_t b) {
  926. uint64_t h;
  927. mul_64x64_128(a, b, &h);
  928. return h;
  929. }
  930. #endif /* mul_64x64_high */
  931. /***************************************************************************/
  932. /* 'magic' primes */
  933. static const uint64_t prime_0 = UINT64_C(0xEC99BF0D8372CAAB);
  934. static const uint64_t prime_1 = UINT64_C(0x82434FE90EDCEF39);
  935. static const uint64_t prime_2 = UINT64_C(0xD4F06DB99D67BE4B);
  936. static const uint64_t prime_3 = UINT64_C(0xBD9CACC22C6E9571);
  937. static const uint64_t prime_4 = UINT64_C(0x9C06FAF4D023E3AB);
  938. static const uint64_t prime_5 = UINT64_C(0xC060724A8424F345);
  939. static const uint64_t prime_6 = UINT64_C(0xCB5AF53AE3AAAC31);
  940. /* xor high and low parts of full 128-bit product */
  941. static __maybe_unused __always_inline uint64_t mux64(uint64_t v,
  942. uint64_t prime) {
  943. uint64_t l, h;
  944. l = mul_64x64_128(v, prime, &h);
  945. return l ^ h;
  946. }
  947. static __always_inline uint64_t final64(uint64_t a, uint64_t b) {
  948. uint64_t x = (a + rot64(b, 41)) * prime_0;
  949. uint64_t y = (rot64(a, 23) + b) * prime_6;
  950. return mux64(x ^ y, prime_5);
  951. }
  952. static __always_inline void mixup64(uint64_t *__restrict a,
  953. uint64_t *__restrict b, uint64_t v,
  954. uint64_t prime) {
  955. uint64_t h;
  956. *a ^= mul_64x64_128(*b + v, prime, &h);
  957. *b += h;
  958. }
  959. /***************************************************************************/
  960. typedef union t1ha_uint128 {
  961. #if defined(__SIZEOF_INT128__) || \
  962. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  963. __uint128_t v;
  964. #endif
  965. struct {
  966. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  967. uint64_t l;
  968. uint64_t h;
  969. #else
  970. uint64_t h;
  971. uint64_t l;
  972. #endif
  973. } p;
  974. } t1ha_uint128_t;
  975. static __always_inline t1ha_uint128_t not128(const t1ha_uint128_t v) {
  976. t1ha_uint128_t r;
  977. #if defined(__SIZEOF_INT128__) || \
  978. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  979. r.v = ~v.v;
  980. #else
  981. r.p.l = ~v.p.l;
  982. r.p.h = ~v.p.h;
  983. #endif
  984. return r;
  985. }
  986. static __always_inline t1ha_uint128_t left128(const t1ha_uint128_t v,
  987. unsigned s) {
  988. t1ha_uint128_t r;
  989. assert(s < 128);
  990. #if defined(__SIZEOF_INT128__) || \
  991. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  992. r.v = v.v << s;
  993. #else
  994. r.p.l = (s < 64) ? v.p.l << s : 0;
  995. r.p.h = (s < 64) ? (v.p.h << s) | (s ? v.p.l >> (64 - s) : 0) : v.p.l << (s - 64);
  996. #endif
  997. return r;
  998. }
  999. static __always_inline t1ha_uint128_t right128(const t1ha_uint128_t v,
  1000. unsigned s) {
  1001. t1ha_uint128_t r;
  1002. assert(s < 128);
  1003. #if defined(__SIZEOF_INT128__) || \
  1004. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  1005. r.v = v.v >> s;
  1006. #else
  1007. r.p.l = (s < 64) ? (s ? v.p.h << (64 - s) : 0) | (v.p.l >> s) : v.p.h >> (s - 64);
  1008. r.p.h = (s < 64) ? v.p.h >> s : 0;
  1009. #endif
  1010. return r;
  1011. }
  1012. static __always_inline t1ha_uint128_t or128(t1ha_uint128_t x,
  1013. t1ha_uint128_t y) {
  1014. t1ha_uint128_t r;
  1015. #if defined(__SIZEOF_INT128__) || \
  1016. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  1017. r.v = x.v | y.v;
  1018. #else
  1019. r.p.l = x.p.l | y.p.l;
  1020. r.p.h = x.p.h | y.p.h;
  1021. #endif
  1022. return r;
  1023. }
  1024. static __always_inline t1ha_uint128_t xor128(t1ha_uint128_t x,
  1025. t1ha_uint128_t y) {
  1026. t1ha_uint128_t r;
  1027. #if defined(__SIZEOF_INT128__) || \
  1028. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  1029. r.v = x.v ^ y.v;
  1030. #else
  1031. r.p.l = x.p.l ^ y.p.l;
  1032. r.p.h = x.p.h ^ y.p.h;
  1033. #endif
  1034. return r;
  1035. }
  1036. static __always_inline t1ha_uint128_t rot128(t1ha_uint128_t v, unsigned s) {
  1037. s &= 127;
  1038. #if defined(__SIZEOF_INT128__) || \
  1039. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  1040. v.v = (v.v << (128 - s)) | (v.v >> s);
  1041. return v;
  1042. #else
  1043. return s ? or128(left128(v, 128 - s), right128(v, s)) : v;
  1044. #endif
  1045. }
  1046. static __always_inline t1ha_uint128_t add128(t1ha_uint128_t x,
  1047. t1ha_uint128_t y) {
  1048. t1ha_uint128_t r;
  1049. #if defined(__SIZEOF_INT128__) || \
  1050. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  1051. r.v = x.v + y.v;
  1052. #else
  1053. add64carry_last(add64carry_first(x.p.l, y.p.l, &r.p.l), x.p.h, y.p.h, &r.p.h);
  1054. #endif
  1055. return r;
  1056. }
  1057. static __always_inline t1ha_uint128_t mul128(t1ha_uint128_t x,
  1058. t1ha_uint128_t y) {
  1059. t1ha_uint128_t r;
  1060. #if defined(__SIZEOF_INT128__) || \
  1061. (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
  1062. r.v = x.v * y.v;
  1063. #else
  1064. r.p.l = mul_64x64_128(x.p.l, y.p.l, &r.p.h);
  1065. r.p.h += x.p.l * y.p.h + y.p.l * x.p.h;
  1066. #endif
  1067. return r;
  1068. }