ASoftFallback.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. using ChocolArm64.State;
  2. using ChocolArm64.Translation;
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.Intrinsics;
  6. using System.Runtime.Intrinsics.X86;
  7. namespace ChocolArm64.Instruction
  8. {
  9. using static AVectorHelper;
  10. static class ASoftFallback
  11. {
  12. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  13. {
  14. Context.EmitCall(typeof(ASoftFallback), MthdName);
  15. }
  16. #region "Saturating"
  17. public static long SignedSrcSignedDstSatQ(long op, int Size, AThreadState State)
  18. {
  19. int ESize = 8 << Size;
  20. long TMaxValue = (1L << (ESize - 1)) - 1L;
  21. long TMinValue = -(1L << (ESize - 1));
  22. if (op > TMaxValue)
  23. {
  24. SetFpsrQCFlag(State);
  25. return TMaxValue;
  26. }
  27. else if (op < TMinValue)
  28. {
  29. SetFpsrQCFlag(State);
  30. return TMinValue;
  31. }
  32. else
  33. {
  34. return op;
  35. }
  36. }
  37. public static ulong SignedSrcUnsignedDstSatQ(long op, int Size, AThreadState State)
  38. {
  39. int ESize = 8 << Size;
  40. ulong TMaxValue = (1UL << ESize) - 1UL;
  41. ulong TMinValue = 0UL;
  42. if (op > (long)TMaxValue)
  43. {
  44. SetFpsrQCFlag(State);
  45. return TMaxValue;
  46. }
  47. else if (op < (long)TMinValue)
  48. {
  49. SetFpsrQCFlag(State);
  50. return TMinValue;
  51. }
  52. else
  53. {
  54. return (ulong)op;
  55. }
  56. }
  57. public static long UnsignedSrcSignedDstSatQ(ulong op, int Size, AThreadState State)
  58. {
  59. int ESize = 8 << Size;
  60. long TMaxValue = (1L << (ESize - 1)) - 1L;
  61. if (op > (ulong)TMaxValue)
  62. {
  63. SetFpsrQCFlag(State);
  64. return TMaxValue;
  65. }
  66. else
  67. {
  68. return (long)op;
  69. }
  70. }
  71. public static ulong UnsignedSrcUnsignedDstSatQ(ulong op, int Size, AThreadState State)
  72. {
  73. int ESize = 8 << Size;
  74. ulong TMaxValue = (1UL << ESize) - 1UL;
  75. if (op > TMaxValue)
  76. {
  77. SetFpsrQCFlag(State);
  78. return TMaxValue;
  79. }
  80. else
  81. {
  82. return op;
  83. }
  84. }
  85. public static long UnarySignedSatQAbsOrNeg(long op, AThreadState State)
  86. {
  87. if (op == long.MinValue)
  88. {
  89. SetFpsrQCFlag(State);
  90. return long.MaxValue;
  91. }
  92. else
  93. {
  94. return op;
  95. }
  96. }
  97. public static long BinarySignedSatQAdd(long op1, long op2, AThreadState State)
  98. {
  99. long Add = op1 + op2;
  100. if ((~(op1 ^ op2) & (op1 ^ Add)) < 0L)
  101. {
  102. SetFpsrQCFlag(State);
  103. if (op1 < 0L)
  104. {
  105. return long.MinValue;
  106. }
  107. else
  108. {
  109. return long.MaxValue;
  110. }
  111. }
  112. else
  113. {
  114. return Add;
  115. }
  116. }
  117. public static ulong BinaryUnsignedSatQAdd(ulong op1, ulong op2, AThreadState State)
  118. {
  119. ulong Add = op1 + op2;
  120. if ((Add < op1) && (Add < op2))
  121. {
  122. SetFpsrQCFlag(State);
  123. return ulong.MaxValue;
  124. }
  125. else
  126. {
  127. return Add;
  128. }
  129. }
  130. public static long BinarySignedSatQSub(long op1, long op2, AThreadState State)
  131. {
  132. long Sub = op1 - op2;
  133. if (((op1 ^ op2) & (op1 ^ Sub)) < 0L)
  134. {
  135. SetFpsrQCFlag(State);
  136. if (op1 < 0L)
  137. {
  138. return long.MinValue;
  139. }
  140. else
  141. {
  142. return long.MaxValue;
  143. }
  144. }
  145. else
  146. {
  147. return Sub;
  148. }
  149. }
  150. public static ulong BinaryUnsignedSatQSub(ulong op1, ulong op2, AThreadState State)
  151. {
  152. ulong Sub = op1 - op2;
  153. if (op1 < op2)
  154. {
  155. SetFpsrQCFlag(State);
  156. return ulong.MinValue;
  157. }
  158. else
  159. {
  160. return Sub;
  161. }
  162. }
  163. public static long BinarySignedSatQAcc(ulong op1, long op2, AThreadState State)
  164. {
  165. if (op1 <= (ulong)long.MaxValue)
  166. {
  167. // op1 from ulong.MinValue to (ulong)long.MaxValue
  168. // op2 from long.MinValue to long.MaxValue
  169. long Add = (long)op1 + op2;
  170. if ((~op2 & Add) < 0L)
  171. {
  172. SetFpsrQCFlag(State);
  173. return long.MaxValue;
  174. }
  175. else
  176. {
  177. return Add;
  178. }
  179. }
  180. else if (op2 >= 0L)
  181. {
  182. // op1 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  183. // op2 from (long)ulong.MinValue to long.MaxValue
  184. SetFpsrQCFlag(State);
  185. return long.MaxValue;
  186. }
  187. else
  188. {
  189. // op1 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  190. // op2 from long.MinValue to (long)ulong.MinValue - 1L
  191. ulong Add = op1 + (ulong)op2;
  192. if (Add > (ulong)long.MaxValue)
  193. {
  194. SetFpsrQCFlag(State);
  195. return long.MaxValue;
  196. }
  197. else
  198. {
  199. return (long)Add;
  200. }
  201. }
  202. }
  203. public static ulong BinaryUnsignedSatQAcc(long op1, ulong op2, AThreadState State)
  204. {
  205. if (op1 >= 0L)
  206. {
  207. // op1 from (long)ulong.MinValue to long.MaxValue
  208. // op2 from ulong.MinValue to ulong.MaxValue
  209. ulong Add = (ulong)op1 + op2;
  210. if ((Add < (ulong)op1) && (Add < op2))
  211. {
  212. SetFpsrQCFlag(State);
  213. return ulong.MaxValue;
  214. }
  215. else
  216. {
  217. return Add;
  218. }
  219. }
  220. else if (op2 > (ulong)long.MaxValue)
  221. {
  222. // op1 from long.MinValue to (long)ulong.MinValue - 1L
  223. // op2 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  224. return (ulong)op1 + op2;
  225. }
  226. else
  227. {
  228. // op1 from long.MinValue to (long)ulong.MinValue - 1L
  229. // op2 from ulong.MinValue to (ulong)long.MaxValue
  230. long Add = op1 + (long)op2;
  231. if (Add < (long)ulong.MinValue)
  232. {
  233. SetFpsrQCFlag(State);
  234. return ulong.MinValue;
  235. }
  236. else
  237. {
  238. return (ulong)Add;
  239. }
  240. }
  241. }
  242. private static void SetFpsrQCFlag(AThreadState State)
  243. {
  244. const int QCFlagBit = 27;
  245. State.Fpsr |= 1 << QCFlagBit;
  246. }
  247. #endregion
  248. #region "Count"
  249. public static ulong CountLeadingSigns(ulong Value, int Size)
  250. {
  251. Value ^= Value >> 1;
  252. int HighBit = Size - 2;
  253. for (int Bit = HighBit; Bit >= 0; Bit--)
  254. {
  255. if (((Value >> Bit) & 0b1) != 0)
  256. {
  257. return (ulong)(HighBit - Bit);
  258. }
  259. }
  260. return (ulong)(Size - 1);
  261. }
  262. private static readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
  263. public static ulong CountLeadingZeros(ulong Value, int Size)
  264. {
  265. if (Value == 0)
  266. {
  267. return (ulong)Size;
  268. }
  269. int NibbleIdx = Size;
  270. int PreCount, Count = 0;
  271. do
  272. {
  273. NibbleIdx -= 4;
  274. PreCount = ClzNibbleTbl[(Value >> NibbleIdx) & 0b1111];
  275. Count += PreCount;
  276. }
  277. while (PreCount == 4);
  278. return (ulong)Count;
  279. }
  280. public static uint CountSetBits8(uint Value)
  281. {
  282. Value = ((Value >> 1) & 0x55) + (Value & 0x55);
  283. Value = ((Value >> 2) & 0x33) + (Value & 0x33);
  284. return (Value >> 4) + (Value & 0x0f);
  285. }
  286. #endregion
  287. #region "Crc32"
  288. private const uint Crc32RevPoly = 0xedb88320;
  289. private const uint Crc32cRevPoly = 0x82f63b78;
  290. public static uint Crc32b(uint Crc, byte Val) => Crc32 (Crc, Crc32RevPoly, Val);
  291. public static uint Crc32h(uint Crc, ushort Val) => Crc32h(Crc, Crc32RevPoly, Val);
  292. public static uint Crc32w(uint Crc, uint Val) => Crc32w(Crc, Crc32RevPoly, Val);
  293. public static uint Crc32x(uint Crc, ulong Val) => Crc32x(Crc, Crc32RevPoly, Val);
  294. public static uint Crc32cb(uint Crc, byte Val) => Crc32 (Crc, Crc32cRevPoly, Val);
  295. public static uint Crc32ch(uint Crc, ushort Val) => Crc32h(Crc, Crc32cRevPoly, Val);
  296. public static uint Crc32cw(uint Crc, uint Val) => Crc32w(Crc, Crc32cRevPoly, Val);
  297. public static uint Crc32cx(uint Crc, ulong Val) => Crc32x(Crc, Crc32cRevPoly, Val);
  298. private static uint Crc32h(uint Crc, uint Poly, ushort Val)
  299. {
  300. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  301. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  302. return Crc;
  303. }
  304. private static uint Crc32w(uint Crc, uint Poly, uint Val)
  305. {
  306. Crc = Crc32(Crc, Poly, (byte)(Val >> 0 ));
  307. Crc = Crc32(Crc, Poly, (byte)(Val >> 8 ));
  308. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  309. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  310. return Crc;
  311. }
  312. private static uint Crc32x(uint Crc, uint Poly, ulong Val)
  313. {
  314. Crc = Crc32(Crc, Poly, (byte)(Val >> 0 ));
  315. Crc = Crc32(Crc, Poly, (byte)(Val >> 8 ));
  316. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  317. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  318. Crc = Crc32(Crc, Poly, (byte)(Val >> 32));
  319. Crc = Crc32(Crc, Poly, (byte)(Val >> 40));
  320. Crc = Crc32(Crc, Poly, (byte)(Val >> 48));
  321. Crc = Crc32(Crc, Poly, (byte)(Val >> 56));
  322. return Crc;
  323. }
  324. private static uint Crc32(uint Crc, uint Poly, byte Val)
  325. {
  326. Crc ^= Val;
  327. for (int Bit = 7; Bit >= 0; Bit--)
  328. {
  329. uint Mask = (uint)(-(int)(Crc & 1));
  330. Crc = (Crc >> 1) ^ (Poly & Mask);
  331. }
  332. return Crc;
  333. }
  334. #endregion
  335. #region "Sha256"
  336. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  337. public static Vector128<float> HashLower(Vector128<float> hash_abcd, Vector128<float> hash_efgh, Vector128<float> wk)
  338. {
  339. return SHA256hash(hash_abcd, hash_efgh, wk, true);
  340. }
  341. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  342. public static Vector128<float> HashUpper(Vector128<float> hash_efgh, Vector128<float> hash_abcd, Vector128<float> wk)
  343. {
  344. return SHA256hash(hash_abcd, hash_efgh, wk, false);
  345. }
  346. public static Vector128<float> SchedulePart1(Vector128<float> w0_3, Vector128<float> w4_7)
  347. {
  348. Vector128<float> result = new Vector128<float>();
  349. for (int e = 0; e <= 3; e++)
  350. {
  351. uint elt = (uint)VectorExtractIntZx(e <= 2 ? w0_3 : w4_7, (byte)(e <= 2 ? e + 1 : 0), 2);
  352. elt = elt.Ror(7) ^ elt.Ror(18) ^ elt.Lsr(3);
  353. elt += (uint)VectorExtractIntZx(w0_3, (byte)e, 2);
  354. result = VectorInsertInt((ulong)elt, result, (byte)e, 2);
  355. }
  356. return result;
  357. }
  358. public static Vector128<float> SchedulePart2(Vector128<float> w0_3, Vector128<float> w8_11, Vector128<float> w12_15)
  359. {
  360. Vector128<float> result = new Vector128<float>();
  361. ulong T1 = VectorExtractIntZx(w12_15, (byte)1, 3);
  362. for (int e = 0; e <= 1; e++)
  363. {
  364. uint elt = T1.ULongPart(e);
  365. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  366. elt += (uint)VectorExtractIntZx(w0_3, (byte)e, 2);
  367. elt += (uint)VectorExtractIntZx(w8_11, (byte)(e + 1), 2);
  368. result = VectorInsertInt((ulong)elt, result, (byte)e, 2);
  369. }
  370. T1 = VectorExtractIntZx(result, (byte)0, 3);
  371. for (int e = 2; e <= 3; e++)
  372. {
  373. uint elt = T1.ULongPart(e - 2);
  374. elt = elt.Ror(17) ^ elt.Ror(19) ^ elt.Lsr(10);
  375. elt += (uint)VectorExtractIntZx(w0_3, (byte)e, 2);
  376. elt += (uint)VectorExtractIntZx(e == 2 ? w8_11 : w12_15, (byte)(e == 2 ? 3 : 0), 2);
  377. result = VectorInsertInt((ulong)elt, result, (byte)e, 2);
  378. }
  379. return result;
  380. }
  381. private static Vector128<float> SHA256hash(Vector128<float> X, Vector128<float> Y, Vector128<float> W, bool part1)
  382. {
  383. for (int e = 0; e <= 3; e++)
  384. {
  385. uint chs = SHAchoose((uint)VectorExtractIntZx(Y, (byte)0, 2),
  386. (uint)VectorExtractIntZx(Y, (byte)1, 2),
  387. (uint)VectorExtractIntZx(Y, (byte)2, 2));
  388. uint maj = SHAmajority((uint)VectorExtractIntZx(X, (byte)0, 2),
  389. (uint)VectorExtractIntZx(X, (byte)1, 2),
  390. (uint)VectorExtractIntZx(X, (byte)2, 2));
  391. uint t1 = (uint)VectorExtractIntZx(Y, (byte)3, 2);
  392. t1 += SHAhashSIGMA1((uint)VectorExtractIntZx(Y, (byte)0, 2)) + chs;
  393. t1 += (uint)VectorExtractIntZx(W, (byte)e, 2);
  394. uint t2 = t1 + (uint)VectorExtractIntZx(X, (byte)3, 2);
  395. X = VectorInsertInt((ulong)t2, X, (byte)3, 2);
  396. t2 = t1 + SHAhashSIGMA0((uint)VectorExtractIntZx(X, (byte)0, 2)) + maj;
  397. Y = VectorInsertInt((ulong)t2, Y, (byte)3, 2);
  398. Rol32_256(ref Y, ref X);
  399. }
  400. return part1 ? X : Y;
  401. }
  402. private static void Rol32_256(ref Vector128<float> Y, ref Vector128<float> X)
  403. {
  404. if (!Sse2.IsSupported)
  405. {
  406. throw new PlatformNotSupportedException();
  407. }
  408. uint yE3 = (uint)VectorExtractIntZx(Y, (byte)3, 2);
  409. uint xE3 = (uint)VectorExtractIntZx(X, (byte)3, 2);
  410. Y = Sse.StaticCast<uint, float>(Sse2.ShiftLeftLogical128BitLane(Sse.StaticCast<float, uint>(Y), (byte)4));
  411. X = Sse.StaticCast<uint, float>(Sse2.ShiftLeftLogical128BitLane(Sse.StaticCast<float, uint>(X), (byte)4));
  412. Y = VectorInsertInt((ulong)xE3, Y, (byte)0, 2);
  413. X = VectorInsertInt((ulong)yE3, X, (byte)0, 2);
  414. }
  415. private static uint SHAhashSIGMA0(uint x)
  416. {
  417. return x.Ror(2) ^ x.Ror(13) ^ x.Ror(22);
  418. }
  419. private static uint SHAhashSIGMA1(uint x)
  420. {
  421. return x.Ror(6) ^ x.Ror(11) ^ x.Ror(25);
  422. }
  423. private static uint SHAmajority(uint x, uint y, uint z)
  424. {
  425. return (x & y) | ((x | y) & z);
  426. }
  427. private static uint SHAchoose(uint x, uint y, uint z)
  428. {
  429. return ((y ^ z) & x) ^ z;
  430. }
  431. private static uint Ror(this uint value, int count)
  432. {
  433. return (value >> count) | (value << (32 - count));
  434. }
  435. private static uint Lsr(this uint value, int count)
  436. {
  437. return value >> count;
  438. }
  439. private static uint ULongPart(this ulong value, int part)
  440. {
  441. return part == 0
  442. ? (uint)(value & 0xFFFFFFFFUL)
  443. : (uint)(value >> 32);
  444. }
  445. #endregion
  446. #region "Reverse"
  447. public static uint ReverseBits8(uint Value)
  448. {
  449. Value = ((Value & 0xaa) >> 1) | ((Value & 0x55) << 1);
  450. Value = ((Value & 0xcc) >> 2) | ((Value & 0x33) << 2);
  451. return (Value >> 4) | ((Value & 0x0f) << 4);
  452. }
  453. public static uint ReverseBits32(uint Value)
  454. {
  455. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  456. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  457. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  458. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  459. return (Value >> 16) | (Value << 16);
  460. }
  461. public static ulong ReverseBits64(ulong Value)
  462. {
  463. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((Value & 0x5555555555555555) << 1 );
  464. Value = ((Value & 0xcccccccccccccccc) >> 2 ) | ((Value & 0x3333333333333333) << 2 );
  465. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4 );
  466. Value = ((Value & 0xff00ff00ff00ff00) >> 8 ) | ((Value & 0x00ff00ff00ff00ff) << 8 );
  467. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  468. return (Value >> 32) | (Value << 32);
  469. }
  470. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  471. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  472. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  473. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  474. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  475. private enum RevSize
  476. {
  477. Rev16,
  478. Rev32,
  479. Rev64
  480. }
  481. private static ulong ReverseBytes(ulong Value, RevSize Size)
  482. {
  483. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  484. if (Size == RevSize.Rev16)
  485. {
  486. return Value;
  487. }
  488. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  489. if (Size == RevSize.Rev32)
  490. {
  491. return Value;
  492. }
  493. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  494. if (Size == RevSize.Rev64)
  495. {
  496. return Value;
  497. }
  498. throw new ArgumentException(nameof(Size));
  499. }
  500. #endregion
  501. #region "MultiplyHigh"
  502. public static long SMulHi128(long LHS, long RHS)
  503. {
  504. long Result = (long)UMulHi128((ulong)LHS, (ulong)RHS);
  505. if (LHS < 0) Result -= RHS;
  506. if (RHS < 0) Result -= LHS;
  507. return Result;
  508. }
  509. public static ulong UMulHi128(ulong LHS, ulong RHS)
  510. {
  511. //long multiplication
  512. //multiply 32 bits at a time in 64 bit, the result is what's carried over 64 bits.
  513. ulong LHigh = LHS >> 32;
  514. ulong LLow = LHS & 0xFFFFFFFF;
  515. ulong RHigh = RHS >> 32;
  516. ulong RLow = RHS & 0xFFFFFFFF;
  517. ulong Z2 = LLow * RLow;
  518. ulong T = LHigh * RLow + (Z2 >> 32);
  519. ulong Z1 = T & 0xFFFFFFFF;
  520. ulong Z0 = T >> 32;
  521. Z1 += LLow * RHigh;
  522. return LHigh * RHigh + Z0 + (Z1 >> 32);
  523. }
  524. #endregion
  525. }
  526. }