InstEmitSimdMove.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. using System.Runtime.Intrinsics.X86;
  7. using static ChocolArm64.Instructions.InstEmitSimdHelper;
  8. namespace ChocolArm64.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. public static void Dup_Gp(ILEmitterCtx context)
  13. {
  14. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  15. if (Optimizations.UseSse2)
  16. {
  17. context.EmitLdintzr(op.Rn);
  18. switch (op.Size)
  19. {
  20. case 0: context.Emit(OpCodes.Conv_U1); break;
  21. case 1: context.Emit(OpCodes.Conv_U2); break;
  22. case 2: context.Emit(OpCodes.Conv_U4); break;
  23. }
  24. Type[] types = new Type[] { UIntTypesPerSizeLog2[op.Size] };
  25. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), types));
  26. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  27. if (op.RegisterSize == RegisterSize.Simd64)
  28. {
  29. EmitVectorZeroUpper(context, op.Rd);
  30. }
  31. }
  32. else
  33. {
  34. int bytes = op.GetBitsCount() >> 3;
  35. int elems = bytes >> op.Size;
  36. for (int index = 0; index < elems; index++)
  37. {
  38. context.EmitLdintzr(op.Rn);
  39. EmitVectorInsert(context, op.Rd, index, op.Size);
  40. }
  41. if (op.RegisterSize == RegisterSize.Simd64)
  42. {
  43. EmitVectorZeroUpper(context, op.Rd);
  44. }
  45. }
  46. }
  47. public static void Dup_S(ILEmitterCtx context)
  48. {
  49. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  50. EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  51. EmitScalarSet(context, op.Rd, op.Size);
  52. }
  53. public static void Dup_V(ILEmitterCtx context)
  54. {
  55. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  56. int bytes = op.GetBitsCount() >> 3;
  57. int elems = bytes >> op.Size;
  58. for (int index = 0; index < elems; index++)
  59. {
  60. EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  61. EmitVectorInsert(context, op.Rd, index, op.Size);
  62. }
  63. if (op.RegisterSize == RegisterSize.Simd64)
  64. {
  65. EmitVectorZeroUpper(context, op.Rd);
  66. }
  67. }
  68. public static void Ext_V(ILEmitterCtx context)
  69. {
  70. OpCodeSimdExt64 op = (OpCodeSimdExt64)context.CurrOp;
  71. context.EmitLdvec(op.Rd);
  72. context.EmitStvectmp();
  73. int bytes = op.GetBitsCount() >> 3;
  74. int position = op.Imm4;
  75. for (int index = 0; index < bytes; index++)
  76. {
  77. int reg = op.Imm4 + index < bytes ? op.Rn : op.Rm;
  78. if (position == bytes)
  79. {
  80. position = 0;
  81. }
  82. EmitVectorExtractZx(context, reg, position++, 0);
  83. EmitVectorInsertTmp(context, index, 0);
  84. }
  85. context.EmitLdvectmp();
  86. context.EmitStvec(op.Rd);
  87. if (op.RegisterSize == RegisterSize.Simd64)
  88. {
  89. EmitVectorZeroUpper(context, op.Rd);
  90. }
  91. }
  92. public static void Fcsel_S(ILEmitterCtx context)
  93. {
  94. OpCodeSimdFcond64 op = (OpCodeSimdFcond64)context.CurrOp;
  95. ILLabel lblTrue = new ILLabel();
  96. ILLabel lblEnd = new ILLabel();
  97. context.EmitCondBranch(lblTrue, op.Cond);
  98. EmitVectorExtractF(context, op.Rm, 0, op.Size);
  99. context.Emit(OpCodes.Br_S, lblEnd);
  100. context.MarkLabel(lblTrue);
  101. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  102. context.MarkLabel(lblEnd);
  103. EmitScalarSetF(context, op.Rd, op.Size);
  104. }
  105. public static void Fmov_Ftoi(ILEmitterCtx context)
  106. {
  107. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  108. EmitVectorExtractZx(context, op.Rn, 0, 3);
  109. EmitIntZeroUpperIfNeeded(context);
  110. context.EmitStintzr(op.Rd);
  111. }
  112. public static void Fmov_Ftoi1(ILEmitterCtx context)
  113. {
  114. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  115. EmitVectorExtractZx(context, op.Rn, 1, 3);
  116. EmitIntZeroUpperIfNeeded(context);
  117. context.EmitStintzr(op.Rd);
  118. }
  119. public static void Fmov_Itof(ILEmitterCtx context)
  120. {
  121. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  122. context.EmitLdintzr(op.Rn);
  123. EmitIntZeroUpperIfNeeded(context);
  124. EmitScalarSet(context, op.Rd, 3);
  125. }
  126. public static void Fmov_Itof1(ILEmitterCtx context)
  127. {
  128. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  129. context.EmitLdintzr(op.Rn);
  130. EmitIntZeroUpperIfNeeded(context);
  131. EmitVectorInsert(context, op.Rd, 1, 3);
  132. }
  133. public static void Fmov_S(ILEmitterCtx context)
  134. {
  135. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  136. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  137. EmitScalarSetF(context, op.Rd, op.Size);
  138. }
  139. public static void Fmov_Si(ILEmitterCtx context)
  140. {
  141. OpCodeSimdFmov64 op = (OpCodeSimdFmov64)context.CurrOp;
  142. context.EmitLdc_I8(op.Imm);
  143. EmitScalarSet(context, op.Rd, op.Size + 2);
  144. }
  145. public static void Fmov_V(ILEmitterCtx context)
  146. {
  147. OpCodeSimdImm64 op = (OpCodeSimdImm64)context.CurrOp;
  148. int elems = op.RegisterSize == RegisterSize.Simd128 ? 4 : 2;
  149. for (int index = 0; index < (elems >> op.Size); index++)
  150. {
  151. context.EmitLdc_I8(op.Imm);
  152. EmitVectorInsert(context, op.Rd, index, op.Size + 2);
  153. }
  154. if (op.RegisterSize == RegisterSize.Simd64)
  155. {
  156. EmitVectorZeroUpper(context, op.Rd);
  157. }
  158. }
  159. public static void Ins_Gp(ILEmitterCtx context)
  160. {
  161. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  162. context.EmitLdintzr(op.Rn);
  163. EmitVectorInsert(context, op.Rd, op.DstIndex, op.Size);
  164. }
  165. public static void Ins_V(ILEmitterCtx context)
  166. {
  167. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  168. EmitVectorExtractZx(context, op.Rn, op.SrcIndex, op.Size);
  169. EmitVectorInsert(context, op.Rd, op.DstIndex, op.Size);
  170. }
  171. public static void Movi_V(ILEmitterCtx context)
  172. {
  173. EmitVectorImmUnaryOp(context, () => { });
  174. }
  175. public static void Mvni_V(ILEmitterCtx context)
  176. {
  177. EmitVectorImmUnaryOp(context, () => context.Emit(OpCodes.Not));
  178. }
  179. public static void Smov_S(ILEmitterCtx context)
  180. {
  181. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  182. EmitVectorExtractSx(context, op.Rn, op.DstIndex, op.Size);
  183. EmitIntZeroUpperIfNeeded(context);
  184. context.EmitStintzr(op.Rd);
  185. }
  186. public static void Tbl_V(ILEmitterCtx context)
  187. {
  188. OpCodeSimdTbl64 op = (OpCodeSimdTbl64)context.CurrOp;
  189. context.EmitLdvec(op.Rm);
  190. for (int index = 0; index < op.Size; index++)
  191. {
  192. context.EmitLdvec((op.Rn + index) & 0x1f);
  193. }
  194. switch (op.Size)
  195. {
  196. case 1: VectorHelper.EmitCall(context,
  197. nameof(VectorHelper.Tbl1_V64),
  198. nameof(VectorHelper.Tbl1_V128)); break;
  199. case 2: VectorHelper.EmitCall(context,
  200. nameof(VectorHelper.Tbl2_V64),
  201. nameof(VectorHelper.Tbl2_V128)); break;
  202. case 3: VectorHelper.EmitCall(context,
  203. nameof(VectorHelper.Tbl3_V64),
  204. nameof(VectorHelper.Tbl3_V128)); break;
  205. case 4: VectorHelper.EmitCall(context,
  206. nameof(VectorHelper.Tbl4_V64),
  207. nameof(VectorHelper.Tbl4_V128)); break;
  208. default: throw new InvalidOperationException();
  209. }
  210. context.EmitStvec(op.Rd);
  211. }
  212. public static void Trn1_V(ILEmitterCtx context)
  213. {
  214. EmitVectorTranspose(context, part: 0);
  215. }
  216. public static void Trn2_V(ILEmitterCtx context)
  217. {
  218. EmitVectorTranspose(context, part: 1);
  219. }
  220. public static void Umov_S(ILEmitterCtx context)
  221. {
  222. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  223. EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  224. context.EmitStintzr(op.Rd);
  225. }
  226. public static void Uzp1_V(ILEmitterCtx context)
  227. {
  228. EmitVectorUnzip(context, part: 0);
  229. }
  230. public static void Uzp2_V(ILEmitterCtx context)
  231. {
  232. EmitVectorUnzip(context, part: 1);
  233. }
  234. public static void Xtn_V(ILEmitterCtx context)
  235. {
  236. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  237. int elems = 8 >> op.Size;
  238. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  239. if (Optimizations.UseSse41 && op.Size < 2)
  240. {
  241. void EmitZeroVector()
  242. {
  243. switch (op.Size)
  244. {
  245. case 0: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInt16Zero)); break;
  246. case 1: VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInt32Zero)); break;
  247. }
  248. }
  249. //For XTN, first operand is source, second operand is 0.
  250. //For XTN2, first operand is 0, second operand is source.
  251. if (part != 0)
  252. {
  253. EmitZeroVector();
  254. }
  255. EmitLdvecWithSignedCast(context, op.Rn, op.Size + 1);
  256. //Set mask to discard the upper half of the wide elements.
  257. switch (op.Size)
  258. {
  259. case 0: context.EmitLdc_I4(0x00ff); break;
  260. case 1: context.EmitLdc_I4(0x0000ffff); break;
  261. }
  262. Type wideType = IntTypesPerSizeLog2[op.Size + 1];
  263. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), new Type[] { wideType }));
  264. wideType = VectorIntTypesPerSizeLog2[op.Size + 1];
  265. Type[] wideTypes = new Type[] { wideType, wideType };
  266. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), wideTypes));
  267. if (part == 0)
  268. {
  269. EmitZeroVector();
  270. }
  271. //Pack values with signed saturation, the signed saturation shouldn't
  272. //saturate anything since the upper bits were masked off.
  273. Type sseType = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  274. context.EmitCall(sseType.GetMethod(nameof(Sse2.PackUnsignedSaturate), wideTypes));
  275. if (part != 0)
  276. {
  277. //For XTN2, we additionally need to discard the upper bits
  278. //of the target register and OR the result with it.
  279. EmitVectorZeroUpper(context, op.Rd);
  280. EmitLdvecWithUnsignedCast(context, op.Rd, op.Size);
  281. Type narrowType = VectorUIntTypesPerSizeLog2[op.Size];
  282. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), new Type[] { narrowType, narrowType }));
  283. }
  284. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  285. }
  286. else
  287. {
  288. if (part != 0)
  289. {
  290. context.EmitLdvec(op.Rd);
  291. context.EmitStvectmp();
  292. }
  293. for (int index = 0; index < elems; index++)
  294. {
  295. EmitVectorExtractZx(context, op.Rn, index, op.Size + 1);
  296. EmitVectorInsertTmp(context, part + index, op.Size);
  297. }
  298. context.EmitLdvectmp();
  299. context.EmitStvec(op.Rd);
  300. if (part == 0)
  301. {
  302. EmitVectorZeroUpper(context, op.Rd);
  303. }
  304. }
  305. }
  306. public static void Zip1_V(ILEmitterCtx context)
  307. {
  308. EmitVectorZip(context, part: 0);
  309. }
  310. public static void Zip2_V(ILEmitterCtx context)
  311. {
  312. EmitVectorZip(context, part: 1);
  313. }
  314. private static void EmitIntZeroUpperIfNeeded(ILEmitterCtx context)
  315. {
  316. if (context.CurrOp.RegisterSize == RegisterSize.Int32 ||
  317. context.CurrOp.RegisterSize == RegisterSize.Simd64)
  318. {
  319. context.Emit(OpCodes.Conv_U4);
  320. context.Emit(OpCodes.Conv_U8);
  321. }
  322. }
  323. private static void EmitVectorTranspose(ILEmitterCtx context, int part)
  324. {
  325. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  326. int words = op.GetBitsCount() >> 4;
  327. int pairs = words >> op.Size;
  328. for (int index = 0; index < pairs; index++)
  329. {
  330. int idx = index << 1;
  331. EmitVectorExtractZx(context, op.Rn, idx + part, op.Size);
  332. EmitVectorExtractZx(context, op.Rm, idx + part, op.Size);
  333. EmitVectorInsertTmp(context, idx + 1, op.Size);
  334. EmitVectorInsertTmp(context, idx, op.Size);
  335. }
  336. context.EmitLdvectmp();
  337. context.EmitStvec(op.Rd);
  338. if (op.RegisterSize == RegisterSize.Simd64)
  339. {
  340. EmitVectorZeroUpper(context, op.Rd);
  341. }
  342. }
  343. private static void EmitVectorUnzip(ILEmitterCtx context, int part)
  344. {
  345. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  346. int words = op.GetBitsCount() >> 4;
  347. int pairs = words >> op.Size;
  348. for (int index = 0; index < pairs; index++)
  349. {
  350. int idx = index << 1;
  351. EmitVectorExtractZx(context, op.Rn, idx + part, op.Size);
  352. EmitVectorExtractZx(context, op.Rm, idx + part, op.Size);
  353. EmitVectorInsertTmp(context, pairs + index, op.Size);
  354. EmitVectorInsertTmp(context, index, op.Size);
  355. }
  356. context.EmitLdvectmp();
  357. context.EmitStvec(op.Rd);
  358. if (op.RegisterSize == RegisterSize.Simd64)
  359. {
  360. EmitVectorZeroUpper(context, op.Rd);
  361. }
  362. }
  363. private static void EmitVectorZip(ILEmitterCtx context, int part)
  364. {
  365. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  366. if (Optimizations.UseSse2)
  367. {
  368. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  369. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  370. Type[] types = new Type[]
  371. {
  372. VectorUIntTypesPerSizeLog2[op.Size],
  373. VectorUIntTypesPerSizeLog2[op.Size]
  374. };
  375. string name = part == 0 || (part != 0 && op.RegisterSize == RegisterSize.Simd64)
  376. ? nameof(Sse2.UnpackLow)
  377. : nameof(Sse2.UnpackHigh);
  378. context.EmitCall(typeof(Sse2).GetMethod(name, types));
  379. if (op.RegisterSize == RegisterSize.Simd64 && part != 0)
  380. {
  381. context.EmitLdc_I4(8);
  382. Type[] shTypes = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  383. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), shTypes));
  384. }
  385. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  386. if (op.RegisterSize == RegisterSize.Simd64 && part == 0)
  387. {
  388. EmitVectorZeroUpper(context, op.Rd);
  389. }
  390. }
  391. else
  392. {
  393. int words = op.GetBitsCount() >> 4;
  394. int pairs = words >> op.Size;
  395. int Base = part != 0 ? pairs : 0;
  396. for (int index = 0; index < pairs; index++)
  397. {
  398. int idx = index << 1;
  399. EmitVectorExtractZx(context, op.Rn, Base + index, op.Size);
  400. EmitVectorExtractZx(context, op.Rm, Base + index, op.Size);
  401. EmitVectorInsertTmp(context, idx + 1, op.Size);
  402. EmitVectorInsertTmp(context, idx, op.Size);
  403. }
  404. context.EmitLdvectmp();
  405. context.EmitStvec(op.Rd);
  406. if (op.RegisterSize == RegisterSize.Simd64)
  407. {
  408. EmitVectorZeroUpper(context, op.Rd);
  409. }
  410. }
  411. }
  412. }
  413. }