SoftFallback.cs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. using ARMeilleure.State;
  2. using System;
  3. namespace ARMeilleure.Instructions
  4. {
  5. static class SoftFallback
  6. {
  7. #region "ShlReg"
  8. public static long SignedShlReg(long value, long shift, bool round, int size)
  9. {
  10. int eSize = 8 << size;
  11. int shiftLsB = (sbyte)shift;
  12. if (shiftLsB < 0)
  13. {
  14. return SignedShrReg(value, -shiftLsB, round, eSize);
  15. }
  16. else if (shiftLsB > 0)
  17. {
  18. if (shiftLsB >= eSize)
  19. {
  20. return 0L;
  21. }
  22. return value << shiftLsB;
  23. }
  24. else /* if (shiftLsB == 0) */
  25. {
  26. return value;
  27. }
  28. }
  29. public static ulong UnsignedShlReg(ulong value, ulong shift, bool round, int size)
  30. {
  31. int eSize = 8 << size;
  32. int shiftLsB = (sbyte)shift;
  33. if (shiftLsB < 0)
  34. {
  35. return UnsignedShrReg(value, -shiftLsB, round, eSize);
  36. }
  37. else if (shiftLsB > 0)
  38. {
  39. if (shiftLsB >= eSize)
  40. {
  41. return 0UL;
  42. }
  43. return value << shiftLsB;
  44. }
  45. else /* if (shiftLsB == 0) */
  46. {
  47. return value;
  48. }
  49. }
  50. public static long SignedShlRegSatQ(long value, long shift, bool round, int size)
  51. {
  52. ExecutionContext context = NativeInterface.GetContext();
  53. int eSize = 8 << size;
  54. int shiftLsB = (sbyte)shift;
  55. if (shiftLsB < 0)
  56. {
  57. return SignedShrReg(value, -shiftLsB, round, eSize);
  58. }
  59. else if (shiftLsB > 0)
  60. {
  61. if (shiftLsB >= eSize)
  62. {
  63. return SignedSignSatQ(value, eSize, context);
  64. }
  65. if (eSize == 64)
  66. {
  67. long shl = value << shiftLsB;
  68. long shr = shl >> shiftLsB;
  69. if (shr != value)
  70. {
  71. return SignedSignSatQ(value, eSize, context);
  72. }
  73. else /* if (shr == value) */
  74. {
  75. return shl;
  76. }
  77. }
  78. else /* if (eSize != 64) */
  79. {
  80. return SignedSrcSignedDstSatQ(value << shiftLsB, size); // InstEmitSimdHelper.EmitSignedSrcSatQ(signedDst: true).
  81. }
  82. }
  83. else /* if (shiftLsB == 0) */
  84. {
  85. return value;
  86. }
  87. }
  88. public static ulong UnsignedShlRegSatQ(ulong value, ulong shift, bool round, int size)
  89. {
  90. ExecutionContext context = NativeInterface.GetContext();
  91. int eSize = 8 << size;
  92. int shiftLsB = (sbyte)shift;
  93. if (shiftLsB < 0)
  94. {
  95. return UnsignedShrReg(value, -shiftLsB, round, eSize);
  96. }
  97. else if (shiftLsB > 0)
  98. {
  99. if (shiftLsB >= eSize)
  100. {
  101. return UnsignedSignSatQ(value, eSize, context);
  102. }
  103. if (eSize == 64)
  104. {
  105. ulong shl = value << shiftLsB;
  106. ulong shr = shl >> shiftLsB;
  107. if (shr != value)
  108. {
  109. return UnsignedSignSatQ(value, eSize, context);
  110. }
  111. else /* if (shr == value) */
  112. {
  113. return shl;
  114. }
  115. }
  116. else /* if (eSize != 64) */
  117. {
  118. return UnsignedSrcUnsignedDstSatQ(value << shiftLsB, size); // InstEmitSimdHelper.EmitUnsignedSrcSatQ(signedDst: false).
  119. }
  120. }
  121. else /* if (shiftLsB == 0) */
  122. {
  123. return value;
  124. }
  125. }
  126. private static long SignedShrReg(long value, int shift, bool round, int eSize) // shift := [1, 128]; eSize := {8, 16, 32, 64}.
  127. {
  128. if (round)
  129. {
  130. if (shift >= eSize)
  131. {
  132. return 0L;
  133. }
  134. long roundConst = 1L << (shift - 1);
  135. long add = value + roundConst;
  136. if (eSize == 64)
  137. {
  138. if ((~value & (value ^ add)) < 0L)
  139. {
  140. return (long)((ulong)add >> shift);
  141. }
  142. else
  143. {
  144. return add >> shift;
  145. }
  146. }
  147. else /* if (eSize != 64) */
  148. {
  149. return add >> shift;
  150. }
  151. }
  152. else /* if (!round) */
  153. {
  154. if (shift >= eSize)
  155. {
  156. if (value < 0L)
  157. {
  158. return -1L;
  159. }
  160. else /* if (value >= 0L) */
  161. {
  162. return 0L;
  163. }
  164. }
  165. return value >> shift;
  166. }
  167. }
  168. private static ulong UnsignedShrReg(ulong value, int shift, bool round, int eSize) // shift := [1, 128]; eSize := {8, 16, 32, 64}.
  169. {
  170. if (round)
  171. {
  172. if (shift > 64)
  173. {
  174. return 0UL;
  175. }
  176. ulong roundConst = 1UL << (shift - 1);
  177. ulong add = value + roundConst;
  178. if (eSize == 64)
  179. {
  180. if ((add < value) && (add < roundConst))
  181. {
  182. if (shift == 64)
  183. {
  184. return 1UL;
  185. }
  186. return (add >> shift) | (0x8000000000000000UL >> (shift - 1));
  187. }
  188. else
  189. {
  190. if (shift == 64)
  191. {
  192. return 0UL;
  193. }
  194. return add >> shift;
  195. }
  196. }
  197. else /* if (eSize != 64) */
  198. {
  199. if (shift == 64)
  200. {
  201. return 0UL;
  202. }
  203. return add >> shift;
  204. }
  205. }
  206. else /* if (!round) */
  207. {
  208. if (shift >= eSize)
  209. {
  210. return 0UL;
  211. }
  212. return value >> shift;
  213. }
  214. }
  215. private static long SignedSignSatQ(long op, int eSize, ExecutionContext context) // eSize := {8, 16, 32, 64}.
  216. {
  217. long tMaxValue = (1L << (eSize - 1)) - 1L;
  218. long tMinValue = -(1L << (eSize - 1));
  219. if (op > 0L)
  220. {
  221. context.Fpsr |= FPSR.Qc;
  222. return tMaxValue;
  223. }
  224. else if (op < 0L)
  225. {
  226. context.Fpsr |= FPSR.Qc;
  227. return tMinValue;
  228. }
  229. else
  230. {
  231. return 0L;
  232. }
  233. }
  234. private static ulong UnsignedSignSatQ(ulong op, int eSize, ExecutionContext context) // eSize := {8, 16, 32, 64}.
  235. {
  236. ulong tMaxValue = ulong.MaxValue >> (64 - eSize);
  237. if (op > 0UL)
  238. {
  239. context.Fpsr |= FPSR.Qc;
  240. return tMaxValue;
  241. }
  242. else
  243. {
  244. return 0UL;
  245. }
  246. }
  247. #endregion
  248. #region "ShrImm64"
  249. public static long SignedShrImm64(long value, long roundConst, int shift)
  250. {
  251. if (roundConst == 0L)
  252. {
  253. if (shift <= 63)
  254. {
  255. return value >> shift;
  256. }
  257. else /* if (shift == 64) */
  258. {
  259. if (value < 0L)
  260. {
  261. return -1L;
  262. }
  263. else /* if (value >= 0L) */
  264. {
  265. return 0L;
  266. }
  267. }
  268. }
  269. else /* if (roundConst == 1L << (shift - 1)) */
  270. {
  271. if (shift <= 63)
  272. {
  273. long add = value + roundConst;
  274. if ((~value & (value ^ add)) < 0L)
  275. {
  276. return (long)((ulong)add >> shift);
  277. }
  278. else
  279. {
  280. return add >> shift;
  281. }
  282. }
  283. else /* if (shift == 64) */
  284. {
  285. return 0L;
  286. }
  287. }
  288. }
  289. public static ulong UnsignedShrImm64(ulong value, long roundConst, int shift)
  290. {
  291. if (roundConst == 0L)
  292. {
  293. if (shift <= 63)
  294. {
  295. return value >> shift;
  296. }
  297. else /* if (shift == 64) */
  298. {
  299. return 0UL;
  300. }
  301. }
  302. else /* if (roundConst == 1L << (shift - 1)) */
  303. {
  304. ulong add = value + (ulong)roundConst;
  305. if ((add < value) && (add < (ulong)roundConst))
  306. {
  307. if (shift <= 63)
  308. {
  309. return (add >> shift) | (0x8000000000000000UL >> (shift - 1));
  310. }
  311. else /* if (shift == 64) */
  312. {
  313. return 1UL;
  314. }
  315. }
  316. else
  317. {
  318. if (shift <= 63)
  319. {
  320. return add >> shift;
  321. }
  322. else /* if (shift == 64) */
  323. {
  324. return 0UL;
  325. }
  326. }
  327. }
  328. }
  329. #endregion
  330. #region "Rounding"
  331. public static double Round(double value)
  332. {
  333. ExecutionContext context = NativeInterface.GetContext();
  334. FPRoundingMode roundMode = context.Fpcr.GetRoundingMode();
  335. if (roundMode == FPRoundingMode.ToNearest)
  336. {
  337. return Math.Round(value); // even
  338. }
  339. else if (roundMode == FPRoundingMode.TowardsPlusInfinity)
  340. {
  341. return Math.Ceiling(value);
  342. }
  343. else if (roundMode == FPRoundingMode.TowardsMinusInfinity)
  344. {
  345. return Math.Floor(value);
  346. }
  347. else /* if (roundMode == FPRoundingMode.TowardsZero) */
  348. {
  349. return Math.Truncate(value);
  350. }
  351. }
  352. public static float RoundF(float value)
  353. {
  354. ExecutionContext context = NativeInterface.GetContext();
  355. FPRoundingMode roundMode = context.Fpcr.GetRoundingMode();
  356. if (roundMode == FPRoundingMode.ToNearest)
  357. {
  358. return MathF.Round(value); // even
  359. }
  360. else if (roundMode == FPRoundingMode.TowardsPlusInfinity)
  361. {
  362. return MathF.Ceiling(value);
  363. }
  364. else if (roundMode == FPRoundingMode.TowardsMinusInfinity)
  365. {
  366. return MathF.Floor(value);
  367. }
  368. else /* if (roundMode == FPRoundingMode.TowardsZero) */
  369. {
  370. return MathF.Truncate(value);
  371. }
  372. }
  373. public static int FloatToInt32(float value)
  374. {
  375. return SatF32ToS32(RoundF(value));
  376. }
  377. public static int DoubleToInt32(double value)
  378. {
  379. return SatF64ToS32(Round(value));
  380. }
  381. public static uint FloatToUInt32(float value)
  382. {
  383. return SatF32ToU32(RoundF(value));
  384. }
  385. public static uint DoubleToUInt32(double value)
  386. {
  387. return SatF64ToU32(Round(value));
  388. }
  389. #endregion
  390. #region "Saturation"
  391. public static int SatF32ToS32(float value)
  392. {
  393. if (float.IsNaN(value)) return 0;
  394. return value >= int.MaxValue ? int.MaxValue :
  395. value <= int.MinValue ? int.MinValue : (int)value;
  396. }
  397. public static long SatF32ToS64(float value)
  398. {
  399. if (float.IsNaN(value)) return 0;
  400. return value >= long.MaxValue ? long.MaxValue :
  401. value <= long.MinValue ? long.MinValue : (long)value;
  402. }
  403. public static uint SatF32ToU32(float value)
  404. {
  405. if (float.IsNaN(value)) return 0;
  406. return value >= uint.MaxValue ? uint.MaxValue :
  407. value <= uint.MinValue ? uint.MinValue : (uint)value;
  408. }
  409. public static ulong SatF32ToU64(float value)
  410. {
  411. if (float.IsNaN(value)) return 0;
  412. return value >= ulong.MaxValue ? ulong.MaxValue :
  413. value <= ulong.MinValue ? ulong.MinValue : (ulong)value;
  414. }
  415. public static int SatF64ToS32(double value)
  416. {
  417. if (double.IsNaN(value)) return 0;
  418. return value >= int.MaxValue ? int.MaxValue :
  419. value <= int.MinValue ? int.MinValue : (int)value;
  420. }
  421. public static long SatF64ToS64(double value)
  422. {
  423. if (double.IsNaN(value)) return 0;
  424. return value >= long.MaxValue ? long.MaxValue :
  425. value <= long.MinValue ? long.MinValue : (long)value;
  426. }
  427. public static uint SatF64ToU32(double value)
  428. {
  429. if (double.IsNaN(value)) return 0;
  430. return value >= uint.MaxValue ? uint.MaxValue :
  431. value <= uint.MinValue ? uint.MinValue : (uint)value;
  432. }
  433. public static ulong SatF64ToU64(double value)
  434. {
  435. if (double.IsNaN(value)) return 0;
  436. return value >= ulong.MaxValue ? ulong.MaxValue :
  437. value <= ulong.MinValue ? ulong.MinValue : (ulong)value;
  438. }
  439. #endregion
  440. #region "Saturating"
  441. private static long SignedSrcSignedDstSatQ(long op, int size)
  442. {
  443. ExecutionContext context = NativeInterface.GetContext();
  444. int eSize = 8 << size;
  445. long tMaxValue = (1L << (eSize - 1)) - 1L;
  446. long tMinValue = -(1L << (eSize - 1));
  447. if (op > tMaxValue)
  448. {
  449. context.Fpsr |= FPSR.Qc;
  450. return tMaxValue;
  451. }
  452. else if (op < tMinValue)
  453. {
  454. context.Fpsr |= FPSR.Qc;
  455. return tMinValue;
  456. }
  457. else
  458. {
  459. return op;
  460. }
  461. }
  462. private static ulong UnsignedSrcUnsignedDstSatQ(ulong op, int size)
  463. {
  464. ExecutionContext context = NativeInterface.GetContext();
  465. int eSize = 8 << size;
  466. ulong tMaxValue = (1UL << eSize) - 1UL;
  467. if (op > tMaxValue)
  468. {
  469. context.Fpsr |= FPSR.Qc;
  470. return tMaxValue;
  471. }
  472. else
  473. {
  474. return op;
  475. }
  476. }
  477. #endregion
  478. #region "Count"
  479. public static ulong CountLeadingSigns(ulong value, int size) // size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.).
  480. {
  481. value ^= value >> 1;
  482. int highBit = size - 2;
  483. for (int bit = highBit; bit >= 0; bit--)
  484. {
  485. if (((int)(value >> bit) & 0b1) != 0)
  486. {
  487. return (ulong)(highBit - bit);
  488. }
  489. }
  490. return (ulong)(size - 1);
  491. }
  492. private static ReadOnlySpan<byte> ClzNibbleTbl => new byte[] { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
  493. public static ulong CountLeadingZeros(ulong value, int size) // size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.).
  494. {
  495. if (value == 0ul)
  496. {
  497. return (ulong)size;
  498. }
  499. int nibbleIdx = size;
  500. int preCount, count = 0;
  501. do
  502. {
  503. nibbleIdx -= 4;
  504. preCount = ClzNibbleTbl[(int)(value >> nibbleIdx) & 0b1111];
  505. count += preCount;
  506. }
  507. while (preCount == 4);
  508. return (ulong)count;
  509. }
  510. #endregion
  511. #region "Table"
  512. public static V128 Tbl1(V128 vector, int bytes, V128 tb0)
  513. {
  514. return TblOrTbx(default, vector, bytes, tb0);
  515. }
  516. public static V128 Tbl2(V128 vector, int bytes, V128 tb0, V128 tb1)
  517. {
  518. return TblOrTbx(default, vector, bytes, tb0, tb1);
  519. }
  520. public static V128 Tbl3(V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2)
  521. {
  522. return TblOrTbx(default, vector, bytes, tb0, tb1, tb2);
  523. }
  524. public static V128 Tbl4(V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2, V128 tb3)
  525. {
  526. return TblOrTbx(default, vector, bytes, tb0, tb1, tb2, tb3);
  527. }
  528. public static V128 Tbx1(V128 dest, V128 vector, int bytes, V128 tb0)
  529. {
  530. return TblOrTbx(dest, vector, bytes, tb0);
  531. }
  532. public static V128 Tbx2(V128 dest, V128 vector, int bytes, V128 tb0, V128 tb1)
  533. {
  534. return TblOrTbx(dest, vector, bytes, tb0, tb1);
  535. }
  536. public static V128 Tbx3(V128 dest, V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2)
  537. {
  538. return TblOrTbx(dest, vector, bytes, tb0, tb1, tb2);
  539. }
  540. public static V128 Tbx4(V128 dest, V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2, V128 tb3)
  541. {
  542. return TblOrTbx(dest, vector, bytes, tb0, tb1, tb2, tb3);
  543. }
  544. private static V128 TblOrTbx(V128 dest, V128 vector, int bytes, params V128[] tb)
  545. {
  546. byte[] res = new byte[16];
  547. if (dest != default)
  548. {
  549. Buffer.BlockCopy(dest.ToArray(), 0, res, 0, bytes);
  550. }
  551. byte[] table = new byte[tb.Length * 16];
  552. for (byte index = 0; index < tb.Length; index++)
  553. {
  554. Buffer.BlockCopy(tb[index].ToArray(), 0, table, index * 16, 16);
  555. }
  556. byte[] v = vector.ToArray();
  557. for (byte index = 0; index < bytes; index++)
  558. {
  559. byte tblIndex = v[index];
  560. if (tblIndex < table.Length)
  561. {
  562. res[index] = table[tblIndex];
  563. }
  564. }
  565. return new V128(res);
  566. }
  567. #endregion
  568. #region "Crc32"
  569. private const uint Crc32RevPoly = 0xedb88320;
  570. private const uint Crc32cRevPoly = 0x82f63b78;
  571. public static uint Crc32b(uint crc, byte value) => Crc32 (crc, Crc32RevPoly, value);
  572. public static uint Crc32h(uint crc, ushort value) => Crc32h(crc, Crc32RevPoly, value);
  573. public static uint Crc32w(uint crc, uint value) => Crc32w(crc, Crc32RevPoly, value);
  574. public static uint Crc32x(uint crc, ulong value) => Crc32x(crc, Crc32RevPoly, value);
  575. public static uint Crc32cb(uint crc, byte value) => Crc32 (crc, Crc32cRevPoly, value);
  576. public static uint Crc32ch(uint crc, ushort value) => Crc32h(crc, Crc32cRevPoly, value);
  577. public static uint Crc32cw(uint crc, uint value) => Crc32w(crc, Crc32cRevPoly, value);
  578. public static uint Crc32cx(uint crc, ulong value) => Crc32x(crc, Crc32cRevPoly, value);
  579. private static uint Crc32h(uint crc, uint poly, ushort val)
  580. {
  581. crc = Crc32(crc, poly, (byte)(val >> 0));
  582. crc = Crc32(crc, poly, (byte)(val >> 8));
  583. return crc;
  584. }
  585. private static uint Crc32w(uint crc, uint poly, uint val)
  586. {
  587. crc = Crc32(crc, poly, (byte)(val >> 0));
  588. crc = Crc32(crc, poly, (byte)(val >> 8));
  589. crc = Crc32(crc, poly, (byte)(val >> 16));
  590. crc = Crc32(crc, poly, (byte)(val >> 24));
  591. return crc;
  592. }
  593. private static uint Crc32x(uint crc, uint poly, ulong val)
  594. {
  595. crc = Crc32(crc, poly, (byte)(val >> 0));
  596. crc = Crc32(crc, poly, (byte)(val >> 8));
  597. crc = Crc32(crc, poly, (byte)(val >> 16));
  598. crc = Crc32(crc, poly, (byte)(val >> 24));
  599. crc = Crc32(crc, poly, (byte)(val >> 32));
  600. crc = Crc32(crc, poly, (byte)(val >> 40));
  601. crc = Crc32(crc, poly, (byte)(val >> 48));
  602. crc = Crc32(crc, poly, (byte)(val >> 56));
  603. return crc;
  604. }
  605. private static uint Crc32(uint crc, uint poly, byte val)
  606. {
  607. crc ^= val;
  608. for (int bit = 7; bit >= 0; bit--)
  609. {
  610. uint mask = (uint)(-(int)(crc & 1));
  611. crc = (crc >> 1) ^ (poly & mask);
  612. }
  613. return crc;
  614. }
  615. #endregion
  616. #region "Aes"
  617. public static V128 Decrypt(V128 value, V128 roundKey)
  618. {
  619. return CryptoHelper.AesInvSubBytes(CryptoHelper.AesInvShiftRows(value ^ roundKey));
  620. }
  621. public static V128 Encrypt(V128 value, V128 roundKey)
  622. {
  623. return CryptoHelper.AesSubBytes(CryptoHelper.AesShiftRows(value ^ roundKey));
  624. }
  625. public static V128 InverseMixColumns(V128 value)
  626. {
  627. return CryptoHelper.AesInvMixColumns(value);
  628. }
  629. public static V128 MixColumns(V128 value)
  630. {
  631. return CryptoHelper.AesMixColumns(value);
  632. }
  633. #endregion
  634. #region "Sha1"
  635. public static V128 HashChoose(V128 hash_abcd, uint hash_e, V128 wk)
  636. {
  637. for (int e = 0; e <= 3; e++)
  638. {
  639. uint t = ShaChoose(hash_abcd.Extract<uint>(1),
  640. hash_abcd.Extract<uint>(2),
  641. hash_abcd.Extract<uint>(3));
  642. hash_e += Rol(hash_abcd.Extract<uint>(0), 5) + t + wk.Extract<uint>(e);
  643. t = Rol(hash_abcd.Extract<uint>(1), 30);
  644. hash_abcd.Insert(1, t);
  645. Rol32_160(ref hash_e, ref hash_abcd);
  646. }
  647. return hash_abcd;
  648. }
  649. public static uint FixedRotate(uint hash_e)
  650. {
  651. return hash_e.Rol(30);
  652. }
  653. public static V128 HashMajority(V128 hash_abcd, uint hash_e, V128 wk)
  654. {
  655. for (int e = 0; e <= 3; e++)
  656. {
  657. uint t = ShaMajority(hash_abcd.Extract<uint>(1),
  658. hash_abcd.Extract<uint>(2),
  659. hash_abcd.Extract<uint>(3));
  660. hash_e += Rol(hash_abcd.Extract<uint>(0), 5) + t + wk.Extract<uint>(e);
  661. t = Rol(hash_abcd.Extract<uint>(1), 30);
  662. hash_abcd.Insert(1, t);
  663. Rol32_160(ref hash_e, ref hash_abcd);
  664. }
  665. return hash_abcd;
  666. }
  667. public static V128 HashParity(V128 hash_abcd, uint hash_e, V128 wk)
  668. {
  669. for (int e = 0; e <= 3; e++)
  670. {
  671. uint t = ShaParity(hash_abcd.Extract<uint>(1),
  672. hash_abcd.Extract<uint>(2),
  673. hash_abcd.Extract<uint>(3));
  674. hash_e += Rol(hash_abcd.Extract<uint>(0), 5) + t + wk.Extract<uint>(e);
  675. t = Rol(hash_abcd.Extract<uint>(1), 30);
  676. hash_abcd.Insert(1, t);
  677. Rol32_160(ref hash_e, ref hash_abcd);
  678. }
  679. return hash_abcd;
  680. }
  681. public static V128 Sha1SchedulePart1(V128 w0_3, V128 w4_7, V128 w8_11)
  682. {
  683. ulong t2 = w4_7.Extract<ulong>(0);
  684. ulong t1 = w0_3.Extract<ulong>(1);
  685. V128 result = new V128(t1, t2);
  686. return result ^ (w0_3 ^ w8_11);
  687. }
  688. public static V128 Sha1SchedulePart2(V128 tw0_3, V128 w12_15)
  689. {
  690. V128 t = tw0_3 ^ (w12_15 >> 32);
  691. uint tE0 = t.Extract<uint>(0);
  692. uint tE1 = t.Extract<uint>(1);
  693. uint tE2 = t.Extract<uint>(2);
  694. uint tE3 = t.Extract<uint>(3);
  695. return new V128(tE0.Rol(1), tE1.Rol(1), tE2.Rol(1), tE3.Rol(1) ^ tE0.Rol(2));
  696. }
  697. private static void Rol32_160(ref uint y, ref V128 x)
  698. {
  699. uint xE3 = x.Extract<uint>(3);
  700. x <<= 32;
  701. x.Insert(0, y);
  702. y = xE3;
  703. }
  704. private static uint ShaChoose(uint x, uint y, uint z)
  705. {
  706. return ((y ^ z) & x) ^ z;
  707. }
  708. private static uint ShaMajority(uint x, uint y, uint z)
  709. {
  710. return (x & y) | ((x | y) & z);
  711. }
  712. private static uint ShaParity(uint x, uint y, uint z)
  713. {
  714. return x ^ y ^ z;
  715. }
  716. private static uint Rol(this uint value, int count)
  717. {
  718. return (value << count) | (value >> (32 - count));
  719. }
  720. #endregion
  721. #region "Sha256"
  722. public static V128 HashLower(V128 hash_abcd, V128 hash_efgh, V128 wk)
  723. {
  724. return Sha256Hash(hash_abcd, hash_efgh, wk, part1: true);
  725. }
  726. public static V128 HashUpper(V128 hash_abcd, V128 hash_efgh, V128 wk)
  727. {
  728. return Sha256Hash(hash_abcd, hash_efgh, wk, part1: false);
  729. }
  730. public static V128 Sha256SchedulePart1(V128 w0_3, V128 w4_7)
  731. {
  732. V128 result = new V128();
  733. for (int e = 0; e <= 3; e++)
  734. {
  735. uint elt = (e <= 2 ? w0_3 : w4_7).Extract<uint>(e <= 2 ? e + 1 : 0);
  736. elt = elt.Ror(7) ^ elt.Ror(18) ^ elt.Lsr(3);
  737. elt += w0_3.Extract<uint>(e);
  738. result.Insert(e, elt);
  739. }
  740. return result;
  741. }
  742. public static V128 Sha256SchedulePart2(V128 w0_3, V128 w8_11, V128 w12_15)
  743. {
  744. V128 result = new V128();
  745. ulong t1 = w12_15.Extract<ulong>(1);
  746. for (int e = 0; e <= 1; e++)
  747. {
  748. uint elt = t1.ULongPart(e);
  749. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  750. elt += w0_3.Extract<uint>(e) + w8_11.Extract<uint>(e + 1);
  751. result.Insert(e, elt);
  752. }
  753. t1 = result.Extract<ulong>(0);
  754. for (int e = 2; e <= 3; e++)
  755. {
  756. uint elt = t1.ULongPart(e - 2);
  757. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  758. elt += w0_3.Extract<uint>(e) + (e == 2 ? w8_11 : w12_15).Extract<uint>(e == 2 ? 3 : 0);
  759. result.Insert(e, elt);
  760. }
  761. return result;
  762. }
  763. private static V128 Sha256Hash(V128 x, V128 y, V128 w, bool part1)
  764. {
  765. for (int e = 0; e <= 3; e++)
  766. {
  767. uint chs = ShaChoose(y.Extract<uint>(0),
  768. y.Extract<uint>(1),
  769. y.Extract<uint>(2));
  770. uint maj = ShaMajority(x.Extract<uint>(0),
  771. x.Extract<uint>(1),
  772. x.Extract<uint>(2));
  773. uint t1 = y.Extract<uint>(3) + ShaHashSigma1(y.Extract<uint>(0)) + chs + w.Extract<uint>(e);
  774. uint t2 = t1 + x.Extract<uint>(3);
  775. x.Insert(3, t2);
  776. t2 = t1 + ShaHashSigma0(x.Extract<uint>(0)) + maj;
  777. y.Insert(3, t2);
  778. Rol32_256(ref y, ref x);
  779. }
  780. return part1 ? x : y;
  781. }
  782. private static void Rol32_256(ref V128 y, ref V128 x)
  783. {
  784. uint yE3 = y.Extract<uint>(3);
  785. uint xE3 = x.Extract<uint>(3);
  786. y <<= 32;
  787. x <<= 32;
  788. y.Insert(0, xE3);
  789. x.Insert(0, yE3);
  790. }
  791. private static uint ShaHashSigma0(uint x)
  792. {
  793. return x.Ror(2) ^ x.Ror(13) ^ x.Ror(22);
  794. }
  795. private static uint ShaHashSigma1(uint x)
  796. {
  797. return x.Ror(6) ^ x.Ror(11) ^ x.Ror(25);
  798. }
  799. private static uint Ror(this uint value, int count)
  800. {
  801. return (value >> count) | (value << (32 - count));
  802. }
  803. private static uint Lsr(this uint value, int count)
  804. {
  805. return value >> count;
  806. }
  807. private static uint ULongPart(this ulong value, int part)
  808. {
  809. return part == 0
  810. ? (uint)(value & 0xFFFFFFFFUL)
  811. : (uint)(value >> 32);
  812. }
  813. #endregion
  814. public static V128 PolynomialMult64_128(ulong op1, ulong op2)
  815. {
  816. V128 result = V128.Zero;
  817. V128 op2_128 = new V128(op2, 0);
  818. for (int i = 0; i < 64; i++)
  819. {
  820. if (((op1 >> i) & 1) == 1)
  821. {
  822. result ^= op2_128 << i;
  823. }
  824. }
  825. return result;
  826. }
  827. }
  828. }