AInstEmitSimdShift.cs 13 KB

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