ASoftFallback.cs 21 KB

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