ASoftFallback.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using ChocolArm64.State;
  2. using ChocolArm64.Translation;
  3. using System;
  4. namespace ChocolArm64.Instruction
  5. {
  6. static class ASoftFallback
  7. {
  8. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  9. {
  10. Context.EmitCall(typeof(ASoftFallback), MthdName);
  11. }
  12. #region "Saturating"
  13. public static long SignedSrcSignedDstSatQ(long op, int Size, AThreadState State)
  14. {
  15. int ESize = 8 << Size;
  16. long TMaxValue = (1L << (ESize - 1)) - 1L;
  17. long TMinValue = -(1L << (ESize - 1));
  18. if (op > TMaxValue)
  19. {
  20. SetFpsrQCFlag(State);
  21. return TMaxValue;
  22. }
  23. else if (op < TMinValue)
  24. {
  25. SetFpsrQCFlag(State);
  26. return TMinValue;
  27. }
  28. else
  29. {
  30. return op;
  31. }
  32. }
  33. public static ulong SignedSrcUnsignedDstSatQ(long op, int Size, AThreadState State)
  34. {
  35. int ESize = 8 << Size;
  36. ulong TMaxValue = (1UL << ESize) - 1UL;
  37. ulong TMinValue = 0UL;
  38. if (op > (long)TMaxValue)
  39. {
  40. SetFpsrQCFlag(State);
  41. return TMaxValue;
  42. }
  43. else if (op < (long)TMinValue)
  44. {
  45. SetFpsrQCFlag(State);
  46. return TMinValue;
  47. }
  48. else
  49. {
  50. return (ulong)op;
  51. }
  52. }
  53. public static long UnsignedSrcSignedDstSatQ(ulong op, int Size, AThreadState State)
  54. {
  55. int ESize = 8 << Size;
  56. long TMaxValue = (1L << (ESize - 1)) - 1L;
  57. if (op > (ulong)TMaxValue)
  58. {
  59. SetFpsrQCFlag(State);
  60. return TMaxValue;
  61. }
  62. else
  63. {
  64. return (long)op;
  65. }
  66. }
  67. public static ulong UnsignedSrcUnsignedDstSatQ(ulong op, int Size, AThreadState State)
  68. {
  69. int ESize = 8 << Size;
  70. ulong TMaxValue = (1UL << ESize) - 1UL;
  71. if (op > TMaxValue)
  72. {
  73. SetFpsrQCFlag(State);
  74. return TMaxValue;
  75. }
  76. else
  77. {
  78. return op;
  79. }
  80. }
  81. public static long UnarySignedSatQAbsOrNeg(long op, AThreadState State)
  82. {
  83. if (op == long.MinValue)
  84. {
  85. SetFpsrQCFlag(State);
  86. return long.MaxValue;
  87. }
  88. else
  89. {
  90. return op;
  91. }
  92. }
  93. public static long BinarySignedSatQAdd(long op1, long op2, AThreadState State)
  94. {
  95. long Add = op1 + op2;
  96. if ((~(op1 ^ op2) & (op1 ^ Add)) < 0L)
  97. {
  98. SetFpsrQCFlag(State);
  99. if (op1 < 0L)
  100. {
  101. return long.MinValue;
  102. }
  103. else
  104. {
  105. return long.MaxValue;
  106. }
  107. }
  108. else
  109. {
  110. return Add;
  111. }
  112. }
  113. public static ulong BinaryUnsignedSatQAdd(ulong op1, ulong op2, AThreadState State)
  114. {
  115. ulong Add = op1 + op2;
  116. if ((Add < op1) && (Add < op2))
  117. {
  118. SetFpsrQCFlag(State);
  119. return ulong.MaxValue;
  120. }
  121. else
  122. {
  123. return Add;
  124. }
  125. }
  126. public static long BinarySignedSatQSub(long op1, long op2, AThreadState State)
  127. {
  128. long Sub = op1 - op2;
  129. if (((op1 ^ op2) & (op1 ^ Sub)) < 0L)
  130. {
  131. SetFpsrQCFlag(State);
  132. if (op1 < 0L)
  133. {
  134. return long.MinValue;
  135. }
  136. else
  137. {
  138. return long.MaxValue;
  139. }
  140. }
  141. else
  142. {
  143. return Sub;
  144. }
  145. }
  146. public static ulong BinaryUnsignedSatQSub(ulong op1, ulong op2, AThreadState State)
  147. {
  148. ulong Sub = op1 - op2;
  149. if (op1 < op2)
  150. {
  151. SetFpsrQCFlag(State);
  152. return ulong.MinValue;
  153. }
  154. else
  155. {
  156. return Sub;
  157. }
  158. }
  159. public static long BinarySignedSatQAcc(ulong op1, long op2, AThreadState State)
  160. {
  161. if (op1 <= (ulong)long.MaxValue)
  162. {
  163. // op1 from ulong.MinValue to (ulong)long.MaxValue
  164. // op2 from long.MinValue to long.MaxValue
  165. long Add = (long)op1 + op2;
  166. if ((~op2 & Add) < 0L)
  167. {
  168. SetFpsrQCFlag(State);
  169. return long.MaxValue;
  170. }
  171. else
  172. {
  173. return Add;
  174. }
  175. }
  176. else if (op2 >= 0L)
  177. {
  178. // op1 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  179. // op2 from (long)ulong.MinValue to long.MaxValue
  180. SetFpsrQCFlag(State);
  181. return long.MaxValue;
  182. }
  183. else
  184. {
  185. // op1 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  186. // op2 from long.MinValue to (long)ulong.MinValue - 1L
  187. ulong Add = op1 + (ulong)op2;
  188. if (Add > (ulong)long.MaxValue)
  189. {
  190. SetFpsrQCFlag(State);
  191. return long.MaxValue;
  192. }
  193. else
  194. {
  195. return (long)Add;
  196. }
  197. }
  198. }
  199. public static ulong BinaryUnsignedSatQAcc(long op1, ulong op2, AThreadState State)
  200. {
  201. if (op1 >= 0L)
  202. {
  203. // op1 from (long)ulong.MinValue to long.MaxValue
  204. // op2 from ulong.MinValue to ulong.MaxValue
  205. ulong Add = (ulong)op1 + op2;
  206. if ((Add < (ulong)op1) && (Add < op2))
  207. {
  208. SetFpsrQCFlag(State);
  209. return ulong.MaxValue;
  210. }
  211. else
  212. {
  213. return Add;
  214. }
  215. }
  216. else if (op2 > (ulong)long.MaxValue)
  217. {
  218. // op1 from long.MinValue to (long)ulong.MinValue - 1L
  219. // op2 from (ulong)long.MaxValue + 1UL to ulong.MaxValue
  220. return (ulong)op1 + op2;
  221. }
  222. else
  223. {
  224. // op1 from long.MinValue to (long)ulong.MinValue - 1L
  225. // op2 from ulong.MinValue to (ulong)long.MaxValue
  226. long Add = op1 + (long)op2;
  227. if (Add < (long)ulong.MinValue)
  228. {
  229. SetFpsrQCFlag(State);
  230. return ulong.MinValue;
  231. }
  232. else
  233. {
  234. return (ulong)Add;
  235. }
  236. }
  237. }
  238. private static void SetFpsrQCFlag(AThreadState State)
  239. {
  240. const int QCFlagBit = 27;
  241. State.Fpsr |= 1 << QCFlagBit;
  242. }
  243. #endregion
  244. #region "Count"
  245. public static ulong CountLeadingSigns(ulong Value, int Size)
  246. {
  247. Value ^= Value >> 1;
  248. int HighBit = Size - 2;
  249. for (int Bit = HighBit; Bit >= 0; Bit--)
  250. {
  251. if (((Value >> Bit) & 0b1) != 0)
  252. {
  253. return (ulong)(HighBit - Bit);
  254. }
  255. }
  256. return (ulong)(Size - 1);
  257. }
  258. private static readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
  259. public static ulong CountLeadingZeros(ulong Value, int Size)
  260. {
  261. if (Value == 0)
  262. {
  263. return (ulong)Size;
  264. }
  265. int NibbleIdx = Size;
  266. int PreCount, Count = 0;
  267. do
  268. {
  269. NibbleIdx -= 4;
  270. PreCount = ClzNibbleTbl[(Value >> NibbleIdx) & 0b1111];
  271. Count += PreCount;
  272. }
  273. while (PreCount == 4);
  274. return (ulong)Count;
  275. }
  276. public static uint CountSetBits8(uint Value)
  277. {
  278. Value = ((Value >> 1) & 0x55) + (Value & 0x55);
  279. Value = ((Value >> 2) & 0x33) + (Value & 0x33);
  280. return (Value >> 4) + (Value & 0x0f);
  281. }
  282. #endregion
  283. #region "Crc32"
  284. private const uint Crc32RevPoly = 0xedb88320;
  285. private const uint Crc32cRevPoly = 0x82f63b78;
  286. public static uint Crc32b(uint Crc, byte Val) => Crc32 (Crc, Crc32RevPoly, Val);
  287. public static uint Crc32h(uint Crc, ushort Val) => Crc32h(Crc, Crc32RevPoly, Val);
  288. public static uint Crc32w(uint Crc, uint Val) => Crc32w(Crc, Crc32RevPoly, Val);
  289. public static uint Crc32x(uint Crc, ulong Val) => Crc32x(Crc, Crc32RevPoly, Val);
  290. public static uint Crc32cb(uint Crc, byte Val) => Crc32 (Crc, Crc32cRevPoly, Val);
  291. public static uint Crc32ch(uint Crc, ushort Val) => Crc32h(Crc, Crc32cRevPoly, Val);
  292. public static uint Crc32cw(uint Crc, uint Val) => Crc32w(Crc, Crc32cRevPoly, Val);
  293. public static uint Crc32cx(uint Crc, ulong Val) => Crc32x(Crc, Crc32cRevPoly, Val);
  294. private static uint Crc32h(uint Crc, uint Poly, ushort Val)
  295. {
  296. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  297. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  298. return Crc;
  299. }
  300. private static uint Crc32w(uint Crc, uint Poly, uint Val)
  301. {
  302. Crc = Crc32(Crc, Poly, (byte)(Val >> 0 ));
  303. Crc = Crc32(Crc, Poly, (byte)(Val >> 8 ));
  304. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  305. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  306. return Crc;
  307. }
  308. private static uint Crc32x(uint Crc, uint Poly, ulong Val)
  309. {
  310. Crc = Crc32(Crc, Poly, (byte)(Val >> 0 ));
  311. Crc = Crc32(Crc, Poly, (byte)(Val >> 8 ));
  312. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  313. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  314. Crc = Crc32(Crc, Poly, (byte)(Val >> 32));
  315. Crc = Crc32(Crc, Poly, (byte)(Val >> 40));
  316. Crc = Crc32(Crc, Poly, (byte)(Val >> 48));
  317. Crc = Crc32(Crc, Poly, (byte)(Val >> 56));
  318. return Crc;
  319. }
  320. private static uint Crc32(uint Crc, uint Poly, byte Val)
  321. {
  322. Crc ^= Val;
  323. for (int Bit = 7; Bit >= 0; Bit--)
  324. {
  325. uint Mask = (uint)(-(int)(Crc & 1));
  326. Crc = (Crc >> 1) ^ (Poly & Mask);
  327. }
  328. return Crc;
  329. }
  330. #endregion
  331. #region "Reverse"
  332. public static uint ReverseBits8(uint Value)
  333. {
  334. Value = ((Value & 0xaa) >> 1) | ((Value & 0x55) << 1);
  335. Value = ((Value & 0xcc) >> 2) | ((Value & 0x33) << 2);
  336. return (Value >> 4) | ((Value & 0x0f) << 4);
  337. }
  338. public static uint ReverseBits32(uint Value)
  339. {
  340. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  341. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  342. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  343. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  344. return (Value >> 16) | (Value << 16);
  345. }
  346. public static ulong ReverseBits64(ulong Value)
  347. {
  348. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((Value & 0x5555555555555555) << 1 );
  349. Value = ((Value & 0xcccccccccccccccc) >> 2 ) | ((Value & 0x3333333333333333) << 2 );
  350. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4 );
  351. Value = ((Value & 0xff00ff00ff00ff00) >> 8 ) | ((Value & 0x00ff00ff00ff00ff) << 8 );
  352. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  353. return (Value >> 32) | (Value << 32);
  354. }
  355. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  356. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  357. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  358. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  359. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  360. private enum RevSize
  361. {
  362. Rev16,
  363. Rev32,
  364. Rev64
  365. }
  366. private static ulong ReverseBytes(ulong Value, RevSize Size)
  367. {
  368. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  369. if (Size == RevSize.Rev16)
  370. {
  371. return Value;
  372. }
  373. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  374. if (Size == RevSize.Rev32)
  375. {
  376. return Value;
  377. }
  378. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  379. if (Size == RevSize.Rev64)
  380. {
  381. return Value;
  382. }
  383. throw new ArgumentException(nameof(Size));
  384. }
  385. #endregion
  386. #region "MultiplyHigh"
  387. public static long SMulHi128(long LHS, long RHS)
  388. {
  389. long Result = (long)UMulHi128((ulong)LHS, (ulong)RHS);
  390. if (LHS < 0) Result -= RHS;
  391. if (RHS < 0) Result -= LHS;
  392. return Result;
  393. }
  394. public static ulong UMulHi128(ulong LHS, ulong RHS)
  395. {
  396. //long multiplication
  397. //multiply 32 bits at a time in 64 bit, the result is what's carried over 64 bits.
  398. ulong LHigh = LHS >> 32;
  399. ulong LLow = LHS & 0xFFFFFFFF;
  400. ulong RHigh = RHS >> 32;
  401. ulong RLow = RHS & 0xFFFFFFFF;
  402. ulong Z2 = LLow * RLow;
  403. ulong T = LHigh * RLow + (Z2 >> 32);
  404. ulong Z1 = T & 0xFFFFFFFF;
  405. ulong Z0 = T >> 32;
  406. Z1 += LLow * RHigh;
  407. return LHigh * RHigh + Z0 + (Z1 >> 32);
  408. }
  409. #endregion
  410. }
  411. }