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

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