SoftFallback.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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);
  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);
  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. public 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. public static ulong SignedSrcUnsignedDstSatQ(long op, int size)
  463. {
  464. ExecutionContext context = NativeInterface.GetContext();
  465. int eSize = 8 << size;
  466. ulong tMaxValue = (1UL << eSize) - 1UL;
  467. ulong tMinValue = 0UL;
  468. if (op > (long)tMaxValue)
  469. {
  470. context.Fpsr |= FPSR.Qc;
  471. return tMaxValue;
  472. }
  473. else if (op < (long)tMinValue)
  474. {
  475. context.Fpsr |= FPSR.Qc;
  476. return tMinValue;
  477. }
  478. else
  479. {
  480. return (ulong)op;
  481. }
  482. }
  483. public static long UnsignedSrcSignedDstSatQ(ulong op, int size)
  484. {
  485. ExecutionContext context = NativeInterface.GetContext();
  486. int eSize = 8 << size;
  487. long tMaxValue = (1L << (eSize - 1)) - 1L;
  488. if (op > (ulong)tMaxValue)
  489. {
  490. context.Fpsr |= FPSR.Qc;
  491. return tMaxValue;
  492. }
  493. else
  494. {
  495. return (long)op;
  496. }
  497. }
  498. public static ulong UnsignedSrcUnsignedDstSatQ(ulong op, int size)
  499. {
  500. ExecutionContext context = NativeInterface.GetContext();
  501. int eSize = 8 << size;
  502. ulong tMaxValue = (1UL << eSize) - 1UL;
  503. if (op > tMaxValue)
  504. {
  505. context.Fpsr |= FPSR.Qc;
  506. return tMaxValue;
  507. }
  508. else
  509. {
  510. return op;
  511. }
  512. }
  513. public static long UnarySignedSatQAbsOrNeg(long op)
  514. {
  515. ExecutionContext context = NativeInterface.GetContext();
  516. if (op == long.MinValue)
  517. {
  518. context.Fpsr |= FPSR.Qc;
  519. return long.MaxValue;
  520. }
  521. else
  522. {
  523. return op;
  524. }
  525. }
  526. public static long BinarySignedSatQAdd(long op1, long op2)
  527. {
  528. ExecutionContext context = NativeInterface.GetContext();
  529. long add = op1 + op2;
  530. if ((~(op1 ^ op2) & (op1 ^ add)) < 0L)
  531. {
  532. context.Fpsr |= FPSR.Qc;
  533. if (op1 < 0L)
  534. {
  535. return long.MinValue;
  536. }
  537. else
  538. {
  539. return long.MaxValue;
  540. }
  541. }
  542. else
  543. {
  544. return add;
  545. }
  546. }
  547. public static ulong BinaryUnsignedSatQAdd(ulong op1, ulong op2)
  548. {
  549. ExecutionContext context = NativeInterface.GetContext();
  550. ulong add = op1 + op2;
  551. if ((add < op1) && (add < op2))
  552. {
  553. context.Fpsr |= FPSR.Qc;
  554. return ulong.MaxValue;
  555. }
  556. else
  557. {
  558. return add;
  559. }
  560. }
  561. public static long BinarySignedSatQSub(long op1, long op2)
  562. {
  563. ExecutionContext context = NativeInterface.GetContext();
  564. long sub = op1 - op2;
  565. if (((op1 ^ op2) & (op1 ^ sub)) < 0L)
  566. {
  567. context.Fpsr |= FPSR.Qc;
  568. if (op1 < 0L)
  569. {
  570. return long.MinValue;
  571. }
  572. else
  573. {
  574. return long.MaxValue;
  575. }
  576. }
  577. else
  578. {
  579. return sub;
  580. }
  581. }
  582. public static ulong BinaryUnsignedSatQSub(ulong op1, ulong op2)
  583. {
  584. ExecutionContext context = NativeInterface.GetContext();
  585. ulong sub = op1 - op2;
  586. if (op1 < op2)
  587. {
  588. context.Fpsr |= FPSR.Qc;
  589. return ulong.MinValue;
  590. }
  591. else
  592. {
  593. return sub;
  594. }
  595. }
  596. public static long BinarySignedSatQAcc(ulong op1, long op2)
  597. {
  598. ExecutionContext context = NativeInterface.GetContext();
  599. if (op1 <= (ulong)long.MaxValue)
  600. {
  601. // op1 from ulong.MinValue to (ulong)long.MaxValue
  602. // op2 from long.MinValue to long.MaxValue
  603. long add = (long)op1 + op2;
  604. if ((~op2 & add) < 0L)
  605. {
  606. context.Fpsr |= FPSR.Qc;
  607. return long.MaxValue;
  608. }
  609. else
  610. {
  611. return add;
  612. }
  613. }
  614. else if (op2 >= 0L)
  615. {
  616. // op1 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  617. // op2 from (long)ulong.MinValue to long.MaxValue
  618. context.Fpsr |= FPSR.Qc;
  619. return long.MaxValue;
  620. }
  621. else
  622. {
  623. // op1 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  624. // op2 from long.MinValue to (long)ulong.MinValue - 1L
  625. ulong add = op1 + (ulong)op2;
  626. if (add > (ulong)long.MaxValue)
  627. {
  628. context.Fpsr |= FPSR.Qc;
  629. return long.MaxValue;
  630. }
  631. else
  632. {
  633. return (long)add;
  634. }
  635. }
  636. }
  637. public static ulong BinaryUnsignedSatQAcc(long op1, ulong op2)
  638. {
  639. ExecutionContext context = NativeInterface.GetContext();
  640. if (op1 >= 0L)
  641. {
  642. // op1 from (long)ulong.MinValue to long.MaxValue
  643. // op2 from ulong.MinValue to ulong.MaxValue
  644. ulong add = (ulong)op1 + op2;
  645. if ((add < (ulong)op1) && (add < op2))
  646. {
  647. context.Fpsr |= FPSR.Qc;
  648. return ulong.MaxValue;
  649. }
  650. else
  651. {
  652. return add;
  653. }
  654. }
  655. else if (op2 > (ulong)long.MaxValue)
  656. {
  657. // op1 from long.MinValue to (long)ulong.MinValue - 1L
  658. // op2 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  659. return (ulong)op1 + op2;
  660. }
  661. else
  662. {
  663. // op1 from long.MinValue to (long)ulong.MinValue - 1L
  664. // op2 from ulong.MinValue to (ulong)long.MaxValue
  665. long add = op1 + (long)op2;
  666. if (add < (long)ulong.MinValue)
  667. {
  668. context.Fpsr |= FPSR.Qc;
  669. return ulong.MinValue;
  670. }
  671. else
  672. {
  673. return (ulong)add;
  674. }
  675. }
  676. }
  677. #endregion
  678. #region "Count"
  679. public static ulong CountLeadingSigns(ulong value, int size) // size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.).
  680. {
  681. value ^= value >> 1;
  682. int highBit = size - 2;
  683. for (int bit = highBit; bit >= 0; bit--)
  684. {
  685. if (((int)(value >> bit) & 0b1) != 0)
  686. {
  687. return (ulong)(highBit - bit);
  688. }
  689. }
  690. return (ulong)(size - 1);
  691. }
  692. private static ReadOnlySpan<byte> ClzNibbleTbl => new byte[] { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
  693. public static ulong CountLeadingZeros(ulong value, int size) // size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.).
  694. {
  695. if (value == 0ul)
  696. {
  697. return (ulong)size;
  698. }
  699. int nibbleIdx = size;
  700. int preCount, count = 0;
  701. do
  702. {
  703. nibbleIdx -= 4;
  704. preCount = ClzNibbleTbl[(int)(value >> nibbleIdx) & 0b1111];
  705. count += preCount;
  706. }
  707. while (preCount == 4);
  708. return (ulong)count;
  709. }
  710. #endregion
  711. #region "Table"
  712. public static V128 Tbl1(V128 vector, int bytes, V128 tb0)
  713. {
  714. return TblOrTbx(default, vector, bytes, tb0);
  715. }
  716. public static V128 Tbl2(V128 vector, int bytes, V128 tb0, V128 tb1)
  717. {
  718. return TblOrTbx(default, vector, bytes, tb0, tb1);
  719. }
  720. public static V128 Tbl3(V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2)
  721. {
  722. return TblOrTbx(default, vector, bytes, tb0, tb1, tb2);
  723. }
  724. public static V128 Tbl4(V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2, V128 tb3)
  725. {
  726. return TblOrTbx(default, vector, bytes, tb0, tb1, tb2, tb3);
  727. }
  728. public static V128 Tbx1(V128 dest, V128 vector, int bytes, V128 tb0)
  729. {
  730. return TblOrTbx(dest, vector, bytes, tb0);
  731. }
  732. public static V128 Tbx2(V128 dest, V128 vector, int bytes, V128 tb0, V128 tb1)
  733. {
  734. return TblOrTbx(dest, vector, bytes, tb0, tb1);
  735. }
  736. public static V128 Tbx3(V128 dest, V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2)
  737. {
  738. return TblOrTbx(dest, vector, bytes, tb0, tb1, tb2);
  739. }
  740. public static V128 Tbx4(V128 dest, V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2, V128 tb3)
  741. {
  742. return TblOrTbx(dest, vector, bytes, tb0, tb1, tb2, tb3);
  743. }
  744. private static V128 TblOrTbx(V128 dest, V128 vector, int bytes, params V128[] tb)
  745. {
  746. byte[] res = new byte[16];
  747. if (dest != default)
  748. {
  749. Buffer.BlockCopy(dest.ToArray(), 0, res, 0, bytes);
  750. }
  751. byte[] table = new byte[tb.Length * 16];
  752. for (byte index = 0; index < tb.Length; index++)
  753. {
  754. Buffer.BlockCopy(tb[index].ToArray(), 0, table, index * 16, 16);
  755. }
  756. byte[] v = vector.ToArray();
  757. for (byte index = 0; index < bytes; index++)
  758. {
  759. byte tblIndex = v[index];
  760. if (tblIndex < table.Length)
  761. {
  762. res[index] = table[tblIndex];
  763. }
  764. }
  765. return new V128(res);
  766. }
  767. #endregion
  768. #region "Crc32"
  769. private const uint Crc32RevPoly = 0xedb88320;
  770. private const uint Crc32cRevPoly = 0x82f63b78;
  771. public static uint Crc32b(uint crc, byte value) => Crc32 (crc, Crc32RevPoly, value);
  772. public static uint Crc32h(uint crc, ushort value) => Crc32h(crc, Crc32RevPoly, value);
  773. public static uint Crc32w(uint crc, uint value) => Crc32w(crc, Crc32RevPoly, value);
  774. public static uint Crc32x(uint crc, ulong value) => Crc32x(crc, Crc32RevPoly, value);
  775. public static uint Crc32cb(uint crc, byte value) => Crc32 (crc, Crc32cRevPoly, value);
  776. public static uint Crc32ch(uint crc, ushort value) => Crc32h(crc, Crc32cRevPoly, value);
  777. public static uint Crc32cw(uint crc, uint value) => Crc32w(crc, Crc32cRevPoly, value);
  778. public static uint Crc32cx(uint crc, ulong value) => Crc32x(crc, Crc32cRevPoly, value);
  779. private static uint Crc32h(uint crc, uint poly, ushort val)
  780. {
  781. crc = Crc32(crc, poly, (byte)(val >> 0));
  782. crc = Crc32(crc, poly, (byte)(val >> 8));
  783. return crc;
  784. }
  785. private static uint Crc32w(uint crc, uint poly, uint val)
  786. {
  787. crc = Crc32(crc, poly, (byte)(val >> 0));
  788. crc = Crc32(crc, poly, (byte)(val >> 8));
  789. crc = Crc32(crc, poly, (byte)(val >> 16));
  790. crc = Crc32(crc, poly, (byte)(val >> 24));
  791. return crc;
  792. }
  793. private static uint Crc32x(uint crc, uint poly, ulong val)
  794. {
  795. crc = Crc32(crc, poly, (byte)(val >> 0));
  796. crc = Crc32(crc, poly, (byte)(val >> 8));
  797. crc = Crc32(crc, poly, (byte)(val >> 16));
  798. crc = Crc32(crc, poly, (byte)(val >> 24));
  799. crc = Crc32(crc, poly, (byte)(val >> 32));
  800. crc = Crc32(crc, poly, (byte)(val >> 40));
  801. crc = Crc32(crc, poly, (byte)(val >> 48));
  802. crc = Crc32(crc, poly, (byte)(val >> 56));
  803. return crc;
  804. }
  805. private static uint Crc32(uint crc, uint poly, byte val)
  806. {
  807. crc ^= val;
  808. for (int bit = 7; bit >= 0; bit--)
  809. {
  810. uint mask = (uint)(-(int)(crc & 1));
  811. crc = (crc >> 1) ^ (poly & mask);
  812. }
  813. return crc;
  814. }
  815. #endregion
  816. #region "Aes"
  817. public static V128 Decrypt(V128 value, V128 roundKey)
  818. {
  819. return CryptoHelper.AesInvSubBytes(CryptoHelper.AesInvShiftRows(value ^ roundKey));
  820. }
  821. public static V128 Encrypt(V128 value, V128 roundKey)
  822. {
  823. return CryptoHelper.AesSubBytes(CryptoHelper.AesShiftRows(value ^ roundKey));
  824. }
  825. public static V128 InverseMixColumns(V128 value)
  826. {
  827. return CryptoHelper.AesInvMixColumns(value);
  828. }
  829. public static V128 MixColumns(V128 value)
  830. {
  831. return CryptoHelper.AesMixColumns(value);
  832. }
  833. #endregion
  834. #region "Sha1"
  835. public static V128 HashChoose(V128 hash_abcd, uint hash_e, V128 wk)
  836. {
  837. for (int e = 0; e <= 3; e++)
  838. {
  839. uint t = ShaChoose(hash_abcd.Extract<uint>(1),
  840. hash_abcd.Extract<uint>(2),
  841. hash_abcd.Extract<uint>(3));
  842. hash_e += Rol(hash_abcd.Extract<uint>(0), 5) + t + wk.Extract<uint>(e);
  843. t = Rol(hash_abcd.Extract<uint>(1), 30);
  844. hash_abcd.Insert(1, t);
  845. Rol32_160(ref hash_e, ref hash_abcd);
  846. }
  847. return hash_abcd;
  848. }
  849. public static uint FixedRotate(uint hash_e)
  850. {
  851. return hash_e.Rol(30);
  852. }
  853. public static V128 HashMajority(V128 hash_abcd, uint hash_e, V128 wk)
  854. {
  855. for (int e = 0; e <= 3; e++)
  856. {
  857. uint t = ShaMajority(hash_abcd.Extract<uint>(1),
  858. hash_abcd.Extract<uint>(2),
  859. hash_abcd.Extract<uint>(3));
  860. hash_e += Rol(hash_abcd.Extract<uint>(0), 5) + t + wk.Extract<uint>(e);
  861. t = Rol(hash_abcd.Extract<uint>(1), 30);
  862. hash_abcd.Insert(1, t);
  863. Rol32_160(ref hash_e, ref hash_abcd);
  864. }
  865. return hash_abcd;
  866. }
  867. public static V128 HashParity(V128 hash_abcd, uint hash_e, V128 wk)
  868. {
  869. for (int e = 0; e <= 3; e++)
  870. {
  871. uint t = ShaParity(hash_abcd.Extract<uint>(1),
  872. hash_abcd.Extract<uint>(2),
  873. hash_abcd.Extract<uint>(3));
  874. hash_e += Rol(hash_abcd.Extract<uint>(0), 5) + t + wk.Extract<uint>(e);
  875. t = Rol(hash_abcd.Extract<uint>(1), 30);
  876. hash_abcd.Insert(1, t);
  877. Rol32_160(ref hash_e, ref hash_abcd);
  878. }
  879. return hash_abcd;
  880. }
  881. public static V128 Sha1SchedulePart1(V128 w0_3, V128 w4_7, V128 w8_11)
  882. {
  883. ulong t2 = w4_7.Extract<ulong>(0);
  884. ulong t1 = w0_3.Extract<ulong>(1);
  885. V128 result = new V128(t1, t2);
  886. return result ^ (w0_3 ^ w8_11);
  887. }
  888. public static V128 Sha1SchedulePart2(V128 tw0_3, V128 w12_15)
  889. {
  890. V128 t = tw0_3 ^ (w12_15 >> 32);
  891. uint tE0 = t.Extract<uint>(0);
  892. uint tE1 = t.Extract<uint>(1);
  893. uint tE2 = t.Extract<uint>(2);
  894. uint tE3 = t.Extract<uint>(3);
  895. return new V128(tE0.Rol(1), tE1.Rol(1), tE2.Rol(1), tE3.Rol(1) ^ tE0.Rol(2));
  896. }
  897. private static void Rol32_160(ref uint y, ref V128 x)
  898. {
  899. uint xE3 = x.Extract<uint>(3);
  900. x <<= 32;
  901. x.Insert(0, y);
  902. y = xE3;
  903. }
  904. private static uint ShaChoose(uint x, uint y, uint z)
  905. {
  906. return ((y ^ z) & x) ^ z;
  907. }
  908. private static uint ShaMajority(uint x, uint y, uint z)
  909. {
  910. return (x & y) | ((x | y) & z);
  911. }
  912. private static uint ShaParity(uint x, uint y, uint z)
  913. {
  914. return x ^ y ^ z;
  915. }
  916. private static uint Rol(this uint value, int count)
  917. {
  918. return (value << count) | (value >> (32 - count));
  919. }
  920. #endregion
  921. #region "Sha256"
  922. public static V128 HashLower(V128 hash_abcd, V128 hash_efgh, V128 wk)
  923. {
  924. return Sha256Hash(hash_abcd, hash_efgh, wk, part1: true);
  925. }
  926. public static V128 HashUpper(V128 hash_efgh, V128 hash_abcd, V128 wk)
  927. {
  928. return Sha256Hash(hash_abcd, hash_efgh, wk, part1: false);
  929. }
  930. public static V128 Sha256SchedulePart1(V128 w0_3, V128 w4_7)
  931. {
  932. V128 result = new V128();
  933. for (int e = 0; e <= 3; e++)
  934. {
  935. uint elt = (e <= 2 ? w0_3 : w4_7).Extract<uint>(e <= 2 ? e + 1 : 0);
  936. elt = elt.Ror(7) ^ elt.Ror(18) ^ elt.Lsr(3);
  937. elt += w0_3.Extract<uint>(e);
  938. result.Insert(e, elt);
  939. }
  940. return result;
  941. }
  942. public static V128 Sha256SchedulePart2(V128 w0_3, V128 w8_11, V128 w12_15)
  943. {
  944. V128 result = new V128();
  945. ulong t1 = w12_15.Extract<ulong>(1);
  946. for (int e = 0; e <= 1; e++)
  947. {
  948. uint elt = t1.ULongPart(e);
  949. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  950. elt += w0_3.Extract<uint>(e) + w8_11.Extract<uint>(e + 1);
  951. result.Insert(e, elt);
  952. }
  953. t1 = result.Extract<ulong>(0);
  954. for (int e = 2; e <= 3; e++)
  955. {
  956. uint elt = t1.ULongPart(e - 2);
  957. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  958. elt += w0_3.Extract<uint>(e) + (e == 2 ? w8_11 : w12_15).Extract<uint>(e == 2 ? 3 : 0);
  959. result.Insert(e, elt);
  960. }
  961. return result;
  962. }
  963. private static V128 Sha256Hash(V128 x, V128 y, V128 w, bool part1)
  964. {
  965. for (int e = 0; e <= 3; e++)
  966. {
  967. uint chs = ShaChoose(y.Extract<uint>(0),
  968. y.Extract<uint>(1),
  969. y.Extract<uint>(2));
  970. uint maj = ShaMajority(x.Extract<uint>(0),
  971. x.Extract<uint>(1),
  972. x.Extract<uint>(2));
  973. uint t1 = y.Extract<uint>(3) + ShaHashSigma1(y.Extract<uint>(0)) + chs + w.Extract<uint>(e);
  974. uint t2 = t1 + x.Extract<uint>(3);
  975. x.Insert(3, t2);
  976. t2 = t1 + ShaHashSigma0(x.Extract<uint>(0)) + maj;
  977. y.Insert(3, t2);
  978. Rol32_256(ref y, ref x);
  979. }
  980. return part1 ? x : y;
  981. }
  982. private static void Rol32_256(ref V128 y, ref V128 x)
  983. {
  984. uint yE3 = y.Extract<uint>(3);
  985. uint xE3 = x.Extract<uint>(3);
  986. y <<= 32;
  987. x <<= 32;
  988. y.Insert(0, xE3);
  989. x.Insert(0, yE3);
  990. }
  991. private static uint ShaHashSigma0(uint x)
  992. {
  993. return x.Ror(2) ^ x.Ror(13) ^ x.Ror(22);
  994. }
  995. private static uint ShaHashSigma1(uint x)
  996. {
  997. return x.Ror(6) ^ x.Ror(11) ^ x.Ror(25);
  998. }
  999. private static uint Ror(this uint value, int count)
  1000. {
  1001. return (value >> count) | (value << (32 - count));
  1002. }
  1003. private static uint Lsr(this uint value, int count)
  1004. {
  1005. return value >> count;
  1006. }
  1007. private static uint ULongPart(this ulong value, int part)
  1008. {
  1009. return part == 0
  1010. ? (uint)(value & 0xFFFFFFFFUL)
  1011. : (uint)(value >> 32);
  1012. }
  1013. #endregion
  1014. public static V128 PolynomialMult64_128(ulong op1, ulong op2)
  1015. {
  1016. V128 result = V128.Zero;
  1017. V128 op2_128 = new V128(op2, 0);
  1018. for (int i = 0; i < 64; i++)
  1019. {
  1020. if (((op1 >> i) & 1) == 1)
  1021. {
  1022. result ^= op2_128 << i;
  1023. }
  1024. }
  1025. return result;
  1026. }
  1027. }
  1028. }