SoftFallback.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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.Extract<uint>(1),
  846. hash_abcd.Extract<uint>(2),
  847. hash_abcd.Extract<uint>(3));
  848. hash_e += Rol(hash_abcd.Extract<uint>(0), 5) + t + wk.Extract<uint>(e);
  849. t = Rol(hash_abcd.Extract<uint>(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.Extract<uint>(1),
  864. hash_abcd.Extract<uint>(2),
  865. hash_abcd.Extract<uint>(3));
  866. hash_e += Rol(hash_abcd.Extract<uint>(0), 5) + t + wk.Extract<uint>(e);
  867. t = Rol(hash_abcd.Extract<uint>(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.Extract<uint>(1),
  878. hash_abcd.Extract<uint>(2),
  879. hash_abcd.Extract<uint>(3));
  880. hash_e += Rol(hash_abcd.Extract<uint>(0), 5) + t + wk.Extract<uint>(e);
  881. t = Rol(hash_abcd.Extract<uint>(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.Extract<ulong>(0);
  890. ulong t1 = w0_3.Extract<ulong>(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.Extract<uint>(0);
  898. uint tE1 = t.Extract<uint>(1);
  899. uint tE2 = t.Extract<uint>(2);
  900. uint tE3 = t.Extract<uint>(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.Extract<uint>(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).Extract<uint>(e <= 2 ? e + 1 : 0);
  942. elt = elt.Ror(7) ^ elt.Ror(18) ^ elt.Lsr(3);
  943. elt += w0_3.Extract<uint>(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.Extract<ulong>(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.Extract<uint>(e) + w8_11.Extract<uint>(e + 1);
  957. result.Insert(e, elt);
  958. }
  959. t1 = result.Extract<ulong>(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.Extract<uint>(e) + (e == 2 ? w8_11 : w12_15).Extract<uint>(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.Extract<uint>(0),
  974. y.Extract<uint>(1),
  975. y.Extract<uint>(2));
  976. uint maj = ShaMajority(x.Extract<uint>(0),
  977. x.Extract<uint>(1),
  978. x.Extract<uint>(2));
  979. uint t1 = y.Extract<uint>(3) + ShaHashSigma1(y.Extract<uint>(0)) + chs + w.Extract<uint>(e);
  980. uint t2 = t1 + x.Extract<uint>(3);
  981. x.Insert(3, t2);
  982. t2 = t1 + ShaHashSigma0(x.Extract<uint>(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.Extract<uint>(3);
  991. uint xE3 = x.Extract<uint>(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. public static V128 PolynomialMult64_128(ulong op1, ulong op2)
  1021. {
  1022. V128 result = V128.Zero;
  1023. V128 op2_128 = new V128(op2, 0);
  1024. for (int i = 0; i < 64; i++)
  1025. {
  1026. if (((op1 >> i) & 1) == 1)
  1027. {
  1028. result ^= op2_128 << i;
  1029. }
  1030. }
  1031. return result;
  1032. }
  1033. }
  1034. }