ASoftFallback.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 Name64, string Name128)
  9. {
  10. bool IsSimd64 = Context.CurrOp.RegisterSize == ARegisterSize.SIMD64;
  11. Context.EmitCall(typeof(ASoftFallback), IsSimd64 ? Name64 : Name128);
  12. }
  13. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  14. {
  15. Context.EmitCall(typeof(ASoftFallback), MthdName);
  16. }
  17. public static uint CountLeadingZeros32(uint Value) => (uint)CountLeadingZeros(Value, 32);
  18. public static ulong CountLeadingZeros64(ulong Value) => (ulong)CountLeadingZeros(Value, 64);
  19. private static ulong CountLeadingZeros(ulong Value, int Size)
  20. {
  21. int HighBit = Size - 1;
  22. for (int Bit = HighBit; Bit >= 0; Bit--)
  23. {
  24. if (((Value >> Bit) & 1) != 0)
  25. {
  26. return (ulong)(HighBit - Bit);
  27. }
  28. }
  29. return (ulong)Size;
  30. }
  31. public static uint ReverseBits32(uint Value)
  32. {
  33. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  34. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  35. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  36. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  37. return (Value >> 16) | (Value << 16);
  38. }
  39. public static ulong ReverseBits64(ulong Value)
  40. {
  41. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1) | ((Value & 0x5555555555555555) << 1);
  42. Value = ((Value & 0xcccccccccccccccc) >> 2) | ((Value & 0x3333333333333333) << 2);
  43. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4);
  44. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  45. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  46. return (Value >> 32) | (Value << 32);
  47. }
  48. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  49. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  50. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  51. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  52. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  53. private enum RevSize
  54. {
  55. Rev16,
  56. Rev32,
  57. Rev64
  58. }
  59. private static ulong ReverseBytes(ulong Value, RevSize Size)
  60. {
  61. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  62. if (Size == RevSize.Rev16)
  63. {
  64. return Value;
  65. }
  66. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  67. if (Size == RevSize.Rev32)
  68. {
  69. return Value;
  70. }
  71. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  72. if (Size == RevSize.Rev64)
  73. {
  74. return Value;
  75. }
  76. throw new ArgumentException(nameof(Size));
  77. }
  78. public static int SatSingleToInt32(float Value, int FBits)
  79. {
  80. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  81. return Value > int.MaxValue ? int.MaxValue :
  82. Value < int.MinValue ? int.MinValue : (int)Value;
  83. }
  84. public static long SatSingleToInt64(float Value, int FBits)
  85. {
  86. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  87. return Value > long.MaxValue ? long.MaxValue :
  88. Value < long.MinValue ? long.MinValue : (long)Value;
  89. }
  90. public static uint SatSingleToUInt32(float Value, int FBits)
  91. {
  92. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  93. return Value > uint.MaxValue ? uint.MaxValue :
  94. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  95. }
  96. public static ulong SatSingleToUInt64(float Value, int FBits)
  97. {
  98. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  99. return Value > ulong.MaxValue ? ulong.MaxValue :
  100. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  101. }
  102. public static int SatDoubleToInt32(double Value, int FBits)
  103. {
  104. if (FBits != 0) Value *= Math.Pow(2, FBits);
  105. return Value > int.MaxValue ? int.MaxValue :
  106. Value < int.MinValue ? int.MinValue : (int)Value;
  107. }
  108. public static long SatDoubleToInt64(double Value, int FBits)
  109. {
  110. if (FBits != 0) Value *= Math.Pow(2, FBits);
  111. return Value > long.MaxValue ? long.MaxValue :
  112. Value < long.MinValue ? long.MinValue : (long)Value;
  113. }
  114. public static uint SatDoubleToUInt32(double Value, int FBits)
  115. {
  116. if (FBits != 0) Value *= Math.Pow(2, FBits);
  117. return Value > uint.MaxValue ? uint.MaxValue :
  118. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  119. }
  120. public static ulong SatDoubleToUInt64(double Value, int FBits)
  121. {
  122. if (FBits != 0) Value *= Math.Pow(2, FBits);
  123. return Value > ulong.MaxValue ? ulong.MaxValue :
  124. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  125. }
  126. public static float Int32ToSingle(int Value, int FBits)
  127. {
  128. float ValueF = Value;
  129. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  130. return ValueF;
  131. }
  132. public static float Int64ToSingle(long Value, int FBits)
  133. {
  134. float ValueF = Value;
  135. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  136. return ValueF;
  137. }
  138. public static float UInt32ToSingle(uint Value, int FBits)
  139. {
  140. float ValueF = Value;
  141. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  142. return ValueF;
  143. }
  144. public static float UInt64ToSingle(ulong Value, int FBits)
  145. {
  146. float ValueF = Value;
  147. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  148. return ValueF;
  149. }
  150. public static double Int32ToDouble(int Value, int FBits)
  151. {
  152. double ValueF = Value;
  153. if (FBits != 0) ValueF *= 1 / Math.Pow(2, FBits);
  154. return ValueF;
  155. }
  156. public static double Int64ToDouble(long Value, int FBits)
  157. {
  158. double ValueF = Value;
  159. if (FBits != 0) ValueF *= 1 / Math.Pow(2, FBits);
  160. return ValueF;
  161. }
  162. public static double UInt32ToDouble(uint Value, int FBits)
  163. {
  164. double ValueF = Value;
  165. if (FBits != 0) ValueF *= 1 / Math.Pow(2, FBits);
  166. return ValueF;
  167. }
  168. public static double UInt64ToDouble(ulong Value, int FBits)
  169. {
  170. double ValueF = Value;
  171. if (FBits != 0) ValueF *= 1 / Math.Pow(2, FBits);
  172. return ValueF;
  173. }
  174. public static ulong SMulHi128(ulong LHS, ulong RHS)
  175. {
  176. long LLo = (uint)(LHS >> 0);
  177. long LHi = (int)(LHS >> 32);
  178. long RLo = (uint)(RHS >> 0);
  179. long RHi = (int)(RHS >> 32);
  180. long LHiRHi = LHi * RHi;
  181. long LHiRLo = LHi * RLo;
  182. long LLoRHi = LLo * RHi;
  183. long LLoRLo = LLo * RLo;
  184. long Carry = ((uint)LHiRLo + ((uint)LLoRHi + (LLoRLo >> 32))) >> 32;
  185. long ResHi = LHiRHi + (LHiRLo >> 32) + (LLoRHi >> 32) + Carry;
  186. return (ulong)ResHi;
  187. }
  188. public static ulong UMulHi128(ulong LHS, ulong RHS)
  189. {
  190. ulong LLo = (uint)(LHS >> 0);
  191. ulong LHi = (uint)(LHS >> 32);
  192. ulong RLo = (uint)(RHS >> 0);
  193. ulong RHi = (uint)(RHS >> 32);
  194. ulong LHiRHi = LHi * RHi;
  195. ulong LHiRLo = LHi * RLo;
  196. ulong LLoRHi = LLo * RHi;
  197. ulong LLoRLo = LLo * RLo;
  198. ulong Carry = ((uint)LHiRLo + ((uint)LLoRHi + (LLoRLo >> 32))) >> 32;
  199. ulong ResHi = LHiRHi + (LHiRLo >> 32) + (LLoRHi >> 32) + Carry;
  200. return ResHi;
  201. }
  202. public static AVec Addp_S(AVec Vector, int Size)
  203. {
  204. ulong Low = ExtractVec(Vector, 0, Size);
  205. ulong High = ExtractVec(Vector, 1, Size);
  206. return InsertVec(new AVec(), 0, Size, Low + High);
  207. }
  208. public static int CountSetBits8(byte Value)
  209. {
  210. return (Value >> 0) & 1 + (Value >> 1) & 1 +
  211. (Value >> 2) & 1 + (Value >> 3) & 1 +
  212. (Value >> 4) & 1 + (Value >> 5) & 1 +
  213. (Value >> 6) & 1 + (Value >> 7);
  214. }
  215. public static AVec Dup_Gp64(ulong Value, int Size)
  216. {
  217. return Dup_Gp(Value, Size, 8);
  218. }
  219. public static AVec Dup_Gp128(ulong Value, int Size)
  220. {
  221. return Dup_Gp(Value, Size, 16);
  222. }
  223. private static AVec Dup_Gp(ulong Value, int Size, int Bytes)
  224. {
  225. AVec Res = new AVec();
  226. for (int Index = 0; Index < (Bytes >> Size); Index++)
  227. {
  228. Res = InsertVec(Res, Index, Size, Value);
  229. }
  230. return Res;
  231. }
  232. public static AVec Dup_S(AVec Vector, int Elem, int Size)
  233. {
  234. return InsertVec(new AVec(), 0, Size, ExtractVec(Vector, Elem, Size));
  235. }
  236. public static AVec Fmov_S(ulong Value, int Elem, int Size)
  237. {
  238. return InsertVec(new AVec(), Elem, Size, Value);
  239. }
  240. public static AVec Tbl1_V64(AVec Vector, AVec Tb0)
  241. {
  242. return Tbl(Vector, 8, Tb0);
  243. }
  244. public static AVec Tbl1_V128(AVec Vector, AVec Tb0)
  245. {
  246. return Tbl(Vector, 16, Tb0);
  247. }
  248. public static AVec Tbl2_V64(AVec Vector, AVec Tb0, AVec Tb1)
  249. {
  250. return Tbl(Vector, 8, Tb0, Tb1);
  251. }
  252. public static AVec Tbl2_V128(AVec Vector, AVec Tb0, AVec Tb1)
  253. {
  254. return Tbl(Vector, 16, Tb0, Tb1);
  255. }
  256. public static AVec Tbl3_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  257. {
  258. return Tbl(Vector, 8, Tb0, Tb1, Tb2);
  259. }
  260. public static AVec Tbl3_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  261. {
  262. return Tbl(Vector, 16, Tb0, Tb1, Tb2);
  263. }
  264. public static AVec Tbl4_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  265. {
  266. return Tbl(Vector, 8, Tb0, Tb1, Tb2, Tb3);
  267. }
  268. public static AVec Tbl4_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  269. {
  270. return Tbl(Vector, 16, Tb0, Tb1, Tb2, Tb3);
  271. }
  272. private static AVec Tbl(AVec Vector, int Bytes, params AVec[] Tb)
  273. {
  274. AVec Res = new AVec();
  275. byte[] Table = new byte[Tb.Length * 16];
  276. for (int Index = 0; Index < Tb.Length; Index++)
  277. for (int Index2 = 0; Index2 < 16; Index2++)
  278. {
  279. Table[Index * 16 + Index2] = (byte)ExtractVec(Tb[Index], Index2, 0);
  280. }
  281. for (int Index = 0; Index < Bytes; Index++)
  282. {
  283. byte TblIdx = (byte)ExtractVec(Vector, Index, 0);
  284. if (TblIdx < Table.Length)
  285. {
  286. Res = InsertVec(Res, Index, 0, Table[TblIdx]);
  287. }
  288. }
  289. return Res;
  290. }
  291. public static ulong ExtractVec(AVec Vector, int Index, int Size)
  292. {
  293. switch (Size)
  294. {
  295. case 0: return Vector.ExtractByte(Index);
  296. case 1: return Vector.ExtractUInt16(Index);
  297. case 2: return Vector.ExtractUInt32(Index);
  298. case 3: return Vector.ExtractUInt64(Index);
  299. }
  300. throw new ArgumentOutOfRangeException(nameof(Size));
  301. }
  302. public static long ExtractSVec(AVec Vector, int Index, int Size)
  303. {
  304. switch (Size)
  305. {
  306. case 0: return (sbyte)Vector.ExtractByte(Index);
  307. case 1: return (short)Vector.ExtractUInt16(Index);
  308. case 2: return (int)Vector.ExtractUInt32(Index);
  309. case 3: return (long)Vector.ExtractUInt64(Index);
  310. }
  311. throw new ArgumentOutOfRangeException(nameof(Size));
  312. }
  313. public static float VectorExtractSingle(AVec Vector, int Index)
  314. {
  315. return Vector.ExtractSingle(Index);
  316. }
  317. public static double VectorExtractDouble(AVec Vector, int Index)
  318. {
  319. return Vector.ExtractDouble(Index);
  320. }
  321. public static AVec VectorInsertSingle(float Value, AVec Vector, int Index)
  322. {
  323. return AVec.InsertSingle(Vector, Index, Value);
  324. }
  325. public static AVec VectorInsertDouble(double Value, AVec Vector, int Index)
  326. {
  327. return AVec.InsertDouble(Vector, Index, Value);
  328. }
  329. public static AVec VectorInsertInt(ulong Value, AVec Vector, int Index, int Size)
  330. {
  331. switch (Size)
  332. {
  333. case 0: return AVec.InsertByte (Vector, Index, (byte)Value);
  334. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  335. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  336. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  337. }
  338. throw new ArgumentOutOfRangeException(nameof(Size));
  339. }
  340. public static AVec InsertVec(AVec Vector, int Index, int Size, ulong Value)
  341. {
  342. switch (Size)
  343. {
  344. case 0: return AVec.InsertByte(Vector, Index, (byte)Value);
  345. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  346. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  347. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  348. }
  349. throw new ArgumentOutOfRangeException(nameof(Size));
  350. }
  351. public static AVec InsertSVec(AVec Vector, int Index, int Size, long Value)
  352. {
  353. switch (Size)
  354. {
  355. case 0: return AVec.InsertByte(Vector, Index, (byte)Value);
  356. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  357. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  358. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  359. }
  360. throw new ArgumentOutOfRangeException(nameof(Size));
  361. }
  362. }
  363. }