XXHash128.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. using System;
  2. using System.Buffers.Binary;
  3. using System.Diagnostics;
  4. using System.Numerics;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.Intrinsics;
  7. using System.Runtime.Intrinsics.X86;
  8. namespace Ryujinx.Common
  9. {
  10. public static class XXHash128
  11. {
  12. private const int StripeLen = 64;
  13. private const int AccNb = StripeLen / sizeof(ulong);
  14. private const int SecretConsumeRate = 8;
  15. private const int SecretLastAccStart = 7;
  16. private const int SecretMergeAccsStart = 11;
  17. private const int SecretSizeMin = 136;
  18. private const int MidSizeStartOffset = 3;
  19. private const int MidSizeLastOffset = 17;
  20. private const uint Prime32_1 = 0x9E3779B1U;
  21. private const uint Prime32_2 = 0x85EBCA77U;
  22. private const uint Prime32_3 = 0xC2B2AE3DU;
  23. private const uint Prime32_4 = 0x27D4EB2FU;
  24. private const uint Prime32_5 = 0x165667B1U;
  25. private const ulong Prime64_1 = 0x9E3779B185EBCA87UL;
  26. private const ulong Prime64_2 = 0xC2B2AE3D27D4EB4FUL;
  27. private const ulong Prime64_3 = 0x165667B19E3779F9UL;
  28. private const ulong Prime64_4 = 0x85EBCA77C2B2AE63UL;
  29. private const ulong Prime64_5 = 0x27D4EB2F165667C5UL;
  30. private static readonly ulong[] Xxh3InitAcc = new ulong[]
  31. {
  32. Prime32_3,
  33. Prime64_1,
  34. Prime64_2,
  35. Prime64_3,
  36. Prime64_4,
  37. Prime32_2,
  38. Prime64_5,
  39. Prime32_1
  40. };
  41. private static readonly byte[] Xxh3KSecret = new byte[]
  42. {
  43. 0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe, 0x7c, 0x01, 0x81, 0x2c, 0xf7, 0x21, 0xad, 0x1c,
  44. 0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb, 0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f,
  45. 0xcb, 0x79, 0xe6, 0x4e, 0xcc, 0xc0, 0xe5, 0x78, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21,
  46. 0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e, 0xe0, 0x35, 0x90, 0xe6, 0x81, 0x3a, 0x26, 0x4c,
  47. 0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb, 0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3,
  48. 0x71, 0x64, 0x48, 0x97, 0xa2, 0x0d, 0xf9, 0x4e, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8,
  49. 0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f, 0xf9, 0xdc, 0xbb, 0xc7, 0xc7, 0x0b, 0x4f, 0x1d,
  50. 0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31, 0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64,
  51. 0xea, 0xc5, 0xac, 0x83, 0x34, 0xd3, 0xeb, 0xc3, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb,
  52. 0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49, 0xd3, 0x16, 0x55, 0x26, 0x29, 0xd4, 0x68, 0x9e,
  53. 0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc, 0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce,
  54. 0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e
  55. };
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. private static ulong Mult32To64(ulong x, ulong y)
  58. {
  59. return (ulong)(uint)x * (ulong)(uint)y;
  60. }
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. private unsafe static Hash128 Mult64To128(ulong lhs, ulong rhs)
  63. {
  64. // TODO: Use BigMul once .NET 5 lands.
  65. if (Bmi2.X64.IsSupported)
  66. {
  67. ulong low;
  68. ulong high = Bmi2.X64.MultiplyNoFlags(lhs, rhs, &low);
  69. return new Hash128
  70. {
  71. Low = low,
  72. High = high
  73. };
  74. }
  75. ulong loLo = Mult32To64((uint)lhs, (uint)rhs);
  76. ulong hiLo = Mult32To64(lhs >> 32, (uint)rhs);
  77. ulong loHi = Mult32To64((uint)lhs, rhs >> 32);
  78. ulong hiHi = Mult32To64(lhs >> 32, rhs >> 32);
  79. ulong cross = (loLo >> 32) + (uint)hiLo + loHi;
  80. ulong upper = (hiLo >> 32) + (cross >> 32) + hiHi;
  81. ulong lower = (cross << 32) | (uint)loLo;
  82. return new Hash128
  83. {
  84. Low = lower,
  85. High = upper
  86. };
  87. }
  88. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  89. private static ulong Mul128Fold64(ulong lhs, ulong rhs)
  90. {
  91. Hash128 product = Mult64To128(lhs, rhs);
  92. return product.Low ^ product.High;
  93. }
  94. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  95. private static ulong XorShift64(ulong v64, int shift)
  96. {
  97. Debug.Assert(0 <= shift && shift < 64);
  98. return v64 ^ (v64 >> shift);
  99. }
  100. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  101. private static ulong Xxh3Avalanche(ulong h64)
  102. {
  103. h64 = XorShift64(h64, 37);
  104. h64 *= 0x165667919E3779F9UL;
  105. h64 = XorShift64(h64, 32);
  106. return h64;
  107. }
  108. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  109. private static ulong Xxh64Avalanche(ulong h64)
  110. {
  111. h64 ^= h64 >> 33;
  112. h64 *= Prime64_2;
  113. h64 ^= h64 >> 29;
  114. h64 *= Prime64_3;
  115. h64 ^= h64 >> 32;
  116. return h64;
  117. }
  118. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  119. private unsafe static void Xxh3Accumulate512(Span<ulong> acc, ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret)
  120. {
  121. if (Avx2.IsSupported)
  122. {
  123. fixed (ulong* pAcc = acc)
  124. {
  125. fixed (byte* pInput = input, pSecret = secret)
  126. {
  127. Vector256<ulong>* xAcc = (Vector256<ulong>*)pAcc;
  128. Vector256<byte>* xInput = (Vector256<byte>*)pInput;
  129. Vector256<byte>* xSecret = (Vector256<byte>*)pSecret;
  130. for (ulong i = 0; i < StripeLen / 32; i++)
  131. {
  132. Vector256<byte> dataVec = xInput[i];
  133. Vector256<byte> keyVec = xSecret[i];
  134. Vector256<byte> dataKey = Avx2.Xor(dataVec, keyVec);
  135. Vector256<uint> dataKeyLo = Avx2.Shuffle(dataKey.AsUInt32(), 0b00110001);
  136. Vector256<ulong> product = Avx2.Multiply(dataKey.AsUInt32(), dataKeyLo);
  137. Vector256<uint> dataSwap = Avx2.Shuffle(dataVec.AsUInt32(), 0b01001110);
  138. Vector256<ulong> sum = Avx2.Add(xAcc[i], dataSwap.AsUInt64());
  139. xAcc[i] = Avx2.Add(product, sum);
  140. }
  141. }
  142. }
  143. }
  144. else if (Sse2.IsSupported)
  145. {
  146. fixed (ulong* pAcc = acc)
  147. {
  148. fixed (byte* pInput = input, pSecret = secret)
  149. {
  150. Vector128<ulong>* xAcc = (Vector128<ulong>*)pAcc;
  151. Vector128<byte>* xInput = (Vector128<byte>*)pInput;
  152. Vector128<byte>* xSecret = (Vector128<byte>*)pSecret;
  153. for (ulong i = 0; i < StripeLen / 16; i++)
  154. {
  155. Vector128<byte> dataVec = xInput[i];
  156. Vector128<byte> keyVec = xSecret[i];
  157. Vector128<byte> dataKey = Sse2.Xor(dataVec, keyVec);
  158. Vector128<uint> dataKeyLo = Sse2.Shuffle(dataKey.AsUInt32(), 0b00110001);
  159. Vector128<ulong> product = Sse2.Multiply(dataKey.AsUInt32(), dataKeyLo);
  160. Vector128<uint> dataSwap = Sse2.Shuffle(dataVec.AsUInt32(), 0b01001110);
  161. Vector128<ulong> sum = Sse2.Add(xAcc[i], dataSwap.AsUInt64());
  162. xAcc[i] = Sse2.Add(product, sum);
  163. }
  164. }
  165. }
  166. }
  167. else
  168. {
  169. for (int i = 0; i < AccNb; i++)
  170. {
  171. ulong dataVal = BinaryPrimitives.ReadUInt64LittleEndian(input.Slice(i * sizeof(ulong)));
  172. ulong dataKey = dataVal ^ BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(i * sizeof(ulong)));
  173. acc[i ^ 1] += dataVal;
  174. acc[i] += Mult32To64((uint)dataKey, dataKey >> 32);
  175. }
  176. }
  177. }
  178. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  179. private unsafe static void Xxh3ScrambleAcc(Span<ulong> acc, ReadOnlySpan<byte> secret)
  180. {
  181. if (Avx2.IsSupported)
  182. {
  183. fixed (ulong* pAcc = acc)
  184. {
  185. fixed (byte* pSecret = secret)
  186. {
  187. Vector256<uint> prime32 = Vector256.Create(Prime32_1);
  188. Vector256<ulong>* xAcc = (Vector256<ulong>*)pAcc;
  189. Vector256<byte>* xSecret = (Vector256<byte>*)pSecret;
  190. for (ulong i = 0; i < StripeLen / 32; i++)
  191. {
  192. Vector256<ulong> accVec = xAcc[i];
  193. Vector256<ulong> shifted = Avx2.ShiftRightLogical(accVec, 47);
  194. Vector256<ulong> dataVec = Avx2.Xor(accVec, shifted);
  195. Vector256<byte> keyVec = xSecret[i];
  196. Vector256<uint> dataKey = Avx2.Xor(dataVec.AsUInt32(), keyVec.AsUInt32());
  197. Vector256<uint> dataKeyHi = Avx2.Shuffle(dataKey.AsUInt32(), 0b00110001);
  198. Vector256<ulong> prodLo = Avx2.Multiply(dataKey, prime32);
  199. Vector256<ulong> prodHi = Avx2.Multiply(dataKeyHi, prime32);
  200. xAcc[i] = Avx2.Add(prodLo, Avx2.ShiftLeftLogical(prodHi, 32));
  201. }
  202. }
  203. }
  204. }
  205. else if (Sse2.IsSupported)
  206. {
  207. fixed (ulong* pAcc = acc)
  208. {
  209. fixed (byte* pSecret = secret)
  210. {
  211. Vector128<uint> prime32 = Vector128.Create(Prime32_1);
  212. Vector128<ulong>* xAcc = (Vector128<ulong>*)pAcc;
  213. Vector128<byte>* xSecret = (Vector128<byte>*)pSecret;
  214. for (ulong i = 0; i < StripeLen / 16; i++)
  215. {
  216. Vector128<ulong> accVec = xAcc[i];
  217. Vector128<ulong> shifted = Sse2.ShiftRightLogical(accVec, 47);
  218. Vector128<ulong> dataVec = Sse2.Xor(accVec, shifted);
  219. Vector128<byte> keyVec = xSecret[i];
  220. Vector128<uint> dataKey = Sse2.Xor(dataVec.AsUInt32(), keyVec.AsUInt32());
  221. Vector128<uint> dataKeyHi = Sse2.Shuffle(dataKey.AsUInt32(), 0b00110001);
  222. Vector128<ulong> prodLo = Sse2.Multiply(dataKey, prime32);
  223. Vector128<ulong> prodHi = Sse2.Multiply(dataKeyHi, prime32);
  224. xAcc[i] = Sse2.Add(prodLo, Sse2.ShiftLeftLogical(prodHi, 32));
  225. }
  226. }
  227. }
  228. }
  229. else
  230. {
  231. for (int i = 0; i < AccNb; i++)
  232. {
  233. ulong key64 = BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(i * sizeof(ulong)));
  234. ulong acc64 = acc[i];
  235. acc64 = XorShift64(acc64, 47);
  236. acc64 ^= key64;
  237. acc64 *= Prime32_1;
  238. acc[i] = acc64;
  239. }
  240. }
  241. }
  242. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  243. private static void Xxh3Accumulate(Span<ulong> acc, ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret, int nbStripes)
  244. {
  245. for (int n = 0; n < nbStripes; n++)
  246. {
  247. ReadOnlySpan<byte> inData = input.Slice(n * StripeLen);
  248. Xxh3Accumulate512(acc, inData, secret.Slice(n * SecretConsumeRate));
  249. }
  250. }
  251. private static void Xxh3HashLongInternalLoop(Span<ulong> acc, ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret)
  252. {
  253. int nbStripesPerBlock = (secret.Length - StripeLen) / SecretConsumeRate;
  254. int blockLen = StripeLen * nbStripesPerBlock;
  255. int nbBlocks = (input.Length - 1) / blockLen;
  256. Debug.Assert(secret.Length >= SecretSizeMin);
  257. for (int n = 0; n < nbBlocks; n++)
  258. {
  259. Xxh3Accumulate(acc, input.Slice(n * blockLen), secret, nbStripesPerBlock);
  260. Xxh3ScrambleAcc(acc, secret.Slice(secret.Length - StripeLen));
  261. }
  262. Debug.Assert(input.Length > StripeLen);
  263. int nbStripes = (input.Length - 1 - (blockLen * nbBlocks)) / StripeLen;
  264. Debug.Assert(nbStripes <= (secret.Length / SecretConsumeRate));
  265. Xxh3Accumulate(acc, input.Slice(nbBlocks * blockLen), secret, nbStripes);
  266. ReadOnlySpan<byte> p = input.Slice(input.Length - StripeLen);
  267. Xxh3Accumulate512(acc, p, secret.Slice(secret.Length - StripeLen - SecretLastAccStart));
  268. }
  269. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  270. private static ulong Xxh3Mix2Accs(Span<ulong> acc, ReadOnlySpan<byte> secret)
  271. {
  272. return Mul128Fold64(
  273. acc[0] ^ BinaryPrimitives.ReadUInt64LittleEndian(secret),
  274. acc[1] ^ BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(8)));
  275. }
  276. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  277. private static ulong Xxh3MergeAccs(Span<ulong> acc, ReadOnlySpan<byte> secret, ulong start)
  278. {
  279. ulong result64 = start;
  280. for (int i = 0; i < 4; i++)
  281. {
  282. result64 += Xxh3Mix2Accs(acc.Slice(2 * i), secret.Slice(16 * i));
  283. }
  284. return Xxh3Avalanche(result64);
  285. }
  286. private static Hash128 Xxh3HashLong128bInternal(ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret)
  287. {
  288. Span<ulong> acc = stackalloc ulong[AccNb]; // TODO: Use SkipLocalsInit attribute once .NET 5 lands.
  289. Xxh3InitAcc.CopyTo(acc);
  290. Xxh3HashLongInternalLoop(acc, input, secret);
  291. Debug.Assert(acc.Length == 8);
  292. Debug.Assert(secret.Length >= acc.Length * sizeof(ulong) + SecretMergeAccsStart);
  293. return new Hash128
  294. {
  295. Low = Xxh3MergeAccs(acc, secret.Slice(SecretMergeAccsStart), (ulong)input.Length * Prime64_1),
  296. High = Xxh3MergeAccs(
  297. acc,
  298. secret.Slice(secret.Length - acc.Length * sizeof(ulong) - SecretMergeAccsStart),
  299. ~((ulong)input.Length * Prime64_2))
  300. };
  301. }
  302. private static Hash128 Xxh3Len1To3128b(ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret, ulong seed)
  303. {
  304. Debug.Assert(1 <= input.Length && input.Length <= 3);
  305. byte c1 = input[0];
  306. byte c2 = input[input.Length >> 1];
  307. byte c3 = input[^1];
  308. uint combinedL = ((uint)c1 << 16) | ((uint)c2 << 24) | c3 | ((uint)input.Length << 8);
  309. uint combinedH = BitOperations.RotateLeft(BinaryPrimitives.ReverseEndianness(combinedL), 13);
  310. ulong bitFlipL = (BinaryPrimitives.ReadUInt32LittleEndian(secret) ^ BinaryPrimitives.ReadUInt32LittleEndian(secret.Slice(4))) + seed;
  311. ulong bitFlipH = (BinaryPrimitives.ReadUInt32LittleEndian(secret.Slice(8)) ^ BinaryPrimitives.ReadUInt32LittleEndian(secret.Slice(12))) - seed;
  312. ulong keyedLo = combinedL ^ bitFlipL;
  313. ulong keyedHi = combinedH ^ bitFlipH;
  314. return new Hash128
  315. {
  316. Low = Xxh64Avalanche(keyedLo),
  317. High = Xxh64Avalanche(keyedHi)
  318. };
  319. }
  320. private static Hash128 Xxh3Len4To8128b(ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret, ulong seed)
  321. {
  322. Debug.Assert(4 <= input.Length && input.Length <= 8);
  323. seed ^= BinaryPrimitives.ReverseEndianness((uint)seed) << 32;
  324. uint inputLo = BinaryPrimitives.ReadUInt32LittleEndian(input);
  325. uint inputHi = BinaryPrimitives.ReadUInt32LittleEndian(input.Slice(input.Length - 4));
  326. ulong input64 = inputLo + ((ulong)inputHi << 32);
  327. ulong bitFlip = (BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(16)) ^ BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(24))) + seed;
  328. ulong keyed = input64 ^ bitFlip;
  329. Hash128 m128 = Mult64To128(keyed, Prime64_1 + ((ulong)input.Length << 2));
  330. m128.High += m128.Low << 1;
  331. m128.Low ^= m128.High >> 3;
  332. m128.Low = XorShift64(m128.Low, 35);
  333. m128.Low *= 0x9FB21C651E98DF25UL;
  334. m128.Low = XorShift64(m128.Low, 28);
  335. m128.High = Xxh3Avalanche(m128.High);
  336. return m128;
  337. }
  338. private static Hash128 Xxh3Len9To16128b(ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret, ulong seed)
  339. {
  340. Debug.Assert(9 <= input.Length && input.Length <= 16);
  341. ulong bitFlipL = (BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(32)) ^ BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(40))) - seed;
  342. ulong bitFlipH = (BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(48)) ^ BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(56))) + seed;
  343. ulong inputLo = BinaryPrimitives.ReadUInt64LittleEndian(input);
  344. ulong inputHi = BinaryPrimitives.ReadUInt64LittleEndian(input.Slice(input.Length - 8));
  345. Hash128 m128 = Mult64To128(inputLo ^ inputHi ^ bitFlipL, Prime64_1);
  346. m128.Low += ((ulong)input.Length - 1) << 54;
  347. inputHi ^= bitFlipH;
  348. m128.High += inputHi + Mult32To64((uint)inputHi, Prime32_2 - 1);
  349. m128.Low ^= BinaryPrimitives.ReverseEndianness(m128.High);
  350. Hash128 h128 = Mult64To128(m128.Low, Prime64_2);
  351. h128.High += m128.High * Prime64_2;
  352. h128.Low = Xxh3Avalanche(h128.Low);
  353. h128.High = Xxh3Avalanche(h128.High);
  354. return h128;
  355. }
  356. private static Hash128 Xxh3Len0To16128b(ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret, ulong seed)
  357. {
  358. Debug.Assert(input.Length <= 16);
  359. if (input.Length > 8)
  360. {
  361. return Xxh3Len9To16128b(input, secret, seed);
  362. }
  363. else if (input.Length >= 4)
  364. {
  365. return Xxh3Len4To8128b(input, secret, seed);
  366. }
  367. else if (input.Length != 0)
  368. {
  369. return Xxh3Len1To3128b(input, secret, seed);
  370. }
  371. else
  372. {
  373. Hash128 h128 = new Hash128();
  374. ulong bitFlipL = BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(64)) ^ BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(72));
  375. ulong bitFlipH = BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(80)) ^ BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(88));
  376. h128.Low = Xxh64Avalanche(seed ^ bitFlipL);
  377. h128.High = Xxh64Avalanche(seed ^ bitFlipH);
  378. return h128;
  379. }
  380. }
  381. private static ulong Xxh3Mix16b(ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret, ulong seed)
  382. {
  383. ulong inputLo = BinaryPrimitives.ReadUInt64LittleEndian(input);
  384. ulong inputHi = BinaryPrimitives.ReadUInt64LittleEndian(input.Slice(8));
  385. return Mul128Fold64(
  386. inputLo ^ (BinaryPrimitives.ReadUInt64LittleEndian(secret) + seed),
  387. inputHi ^ (BinaryPrimitives.ReadUInt64LittleEndian(secret.Slice(8)) - seed));
  388. }
  389. private static Hash128 Xxh128Mix32b(Hash128 acc, ReadOnlySpan<byte> input, ReadOnlySpan<byte> input2, ReadOnlySpan<byte> secret, ulong seed)
  390. {
  391. acc.Low += Xxh3Mix16b(input, secret, seed);
  392. acc.Low ^= BinaryPrimitives.ReadUInt64LittleEndian(input2) + BinaryPrimitives.ReadUInt64LittleEndian(input2.Slice(8));
  393. acc.High += Xxh3Mix16b(input2, secret.Slice(16), seed);
  394. acc.High ^= BinaryPrimitives.ReadUInt64LittleEndian(input) + BinaryPrimitives.ReadUInt64LittleEndian(input.Slice(8));
  395. return acc;
  396. }
  397. private static Hash128 Xxh3Len17To128128b(ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret, ulong seed)
  398. {
  399. Debug.Assert(secret.Length >= SecretSizeMin);
  400. Debug.Assert(16 < input.Length && input.Length <= 128);
  401. Hash128 acc = new Hash128
  402. {
  403. Low = (ulong)input.Length * Prime64_1,
  404. High = 0
  405. };
  406. if (input.Length > 32)
  407. {
  408. if (input.Length > 64)
  409. {
  410. if (input.Length > 96)
  411. {
  412. acc = Xxh128Mix32b(acc, input.Slice(48), input.Slice(input.Length - 64), secret.Slice(96), seed);
  413. }
  414. acc = Xxh128Mix32b(acc, input.Slice(32), input.Slice(input.Length - 48), secret.Slice(64), seed);
  415. }
  416. acc = Xxh128Mix32b(acc, input.Slice(16), input.Slice(input.Length - 32), secret.Slice(32), seed);
  417. }
  418. acc = Xxh128Mix32b(acc, input, input.Slice(input.Length - 16), secret, seed);
  419. Hash128 h128 = new Hash128
  420. {
  421. Low = acc.Low + acc.High,
  422. High = acc.Low * Prime64_1 + acc.High * Prime64_4 + ((ulong)input.Length - seed) * Prime64_2
  423. };
  424. h128.Low = Xxh3Avalanche(h128.Low);
  425. h128.High = 0UL - Xxh3Avalanche(h128.High);
  426. return h128;
  427. }
  428. private static Hash128 Xxh3Len129To240128b(ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret, ulong seed)
  429. {
  430. Debug.Assert(secret.Length >= SecretSizeMin);
  431. Debug.Assert(128 < input.Length && input.Length <= 240);
  432. Hash128 acc = new Hash128();
  433. int nbRounds = input.Length / 32;
  434. acc.Low = (ulong)input.Length * Prime64_1;
  435. acc.High = 0;
  436. for (int i = 0; i < 4; i++)
  437. {
  438. acc = Xxh128Mix32b(acc, input.Slice(32 * i), input.Slice(32 * i + 16), secret.Slice(32 * i), seed);
  439. }
  440. acc.Low = Xxh3Avalanche(acc.Low);
  441. acc.High = Xxh3Avalanche(acc.High);
  442. Debug.Assert(nbRounds >= 4);
  443. for (int i = 4; i < nbRounds; i++)
  444. {
  445. acc = Xxh128Mix32b(acc, input.Slice(32 * i), input.Slice(32 * i + 16), secret.Slice(MidSizeStartOffset + 32 * (i - 4)), seed);
  446. }
  447. acc = Xxh128Mix32b(acc, input.Slice(input.Length - 16), input.Slice(input.Length - 32), secret.Slice(SecretSizeMin - MidSizeLastOffset - 16), 0UL - seed);
  448. Hash128 h128 = new Hash128
  449. {
  450. Low = acc.Low + acc.High,
  451. High = acc.Low * Prime64_1 + acc.High * Prime64_4 + ((ulong)input.Length - seed) * Prime64_2
  452. };
  453. h128.Low = Xxh3Avalanche(h128.Low);
  454. h128.High = 0UL - Xxh3Avalanche(h128.High);
  455. return h128;
  456. }
  457. private static Hash128 Xxh3128bitsInternal(ReadOnlySpan<byte> input, ReadOnlySpan<byte> secret, ulong seed)
  458. {
  459. Debug.Assert(secret.Length >= SecretSizeMin);
  460. if (input.Length <= 16)
  461. {
  462. return Xxh3Len0To16128b(input, secret, seed);
  463. }
  464. else if (input.Length <= 128)
  465. {
  466. return Xxh3Len17To128128b(input, secret, seed);
  467. }
  468. else if (input.Length <= 240)
  469. {
  470. return Xxh3Len129To240128b(input, secret, seed);
  471. }
  472. else
  473. {
  474. return Xxh3HashLong128bInternal(input, secret);
  475. }
  476. }
  477. public static Hash128 ComputeHash(ReadOnlySpan<byte> input)
  478. {
  479. return Xxh3128bitsInternal(input, Xxh3KSecret, 0UL);
  480. }
  481. }
  482. }