ASoftFallback.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. using ChocolArm64.State;
  2. using ChocolArm64.Translation;
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.Intrinsics;
  6. using System.Runtime.Intrinsics.X86;
  7. namespace ChocolArm64.Instruction
  8. {
  9. using static AVectorHelper;
  10. static class ASoftFallback
  11. {
  12. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  13. {
  14. Context.EmitCall(typeof(ASoftFallback), MthdName);
  15. }
  16. #region "ShrImm_64"
  17. public static long SignedShrImm_64(long Value, long RoundConst, int Shift)
  18. {
  19. if (RoundConst == 0L)
  20. {
  21. if (Shift <= 63)
  22. {
  23. return Value >> Shift;
  24. }
  25. else /* if (Shift == 64) */
  26. {
  27. if (Value < 0L)
  28. {
  29. return -1L;
  30. }
  31. else
  32. {
  33. return 0L;
  34. }
  35. }
  36. }
  37. else /* if (RoundConst == 1L << (Shift - 1)) */
  38. {
  39. if (Shift <= 63)
  40. {
  41. long Add = Value + RoundConst;
  42. if ((~Value & (Value ^ Add)) < 0L)
  43. {
  44. return (long)((ulong)Add >> Shift);
  45. }
  46. else
  47. {
  48. return Add >> Shift;
  49. }
  50. }
  51. else /* if (Shift == 64) */
  52. {
  53. return 0L;
  54. }
  55. }
  56. }
  57. public static ulong UnsignedShrImm_64(ulong Value, long RoundConst, int Shift)
  58. {
  59. if (RoundConst == 0L)
  60. {
  61. if (Shift <= 63)
  62. {
  63. return Value >> Shift;
  64. }
  65. else /* if (Shift == 64) */
  66. {
  67. return 0UL;
  68. }
  69. }
  70. else /* if (RoundConst == 1L << (Shift - 1)) */
  71. {
  72. ulong Add = Value + (ulong)RoundConst;
  73. if ((Add < Value) && (Add < (ulong)RoundConst))
  74. {
  75. if (Shift <= 63)
  76. {
  77. return (Add >> Shift) | (0x8000000000000000UL >> (Shift - 1));
  78. }
  79. else /* if (Shift == 64) */
  80. {
  81. return 1UL;
  82. }
  83. }
  84. else
  85. {
  86. if (Shift <= 63)
  87. {
  88. return Add >> Shift;
  89. }
  90. else /* if (Shift == 64) */
  91. {
  92. return 0UL;
  93. }
  94. }
  95. }
  96. }
  97. #endregion
  98. #region "Saturating"
  99. public static long SignedSrcSignedDstSatQ(long op, int Size, AThreadState State)
  100. {
  101. int ESize = 8 << Size;
  102. long TMaxValue = (1L << (ESize - 1)) - 1L;
  103. long TMinValue = -(1L << (ESize - 1));
  104. if (op > TMaxValue)
  105. {
  106. SetFpsrQCFlag(State);
  107. return TMaxValue;
  108. }
  109. else if (op < TMinValue)
  110. {
  111. SetFpsrQCFlag(State);
  112. return TMinValue;
  113. }
  114. else
  115. {
  116. return op;
  117. }
  118. }
  119. public static ulong SignedSrcUnsignedDstSatQ(long op, int Size, AThreadState State)
  120. {
  121. int ESize = 8 << Size;
  122. ulong TMaxValue = (1UL << ESize) - 1UL;
  123. ulong TMinValue = 0UL;
  124. if (op > (long)TMaxValue)
  125. {
  126. SetFpsrQCFlag(State);
  127. return TMaxValue;
  128. }
  129. else if (op < (long)TMinValue)
  130. {
  131. SetFpsrQCFlag(State);
  132. return TMinValue;
  133. }
  134. else
  135. {
  136. return (ulong)op;
  137. }
  138. }
  139. public static long UnsignedSrcSignedDstSatQ(ulong op, int Size, AThreadState State)
  140. {
  141. int ESize = 8 << Size;
  142. long TMaxValue = (1L << (ESize - 1)) - 1L;
  143. if (op > (ulong)TMaxValue)
  144. {
  145. SetFpsrQCFlag(State);
  146. return TMaxValue;
  147. }
  148. else
  149. {
  150. return (long)op;
  151. }
  152. }
  153. public static ulong UnsignedSrcUnsignedDstSatQ(ulong op, int Size, AThreadState State)
  154. {
  155. int ESize = 8 << Size;
  156. ulong TMaxValue = (1UL << ESize) - 1UL;
  157. if (op > TMaxValue)
  158. {
  159. SetFpsrQCFlag(State);
  160. return TMaxValue;
  161. }
  162. else
  163. {
  164. return op;
  165. }
  166. }
  167. public static long UnarySignedSatQAbsOrNeg(long op, AThreadState State)
  168. {
  169. if (op == long.MinValue)
  170. {
  171. SetFpsrQCFlag(State);
  172. return long.MaxValue;
  173. }
  174. else
  175. {
  176. return op;
  177. }
  178. }
  179. public static long BinarySignedSatQAdd(long op1, long op2, AThreadState State)
  180. {
  181. long Add = op1 + op2;
  182. if ((~(op1 ^ op2) & (op1 ^ Add)) < 0L)
  183. {
  184. SetFpsrQCFlag(State);
  185. if (op1 < 0L)
  186. {
  187. return long.MinValue;
  188. }
  189. else
  190. {
  191. return long.MaxValue;
  192. }
  193. }
  194. else
  195. {
  196. return Add;
  197. }
  198. }
  199. public static ulong BinaryUnsignedSatQAdd(ulong op1, ulong op2, AThreadState State)
  200. {
  201. ulong Add = op1 + op2;
  202. if ((Add < op1) && (Add < op2))
  203. {
  204. SetFpsrQCFlag(State);
  205. return ulong.MaxValue;
  206. }
  207. else
  208. {
  209. return Add;
  210. }
  211. }
  212. public static long BinarySignedSatQSub(long op1, long op2, AThreadState State)
  213. {
  214. long Sub = op1 - op2;
  215. if (((op1 ^ op2) & (op1 ^ Sub)) < 0L)
  216. {
  217. SetFpsrQCFlag(State);
  218. if (op1 < 0L)
  219. {
  220. return long.MinValue;
  221. }
  222. else
  223. {
  224. return long.MaxValue;
  225. }
  226. }
  227. else
  228. {
  229. return Sub;
  230. }
  231. }
  232. public static ulong BinaryUnsignedSatQSub(ulong op1, ulong op2, AThreadState State)
  233. {
  234. ulong Sub = op1 - op2;
  235. if (op1 < op2)
  236. {
  237. SetFpsrQCFlag(State);
  238. return ulong.MinValue;
  239. }
  240. else
  241. {
  242. return Sub;
  243. }
  244. }
  245. public static long BinarySignedSatQAcc(ulong op1, long op2, AThreadState State)
  246. {
  247. if (op1 <= (ulong)long.MaxValue)
  248. {
  249. // op1 from ulong.MinValue to (ulong)long.MaxValue
  250. // op2 from long.MinValue to long.MaxValue
  251. long Add = (long)op1 + op2;
  252. if ((~op2 & Add) < 0L)
  253. {
  254. SetFpsrQCFlag(State);
  255. return long.MaxValue;
  256. }
  257. else
  258. {
  259. return Add;
  260. }
  261. }
  262. else if (op2 >= 0L)
  263. {
  264. // op1 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  265. // op2 from (long)ulong.MinValue to long.MaxValue
  266. SetFpsrQCFlag(State);
  267. return long.MaxValue;
  268. }
  269. else
  270. {
  271. // op1 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  272. // op2 from long.MinValue to (long)ulong.MinValue - 1L
  273. ulong Add = op1 + (ulong)op2;
  274. if (Add > (ulong)long.MaxValue)
  275. {
  276. SetFpsrQCFlag(State);
  277. return long.MaxValue;
  278. }
  279. else
  280. {
  281. return (long)Add;
  282. }
  283. }
  284. }
  285. public static ulong BinaryUnsignedSatQAcc(long op1, ulong op2, AThreadState State)
  286. {
  287. if (op1 >= 0L)
  288. {
  289. // op1 from (long)ulong.MinValue to long.MaxValue
  290. // op2 from ulong.MinValue to ulong.MaxValue
  291. ulong Add = (ulong)op1 + op2;
  292. if ((Add < (ulong)op1) && (Add < op2))
  293. {
  294. SetFpsrQCFlag(State);
  295. return ulong.MaxValue;
  296. }
  297. else
  298. {
  299. return Add;
  300. }
  301. }
  302. else if (op2 > (ulong)long.MaxValue)
  303. {
  304. // op1 from long.MinValue to (long)ulong.MinValue - 1L
  305. // op2 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  306. return (ulong)op1 + op2;
  307. }
  308. else
  309. {
  310. // op1 from long.MinValue to (long)ulong.MinValue - 1L
  311. // op2 from ulong.MinValue to (ulong)long.MaxValue
  312. long Add = op1 + (long)op2;
  313. if (Add < (long)ulong.MinValue)
  314. {
  315. SetFpsrQCFlag(State);
  316. return ulong.MinValue;
  317. }
  318. else
  319. {
  320. return (ulong)Add;
  321. }
  322. }
  323. }
  324. private static void SetFpsrQCFlag(AThreadState State)
  325. {
  326. const int QCFlagBit = 27;
  327. State.Fpsr |= 1 << QCFlagBit;
  328. }
  329. #endregion
  330. #region "Count"
  331. public static ulong CountLeadingSigns(ulong Value, int Size) // Size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.).
  332. {
  333. Value ^= Value >> 1;
  334. int HighBit = Size - 2;
  335. for (int Bit = HighBit; Bit >= 0; Bit--)
  336. {
  337. if (((Value >> Bit) & 0b1) != 0)
  338. {
  339. return (ulong)(HighBit - Bit);
  340. }
  341. }
  342. return (ulong)(Size - 1);
  343. }
  344. private static readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
  345. public static ulong CountLeadingZeros(ulong Value, int Size) // Size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.).
  346. {
  347. if (Value == 0ul)
  348. {
  349. return (ulong)Size;
  350. }
  351. int NibbleIdx = Size;
  352. int PreCount, Count = 0;
  353. do
  354. {
  355. NibbleIdx -= 4;
  356. PreCount = ClzNibbleTbl[(Value >> NibbleIdx) & 0b1111];
  357. Count += PreCount;
  358. }
  359. while (PreCount == 4);
  360. return (ulong)Count;
  361. }
  362. public static ulong CountSetBits8(ulong Value) // "Size" is 8 (SIMD&FP Inst.).
  363. {
  364. if (Value == 0xfful)
  365. {
  366. return 8ul;
  367. }
  368. Value = ((Value >> 1) & 0x55ul) + (Value & 0x55ul);
  369. Value = ((Value >> 2) & 0x33ul) + (Value & 0x33ul);
  370. return (Value >> 4) + (Value & 0x0ful);
  371. }
  372. #endregion
  373. #region "Crc32"
  374. private const uint Crc32RevPoly = 0xedb88320;
  375. private const uint Crc32cRevPoly = 0x82f63b78;
  376. public static uint Crc32b(uint Crc, byte Val) => Crc32 (Crc, Crc32RevPoly, Val);
  377. public static uint Crc32h(uint Crc, ushort Val) => Crc32h(Crc, Crc32RevPoly, Val);
  378. public static uint Crc32w(uint Crc, uint Val) => Crc32w(Crc, Crc32RevPoly, Val);
  379. public static uint Crc32x(uint Crc, ulong Val) => Crc32x(Crc, Crc32RevPoly, Val);
  380. public static uint Crc32cb(uint Crc, byte Val) => Crc32 (Crc, Crc32cRevPoly, Val);
  381. public static uint Crc32ch(uint Crc, ushort Val) => Crc32h(Crc, Crc32cRevPoly, Val);
  382. public static uint Crc32cw(uint Crc, uint Val) => Crc32w(Crc, Crc32cRevPoly, Val);
  383. public static uint Crc32cx(uint Crc, ulong Val) => Crc32x(Crc, Crc32cRevPoly, Val);
  384. private static uint Crc32h(uint Crc, uint Poly, ushort Val)
  385. {
  386. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  387. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  388. return Crc;
  389. }
  390. private static uint Crc32w(uint Crc, uint Poly, uint Val)
  391. {
  392. Crc = Crc32(Crc, Poly, (byte)(Val >> 0 ));
  393. Crc = Crc32(Crc, Poly, (byte)(Val >> 8 ));
  394. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  395. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  396. return Crc;
  397. }
  398. private static uint Crc32x(uint Crc, uint Poly, ulong Val)
  399. {
  400. Crc = Crc32(Crc, Poly, (byte)(Val >> 0 ));
  401. Crc = Crc32(Crc, Poly, (byte)(Val >> 8 ));
  402. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  403. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  404. Crc = Crc32(Crc, Poly, (byte)(Val >> 32));
  405. Crc = Crc32(Crc, Poly, (byte)(Val >> 40));
  406. Crc = Crc32(Crc, Poly, (byte)(Val >> 48));
  407. Crc = Crc32(Crc, Poly, (byte)(Val >> 56));
  408. return Crc;
  409. }
  410. private static uint Crc32(uint Crc, uint Poly, byte Val)
  411. {
  412. Crc ^= Val;
  413. for (int Bit = 7; Bit >= 0; Bit--)
  414. {
  415. uint Mask = (uint)(-(int)(Crc & 1));
  416. Crc = (Crc >> 1) ^ (Poly & Mask);
  417. }
  418. return Crc;
  419. }
  420. #endregion
  421. #region "Aes"
  422. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  423. public static Vector128<float> Decrypt(Vector128<float> value, Vector128<float> roundKey)
  424. {
  425. if (!Sse.IsSupported)
  426. {
  427. throw new PlatformNotSupportedException();
  428. }
  429. return ACryptoHelper.AESInvSubBytes(ACryptoHelper.AESInvShiftRows(Sse.Xor(value, roundKey)));
  430. }
  431. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  432. public static Vector128<float> Encrypt(Vector128<float> value, Vector128<float> roundKey)
  433. {
  434. if (!Sse.IsSupported)
  435. {
  436. throw new PlatformNotSupportedException();
  437. }
  438. return ACryptoHelper.AESSubBytes(ACryptoHelper.AESShiftRows(Sse.Xor(value, roundKey)));
  439. }
  440. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  441. public static Vector128<float> InverseMixColumns(Vector128<float> value)
  442. {
  443. return ACryptoHelper.AESInvMixColumns(value);
  444. }
  445. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  446. public static Vector128<float> MixColumns(Vector128<float> value)
  447. {
  448. return ACryptoHelper.AESMixColumns(value);
  449. }
  450. #endregion
  451. #region "Sha256"
  452. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  453. public static Vector128<float> HashLower(Vector128<float> hash_abcd, Vector128<float> hash_efgh, Vector128<float> wk)
  454. {
  455. return SHA256hash(hash_abcd, hash_efgh, wk, true);
  456. }
  457. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  458. public static Vector128<float> HashUpper(Vector128<float> hash_efgh, Vector128<float> hash_abcd, Vector128<float> wk)
  459. {
  460. return SHA256hash(hash_abcd, hash_efgh, wk, false);
  461. }
  462. public static Vector128<float> SchedulePart1(Vector128<float> w0_3, Vector128<float> w4_7)
  463. {
  464. Vector128<float> result = new Vector128<float>();
  465. for (int e = 0; e <= 3; e++)
  466. {
  467. uint elt = (uint)VectorExtractIntZx(e <= 2 ? w0_3 : w4_7, (byte)(e <= 2 ? e + 1 : 0), 2);
  468. elt = elt.Ror(7) ^ elt.Ror(18) ^ elt.Lsr(3);
  469. elt += (uint)VectorExtractIntZx(w0_3, (byte)e, 2);
  470. result = VectorInsertInt((ulong)elt, result, (byte)e, 2);
  471. }
  472. return result;
  473. }
  474. public static Vector128<float> SchedulePart2(Vector128<float> w0_3, Vector128<float> w8_11, Vector128<float> w12_15)
  475. {
  476. Vector128<float> result = new Vector128<float>();
  477. ulong T1 = VectorExtractIntZx(w12_15, (byte)1, 3);
  478. for (int e = 0; e <= 1; e++)
  479. {
  480. uint elt = T1.ULongPart(e);
  481. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  482. elt += (uint)VectorExtractIntZx(w0_3, (byte)e, 2);
  483. elt += (uint)VectorExtractIntZx(w8_11, (byte)(e + 1), 2);
  484. result = VectorInsertInt((ulong)elt, result, (byte)e, 2);
  485. }
  486. T1 = VectorExtractIntZx(result, (byte)0, 3);
  487. for (int e = 2; e <= 3; e++)
  488. {
  489. uint elt = T1.ULongPart(e - 2);
  490. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  491. elt += (uint)VectorExtractIntZx(w0_3, (byte)e, 2);
  492. elt += (uint)VectorExtractIntZx(e == 2 ? w8_11 : w12_15, (byte)(e == 2 ? 3 : 0), 2);
  493. result = VectorInsertInt((ulong)elt, result, (byte)e, 2);
  494. }
  495. return result;
  496. }
  497. private static Vector128<float> SHA256hash(Vector128<float> X, Vector128<float> Y, Vector128<float> W, bool part1)
  498. {
  499. for (int e = 0; e <= 3; e++)
  500. {
  501. uint chs = SHAchoose((uint)VectorExtractIntZx(Y, (byte)0, 2),
  502. (uint)VectorExtractIntZx(Y, (byte)1, 2),
  503. (uint)VectorExtractIntZx(Y, (byte)2, 2));
  504. uint maj = SHAmajority((uint)VectorExtractIntZx(X, (byte)0, 2),
  505. (uint)VectorExtractIntZx(X, (byte)1, 2),
  506. (uint)VectorExtractIntZx(X, (byte)2, 2));
  507. uint t1 = (uint)VectorExtractIntZx(Y, (byte)3, 2);
  508. t1 += SHAhashSIGMA1((uint)VectorExtractIntZx(Y, (byte)0, 2)) + chs;
  509. t1 += (uint)VectorExtractIntZx(W, (byte)e, 2);
  510. uint t2 = t1 + (uint)VectorExtractIntZx(X, (byte)3, 2);
  511. X = VectorInsertInt((ulong)t2, X, (byte)3, 2);
  512. t2 = t1 + SHAhashSIGMA0((uint)VectorExtractIntZx(X, (byte)0, 2)) + maj;
  513. Y = VectorInsertInt((ulong)t2, Y, (byte)3, 2);
  514. Rol32_256(ref Y, ref X);
  515. }
  516. return part1 ? X : Y;
  517. }
  518. private static void Rol32_256(ref Vector128<float> Y, ref Vector128<float> X)
  519. {
  520. if (!Sse2.IsSupported)
  521. {
  522. throw new PlatformNotSupportedException();
  523. }
  524. uint yE3 = (uint)VectorExtractIntZx(Y, (byte)3, 2);
  525. uint xE3 = (uint)VectorExtractIntZx(X, (byte)3, 2);
  526. Y = Sse.StaticCast<uint, float>(Sse2.ShiftLeftLogical128BitLane(Sse.StaticCast<float, uint>(Y), (byte)4));
  527. X = Sse.StaticCast<uint, float>(Sse2.ShiftLeftLogical128BitLane(Sse.StaticCast<float, uint>(X), (byte)4));
  528. Y = VectorInsertInt((ulong)xE3, Y, (byte)0, 2);
  529. X = VectorInsertInt((ulong)yE3, X, (byte)0, 2);
  530. }
  531. private static uint SHAhashSIGMA0(uint x)
  532. {
  533. return x.Ror(2) ^ x.Ror(13) ^ x.Ror(22);
  534. }
  535. private static uint SHAhashSIGMA1(uint x)
  536. {
  537. return x.Ror(6) ^ x.Ror(11) ^ x.Ror(25);
  538. }
  539. private static uint SHAmajority(uint x, uint y, uint z)
  540. {
  541. return (x & y) | ((x | y) & z);
  542. }
  543. private static uint SHAchoose(uint x, uint y, uint z)
  544. {
  545. return ((y ^ z) & x) ^ z;
  546. }
  547. private static uint Ror(this uint value, int count)
  548. {
  549. return (value >> count) | (value << (32 - count));
  550. }
  551. private static uint Lsr(this uint value, int count)
  552. {
  553. return value >> count;
  554. }
  555. private static uint ULongPart(this ulong value, int part)
  556. {
  557. return part == 0
  558. ? (uint)(value & 0xFFFFFFFFUL)
  559. : (uint)(value >> 32);
  560. }
  561. #endregion
  562. #region "Reverse"
  563. public static uint ReverseBits8(uint Value)
  564. {
  565. Value = ((Value & 0xaa) >> 1) | ((Value & 0x55) << 1);
  566. Value = ((Value & 0xcc) >> 2) | ((Value & 0x33) << 2);
  567. return (Value >> 4) | ((Value & 0x0f) << 4);
  568. }
  569. public static uint ReverseBits32(uint Value)
  570. {
  571. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  572. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  573. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  574. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  575. return (Value >> 16) | (Value << 16);
  576. }
  577. public static ulong ReverseBits64(ulong Value)
  578. {
  579. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((Value & 0x5555555555555555) << 1 );
  580. Value = ((Value & 0xcccccccccccccccc) >> 2 ) | ((Value & 0x3333333333333333) << 2 );
  581. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4 );
  582. Value = ((Value & 0xff00ff00ff00ff00) >> 8 ) | ((Value & 0x00ff00ff00ff00ff) << 8 );
  583. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  584. return (Value >> 32) | (Value << 32);
  585. }
  586. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  587. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  588. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  589. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  590. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  591. private enum RevSize
  592. {
  593. Rev16,
  594. Rev32,
  595. Rev64
  596. }
  597. private static ulong ReverseBytes(ulong Value, RevSize Size)
  598. {
  599. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  600. if (Size == RevSize.Rev16)
  601. {
  602. return Value;
  603. }
  604. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  605. if (Size == RevSize.Rev32)
  606. {
  607. return Value;
  608. }
  609. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  610. if (Size == RevSize.Rev64)
  611. {
  612. return Value;
  613. }
  614. throw new ArgumentException(nameof(Size));
  615. }
  616. #endregion
  617. #region "MultiplyHigh"
  618. public static long SMulHi128(long LHS, long RHS)
  619. {
  620. long Result = (long)UMulHi128((ulong)LHS, (ulong)RHS);
  621. if (LHS < 0) Result -= RHS;
  622. if (RHS < 0) Result -= LHS;
  623. return Result;
  624. }
  625. public static ulong UMulHi128(ulong LHS, ulong RHS)
  626. {
  627. //long multiplication
  628. //multiply 32 bits at a time in 64 bit, the result is what's carried over 64 bits.
  629. ulong LHigh = LHS >> 32;
  630. ulong LLow = LHS & 0xFFFFFFFF;
  631. ulong RHigh = RHS >> 32;
  632. ulong RLow = RHS & 0xFFFFFFFF;
  633. ulong Z2 = LLow * RLow;
  634. ulong T = LHigh * RLow + (Z2 >> 32);
  635. ulong Z1 = T & 0xFFFFFFFF;
  636. ulong Z0 = T >> 32;
  637. Z1 += LLow * RHigh;
  638. return LHigh * RHigh + Z0 + (Z1 >> 32);
  639. }
  640. #endregion
  641. }
  642. }