AInstEmitSimdShift.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 Shl_S(AILEmitterCtx Context)
  12. {
  13. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  14. EmitVectorExtractZx(Context, Op.Rn, 0, Op.Size);
  15. Context.EmitLdc_I4(GetImmShl(Op));
  16. Context.Emit(OpCodes.Shl);
  17. EmitScalarSet(Context, Op.Rd, Op.Size);
  18. }
  19. public static void Shl_V(AILEmitterCtx Context)
  20. {
  21. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  22. int Shift = Op.Imm - (8 << Op.Size);
  23. EmitVectorBinaryShImmBinaryZx(Context, () => Context.Emit(OpCodes.Shl), Shift);
  24. }
  25. public static void Shll_V(AILEmitterCtx Context)
  26. {
  27. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  28. int Shift = 8 << Op.Size;
  29. EmitVectorShImmWidenBinaryZx(Context, () => Context.Emit(OpCodes.Shl), Shift);
  30. }
  31. public static void Shrn_V(AILEmitterCtx Context)
  32. {
  33. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  34. int Shift = (8 << (Op.Size + 1)) - Op.Imm;
  35. EmitVectorShImmNarrowBinaryZx(Context, () => Context.Emit(OpCodes.Shr_Un), Shift);
  36. }
  37. public static void Sshl_V(AILEmitterCtx Context)
  38. {
  39. EmitVectorShl(Context, Signed: true);
  40. }
  41. public static void Sshll_V(AILEmitterCtx Context)
  42. {
  43. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  44. int Shift = Op.Imm - (8 << Op.Size);
  45. EmitVectorShImmWidenBinarySx(Context, () => Context.Emit(OpCodes.Shl), Shift);
  46. }
  47. public static void Sshr_S(AILEmitterCtx Context)
  48. {
  49. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  50. EmitVectorExtractSx(Context, Op.Rn, 0, Op.Size);
  51. Context.EmitLdc_I4(GetImmShr(Op));
  52. Context.Emit(OpCodes.Shr);
  53. EmitScalarSet(Context, Op.Rd, Op.Size);
  54. }
  55. public static void Sshr_V(AILEmitterCtx Context)
  56. {
  57. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  58. int Shift = (8 << (Op.Size + 1)) - Op.Imm;
  59. EmitVectorBinaryShImmBinarySx(Context, () => Context.Emit(OpCodes.Shr), Shift);
  60. }
  61. public static void Ssra_V(AILEmitterCtx Context)
  62. {
  63. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  64. int Shift = (8 << (Op.Size + 1)) - Op.Imm;
  65. Action Emit = () =>
  66. {
  67. Context.Emit(OpCodes.Shr);
  68. Context.Emit(OpCodes.Add);
  69. };
  70. EmitVectorTernaryShImmBinarySx(Context, Emit, Shift);
  71. }
  72. public static void Ushl_V(AILEmitterCtx Context)
  73. {
  74. EmitVectorShl(Context, Signed: false);
  75. }
  76. public static void Ushll_V(AILEmitterCtx Context)
  77. {
  78. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  79. int Shift = Op.Imm - (8 << Op.Size);
  80. EmitVectorShImmWidenBinaryZx(Context, () => Context.Emit(OpCodes.Shl), Shift);
  81. }
  82. public static void Ushr_S(AILEmitterCtx Context)
  83. {
  84. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  85. EmitScalarUnaryOpZx(Context, () =>
  86. {
  87. Context.EmitLdc_I4(GetImmShr(Op));
  88. Context.Emit(OpCodes.Shr_Un);
  89. });
  90. }
  91. public static void Ushr_V(AILEmitterCtx Context)
  92. {
  93. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  94. EmitVectorUnaryOpZx(Context, () =>
  95. {
  96. Context.EmitLdc_I4(GetImmShr(Op));
  97. Context.Emit(OpCodes.Shr_Un);
  98. });
  99. }
  100. public static void Usra_V(AILEmitterCtx Context)
  101. {
  102. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  103. Action Emit = () =>
  104. {
  105. Context.EmitLdc_I4(GetImmShr(Op));
  106. Context.Emit(OpCodes.Shr_Un);
  107. Context.Emit(OpCodes.Add);
  108. };
  109. EmitVectorOp(Context, Emit, OperFlags.RdRn, Signed: false);
  110. }
  111. private static void EmitVectorShl(AILEmitterCtx Context, bool Signed)
  112. {
  113. //This instruction shifts the value on vector A by the number of bits
  114. //specified on the signed, lower 8 bits of vector B. If the shift value
  115. //is greater or equal to the data size of each lane, then the result is zero.
  116. //Additionally, negative shifts produces right shifts by the negated shift value.
  117. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  118. int MaxShift = 8 << Op.Size;
  119. Action Emit = () =>
  120. {
  121. AILLabel LblShl = new AILLabel();
  122. AILLabel LblZero = new AILLabel();
  123. AILLabel LblEnd = new AILLabel();
  124. void EmitShift(OpCode ILOp)
  125. {
  126. Context.Emit(OpCodes.Dup);
  127. Context.EmitLdc_I4(MaxShift);
  128. Context.Emit(OpCodes.Bge_S, LblZero);
  129. Context.Emit(ILOp);
  130. Context.Emit(OpCodes.Br_S, LblEnd);
  131. }
  132. Context.Emit(OpCodes.Conv_I1);
  133. Context.Emit(OpCodes.Dup);
  134. Context.EmitLdc_I4(0);
  135. Context.Emit(OpCodes.Bge_S, LblShl);
  136. Context.Emit(OpCodes.Neg);
  137. EmitShift(Signed
  138. ? OpCodes.Shr
  139. : OpCodes.Shr_Un);
  140. Context.MarkLabel(LblShl);
  141. EmitShift(OpCodes.Shl);
  142. Context.MarkLabel(LblZero);
  143. Context.Emit(OpCodes.Pop);
  144. Context.Emit(OpCodes.Pop);
  145. Context.EmitLdc_I8(0);
  146. Context.MarkLabel(LblEnd);
  147. };
  148. if (Signed)
  149. {
  150. EmitVectorBinaryOpSx(Context, Emit);
  151. }
  152. else
  153. {
  154. EmitVectorBinaryOpZx(Context, Emit);
  155. }
  156. }
  157. private static void EmitVectorBinaryShImmBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  158. {
  159. EmitVectorShImmBinaryOp(Context, Emit, Imm, false, true);
  160. }
  161. private static void EmitVectorTernaryShImmBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  162. {
  163. EmitVectorShImmBinaryOp(Context, Emit, Imm, true, true);
  164. }
  165. private static void EmitVectorBinaryShImmBinaryZx(AILEmitterCtx Context, Action Emit, int Imm)
  166. {
  167. EmitVectorShImmBinaryOp(Context, Emit, Imm, false, false);
  168. }
  169. private static void EmitVectorShImmBinaryOp(AILEmitterCtx Context, Action Emit, int Imm, bool Ternary, bool Signed)
  170. {
  171. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  172. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  173. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  174. {
  175. if (Ternary)
  176. {
  177. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  178. }
  179. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  180. Context.EmitLdc_I4(Imm);
  181. Emit();
  182. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  183. }
  184. if (Op.RegisterSize == ARegisterSize.SIMD64)
  185. {
  186. EmitVectorZeroUpper(Context, Op.Rd);
  187. }
  188. }
  189. private static void EmitVectorShImmNarrowBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  190. {
  191. EmitVectorShImmNarrowBinaryOp(Context, Emit, Imm, true);
  192. }
  193. private static void EmitVectorShImmNarrowBinaryZx(AILEmitterCtx Context, Action Emit, int Imm)
  194. {
  195. EmitVectorShImmNarrowBinaryOp(Context, Emit, Imm, false);
  196. }
  197. private static void EmitVectorShImmNarrowBinaryOp(AILEmitterCtx Context, Action Emit, int Imm, bool Signed)
  198. {
  199. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  200. int Elems = 8 >> Op.Size;
  201. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  202. for (int Index = 0; Index < Elems; Index++)
  203. {
  204. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  205. Context.EmitLdc_I4(Imm);
  206. Emit();
  207. EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
  208. }
  209. if (Part == 0)
  210. {
  211. EmitVectorZeroUpper(Context, Op.Rd);
  212. }
  213. }
  214. private static void EmitVectorShImmWidenBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  215. {
  216. EmitVectorShImmWidenBinaryOp(Context, Emit, Imm, true);
  217. }
  218. private static void EmitVectorShImmWidenBinaryZx(AILEmitterCtx Context, Action Emit, int Imm)
  219. {
  220. EmitVectorShImmWidenBinaryOp(Context, Emit, Imm, false);
  221. }
  222. private static void EmitVectorShImmWidenBinaryOp(AILEmitterCtx Context, Action Emit, int Imm, bool Signed)
  223. {
  224. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  225. int Elems = 8 >> Op.Size;
  226. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  227. for (int Index = 0; Index < Elems; Index++)
  228. {
  229. EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
  230. Context.EmitLdc_I4(Imm);
  231. Emit();
  232. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  233. }
  234. Context.EmitLdvectmp();
  235. Context.EmitStvec(Op.Rd);
  236. }
  237. }
  238. }