AInstEmitSimdMove.cs 9.4 KB

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