AInstEmitSimdMove.cs 10 KB

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