SoftFallback.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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.Instructions
  8. {
  9. using static VectorHelper;
  10. static class SoftFallback
  11. {
  12. public static void EmitCall(ILEmitterCtx context, string mthdName)
  13. {
  14. context.EmitCall(typeof(SoftFallback), 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, CpuThreadState 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. state.SetFpsrFlag(Fpsr.Qc);
  107. return tMaxValue;
  108. }
  109. else if (op < tMinValue)
  110. {
  111. state.SetFpsrFlag(Fpsr.Qc);
  112. return tMinValue;
  113. }
  114. else
  115. {
  116. return op;
  117. }
  118. }
  119. public static ulong SignedSrcUnsignedDstSatQ(long op, int size, CpuThreadState state)
  120. {
  121. int eSize = 8 << size;
  122. ulong tMaxValue = (1UL << eSize) - 1UL;
  123. ulong tMinValue = 0UL;
  124. if (op > (long)tMaxValue)
  125. {
  126. state.SetFpsrFlag(Fpsr.Qc);
  127. return tMaxValue;
  128. }
  129. else if (op < (long)tMinValue)
  130. {
  131. state.SetFpsrFlag(Fpsr.Qc);
  132. return tMinValue;
  133. }
  134. else
  135. {
  136. return (ulong)op;
  137. }
  138. }
  139. public static long UnsignedSrcSignedDstSatQ(ulong op, int size, CpuThreadState state)
  140. {
  141. int eSize = 8 << size;
  142. long tMaxValue = (1L << (eSize - 1)) - 1L;
  143. if (op > (ulong)tMaxValue)
  144. {
  145. state.SetFpsrFlag(Fpsr.Qc);
  146. return tMaxValue;
  147. }
  148. else
  149. {
  150. return (long)op;
  151. }
  152. }
  153. public static ulong UnsignedSrcUnsignedDstSatQ(ulong op, int size, CpuThreadState state)
  154. {
  155. int eSize = 8 << size;
  156. ulong tMaxValue = (1UL << eSize) - 1UL;
  157. if (op > tMaxValue)
  158. {
  159. state.SetFpsrFlag(Fpsr.Qc);
  160. return tMaxValue;
  161. }
  162. else
  163. {
  164. return op;
  165. }
  166. }
  167. public static long UnarySignedSatQAbsOrNeg(long op, CpuThreadState state)
  168. {
  169. if (op == long.MinValue)
  170. {
  171. state.SetFpsrFlag(Fpsr.Qc);
  172. return long.MaxValue;
  173. }
  174. else
  175. {
  176. return op;
  177. }
  178. }
  179. public static long BinarySignedSatQAdd(long op1, long op2, CpuThreadState state)
  180. {
  181. long add = op1 + op2;
  182. if ((~(op1 ^ op2) & (op1 ^ add)) < 0L)
  183. {
  184. state.SetFpsrFlag(Fpsr.Qc);
  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, CpuThreadState state)
  200. {
  201. ulong add = op1 + op2;
  202. if ((add < op1) && (add < op2))
  203. {
  204. state.SetFpsrFlag(Fpsr.Qc);
  205. return ulong.MaxValue;
  206. }
  207. else
  208. {
  209. return add;
  210. }
  211. }
  212. public static long BinarySignedSatQSub(long op1, long op2, CpuThreadState state)
  213. {
  214. long sub = op1 - op2;
  215. if (((op1 ^ op2) & (op1 ^ sub)) < 0L)
  216. {
  217. state.SetFpsrFlag(Fpsr.Qc);
  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, CpuThreadState state)
  233. {
  234. ulong sub = op1 - op2;
  235. if (op1 < op2)
  236. {
  237. state.SetFpsrFlag(Fpsr.Qc);
  238. return ulong.MinValue;
  239. }
  240. else
  241. {
  242. return sub;
  243. }
  244. }
  245. public static long BinarySignedSatQAcc(ulong op1, long op2, CpuThreadState 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. state.SetFpsrFlag(Fpsr.Qc);
  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. state.SetFpsrFlag(Fpsr.Qc);
  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. state.SetFpsrFlag(Fpsr.Qc);
  277. return long.MaxValue;
  278. }
  279. else
  280. {
  281. return (long)add;
  282. }
  283. }
  284. }
  285. public static ulong BinaryUnsignedSatQAcc(long op1, ulong op2, CpuThreadState 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. state.SetFpsrFlag(Fpsr.Qc);
  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. state.SetFpsrFlag(Fpsr.Qc);
  316. return ulong.MinValue;
  317. }
  318. else
  319. {
  320. return (ulong)add;
  321. }
  322. }
  323. }
  324. #endregion
  325. #region "Count"
  326. public static ulong CountLeadingSigns(ulong value, int size) // Size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.).
  327. {
  328. value ^= value >> 1;
  329. int highBit = size - 2;
  330. for (int bit = highBit; bit >= 0; bit--)
  331. {
  332. if (((value >> bit) & 0b1) != 0)
  333. {
  334. return (ulong)(highBit - bit);
  335. }
  336. }
  337. return (ulong)(size - 1);
  338. }
  339. private static readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
  340. public static ulong CountLeadingZeros(ulong value, int size) // Size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.).
  341. {
  342. if (value == 0ul)
  343. {
  344. return (ulong)size;
  345. }
  346. int nibbleIdx = size;
  347. int preCount, count = 0;
  348. do
  349. {
  350. nibbleIdx -= 4;
  351. preCount = ClzNibbleTbl[(value >> nibbleIdx) & 0b1111];
  352. count += preCount;
  353. }
  354. while (preCount == 4);
  355. return (ulong)count;
  356. }
  357. public static ulong CountSetBits8(ulong value) // "Size" is 8 (SIMD&FP Inst.).
  358. {
  359. if (value == 0xfful)
  360. {
  361. return 8ul;
  362. }
  363. value = ((value >> 1) & 0x55ul) + (value & 0x55ul);
  364. value = ((value >> 2) & 0x33ul) + (value & 0x33ul);
  365. return (value >> 4) + (value & 0x0ful);
  366. }
  367. #endregion
  368. #region "Crc32"
  369. private const uint Crc32RevPoly = 0xedb88320;
  370. private const uint Crc32CRevPoly = 0x82f63b78;
  371. public static uint Crc32B(uint crc, byte val) => Crc32 (crc, Crc32RevPoly, val);
  372. public static uint Crc32H(uint crc, ushort val) => Crc32H(crc, Crc32RevPoly, val);
  373. public static uint Crc32W(uint crc, uint val) => Crc32W(crc, Crc32RevPoly, val);
  374. public static uint Crc32X(uint crc, ulong val) => Crc32X(crc, Crc32RevPoly, val);
  375. public static uint Crc32Cb(uint crc, byte val) => Crc32 (crc, Crc32CRevPoly, val);
  376. public static uint Crc32Ch(uint crc, ushort val) => Crc32H(crc, Crc32CRevPoly, val);
  377. public static uint Crc32Cw(uint crc, uint val) => Crc32W(crc, Crc32CRevPoly, val);
  378. public static uint Crc32Cx(uint crc, ulong val) => Crc32X(crc, Crc32CRevPoly, val);
  379. private static uint Crc32H(uint crc, uint poly, ushort val)
  380. {
  381. crc = Crc32(crc, poly, (byte)(val >> 0));
  382. crc = Crc32(crc, poly, (byte)(val >> 8));
  383. return crc;
  384. }
  385. private static uint Crc32W(uint crc, uint poly, uint val)
  386. {
  387. crc = Crc32(crc, poly, (byte)(val >> 0 ));
  388. crc = Crc32(crc, poly, (byte)(val >> 8 ));
  389. crc = Crc32(crc, poly, (byte)(val >> 16));
  390. crc = Crc32(crc, poly, (byte)(val >> 24));
  391. return crc;
  392. }
  393. private static uint Crc32X(uint crc, uint poly, ulong val)
  394. {
  395. crc = Crc32(crc, poly, (byte)(val >> 0 ));
  396. crc = Crc32(crc, poly, (byte)(val >> 8 ));
  397. crc = Crc32(crc, poly, (byte)(val >> 16));
  398. crc = Crc32(crc, poly, (byte)(val >> 24));
  399. crc = Crc32(crc, poly, (byte)(val >> 32));
  400. crc = Crc32(crc, poly, (byte)(val >> 40));
  401. crc = Crc32(crc, poly, (byte)(val >> 48));
  402. crc = Crc32(crc, poly, (byte)(val >> 56));
  403. return crc;
  404. }
  405. private static uint Crc32(uint crc, uint poly, byte val)
  406. {
  407. crc ^= val;
  408. for (int bit = 7; bit >= 0; bit--)
  409. {
  410. uint mask = (uint)(-(int)(crc & 1));
  411. crc = (crc >> 1) ^ (poly & mask);
  412. }
  413. return crc;
  414. }
  415. #endregion
  416. #region "Aes"
  417. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  418. public static Vector128<float> Decrypt(Vector128<float> value, Vector128<float> roundKey)
  419. {
  420. if (!Sse.IsSupported)
  421. {
  422. throw new PlatformNotSupportedException();
  423. }
  424. return CryptoHelper.AesInvSubBytes(CryptoHelper.AesInvShiftRows(Sse.Xor(value, roundKey)));
  425. }
  426. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  427. public static Vector128<float> Encrypt(Vector128<float> value, Vector128<float> roundKey)
  428. {
  429. if (!Sse.IsSupported)
  430. {
  431. throw new PlatformNotSupportedException();
  432. }
  433. return CryptoHelper.AesSubBytes(CryptoHelper.AesShiftRows(Sse.Xor(value, roundKey)));
  434. }
  435. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  436. public static Vector128<float> InverseMixColumns(Vector128<float> value)
  437. {
  438. return CryptoHelper.AesInvMixColumns(value);
  439. }
  440. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  441. public static Vector128<float> MixColumns(Vector128<float> value)
  442. {
  443. return CryptoHelper.AesMixColumns(value);
  444. }
  445. #endregion
  446. #region "Sha1"
  447. public static Vector128<float> HashChoose(Vector128<float> hashAbcd, uint hashE, Vector128<float> wk)
  448. {
  449. for (int e = 0; e <= 3; e++)
  450. {
  451. uint t = ShaChoose((uint)VectorExtractIntZx(hashAbcd, (byte)1, 2),
  452. (uint)VectorExtractIntZx(hashAbcd, (byte)2, 2),
  453. (uint)VectorExtractIntZx(hashAbcd, (byte)3, 2));
  454. hashE += Rol((uint)VectorExtractIntZx(hashAbcd, (byte)0, 2), 5) + t;
  455. hashE += (uint)VectorExtractIntZx(wk, (byte)e, 2);
  456. t = Rol((uint)VectorExtractIntZx(hashAbcd, (byte)1, 2), 30);
  457. hashAbcd = VectorInsertInt((ulong)t, hashAbcd, (byte)1, 2);
  458. Rol32_160(ref hashE, ref hashAbcd);
  459. }
  460. return hashAbcd;
  461. }
  462. public static uint FixedRotate(uint hashE)
  463. {
  464. return hashE.Rol(30);
  465. }
  466. public static Vector128<float> HashMajority(Vector128<float> hashAbcd, uint hashE, Vector128<float> wk)
  467. {
  468. for (int e = 0; e <= 3; e++)
  469. {
  470. uint t = ShaMajority((uint)VectorExtractIntZx(hashAbcd, (byte)1, 2),
  471. (uint)VectorExtractIntZx(hashAbcd, (byte)2, 2),
  472. (uint)VectorExtractIntZx(hashAbcd, (byte)3, 2));
  473. hashE += Rol((uint)VectorExtractIntZx(hashAbcd, (byte)0, 2), 5) + t;
  474. hashE += (uint)VectorExtractIntZx(wk, (byte)e, 2);
  475. t = Rol((uint)VectorExtractIntZx(hashAbcd, (byte)1, 2), 30);
  476. hashAbcd = VectorInsertInt((ulong)t, hashAbcd, (byte)1, 2);
  477. Rol32_160(ref hashE, ref hashAbcd);
  478. }
  479. return hashAbcd;
  480. }
  481. public static Vector128<float> HashParity(Vector128<float> hashAbcd, uint hashE, Vector128<float> wk)
  482. {
  483. for (int e = 0; e <= 3; e++)
  484. {
  485. uint t = ShaParity((uint)VectorExtractIntZx(hashAbcd, (byte)1, 2),
  486. (uint)VectorExtractIntZx(hashAbcd, (byte)2, 2),
  487. (uint)VectorExtractIntZx(hashAbcd, (byte)3, 2));
  488. hashE += Rol((uint)VectorExtractIntZx(hashAbcd, (byte)0, 2), 5) + t;
  489. hashE += (uint)VectorExtractIntZx(wk, (byte)e, 2);
  490. t = Rol((uint)VectorExtractIntZx(hashAbcd, (byte)1, 2), 30);
  491. hashAbcd = VectorInsertInt((ulong)t, hashAbcd, (byte)1, 2);
  492. Rol32_160(ref hashE, ref hashAbcd);
  493. }
  494. return hashAbcd;
  495. }
  496. public static Vector128<float> Sha1SchedulePart1(Vector128<float> w03, Vector128<float> w47, Vector128<float> w811)
  497. {
  498. if (!Sse.IsSupported)
  499. {
  500. throw new PlatformNotSupportedException();
  501. }
  502. Vector128<float> result = new Vector128<float>();
  503. ulong t2 = VectorExtractIntZx(w47, (byte)0, 3);
  504. ulong t1 = VectorExtractIntZx(w03, (byte)1, 3);
  505. result = VectorInsertInt((ulong)t1, result, (byte)0, 3);
  506. result = VectorInsertInt((ulong)t2, result, (byte)1, 3);
  507. return Sse.Xor(result, Sse.Xor(w03, w811));
  508. }
  509. public static Vector128<float> Sha1SchedulePart2(Vector128<float> tw03, Vector128<float> w1215)
  510. {
  511. if (!Sse2.IsSupported)
  512. {
  513. throw new PlatformNotSupportedException();
  514. }
  515. Vector128<float> result = new Vector128<float>();
  516. Vector128<float> t = Sse.Xor(tw03, Sse.StaticCast<uint, float>(
  517. Sse2.ShiftRightLogical128BitLane(Sse.StaticCast<float, uint>(w1215), (byte)4)));
  518. uint tE0 = (uint)VectorExtractIntZx(t, (byte)0, 2);
  519. uint tE1 = (uint)VectorExtractIntZx(t, (byte)1, 2);
  520. uint tE2 = (uint)VectorExtractIntZx(t, (byte)2, 2);
  521. uint tE3 = (uint)VectorExtractIntZx(t, (byte)3, 2);
  522. result = VectorInsertInt((ulong)tE0.Rol(1), result, (byte)0, 2);
  523. result = VectorInsertInt((ulong)tE1.Rol(1), result, (byte)1, 2);
  524. result = VectorInsertInt((ulong)tE2.Rol(1), result, (byte)2, 2);
  525. return VectorInsertInt((ulong)(tE3.Rol(1) ^ tE0.Rol(2)), result, (byte)3, 2);
  526. }
  527. private static void Rol32_160(ref uint y, ref Vector128<float> x)
  528. {
  529. if (!Sse2.IsSupported)
  530. {
  531. throw new PlatformNotSupportedException();
  532. }
  533. uint xE3 = (uint)VectorExtractIntZx(x, (byte)3, 2);
  534. x = Sse.StaticCast<uint, float>(Sse2.ShiftLeftLogical128BitLane(Sse.StaticCast<float, uint>(x), (byte)4));
  535. x = VectorInsertInt((ulong)y, x, (byte)0, 2);
  536. y = xE3;
  537. }
  538. private static uint ShaChoose(uint x, uint y, uint z)
  539. {
  540. return ((y ^ z) & x) ^ z;
  541. }
  542. private static uint ShaMajority(uint x, uint y, uint z)
  543. {
  544. return (x & y) | ((x | y) & z);
  545. }
  546. private static uint ShaParity(uint x, uint y, uint z)
  547. {
  548. return x ^ y ^ z;
  549. }
  550. private static uint Rol(this uint value, int count)
  551. {
  552. return (value << count) | (value >> (32 - count));
  553. }
  554. #endregion
  555. #region "Sha256"
  556. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  557. public static Vector128<float> HashLower(Vector128<float> hashAbcd, Vector128<float> hashEfgh, Vector128<float> wk)
  558. {
  559. return Sha256Hash(hashAbcd, hashEfgh, wk, true);
  560. }
  561. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  562. public static Vector128<float> HashUpper(Vector128<float> hashEfgh, Vector128<float> hashAbcd, Vector128<float> wk)
  563. {
  564. return Sha256Hash(hashAbcd, hashEfgh, wk, false);
  565. }
  566. public static Vector128<float> Sha256SchedulePart1(Vector128<float> w03, Vector128<float> w47)
  567. {
  568. Vector128<float> result = new Vector128<float>();
  569. for (int e = 0; e <= 3; e++)
  570. {
  571. uint elt = (uint)VectorExtractIntZx(e <= 2 ? w03 : w47, (byte)(e <= 2 ? e + 1 : 0), 2);
  572. elt = elt.Ror(7) ^ elt.Ror(18) ^ elt.Lsr(3);
  573. elt += (uint)VectorExtractIntZx(w03, (byte)e, 2);
  574. result = VectorInsertInt((ulong)elt, result, (byte)e, 2);
  575. }
  576. return result;
  577. }
  578. public static Vector128<float> Sha256SchedulePart2(Vector128<float> w03, Vector128<float> w811, Vector128<float> w1215)
  579. {
  580. Vector128<float> result = new Vector128<float>();
  581. ulong t1 = VectorExtractIntZx(w1215, (byte)1, 3);
  582. for (int e = 0; e <= 1; e++)
  583. {
  584. uint elt = t1.ULongPart(e);
  585. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  586. elt += (uint)VectorExtractIntZx(w03, (byte)e, 2);
  587. elt += (uint)VectorExtractIntZx(w811, (byte)(e + 1), 2);
  588. result = VectorInsertInt((ulong)elt, result, (byte)e, 2);
  589. }
  590. t1 = VectorExtractIntZx(result, (byte)0, 3);
  591. for (int e = 2; e <= 3; e++)
  592. {
  593. uint elt = t1.ULongPart(e - 2);
  594. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  595. elt += (uint)VectorExtractIntZx(w03, (byte)e, 2);
  596. elt += (uint)VectorExtractIntZx(e == 2 ? w811 : w1215, (byte)(e == 2 ? 3 : 0), 2);
  597. result = VectorInsertInt((ulong)elt, result, (byte)e, 2);
  598. }
  599. return result;
  600. }
  601. private static Vector128<float> Sha256Hash(Vector128<float> x, Vector128<float> y, Vector128<float> w, bool part1)
  602. {
  603. for (int e = 0; e <= 3; e++)
  604. {
  605. uint chs = ShaChoose((uint)VectorExtractIntZx(y, (byte)0, 2),
  606. (uint)VectorExtractIntZx(y, (byte)1, 2),
  607. (uint)VectorExtractIntZx(y, (byte)2, 2));
  608. uint maj = ShaMajority((uint)VectorExtractIntZx(x, (byte)0, 2),
  609. (uint)VectorExtractIntZx(x, (byte)1, 2),
  610. (uint)VectorExtractIntZx(x, (byte)2, 2));
  611. uint t1 = (uint)VectorExtractIntZx(y, (byte)3, 2);
  612. t1 += ShaHashSigma1((uint)VectorExtractIntZx(y, (byte)0, 2)) + chs;
  613. t1 += (uint)VectorExtractIntZx(w, (byte)e, 2);
  614. uint t2 = t1 + (uint)VectorExtractIntZx(x, (byte)3, 2);
  615. x = VectorInsertInt((ulong)t2, x, (byte)3, 2);
  616. t2 = t1 + ShaHashSigma0((uint)VectorExtractIntZx(x, (byte)0, 2)) + maj;
  617. y = VectorInsertInt((ulong)t2, y, (byte)3, 2);
  618. Rol32_256(ref y, ref x);
  619. }
  620. return part1 ? x : y;
  621. }
  622. private static void Rol32_256(ref Vector128<float> y, ref Vector128<float> x)
  623. {
  624. if (!Sse2.IsSupported)
  625. {
  626. throw new PlatformNotSupportedException();
  627. }
  628. uint yE3 = (uint)VectorExtractIntZx(y, (byte)3, 2);
  629. uint xE3 = (uint)VectorExtractIntZx(x, (byte)3, 2);
  630. y = Sse.StaticCast<uint, float>(Sse2.ShiftLeftLogical128BitLane(Sse.StaticCast<float, uint>(y), (byte)4));
  631. x = Sse.StaticCast<uint, float>(Sse2.ShiftLeftLogical128BitLane(Sse.StaticCast<float, uint>(x), (byte)4));
  632. y = VectorInsertInt((ulong)xE3, y, (byte)0, 2);
  633. x = VectorInsertInt((ulong)yE3, x, (byte)0, 2);
  634. }
  635. private static uint ShaHashSigma0(uint x)
  636. {
  637. return x.Ror(2) ^ x.Ror(13) ^ x.Ror(22);
  638. }
  639. private static uint ShaHashSigma1(uint x)
  640. {
  641. return x.Ror(6) ^ x.Ror(11) ^ x.Ror(25);
  642. }
  643. private static uint Ror(this uint value, int count)
  644. {
  645. return (value >> count) | (value << (32 - count));
  646. }
  647. private static uint Lsr(this uint value, int count)
  648. {
  649. return value >> count;
  650. }
  651. private static uint ULongPart(this ulong value, int part)
  652. {
  653. return part == 0
  654. ? (uint)(value & 0xFFFFFFFFUL)
  655. : (uint)(value >> 32);
  656. }
  657. #endregion
  658. #region "Reverse"
  659. public static uint ReverseBits8(uint value)
  660. {
  661. value = ((value & 0xaa) >> 1) | ((value & 0x55) << 1);
  662. value = ((value & 0xcc) >> 2) | ((value & 0x33) << 2);
  663. return (value >> 4) | ((value & 0x0f) << 4);
  664. }
  665. public static uint ReverseBits32(uint value)
  666. {
  667. value = ((value & 0xaaaaaaaa) >> 1) | ((value & 0x55555555) << 1);
  668. value = ((value & 0xcccccccc) >> 2) | ((value & 0x33333333) << 2);
  669. value = ((value & 0xf0f0f0f0) >> 4) | ((value & 0x0f0f0f0f) << 4);
  670. value = ((value & 0xff00ff00) >> 8) | ((value & 0x00ff00ff) << 8);
  671. return (value >> 16) | (value << 16);
  672. }
  673. public static ulong ReverseBits64(ulong value)
  674. {
  675. value = ((value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((value & 0x5555555555555555) << 1 );
  676. value = ((value & 0xcccccccccccccccc) >> 2 ) | ((value & 0x3333333333333333) << 2 );
  677. value = ((value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((value & 0x0f0f0f0f0f0f0f0f) << 4 );
  678. value = ((value & 0xff00ff00ff00ff00) >> 8 ) | ((value & 0x00ff00ff00ff00ff) << 8 );
  679. value = ((value & 0xffff0000ffff0000) >> 16) | ((value & 0x0000ffff0000ffff) << 16);
  680. return (value >> 32) | (value << 32);
  681. }
  682. public static uint ReverseBytes16_32(uint value) => (uint)ReverseBytes16_64(value);
  683. public static uint ReverseBytes32_32(uint value) => (uint)ReverseBytes32_64(value);
  684. public static ulong ReverseBytes16_64(ulong value) => ReverseBytes(value, RevSize.Rev16);
  685. public static ulong ReverseBytes32_64(ulong value) => ReverseBytes(value, RevSize.Rev32);
  686. public static ulong ReverseBytes64(ulong value) => ReverseBytes(value, RevSize.Rev64);
  687. private enum RevSize
  688. {
  689. Rev16,
  690. Rev32,
  691. Rev64
  692. }
  693. private static ulong ReverseBytes(ulong value, RevSize size)
  694. {
  695. value = ((value & 0xff00ff00ff00ff00) >> 8) | ((value & 0x00ff00ff00ff00ff) << 8);
  696. if (size == RevSize.Rev16)
  697. {
  698. return value;
  699. }
  700. value = ((value & 0xffff0000ffff0000) >> 16) | ((value & 0x0000ffff0000ffff) << 16);
  701. if (size == RevSize.Rev32)
  702. {
  703. return value;
  704. }
  705. value = ((value & 0xffffffff00000000) >> 32) | ((value & 0x00000000ffffffff) << 32);
  706. if (size == RevSize.Rev64)
  707. {
  708. return value;
  709. }
  710. throw new ArgumentException(nameof(size));
  711. }
  712. #endregion
  713. #region "MultiplyHigh"
  714. public static long SMulHi128(long left, long right)
  715. {
  716. long result = (long)UMulHi128((ulong)left, (ulong)right);
  717. if (left < 0)
  718. {
  719. result -= right;
  720. }
  721. if (right < 0)
  722. {
  723. result -= left;
  724. }
  725. return result;
  726. }
  727. public static ulong UMulHi128(ulong left, ulong right)
  728. {
  729. ulong lHigh = left >> 32;
  730. ulong lLow = left & 0xFFFFFFFF;
  731. ulong rHigh = right >> 32;
  732. ulong rLow = right & 0xFFFFFFFF;
  733. ulong z2 = lLow * rLow;
  734. ulong t = lHigh * rLow + (z2 >> 32);
  735. ulong z1 = t & 0xFFFFFFFF;
  736. ulong z0 = t >> 32;
  737. z1 += lLow * rHigh;
  738. return lHigh * rHigh + z0 + (z1 >> 32);
  739. }
  740. #endregion
  741. }
  742. }