SoftFallback.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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 readonly byte[] ClzNibbleTbl = { 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. public static ulong CountSetBits8(ulong value) // "size" is 8 (SIMD&FP Inst.).
  711. {
  712. value = ((value >> 1) & 0x55ul) + (value & 0x55ul);
  713. value = ((value >> 2) & 0x33ul) + (value & 0x33ul);
  714. return (value >> 4) + (value & 0x0ful);
  715. }
  716. #endregion
  717. #region "Table"
  718. public static V128 Tbl1(V128 vector, int bytes, V128 tb0)
  719. {
  720. return TblOrTbx(default, vector, bytes, tb0);
  721. }
  722. public static V128 Tbl2(V128 vector, int bytes, V128 tb0, V128 tb1)
  723. {
  724. return TblOrTbx(default, vector, bytes, tb0, tb1);
  725. }
  726. public static V128 Tbl3(V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2)
  727. {
  728. return TblOrTbx(default, vector, bytes, tb0, tb1, tb2);
  729. }
  730. public static V128 Tbl4(V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2, V128 tb3)
  731. {
  732. return TblOrTbx(default, vector, bytes, tb0, tb1, tb2, tb3);
  733. }
  734. public static V128 Tbx1(V128 dest, V128 vector, int bytes, V128 tb0)
  735. {
  736. return TblOrTbx(dest, vector, bytes, tb0);
  737. }
  738. public static V128 Tbx2(V128 dest, V128 vector, int bytes, V128 tb0, V128 tb1)
  739. {
  740. return TblOrTbx(dest, vector, bytes, tb0, tb1);
  741. }
  742. public static V128 Tbx3(V128 dest, V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2)
  743. {
  744. return TblOrTbx(dest, vector, bytes, tb0, tb1, tb2);
  745. }
  746. public static V128 Tbx4(V128 dest, V128 vector, int bytes, V128 tb0, V128 tb1, V128 tb2, V128 tb3)
  747. {
  748. return TblOrTbx(dest, vector, bytes, tb0, tb1, tb2, tb3);
  749. }
  750. private static V128 TblOrTbx(V128 dest, V128 vector, int bytes, params V128[] tb)
  751. {
  752. byte[] res = new byte[16];
  753. if (dest != default)
  754. {
  755. Buffer.BlockCopy(dest.ToArray(), 0, res, 0, bytes);
  756. }
  757. byte[] table = new byte[tb.Length * 16];
  758. for (byte index = 0; index < tb.Length; index++)
  759. {
  760. Buffer.BlockCopy(tb[index].ToArray(), 0, table, index * 16, 16);
  761. }
  762. byte[] v = vector.ToArray();
  763. for (byte index = 0; index < bytes; index++)
  764. {
  765. byte tblIndex = v[index];
  766. if (tblIndex < table.Length)
  767. {
  768. res[index] = table[tblIndex];
  769. }
  770. }
  771. return new V128(res);
  772. }
  773. #endregion
  774. #region "Crc32"
  775. private const uint Crc32RevPoly = 0xedb88320;
  776. private const uint Crc32cRevPoly = 0x82f63b78;
  777. public static uint Crc32b(uint crc, byte value) => Crc32 (crc, Crc32RevPoly, value);
  778. public static uint Crc32h(uint crc, ushort value) => Crc32h(crc, Crc32RevPoly, value);
  779. public static uint Crc32w(uint crc, uint value) => Crc32w(crc, Crc32RevPoly, value);
  780. public static uint Crc32x(uint crc, ulong value) => Crc32x(crc, Crc32RevPoly, value);
  781. public static uint Crc32cb(uint crc, byte value) => Crc32 (crc, Crc32cRevPoly, value);
  782. public static uint Crc32ch(uint crc, ushort value) => Crc32h(crc, Crc32cRevPoly, value);
  783. public static uint Crc32cw(uint crc, uint value) => Crc32w(crc, Crc32cRevPoly, value);
  784. public static uint Crc32cx(uint crc, ulong value) => Crc32x(crc, Crc32cRevPoly, value);
  785. private static uint Crc32h(uint crc, uint poly, ushort val)
  786. {
  787. crc = Crc32(crc, poly, (byte)(val >> 0));
  788. crc = Crc32(crc, poly, (byte)(val >> 8));
  789. return crc;
  790. }
  791. private static uint Crc32w(uint crc, uint poly, uint val)
  792. {
  793. crc = Crc32(crc, poly, (byte)(val >> 0));
  794. crc = Crc32(crc, poly, (byte)(val >> 8));
  795. crc = Crc32(crc, poly, (byte)(val >> 16));
  796. crc = Crc32(crc, poly, (byte)(val >> 24));
  797. return crc;
  798. }
  799. private static uint Crc32x(uint crc, uint poly, ulong val)
  800. {
  801. crc = Crc32(crc, poly, (byte)(val >> 0));
  802. crc = Crc32(crc, poly, (byte)(val >> 8));
  803. crc = Crc32(crc, poly, (byte)(val >> 16));
  804. crc = Crc32(crc, poly, (byte)(val >> 24));
  805. crc = Crc32(crc, poly, (byte)(val >> 32));
  806. crc = Crc32(crc, poly, (byte)(val >> 40));
  807. crc = Crc32(crc, poly, (byte)(val >> 48));
  808. crc = Crc32(crc, poly, (byte)(val >> 56));
  809. return crc;
  810. }
  811. private static uint Crc32(uint crc, uint poly, byte val)
  812. {
  813. crc ^= val;
  814. for (int bit = 7; bit >= 0; bit--)
  815. {
  816. uint mask = (uint)(-(int)(crc & 1));
  817. crc = (crc >> 1) ^ (poly & mask);
  818. }
  819. return crc;
  820. }
  821. #endregion
  822. #region "Aes"
  823. public static V128 Decrypt(V128 value, V128 roundKey)
  824. {
  825. return CryptoHelper.AesInvSubBytes(CryptoHelper.AesInvShiftRows(value ^ roundKey));
  826. }
  827. public static V128 Encrypt(V128 value, V128 roundKey)
  828. {
  829. return CryptoHelper.AesSubBytes(CryptoHelper.AesShiftRows(value ^ roundKey));
  830. }
  831. public static V128 InverseMixColumns(V128 value)
  832. {
  833. return CryptoHelper.AesInvMixColumns(value);
  834. }
  835. public static V128 MixColumns(V128 value)
  836. {
  837. return CryptoHelper.AesMixColumns(value);
  838. }
  839. #endregion
  840. #region "Sha1"
  841. public static V128 HashChoose(V128 hash_abcd, uint hash_e, V128 wk)
  842. {
  843. for (int e = 0; e <= 3; e++)
  844. {
  845. uint t = ShaChoose(hash_abcd.GetUInt32(1),
  846. hash_abcd.GetUInt32(2),
  847. hash_abcd.GetUInt32(3));
  848. hash_e += Rol(hash_abcd.GetUInt32(0), 5) + t + wk.GetUInt32(e);
  849. t = Rol(hash_abcd.GetUInt32(1), 30);
  850. hash_abcd.Insert(1, t);
  851. Rol32_160(ref hash_e, ref hash_abcd);
  852. }
  853. return hash_abcd;
  854. }
  855. public static uint FixedRotate(uint hash_e)
  856. {
  857. return hash_e.Rol(30);
  858. }
  859. public static V128 HashMajority(V128 hash_abcd, uint hash_e, V128 wk)
  860. {
  861. for (int e = 0; e <= 3; e++)
  862. {
  863. uint t = ShaMajority(hash_abcd.GetUInt32(1),
  864. hash_abcd.GetUInt32(2),
  865. hash_abcd.GetUInt32(3));
  866. hash_e += Rol(hash_abcd.GetUInt32(0), 5) + t + wk.GetUInt32(e);
  867. t = Rol(hash_abcd.GetUInt32(1), 30);
  868. hash_abcd.Insert(1, t);
  869. Rol32_160(ref hash_e, ref hash_abcd);
  870. }
  871. return hash_abcd;
  872. }
  873. public static V128 HashParity(V128 hash_abcd, uint hash_e, V128 wk)
  874. {
  875. for (int e = 0; e <= 3; e++)
  876. {
  877. uint t = ShaParity(hash_abcd.GetUInt32(1),
  878. hash_abcd.GetUInt32(2),
  879. hash_abcd.GetUInt32(3));
  880. hash_e += Rol(hash_abcd.GetUInt32(0), 5) + t + wk.GetUInt32(e);
  881. t = Rol(hash_abcd.GetUInt32(1), 30);
  882. hash_abcd.Insert(1, t);
  883. Rol32_160(ref hash_e, ref hash_abcd);
  884. }
  885. return hash_abcd;
  886. }
  887. public static V128 Sha1SchedulePart1(V128 w0_3, V128 w4_7, V128 w8_11)
  888. {
  889. ulong t2 = w4_7.GetUInt64(0);
  890. ulong t1 = w0_3.GetUInt64(1);
  891. V128 result = new V128(t1, t2);
  892. return result ^ (w0_3 ^ w8_11);
  893. }
  894. public static V128 Sha1SchedulePart2(V128 tw0_3, V128 w12_15)
  895. {
  896. V128 t = tw0_3 ^ (w12_15 >> 32);
  897. uint tE0 = t.GetUInt32(0);
  898. uint tE1 = t.GetUInt32(1);
  899. uint tE2 = t.GetUInt32(2);
  900. uint tE3 = t.GetUInt32(3);
  901. return new V128(tE0.Rol(1), tE1.Rol(1), tE2.Rol(1), tE3.Rol(1) ^ tE0.Rol(2));
  902. }
  903. private static void Rol32_160(ref uint y, ref V128 x)
  904. {
  905. uint xE3 = x.GetUInt32(3);
  906. x <<= 32;
  907. x.Insert(0, y);
  908. y = xE3;
  909. }
  910. private static uint ShaChoose(uint x, uint y, uint z)
  911. {
  912. return ((y ^ z) & x) ^ z;
  913. }
  914. private static uint ShaMajority(uint x, uint y, uint z)
  915. {
  916. return (x & y) | ((x | y) & z);
  917. }
  918. private static uint ShaParity(uint x, uint y, uint z)
  919. {
  920. return x ^ y ^ z;
  921. }
  922. private static uint Rol(this uint value, int count)
  923. {
  924. return (value << count) | (value >> (32 - count));
  925. }
  926. #endregion
  927. #region "Sha256"
  928. public static V128 HashLower(V128 hash_abcd, V128 hash_efgh, V128 wk)
  929. {
  930. return Sha256Hash(hash_abcd, hash_efgh, wk, part1: true);
  931. }
  932. public static V128 HashUpper(V128 hash_efgh, V128 hash_abcd, V128 wk)
  933. {
  934. return Sha256Hash(hash_abcd, hash_efgh, wk, part1: false);
  935. }
  936. public static V128 Sha256SchedulePart1(V128 w0_3, V128 w4_7)
  937. {
  938. V128 result = new V128();
  939. for (int e = 0; e <= 3; e++)
  940. {
  941. uint elt = (e <= 2 ? w0_3 : w4_7).GetUInt32(e <= 2 ? e + 1 : 0);
  942. elt = elt.Ror(7) ^ elt.Ror(18) ^ elt.Lsr(3);
  943. elt += w0_3.GetUInt32(e);
  944. result.Insert(e, elt);
  945. }
  946. return result;
  947. }
  948. public static V128 Sha256SchedulePart2(V128 w0_3, V128 w8_11, V128 w12_15)
  949. {
  950. V128 result = new V128();
  951. ulong t1 = w12_15.GetUInt64(1);
  952. for (int e = 0; e <= 1; e++)
  953. {
  954. uint elt = t1.ULongPart(e);
  955. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  956. elt += w0_3.GetUInt32(e) + w8_11.GetUInt32(e + 1);
  957. result.Insert(e, elt);
  958. }
  959. t1 = result.GetUInt64(0);
  960. for (int e = 2; e <= 3; e++)
  961. {
  962. uint elt = t1.ULongPart(e - 2);
  963. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  964. elt += w0_3.GetUInt32(e) + (e == 2 ? w8_11 : w12_15).GetUInt32(e == 2 ? 3 : 0);
  965. result.Insert(e, elt);
  966. }
  967. return result;
  968. }
  969. private static V128 Sha256Hash(V128 x, V128 y, V128 w, bool part1)
  970. {
  971. for (int e = 0; e <= 3; e++)
  972. {
  973. uint chs = ShaChoose(y.GetUInt32(0),
  974. y.GetUInt32(1),
  975. y.GetUInt32(2));
  976. uint maj = ShaMajority(x.GetUInt32(0),
  977. x.GetUInt32(1),
  978. x.GetUInt32(2));
  979. uint t1 = y.GetUInt32(3) + ShaHashSigma1(y.GetUInt32(0)) + chs + w.GetUInt32(e);
  980. uint t2 = t1 + x.GetUInt32(3);
  981. x.Insert(3, t2);
  982. t2 = t1 + ShaHashSigma0(x.GetUInt32(0)) + maj;
  983. y.Insert(3, t2);
  984. Rol32_256(ref y, ref x);
  985. }
  986. return part1 ? x : y;
  987. }
  988. private static void Rol32_256(ref V128 y, ref V128 x)
  989. {
  990. uint yE3 = y.GetUInt32(3);
  991. uint xE3 = x.GetUInt32(3);
  992. y <<= 32;
  993. x <<= 32;
  994. y.Insert(0, xE3);
  995. x.Insert(0, yE3);
  996. }
  997. private static uint ShaHashSigma0(uint x)
  998. {
  999. return x.Ror(2) ^ x.Ror(13) ^ x.Ror(22);
  1000. }
  1001. private static uint ShaHashSigma1(uint x)
  1002. {
  1003. return x.Ror(6) ^ x.Ror(11) ^ x.Ror(25);
  1004. }
  1005. private static uint Ror(this uint value, int count)
  1006. {
  1007. return (value >> count) | (value << (32 - count));
  1008. }
  1009. private static uint Lsr(this uint value, int count)
  1010. {
  1011. return value >> count;
  1012. }
  1013. private static uint ULongPart(this ulong value, int part)
  1014. {
  1015. return part == 0
  1016. ? (uint)(value & 0xFFFFFFFFUL)
  1017. : (uint)(value >> 32);
  1018. }
  1019. #endregion
  1020. }
  1021. }