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