ASoftFallback.cs 14 KB

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