AInstEmitSimdShift.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. EmitVectorShImmBinaryZx(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. EmitVectorShImmBinarySx(Context, () => Context.Emit(OpCodes.Shr), Shift);
  60. }
  61. public static void Ushl_V(AILEmitterCtx Context)
  62. {
  63. EmitVectorShl(Context, Signed: false);
  64. }
  65. public static void Ushll_V(AILEmitterCtx Context)
  66. {
  67. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  68. int Shift = Op.Imm - (8 << Op.Size);
  69. EmitVectorShImmWidenBinaryZx(Context, () => Context.Emit(OpCodes.Shl), Shift);
  70. }
  71. public static void Ushr_S(AILEmitterCtx Context)
  72. {
  73. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  74. EmitScalarUnaryOpZx(Context, () =>
  75. {
  76. Context.EmitLdc_I4(GetImmShr(Op));
  77. Context.Emit(OpCodes.Shr_Un);
  78. });
  79. }
  80. public static void Ushr_V(AILEmitterCtx Context)
  81. {
  82. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  83. EmitVectorUnaryOpZx(Context, () =>
  84. {
  85. Context.EmitLdc_I4(GetImmShr(Op));
  86. Context.Emit(OpCodes.Shr_Un);
  87. });
  88. }
  89. public static void Usra_V(AILEmitterCtx Context)
  90. {
  91. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  92. Action Emit = () =>
  93. {
  94. Context.EmitLdc_I4(GetImmShr(Op));
  95. Context.Emit(OpCodes.Shr_Un);
  96. Context.Emit(OpCodes.Add);
  97. };
  98. EmitVectorOp(Context, Emit, OperFlags.RdRn, Signed: false);
  99. }
  100. private static void EmitVectorShl(AILEmitterCtx Context, bool Signed)
  101. {
  102. //This instruction shifts the value on vector A by the number of bits
  103. //specified on the signed, lower 8 bits of vector B. If the shift value
  104. //is greater or equal to the data size of each lane, then the result is zero.
  105. //Additionally, negative shifts produces right shifts by the negated shift value.
  106. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  107. int MaxShift = 8 << Op.Size;
  108. Action Emit = () =>
  109. {
  110. AILLabel LblShl = new AILLabel();
  111. AILLabel LblZero = new AILLabel();
  112. AILLabel LblEnd = new AILLabel();
  113. void EmitShift(OpCode ILOp)
  114. {
  115. Context.Emit(OpCodes.Dup);
  116. Context.EmitLdc_I4(MaxShift);
  117. Context.Emit(OpCodes.Bge_S, LblZero);
  118. Context.Emit(ILOp);
  119. Context.Emit(OpCodes.Br_S, LblEnd);
  120. }
  121. Context.Emit(OpCodes.Conv_I1);
  122. Context.Emit(OpCodes.Dup);
  123. Context.EmitLdc_I4(0);
  124. Context.Emit(OpCodes.Bge_S, LblShl);
  125. Context.Emit(OpCodes.Neg);
  126. EmitShift(Signed
  127. ? OpCodes.Shr
  128. : OpCodes.Shr_Un);
  129. Context.MarkLabel(LblShl);
  130. EmitShift(OpCodes.Shl);
  131. Context.MarkLabel(LblZero);
  132. Context.Emit(OpCodes.Pop);
  133. Context.Emit(OpCodes.Pop);
  134. Context.EmitLdc_I8(0);
  135. Context.MarkLabel(LblEnd);
  136. };
  137. if (Signed)
  138. {
  139. EmitVectorBinaryOpSx(Context, Emit);
  140. }
  141. else
  142. {
  143. EmitVectorBinaryOpZx(Context, Emit);
  144. }
  145. }
  146. private static void EmitVectorShImmBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  147. {
  148. EmitVectorShImmBinaryOp(Context, Emit, Imm, true);
  149. }
  150. private static void EmitVectorShImmBinaryZx(AILEmitterCtx Context, Action Emit, int Imm)
  151. {
  152. EmitVectorShImmBinaryOp(Context, Emit, Imm, false);
  153. }
  154. private static void EmitVectorShImmBinaryOp(AILEmitterCtx Context, Action Emit, int Imm, bool Signed)
  155. {
  156. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  157. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  158. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  159. {
  160. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  161. Context.EmitLdc_I4(Imm);
  162. Emit();
  163. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  164. }
  165. if (Op.RegisterSize == ARegisterSize.SIMD64)
  166. {
  167. EmitVectorZeroUpper(Context, Op.Rd);
  168. }
  169. }
  170. private static void EmitVectorShImmNarrowBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  171. {
  172. EmitVectorShImmNarrowBinaryOp(Context, Emit, Imm, true);
  173. }
  174. private static void EmitVectorShImmNarrowBinaryZx(AILEmitterCtx Context, Action Emit, int Imm)
  175. {
  176. EmitVectorShImmNarrowBinaryOp(Context, Emit, Imm, false);
  177. }
  178. private static void EmitVectorShImmNarrowBinaryOp(AILEmitterCtx Context, Action Emit, int Imm, bool Signed)
  179. {
  180. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  181. int Elems = 8 >> Op.Size;
  182. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  183. for (int Index = 0; Index < Elems; Index++)
  184. {
  185. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  186. Context.EmitLdc_I4(Imm);
  187. Emit();
  188. EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
  189. }
  190. if (Part == 0)
  191. {
  192. EmitVectorZeroUpper(Context, Op.Rd);
  193. }
  194. }
  195. private static void EmitVectorShImmWidenBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  196. {
  197. EmitVectorShImmWidenBinaryOp(Context, Emit, Imm, true);
  198. }
  199. private static void EmitVectorShImmWidenBinaryZx(AILEmitterCtx Context, Action Emit, int Imm)
  200. {
  201. EmitVectorShImmWidenBinaryOp(Context, Emit, Imm, false);
  202. }
  203. private static void EmitVectorShImmWidenBinaryOp(AILEmitterCtx Context, Action Emit, int Imm, bool Signed)
  204. {
  205. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  206. int Elems = 8 >> Op.Size;
  207. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  208. for (int Index = 0; Index < Elems; Index++)
  209. {
  210. EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
  211. Context.EmitLdc_I4(Imm);
  212. Emit();
  213. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  214. }
  215. Context.EmitLdvectmp();
  216. Context.EmitStvec(Op.Rd);
  217. }
  218. }
  219. }