AInstEmitSimdShift.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. EmitVectorShImmBinaryZx(Context, () => Context.Emit(OpCodes.Shl), GetImmShl(Op));
  23. }
  24. public static void Shll_V(AILEmitterCtx Context)
  25. {
  26. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  27. int Shift = 8 << Op.Size;
  28. EmitVectorShImmWidenBinaryZx(Context, () => Context.Emit(OpCodes.Shl), Shift);
  29. }
  30. public static void Shrn_V(AILEmitterCtx Context)
  31. {
  32. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  33. EmitVectorShImmNarrowBinaryZx(Context, () => Context.Emit(OpCodes.Shr_Un), GetImmShr(Op));
  34. }
  35. public static void Sli_V(AILEmitterCtx Context)
  36. {
  37. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  38. int Bytes = Op.GetBitsCount() >> 3;
  39. int Elems = Bytes >> Op.Size;
  40. int Shift = GetImmShl(Op);
  41. ulong Mask = Shift != 0 ? ulong.MaxValue >> (64 - Shift) : 0;
  42. for (int Index = 0; Index < Elems; Index++)
  43. {
  44. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size);
  45. Context.EmitLdc_I4(Shift);
  46. Context.Emit(OpCodes.Shl);
  47. EmitVectorExtractZx(Context, Op.Rd, Index, Op.Size);
  48. Context.EmitLdc_I8((long)Mask);
  49. Context.Emit(OpCodes.And);
  50. Context.Emit(OpCodes.Or);
  51. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  52. }
  53. if (Op.RegisterSize == ARegisterSize.SIMD64)
  54. {
  55. EmitVectorZeroUpper(Context, Op.Rd);
  56. }
  57. }
  58. public static void Sqrshrn_V(AILEmitterCtx Context)
  59. {
  60. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  61. int Shift = GetImmShr(Op);
  62. long RoundConst = 1L << (Shift - 1);
  63. Action Emit = () =>
  64. {
  65. Context.EmitLdc_I8(RoundConst);
  66. Context.Emit(OpCodes.Add);
  67. Context.EmitLdc_I4(Shift);
  68. Context.Emit(OpCodes.Shr);
  69. };
  70. EmitVectorSaturatingNarrowOpSxSx(Context, Emit);
  71. }
  72. public static void Srshr_V(AILEmitterCtx Context)
  73. {
  74. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  75. int Shift = GetImmShr(Op);
  76. long RoundConst = 1L << (Shift - 1);
  77. EmitVectorRoundShImmBinarySx(Context, () => Context.Emit(OpCodes.Shr), Shift, RoundConst);
  78. }
  79. public static void Sshl_V(AILEmitterCtx Context)
  80. {
  81. EmitVectorShl(Context, Signed: true);
  82. }
  83. public static void Sshll_V(AILEmitterCtx Context)
  84. {
  85. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  86. EmitVectorShImmWidenBinarySx(Context, () => Context.Emit(OpCodes.Shl), GetImmShl(Op));
  87. }
  88. public static void Sshr_S(AILEmitterCtx Context)
  89. {
  90. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  91. EmitVectorExtractSx(Context, Op.Rn, 0, Op.Size);
  92. Context.EmitLdc_I4(GetImmShr(Op));
  93. Context.Emit(OpCodes.Shr);
  94. EmitScalarSet(Context, Op.Rd, Op.Size);
  95. }
  96. public static void Sshr_V(AILEmitterCtx Context)
  97. {
  98. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  99. EmitVectorShImmBinarySx(Context, () => Context.Emit(OpCodes.Shr), GetImmShr(Op));
  100. }
  101. public static void Ssra_V(AILEmitterCtx Context)
  102. {
  103. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  104. Action Emit = () =>
  105. {
  106. Context.Emit(OpCodes.Shr);
  107. Context.Emit(OpCodes.Add);
  108. };
  109. EmitVectorShImmTernarySx(Context, Emit, GetImmShr(Op));
  110. }
  111. public static void Ushl_V(AILEmitterCtx Context)
  112. {
  113. EmitVectorShl(Context, Signed: false);
  114. }
  115. public static void Ushll_V(AILEmitterCtx Context)
  116. {
  117. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  118. EmitVectorShImmWidenBinaryZx(Context, () => Context.Emit(OpCodes.Shl), GetImmShl(Op));
  119. }
  120. public static void Ushr_S(AILEmitterCtx Context)
  121. {
  122. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  123. EmitScalarUnaryOpZx(Context, () =>
  124. {
  125. Context.EmitLdc_I4(GetImmShr(Op));
  126. Context.Emit(OpCodes.Shr_Un);
  127. });
  128. }
  129. public static void Ushr_V(AILEmitterCtx Context)
  130. {
  131. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  132. EmitVectorUnaryOpZx(Context, () =>
  133. {
  134. Context.EmitLdc_I4(GetImmShr(Op));
  135. Context.Emit(OpCodes.Shr_Un);
  136. });
  137. }
  138. public static void Usra_V(AILEmitterCtx Context)
  139. {
  140. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  141. Action Emit = () =>
  142. {
  143. Context.EmitLdc_I4(GetImmShr(Op));
  144. Context.Emit(OpCodes.Shr_Un);
  145. Context.Emit(OpCodes.Add);
  146. };
  147. EmitVectorOp(Context, Emit, OperFlags.RdRn, Signed: false);
  148. }
  149. private static void EmitVectorShl(AILEmitterCtx Context, bool Signed)
  150. {
  151. //This instruction shifts the value on vector A by the number of bits
  152. //specified on the signed, lower 8 bits of vector B. If the shift value
  153. //is greater or equal to the data size of each lane, then the result is zero.
  154. //Additionally, negative shifts produces right shifts by the negated shift value.
  155. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  156. int MaxShift = 8 << Op.Size;
  157. Action Emit = () =>
  158. {
  159. AILLabel LblShl = new AILLabel();
  160. AILLabel LblZero = new AILLabel();
  161. AILLabel LblEnd = new AILLabel();
  162. void EmitShift(OpCode ILOp)
  163. {
  164. Context.Emit(OpCodes.Dup);
  165. Context.EmitLdc_I4(MaxShift);
  166. Context.Emit(OpCodes.Bge_S, LblZero);
  167. Context.Emit(ILOp);
  168. Context.Emit(OpCodes.Br_S, LblEnd);
  169. }
  170. Context.Emit(OpCodes.Conv_I1);
  171. Context.Emit(OpCodes.Dup);
  172. Context.EmitLdc_I4(0);
  173. Context.Emit(OpCodes.Bge_S, LblShl);
  174. Context.Emit(OpCodes.Neg);
  175. EmitShift(Signed
  176. ? OpCodes.Shr
  177. : OpCodes.Shr_Un);
  178. Context.MarkLabel(LblShl);
  179. EmitShift(OpCodes.Shl);
  180. Context.MarkLabel(LblZero);
  181. Context.Emit(OpCodes.Pop);
  182. Context.Emit(OpCodes.Pop);
  183. Context.EmitLdc_I8(0);
  184. Context.MarkLabel(LblEnd);
  185. };
  186. if (Signed)
  187. {
  188. EmitVectorBinaryOpSx(Context, Emit);
  189. }
  190. else
  191. {
  192. EmitVectorBinaryOpZx(Context, Emit);
  193. }
  194. }
  195. [Flags]
  196. private enum ShImmFlags
  197. {
  198. None = 0,
  199. Signed = 1 << 0,
  200. Ternary = 1 << 1,
  201. Rounded = 1 << 2,
  202. SignedTernary = Signed | Ternary,
  203. SignedRounded = Signed | Rounded
  204. }
  205. private static void EmitVectorShImmBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  206. {
  207. EmitVectorShImmOp(Context, Emit, Imm, ShImmFlags.Signed);
  208. }
  209. private static void EmitVectorShImmTernarySx(AILEmitterCtx Context, Action Emit, int Imm)
  210. {
  211. EmitVectorShImmOp(Context, Emit, Imm, ShImmFlags.SignedTernary);
  212. }
  213. private static void EmitVectorShImmBinaryZx(AILEmitterCtx Context, Action Emit, int Imm)
  214. {
  215. EmitVectorShImmOp(Context, Emit, Imm, ShImmFlags.None);
  216. }
  217. private static void EmitVectorRoundShImmBinarySx(AILEmitterCtx Context, Action Emit, int Imm, long Rc)
  218. {
  219. EmitVectorShImmOp(Context, Emit, Imm, ShImmFlags.SignedRounded, Rc);
  220. }
  221. private static void EmitVectorShImmOp(AILEmitterCtx Context, Action Emit, int Imm, ShImmFlags Flags, long Rc = 0)
  222. {
  223. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  224. int Bytes = Op.GetBitsCount() >> 3;
  225. int Elems = Bytes >> Op.Size;
  226. bool Signed = (Flags & ShImmFlags.Signed) != 0;
  227. bool Ternary = (Flags & ShImmFlags.Ternary) != 0;
  228. bool Rounded = (Flags & ShImmFlags.Rounded) != 0;
  229. for (int Index = 0; Index < Elems; Index++)
  230. {
  231. if (Ternary)
  232. {
  233. EmitVectorExtract(Context, Op.Rd, Index, Op.Size, Signed);
  234. }
  235. EmitVectorExtract(Context, Op.Rn, Index, Op.Size, Signed);
  236. if (Rounded)
  237. {
  238. Context.EmitLdc_I8(Rc);
  239. Context.Emit(OpCodes.Add);
  240. }
  241. Context.EmitLdc_I4(Imm);
  242. Emit();
  243. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  244. }
  245. if (Op.RegisterSize == ARegisterSize.SIMD64)
  246. {
  247. EmitVectorZeroUpper(Context, Op.Rd);
  248. }
  249. }
  250. private static void EmitVectorShImmNarrowBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  251. {
  252. EmitVectorShImmNarrowBinaryOp(Context, Emit, Imm, true);
  253. }
  254. private static void EmitVectorShImmNarrowBinaryZx(AILEmitterCtx Context, Action Emit, int Imm)
  255. {
  256. EmitVectorShImmNarrowBinaryOp(Context, Emit, Imm, false);
  257. }
  258. private static void EmitVectorShImmNarrowBinaryOp(AILEmitterCtx Context, Action Emit, int Imm, bool Signed)
  259. {
  260. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  261. int Elems = 8 >> Op.Size;
  262. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  263. for (int Index = 0; Index < Elems; Index++)
  264. {
  265. EmitVectorExtract(Context, Op.Rn, Index, Op.Size + 1, Signed);
  266. Context.EmitLdc_I4(Imm);
  267. Emit();
  268. EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
  269. }
  270. if (Part == 0)
  271. {
  272. EmitVectorZeroUpper(Context, Op.Rd);
  273. }
  274. }
  275. private static void EmitVectorShImmWidenBinarySx(AILEmitterCtx Context, Action Emit, int Imm)
  276. {
  277. EmitVectorShImmWidenBinaryOp(Context, Emit, Imm, true);
  278. }
  279. private static void EmitVectorShImmWidenBinaryZx(AILEmitterCtx Context, Action Emit, int Imm)
  280. {
  281. EmitVectorShImmWidenBinaryOp(Context, Emit, Imm, false);
  282. }
  283. private static void EmitVectorShImmWidenBinaryOp(AILEmitterCtx Context, Action Emit, int Imm, bool Signed)
  284. {
  285. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  286. int Elems = 8 >> Op.Size;
  287. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  288. for (int Index = 0; Index < Elems; Index++)
  289. {
  290. EmitVectorExtract(Context, Op.Rn, Part + Index, Op.Size, Signed);
  291. Context.EmitLdc_I4(Imm);
  292. Emit();
  293. EmitVectorInsertTmp(Context, Index, Op.Size + 1);
  294. }
  295. Context.EmitLdvectmp();
  296. Context.EmitStvec(Op.Rd);
  297. }
  298. }
  299. }