AInstEmitSimdMove.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. Context.EmitStintzr(Op.Rd);
  63. }
  64. public static void Fmov_Ftoi1(AILEmitterCtx Context)
  65. {
  66. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  67. EmitVectorExtractZx(Context, Op.Rn, 1, 3);
  68. Context.EmitStintzr(Op.Rd);
  69. }
  70. public static void Fmov_Itof(AILEmitterCtx Context)
  71. {
  72. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  73. Context.EmitLdintzr(Op.Rn);
  74. EmitScalarSet(Context, Op.Rd, 3);
  75. }
  76. public static void Fmov_Itof1(AILEmitterCtx Context)
  77. {
  78. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  79. Context.EmitLdintzr(Op.Rn);
  80. EmitVectorInsert(Context, Op.Rd, 1, 3);
  81. }
  82. public static void Fmov_S(AILEmitterCtx Context)
  83. {
  84. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  85. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  86. EmitScalarSetF(Context, Op.Rd, Op.Size);
  87. }
  88. public static void Fmov_Si(AILEmitterCtx Context)
  89. {
  90. AOpCodeSimdFmov Op = (AOpCodeSimdFmov)Context.CurrOp;
  91. Context.EmitLdc_I8(Op.Imm);
  92. EmitScalarSet(Context, Op.Rd, Op.Size + 2);
  93. }
  94. public static void Fmov_V(AILEmitterCtx Context)
  95. {
  96. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  97. for (int Index = 0; Index < (4 >> Op.Size); Index++)
  98. {
  99. Context.EmitLdc_I8(Op.Imm);
  100. EmitVectorInsert(Context, Op.Rd, Index, Op.Size + 2);
  101. }
  102. }
  103. public static void Ins_Gp(AILEmitterCtx Context)
  104. {
  105. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  106. Context.EmitLdintzr(Op.Rn);
  107. EmitVectorInsert(Context, Op.Rd, Op.DstIndex, Op.Size);
  108. }
  109. public static void Ins_V(AILEmitterCtx Context)
  110. {
  111. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  112. EmitVectorExtractZx(Context, Op.Rn, Op.SrcIndex, Op.Size);
  113. EmitVectorInsert(Context, Op.Rd, Op.DstIndex, Op.Size);
  114. }
  115. public static void Movi_V(AILEmitterCtx Context)
  116. {
  117. EmitVectorImmUnaryOp(Context, () => { });
  118. }
  119. public static void Mvni_V(AILEmitterCtx Context)
  120. {
  121. EmitVectorImmUnaryOp(Context, () => Context.Emit(OpCodes.Not));
  122. }
  123. public static void Tbl_V(AILEmitterCtx Context)
  124. {
  125. AOpCodeSimdTbl Op = (AOpCodeSimdTbl)Context.CurrOp;
  126. Context.EmitLdvec(Op.Rm);
  127. for (int Index = 0; Index < Op.Size; Index++)
  128. {
  129. Context.EmitLdvec((Op.Rn + Index) & 0x1f);
  130. }
  131. switch (Op.Size)
  132. {
  133. case 1: ASoftFallback.EmitCall(Context,
  134. nameof(ASoftFallback.Tbl1_V64),
  135. nameof(ASoftFallback.Tbl1_V128)); break;
  136. case 2: ASoftFallback.EmitCall(Context,
  137. nameof(ASoftFallback.Tbl2_V64),
  138. nameof(ASoftFallback.Tbl2_V128)); break;
  139. case 3: ASoftFallback.EmitCall(Context,
  140. nameof(ASoftFallback.Tbl3_V64),
  141. nameof(ASoftFallback.Tbl3_V128)); break;
  142. case 4: ASoftFallback.EmitCall(Context,
  143. nameof(ASoftFallback.Tbl4_V64),
  144. nameof(ASoftFallback.Tbl4_V128)); break;
  145. default: throw new InvalidOperationException();
  146. }
  147. Context.EmitStvec(Op.Rd);
  148. }
  149. public static void Umov_S(AILEmitterCtx Context)
  150. {
  151. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  152. EmitVectorExtractZx(Context, Op.Rn, Op.DstIndex, Op.Size);
  153. Context.EmitStintzr(Op.Rd);
  154. }
  155. public static void Uzp1_V(AILEmitterCtx Context)
  156. {
  157. EmitVectorUnzip(Context, Part: 0);
  158. }
  159. public static void Uzp2_V(AILEmitterCtx Context)
  160. {
  161. EmitVectorUnzip(Context, Part: 1);
  162. }
  163. public static void Xtn_V(AILEmitterCtx Context)
  164. {
  165. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  166. int Elems = 8 >> Op.Size;
  167. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  168. for (int Index = 0; Index < Elems; Index++)
  169. {
  170. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size + 1);
  171. EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
  172. }
  173. if (Part == 0)
  174. {
  175. EmitVectorZeroUpper(Context, Op.Rd);
  176. }
  177. }
  178. private static void EmitVectorUnzip(AILEmitterCtx Context, int Part)
  179. {
  180. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  181. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  182. int Elems = Bytes >> Op.Size;
  183. int Half = Elems >> 1;
  184. for (int Index = 0; Index < Elems; Index++)
  185. {
  186. int Elem = Part + ((Index & (Half - 1)) << 1);
  187. EmitVectorExtractZx(Context, Index < Half ? Op.Rn : Op.Rm, Elem, Op.Size);
  188. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  189. }
  190. if (Op.RegisterSize == ARegisterSize.SIMD64)
  191. {
  192. EmitVectorZeroUpper(Context, Op.Rd);
  193. }
  194. }
  195. }
  196. }