SoftFallback.cs 37 KB

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