AInstEmitSimdMove.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. using static ChocolArm64.Instruction.AInstEmitSimdHelper;
  7. namespace ChocolArm64.Instruction
  8. {
  9. static partial class AInstEmit
  10. {
  11. public static void Dup_Gp(AILEmitterCtx Context)
  12. {
  13. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  14. int Bytes = Op.GetBitsCount() >> 3;
  15. int Elems = Bytes >> Op.Size;
  16. for (int Index = 0; Index < Elems; Index++)
  17. {
  18. Context.EmitLdintzr(Op.Rn);
  19. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  20. }
  21. if (Op.RegisterSize == ARegisterSize.SIMD64)
  22. {
  23. EmitVectorZeroUpper(Context, Op.Rd);
  24. }
  25. }
  26. public static void Dup_S(AILEmitterCtx Context)
  27. {
  28. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  29. EmitVectorExtractZx(Context, Op.Rn, Op.DstIndex, Op.Size);
  30. EmitScalarSet(Context, Op.Rd, Op.Size);
  31. }
  32. public static void Dup_V(AILEmitterCtx Context)
  33. {
  34. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  35. int Bytes = Op.GetBitsCount() >> 3;
  36. int Elems = Bytes >> Op.Size;
  37. for (int Index = 0; Index < Elems; Index++)
  38. {
  39. EmitVectorExtractZx(Context, Op.Rn, Op.DstIndex, Op.Size);
  40. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  41. }
  42. if (Op.RegisterSize == ARegisterSize.SIMD64)
  43. {
  44. EmitVectorZeroUpper(Context, Op.Rd);
  45. }
  46. }
  47. public static void Ext_V(AILEmitterCtx Context)
  48. {
  49. AOpCodeSimdExt Op = (AOpCodeSimdExt)Context.CurrOp;
  50. Context.EmitLdvec(Op.Rd);
  51. Context.EmitStvectmp();
  52. int Bytes = Op.GetBitsCount() >> 3;
  53. int Position = Op.Imm4;
  54. for (int Index = 0; Index < Bytes; Index++)
  55. {
  56. int Reg = Op.Imm4 + Index < Bytes ? Op.Rn : Op.Rm;
  57. if (Position == Bytes)
  58. {
  59. Position = 0;
  60. }
  61. EmitVectorExtractZx(Context, Reg, Position++, 0);
  62. EmitVectorInsertTmp(Context, Index, 0);
  63. }
  64. Context.EmitLdvectmp();
  65. Context.EmitStvec(Op.Rd);
  66. if (Op.RegisterSize == ARegisterSize.SIMD64)
  67. {
  68. EmitVectorZeroUpper(Context, Op.Rd);
  69. }
  70. }
  71. public static void Fcsel_S(AILEmitterCtx Context)
  72. {
  73. AOpCodeSimdFcond Op = (AOpCodeSimdFcond)Context.CurrOp;
  74. AILLabel LblTrue = new AILLabel();
  75. AILLabel LblEnd = new AILLabel();
  76. Context.EmitCondBranch(LblTrue, Op.Cond);
  77. EmitVectorExtractF(Context, Op.Rm, 0, Op.Size);
  78. Context.Emit(OpCodes.Br_S, LblEnd);
  79. Context.MarkLabel(LblTrue);
  80. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  81. Context.MarkLabel(LblEnd);
  82. EmitScalarSetF(Context, Op.Rd, Op.Size);
  83. }
  84. public static void Fmov_Ftoi(AILEmitterCtx Context)
  85. {
  86. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  87. EmitVectorExtractZx(Context, Op.Rn, 0, 3);
  88. EmitIntZeroUpperIfNeeded(Context);
  89. Context.EmitStintzr(Op.Rd);
  90. }
  91. public static void Fmov_Ftoi1(AILEmitterCtx Context)
  92. {
  93. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  94. EmitVectorExtractZx(Context, Op.Rn, 1, 3);
  95. EmitIntZeroUpperIfNeeded(Context);
  96. Context.EmitStintzr(Op.Rd);
  97. }
  98. public static void Fmov_Itof(AILEmitterCtx Context)
  99. {
  100. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  101. Context.EmitLdintzr(Op.Rn);
  102. EmitIntZeroUpperIfNeeded(Context);
  103. EmitScalarSet(Context, Op.Rd, 3);
  104. }
  105. public static void Fmov_Itof1(AILEmitterCtx Context)
  106. {
  107. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  108. Context.EmitLdintzr(Op.Rn);
  109. EmitIntZeroUpperIfNeeded(Context);
  110. EmitVectorInsert(Context, Op.Rd, 1, 3);
  111. }
  112. public static void Fmov_S(AILEmitterCtx Context)
  113. {
  114. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  115. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  116. EmitScalarSetF(Context, Op.Rd, Op.Size);
  117. }
  118. public static void Fmov_Si(AILEmitterCtx Context)
  119. {
  120. AOpCodeSimdFmov Op = (AOpCodeSimdFmov)Context.CurrOp;
  121. Context.EmitLdc_I8(Op.Imm);
  122. EmitScalarSet(Context, Op.Rd, Op.Size + 2);
  123. }
  124. public static void Fmov_V(AILEmitterCtx Context)
  125. {
  126. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  127. int Elems = Op.RegisterSize == ARegisterSize.SIMD128 ? 4 : 2;
  128. for (int Index = 0; Index < (Elems >> Op.Size); Index++)
  129. {
  130. Context.EmitLdc_I8(Op.Imm);
  131. EmitVectorInsert(Context, Op.Rd, Index, Op.Size + 2);
  132. }
  133. if (Op.RegisterSize == ARegisterSize.SIMD64)
  134. {
  135. EmitVectorZeroUpper(Context, Op.Rd);
  136. }
  137. }
  138. public static void Ins_Gp(AILEmitterCtx Context)
  139. {
  140. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  141. Context.EmitLdintzr(Op.Rn);
  142. EmitVectorInsert(Context, Op.Rd, Op.DstIndex, Op.Size);
  143. }
  144. public static void Ins_V(AILEmitterCtx Context)
  145. {
  146. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  147. EmitVectorExtractZx(Context, Op.Rn, Op.SrcIndex, Op.Size);
  148. EmitVectorInsert(Context, Op.Rd, Op.DstIndex, Op.Size);
  149. }
  150. public static void Movi_V(AILEmitterCtx Context)
  151. {
  152. EmitVectorImmUnaryOp(Context, () => { });
  153. }
  154. public static void Mvni_V(AILEmitterCtx Context)
  155. {
  156. EmitVectorImmUnaryOp(Context, () => Context.Emit(OpCodes.Not));
  157. }
  158. public static void Tbl_V(AILEmitterCtx Context)
  159. {
  160. AOpCodeSimdTbl Op = (AOpCodeSimdTbl)Context.CurrOp;
  161. Context.EmitLdvec(Op.Rm);
  162. for (int Index = 0; Index < Op.Size; Index++)
  163. {
  164. Context.EmitLdvec((Op.Rn + Index) & 0x1f);
  165. }
  166. switch (Op.Size)
  167. {
  168. case 1: AVectorHelper.EmitCall(Context,
  169. nameof(AVectorHelper.Tbl1_V64),
  170. nameof(AVectorHelper.Tbl1_V128)); break;
  171. case 2: AVectorHelper.EmitCall(Context,
  172. nameof(AVectorHelper.Tbl2_V64),
  173. nameof(AVectorHelper.Tbl2_V128)); break;
  174. case 3: AVectorHelper.EmitCall(Context,
  175. nameof(AVectorHelper.Tbl3_V64),
  176. nameof(AVectorHelper.Tbl3_V128)); break;
  177. case 4: AVectorHelper.EmitCall(Context,
  178. nameof(AVectorHelper.Tbl4_V64),
  179. nameof(AVectorHelper.Tbl4_V128)); break;
  180. default: throw new InvalidOperationException();
  181. }
  182. Context.EmitStvec(Op.Rd);
  183. }
  184. public static void Trn1_V(AILEmitterCtx Context)
  185. {
  186. EmitVectorTranspose(Context, Part: 0);
  187. }
  188. public static void Trn2_V(AILEmitterCtx Context)
  189. {
  190. EmitVectorTranspose(Context, Part: 1);
  191. }
  192. public static void Umov_S(AILEmitterCtx Context)
  193. {
  194. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  195. EmitVectorExtractZx(Context, Op.Rn, Op.DstIndex, Op.Size);
  196. Context.EmitStintzr(Op.Rd);
  197. }
  198. public static void Uzp1_V(AILEmitterCtx Context)
  199. {
  200. EmitVectorUnzip(Context, Part: 0);
  201. }
  202. public static void Uzp2_V(AILEmitterCtx Context)
  203. {
  204. EmitVectorUnzip(Context, Part: 1);
  205. }
  206. public static void Xtn_V(AILEmitterCtx Context)
  207. {
  208. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  209. int Elems = 8 >> Op.Size;
  210. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  211. for (int Index = 0; Index < Elems; Index++)
  212. {
  213. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size + 1);
  214. EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
  215. }
  216. if (Part == 0)
  217. {
  218. EmitVectorZeroUpper(Context, Op.Rd);
  219. }
  220. }
  221. public static void Zip1_V(AILEmitterCtx Context)
  222. {
  223. EmitVectorZip(Context, Part: 0);
  224. }
  225. public static void Zip2_V(AILEmitterCtx Context)
  226. {
  227. EmitVectorZip(Context, Part: 1);
  228. }
  229. private static void EmitIntZeroUpperIfNeeded(AILEmitterCtx Context)
  230. {
  231. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  232. {
  233. Context.Emit(OpCodes.Conv_U4);
  234. Context.Emit(OpCodes.Conv_U8);
  235. }
  236. }
  237. private static void EmitVectorTranspose(AILEmitterCtx Context, int Part)
  238. {
  239. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  240. int Bytes = Op.GetBitsCount() >> 3;
  241. int Elems = Bytes >> Op.Size;
  242. for (int Index = 0; Index < Elems; Index++)
  243. {
  244. int Elem = (Index & ~1) + Part;
  245. EmitVectorExtractZx(Context, (Index & 1) == 0 ? Op.Rn : Op.Rm, Elem, Op.Size);
  246. EmitVectorInsertTmp(Context, Index, Op.Size);
  247. }
  248. Context.EmitLdvectmp();
  249. Context.EmitStvec(Op.Rd);
  250. if (Op.RegisterSize == ARegisterSize.SIMD64)
  251. {
  252. EmitVectorZeroUpper(Context, Op.Rd);
  253. }
  254. }
  255. private static void EmitVectorUnzip(AILEmitterCtx Context, int Part)
  256. {
  257. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  258. int Bytes = Op.GetBitsCount() >> 3;
  259. int Elems = Bytes >> Op.Size;
  260. int Half = Elems >> 1;
  261. for (int Index = 0; Index < Elems; Index++)
  262. {
  263. int Elem = Part + ((Index & (Half - 1)) << 1);
  264. EmitVectorExtractZx(Context, Index < Half ? Op.Rn : Op.Rm, Elem, Op.Size);
  265. EmitVectorInsertTmp(Context, Index, Op.Size);
  266. }
  267. Context.EmitLdvectmp();
  268. Context.EmitStvec(Op.Rd);
  269. if (Op.RegisterSize == ARegisterSize.SIMD64)
  270. {
  271. EmitVectorZeroUpper(Context, Op.Rd);
  272. }
  273. }
  274. private static void EmitVectorZip(AILEmitterCtx Context, int Part)
  275. {
  276. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  277. int Bytes = Op.GetBitsCount() >> 3;
  278. int Elems = Bytes >> Op.Size;
  279. int Half = Elems >> 1;
  280. for (int Index = 0; Index < Elems; Index++)
  281. {
  282. int Elem = Part * Half + (Index >> 1);
  283. EmitVectorExtractZx(Context, (Index & 1) == 0 ? Op.Rn : Op.Rm, Elem, Op.Size);
  284. EmitVectorInsertTmp(Context, Index, Op.Size);
  285. }
  286. Context.EmitLdvectmp();
  287. Context.EmitStvec(Op.Rd);
  288. if (Op.RegisterSize == ARegisterSize.SIMD64)
  289. {
  290. EmitVectorZeroUpper(Context, Op.Rd);
  291. }
  292. }
  293. }
  294. }