InstEmitSimdMove.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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;
  7. using System.Runtime.Intrinsics.X86;
  8. using static ChocolArm64.Instructions.InstEmitSimdHelper;
  9. namespace ChocolArm64.Instructions
  10. {
  11. static partial class InstEmit
  12. {
  13. #region "Masks"
  14. private static readonly long[] _masksE0_TrnUzpXtn = new long[]
  15. {
  16. 14L << 56 | 12L << 48 | 10L << 40 | 08L << 32 | 06L << 24 | 04L << 16 | 02L << 8 | 00L << 0,
  17. 13L << 56 | 12L << 48 | 09L << 40 | 08L << 32 | 05L << 24 | 04L << 16 | 01L << 8 | 00L << 0,
  18. 11L << 56 | 10L << 48 | 09L << 40 | 08L << 32 | 03L << 24 | 02L << 16 | 01L << 8 | 00L << 0
  19. };
  20. private static readonly long[] _masksE1_TrnUzp = new long[]
  21. {
  22. 15L << 56 | 13L << 48 | 11L << 40 | 09L << 32 | 07L << 24 | 05L << 16 | 03L << 8 | 01L << 0,
  23. 15L << 56 | 14L << 48 | 11L << 40 | 10L << 32 | 07L << 24 | 06L << 16 | 03L << 8 | 02L << 0,
  24. 15L << 56 | 14L << 48 | 13L << 40 | 12L << 32 | 07L << 24 | 06L << 16 | 05L << 8 | 04L << 0
  25. };
  26. private static readonly long[] _masksE0_Uzp = new long[]
  27. {
  28. 13L << 56 | 09L << 48 | 05L << 40 | 01L << 32 | 12L << 24 | 08L << 16 | 04L << 8 | 00L << 0,
  29. 11L << 56 | 10L << 48 | 03L << 40 | 02L << 32 | 09L << 24 | 08L << 16 | 01L << 8 | 00L << 0
  30. };
  31. private static readonly long[] _masksE1_Uzp = new long[]
  32. {
  33. 15L << 56 | 11L << 48 | 07L << 40 | 03L << 32 | 14L << 24 | 10L << 16 | 06L << 8 | 02L << 0,
  34. 15L << 56 | 14L << 48 | 07L << 40 | 06L << 32 | 13L << 24 | 12L << 16 | 05L << 8 | 04L << 0
  35. };
  36. #endregion
  37. public static void Dup_Gp(ILEmitterCtx context)
  38. {
  39. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  40. if (Optimizations.UseSse2)
  41. {
  42. Type[] typesSav = new Type[] { UIntTypesPerSizeLog2[op.Size] };
  43. context.EmitLdintzr(op.Rn);
  44. switch (op.Size)
  45. {
  46. case 0: context.Emit(OpCodes.Conv_U1); break;
  47. case 1: context.Emit(OpCodes.Conv_U2); break;
  48. case 2: context.Emit(OpCodes.Conv_U4); break;
  49. }
  50. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  51. context.EmitStvec(op.Rd);
  52. }
  53. else
  54. {
  55. int bytes = op.GetBitsCount() >> 3;
  56. int elems = bytes >> op.Size;
  57. for (int index = 0; index < elems; index++)
  58. {
  59. context.EmitLdintzr(op.Rn);
  60. EmitVectorInsert(context, op.Rd, index, op.Size);
  61. }
  62. }
  63. if (op.RegisterSize == RegisterSize.Simd64)
  64. {
  65. EmitVectorZeroUpper(context, op.Rd);
  66. }
  67. }
  68. public static void Dup_S(ILEmitterCtx context)
  69. {
  70. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  71. EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  72. EmitScalarSet(context, op.Rd, op.Size);
  73. }
  74. public static void Dup_V(ILEmitterCtx context)
  75. {
  76. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  77. if (Optimizations.UseSse2)
  78. {
  79. Type[] typesSav = new Type[] { UIntTypesPerSizeLog2[op.Size] };
  80. EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  81. switch (op.Size)
  82. {
  83. case 0: context.Emit(OpCodes.Conv_U1); break;
  84. case 1: context.Emit(OpCodes.Conv_U2); break;
  85. case 2: context.Emit(OpCodes.Conv_U4); break;
  86. }
  87. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  88. context.EmitStvec(op.Rd);
  89. }
  90. else
  91. {
  92. int bytes = op.GetBitsCount() >> 3;
  93. int elems = bytes >> op.Size;
  94. for (int index = 0; index < elems; index++)
  95. {
  96. EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  97. EmitVectorInsert(context, op.Rd, index, op.Size);
  98. }
  99. }
  100. if (op.RegisterSize == RegisterSize.Simd64)
  101. {
  102. EmitVectorZeroUpper(context, op.Rd);
  103. }
  104. }
  105. public static void Ext_V(ILEmitterCtx context)
  106. {
  107. OpCodeSimdExt64 op = (OpCodeSimdExt64)context.CurrOp;
  108. if (Optimizations.UseSse2)
  109. {
  110. Type[] typesShs = new Type[] { typeof(Vector128<byte>), typeof(byte) };
  111. Type[] typesOr = new Type[] { typeof(Vector128<byte>), typeof(Vector128<byte>) };
  112. context.EmitLdvec(op.Rn);
  113. if (op.RegisterSize == RegisterSize.Simd64)
  114. {
  115. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  116. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  117. }
  118. context.EmitLdc_I4(op.Imm4);
  119. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesShs));
  120. context.EmitLdvec(op.Rm);
  121. context.EmitLdc_I4((op.RegisterSize == RegisterSize.Simd64 ? 8 : 16) - op.Imm4);
  122. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftLeftLogical128BitLane), typesShs));
  123. if (op.RegisterSize == RegisterSize.Simd64)
  124. {
  125. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  126. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  127. }
  128. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesOr));
  129. context.EmitStvec(op.Rd);
  130. }
  131. else
  132. {
  133. int bytes = op.GetBitsCount() >> 3;
  134. int position = op.Imm4;
  135. for (int index = 0; index < bytes; index++)
  136. {
  137. int reg = op.Imm4 + index < bytes ? op.Rn : op.Rm;
  138. if (position == bytes)
  139. {
  140. position = 0;
  141. }
  142. EmitVectorExtractZx(context, reg, position++, 0);
  143. EmitVectorInsertTmp(context, index, 0);
  144. }
  145. context.EmitLdvectmp();
  146. context.EmitStvec(op.Rd);
  147. if (op.RegisterSize == RegisterSize.Simd64)
  148. {
  149. EmitVectorZeroUpper(context, op.Rd);
  150. }
  151. }
  152. }
  153. public static void Fcsel_S(ILEmitterCtx context)
  154. {
  155. OpCodeSimdFcond64 op = (OpCodeSimdFcond64)context.CurrOp;
  156. ILLabel lblTrue = new ILLabel();
  157. ILLabel lblEnd = new ILLabel();
  158. context.EmitCondBranch(lblTrue, op.Cond);
  159. EmitVectorExtractF(context, op.Rm, 0, op.Size);
  160. context.Emit(OpCodes.Br_S, lblEnd);
  161. context.MarkLabel(lblTrue);
  162. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  163. context.MarkLabel(lblEnd);
  164. EmitScalarSetF(context, op.Rd, op.Size);
  165. }
  166. public static void Fmov_Ftoi(ILEmitterCtx context)
  167. {
  168. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  169. EmitVectorExtractZx(context, op.Rn, 0, 3);
  170. EmitIntZeroUpperIfNeeded(context);
  171. context.EmitStintzr(op.Rd);
  172. }
  173. public static void Fmov_Ftoi1(ILEmitterCtx context)
  174. {
  175. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  176. EmitVectorExtractZx(context, op.Rn, 1, 3);
  177. EmitIntZeroUpperIfNeeded(context);
  178. context.EmitStintzr(op.Rd);
  179. }
  180. public static void Fmov_Itof(ILEmitterCtx context)
  181. {
  182. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  183. context.EmitLdintzr(op.Rn);
  184. EmitIntZeroUpperIfNeeded(context);
  185. EmitScalarSet(context, op.Rd, 3);
  186. }
  187. public static void Fmov_Itof1(ILEmitterCtx context)
  188. {
  189. OpCodeSimdCvt64 op = (OpCodeSimdCvt64)context.CurrOp;
  190. context.EmitLdintzr(op.Rn);
  191. EmitIntZeroUpperIfNeeded(context);
  192. EmitVectorInsert(context, op.Rd, 1, 3);
  193. }
  194. public static void Fmov_S(ILEmitterCtx context)
  195. {
  196. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  197. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  198. EmitScalarSetF(context, op.Rd, op.Size);
  199. }
  200. public static void Fmov_Si(ILEmitterCtx context)
  201. {
  202. OpCodeSimdFmov64 op = (OpCodeSimdFmov64)context.CurrOp;
  203. context.EmitLdc_I8(op.Imm);
  204. EmitScalarSet(context, op.Rd, op.Size + 2);
  205. }
  206. public static void Fmov_V(ILEmitterCtx context)
  207. {
  208. OpCodeSimdImm64 op = (OpCodeSimdImm64)context.CurrOp;
  209. int elems = op.RegisterSize == RegisterSize.Simd128 ? 4 : 2;
  210. for (int index = 0; index < (elems >> op.Size); index++)
  211. {
  212. context.EmitLdc_I8(op.Imm);
  213. EmitVectorInsert(context, op.Rd, index, op.Size + 2);
  214. }
  215. if (op.RegisterSize == RegisterSize.Simd64)
  216. {
  217. EmitVectorZeroUpper(context, op.Rd);
  218. }
  219. }
  220. public static void Ins_Gp(ILEmitterCtx context)
  221. {
  222. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  223. context.EmitLdintzr(op.Rn);
  224. EmitVectorInsert(context, op.Rd, op.DstIndex, op.Size);
  225. }
  226. public static void Ins_V(ILEmitterCtx context)
  227. {
  228. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  229. EmitVectorExtractZx(context, op.Rn, op.SrcIndex, op.Size);
  230. EmitVectorInsert(context, op.Rd, op.DstIndex, op.Size);
  231. }
  232. public static void Movi_V(ILEmitterCtx context)
  233. {
  234. if (Optimizations.UseSse2)
  235. {
  236. EmitMoviMvni(context, not: false);
  237. }
  238. else
  239. {
  240. EmitVectorImmUnaryOp(context, () => { });
  241. }
  242. }
  243. public static void Mvni_V(ILEmitterCtx context)
  244. {
  245. if (Optimizations.UseSse2)
  246. {
  247. EmitMoviMvni(context, not: true);
  248. }
  249. else
  250. {
  251. EmitVectorImmUnaryOp(context, () => context.Emit(OpCodes.Not));
  252. }
  253. }
  254. public static void Smov_S(ILEmitterCtx context)
  255. {
  256. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  257. EmitVectorExtractSx(context, op.Rn, op.DstIndex, op.Size);
  258. EmitIntZeroUpperIfNeeded(context);
  259. context.EmitStintzr(op.Rd);
  260. }
  261. public static void Tbl_V(ILEmitterCtx context)
  262. {
  263. OpCodeSimdTbl64 op = (OpCodeSimdTbl64)context.CurrOp;
  264. if (Optimizations.UseSsse3)
  265. {
  266. Type[] typesCmpSflSub = new Type[] { typeof(Vector128<sbyte>), typeof(Vector128<sbyte>) };
  267. Type[] typesOr = new Type[] { typeof(Vector128<long>), typeof(Vector128<long>) };
  268. Type[] typesSav = new Type[] { typeof(long) };
  269. context.EmitLdvec(op.Rn);
  270. context.EmitLdvec(op.Rm);
  271. context.EmitLdc_I8(0x0F0F0F0F0F0F0F0FL);
  272. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  273. context.EmitStvectmp2();
  274. context.EmitLdvectmp2();
  275. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), typesCmpSflSub));
  276. context.EmitLdvec(op.Rm);
  277. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesOr));
  278. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), typesCmpSflSub));
  279. for (int index = 1; index < op.Size; index++)
  280. {
  281. context.EmitLdvec((op.Rn + index) & 0x1F);
  282. context.EmitLdvec(op.Rm);
  283. context.EmitLdc_I8(0x1010101010101010L * index);
  284. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  285. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesCmpSflSub));
  286. context.EmitStvectmp();
  287. context.EmitLdvectmp();
  288. context.EmitLdvectmp2();
  289. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.CompareGreaterThan), typesCmpSflSub));
  290. context.EmitLdvectmp();
  291. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesOr));
  292. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), typesCmpSflSub));
  293. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Or), typesOr));
  294. }
  295. context.EmitStvec(op.Rd);
  296. if (op.RegisterSize == RegisterSize.Simd64)
  297. {
  298. EmitVectorZeroUpper(context, op.Rd);
  299. }
  300. }
  301. else
  302. {
  303. context.EmitLdvec(op.Rm);
  304. for (int index = 0; index < op.Size; index++)
  305. {
  306. context.EmitLdvec((op.Rn + index) & 0x1F);
  307. }
  308. switch (op.Size)
  309. {
  310. case 1: VectorHelper.EmitCall(context,
  311. nameof(VectorHelper.Tbl1_V64),
  312. nameof(VectorHelper.Tbl1_V128)); break;
  313. case 2: VectorHelper.EmitCall(context,
  314. nameof(VectorHelper.Tbl2_V64),
  315. nameof(VectorHelper.Tbl2_V128)); break;
  316. case 3: VectorHelper.EmitCall(context,
  317. nameof(VectorHelper.Tbl3_V64),
  318. nameof(VectorHelper.Tbl3_V128)); break;
  319. case 4: VectorHelper.EmitCall(context,
  320. nameof(VectorHelper.Tbl4_V64),
  321. nameof(VectorHelper.Tbl4_V128)); break;
  322. default: throw new InvalidOperationException();
  323. }
  324. context.EmitStvec(op.Rd);
  325. }
  326. }
  327. public static void Trn1_V(ILEmitterCtx context)
  328. {
  329. EmitVectorTranspose(context, part: 0);
  330. }
  331. public static void Trn2_V(ILEmitterCtx context)
  332. {
  333. EmitVectorTranspose(context, part: 1);
  334. }
  335. public static void Umov_S(ILEmitterCtx context)
  336. {
  337. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  338. EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  339. context.EmitStintzr(op.Rd);
  340. }
  341. public static void Uzp1_V(ILEmitterCtx context)
  342. {
  343. EmitVectorUnzip(context, part: 0);
  344. }
  345. public static void Uzp2_V(ILEmitterCtx context)
  346. {
  347. EmitVectorUnzip(context, part: 1);
  348. }
  349. public static void Xtn_V(ILEmitterCtx context)
  350. {
  351. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  352. if (Optimizations.UseSsse3)
  353. {
  354. Type[] typesSve = new Type[] { typeof(long), typeof(long) };
  355. string nameMov = op.RegisterSize == RegisterSize.Simd128
  356. ? nameof(Sse.MoveLowToHigh)
  357. : nameof(Sse.MoveHighToLow);
  358. context.EmitLdvec(op.Rd);
  359. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  360. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  361. context.EmitLdvec(op.Rn); // value
  362. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // mask
  363. context.Emit(OpCodes.Dup); // mask
  364. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  365. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  366. context.EmitCall(typeof(Sse).GetMethod(nameMov));
  367. context.EmitStvec(op.Rd);
  368. }
  369. else
  370. {
  371. int elems = 8 >> op.Size;
  372. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  373. if (part != 0)
  374. {
  375. context.EmitLdvec(op.Rd);
  376. context.EmitStvectmp();
  377. }
  378. for (int index = 0; index < elems; index++)
  379. {
  380. EmitVectorExtractZx(context, op.Rn, index, op.Size + 1);
  381. EmitVectorInsertTmp(context, part + index, op.Size);
  382. }
  383. context.EmitLdvectmp();
  384. context.EmitStvec(op.Rd);
  385. if (part == 0)
  386. {
  387. EmitVectorZeroUpper(context, op.Rd);
  388. }
  389. }
  390. }
  391. public static void Zip1_V(ILEmitterCtx context)
  392. {
  393. EmitVectorZip(context, part: 0);
  394. }
  395. public static void Zip2_V(ILEmitterCtx context)
  396. {
  397. EmitVectorZip(context, part: 1);
  398. }
  399. private static void EmitIntZeroUpperIfNeeded(ILEmitterCtx context)
  400. {
  401. if (context.CurrOp.RegisterSize == RegisterSize.Int32 ||
  402. context.CurrOp.RegisterSize == RegisterSize.Simd64)
  403. {
  404. context.Emit(OpCodes.Conv_U4);
  405. context.Emit(OpCodes.Conv_U8);
  406. }
  407. }
  408. private static void EmitMoviMvni(ILEmitterCtx context, bool not)
  409. {
  410. OpCodeSimdImm64 op = (OpCodeSimdImm64)context.CurrOp;
  411. Type[] typesSav = new Type[] { UIntTypesPerSizeLog2[op.Size] };
  412. long imm = op.Imm;
  413. if (not)
  414. {
  415. imm = ~imm;
  416. }
  417. if (op.Size < 3)
  418. {
  419. context.EmitLdc_I4((int)imm);
  420. }
  421. else
  422. {
  423. context.EmitLdc_I8(imm);
  424. }
  425. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  426. context.EmitStvec(op.Rd);
  427. if (op.RegisterSize == RegisterSize.Simd64)
  428. {
  429. EmitVectorZeroUpper(context, op.Rd);
  430. }
  431. }
  432. private static void EmitVectorTranspose(ILEmitterCtx context, int part)
  433. {
  434. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  435. if (Optimizations.UseSsse3)
  436. {
  437. Type[] typesSve = new Type[] { typeof(long), typeof(long) };
  438. string nameUpk = part == 0
  439. ? nameof(Sse2.UnpackLow)
  440. : nameof(Sse2.UnpackHigh);
  441. context.EmitLdvec(op.Rn); // value
  442. if (op.Size < 3)
  443. {
  444. context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1
  445. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0
  446. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  447. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  448. }
  449. context.EmitLdvec(op.Rm); // value
  450. if (op.Size < 3)
  451. {
  452. context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1
  453. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0
  454. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  455. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  456. }
  457. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(op.Size)));
  458. context.EmitStvec(op.Rd);
  459. }
  460. else
  461. {
  462. int words = op.GetBitsCount() >> 4;
  463. int pairs = words >> op.Size;
  464. for (int index = 0; index < pairs; index++)
  465. {
  466. int idx = index << 1;
  467. EmitVectorExtractZx(context, op.Rn, idx + part, op.Size);
  468. EmitVectorExtractZx(context, op.Rm, idx + part, op.Size);
  469. EmitVectorInsertTmp(context, idx + 1, op.Size);
  470. EmitVectorInsertTmp(context, idx, op.Size);
  471. }
  472. context.EmitLdvectmp();
  473. context.EmitStvec(op.Rd);
  474. }
  475. if (op.RegisterSize == RegisterSize.Simd64)
  476. {
  477. EmitVectorZeroUpper(context, op.Rd);
  478. }
  479. }
  480. private static void EmitVectorUnzip(ILEmitterCtx context, int part)
  481. {
  482. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  483. if (Optimizations.UseSsse3)
  484. {
  485. Type[] typesSve = new Type[] { typeof(long), typeof(long) };
  486. string nameUpk = part == 0
  487. ? nameof(Sse2.UnpackLow)
  488. : nameof(Sse2.UnpackHigh);
  489. if (op.RegisterSize == RegisterSize.Simd128)
  490. {
  491. context.EmitLdvec(op.Rn); // value
  492. if (op.Size < 3)
  493. {
  494. context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1
  495. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0
  496. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  497. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  498. }
  499. context.EmitLdvec(op.Rm); // value
  500. if (op.Size < 3)
  501. {
  502. context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1
  503. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0
  504. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  505. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  506. }
  507. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(3)));
  508. context.EmitStvec(op.Rd);
  509. }
  510. else
  511. {
  512. context.EmitLdvec(op.Rn);
  513. context.EmitLdvec(op.Rm);
  514. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackLow), GetTypesSflUpk(op.Size))); // value
  515. if (op.Size < 2)
  516. {
  517. context.EmitLdc_I8(_masksE1_Uzp[op.Size]); // maskE1
  518. context.EmitLdc_I8(_masksE0_Uzp[op.Size]); // maskE0
  519. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  520. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  521. }
  522. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  523. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(3)));
  524. context.EmitStvec(op.Rd);
  525. }
  526. }
  527. else
  528. {
  529. int words = op.GetBitsCount() >> 4;
  530. int pairs = words >> op.Size;
  531. for (int index = 0; index < pairs; index++)
  532. {
  533. int idx = index << 1;
  534. EmitVectorExtractZx(context, op.Rn, idx + part, op.Size);
  535. EmitVectorExtractZx(context, op.Rm, idx + part, op.Size);
  536. EmitVectorInsertTmp(context, pairs + index, op.Size);
  537. EmitVectorInsertTmp(context, index, op.Size);
  538. }
  539. context.EmitLdvectmp();
  540. context.EmitStvec(op.Rd);
  541. if (op.RegisterSize == RegisterSize.Simd64)
  542. {
  543. EmitVectorZeroUpper(context, op.Rd);
  544. }
  545. }
  546. }
  547. private static void EmitVectorZip(ILEmitterCtx context, int part)
  548. {
  549. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  550. if (Optimizations.UseSse2)
  551. {
  552. string nameUpk = part == 0
  553. ? nameof(Sse2.UnpackLow)
  554. : nameof(Sse2.UnpackHigh);
  555. context.EmitLdvec(op.Rn);
  556. context.EmitLdvec(op.Rm);
  557. if (op.RegisterSize == RegisterSize.Simd128)
  558. {
  559. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(op.Size)));
  560. }
  561. else
  562. {
  563. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackLow), GetTypesSflUpk(op.Size)));
  564. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  565. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(3)));
  566. }
  567. context.EmitStvec(op.Rd);
  568. }
  569. else
  570. {
  571. int words = op.GetBitsCount() >> 4;
  572. int pairs = words >> op.Size;
  573. int Base = part != 0 ? pairs : 0;
  574. for (int index = 0; index < pairs; index++)
  575. {
  576. int idx = index << 1;
  577. EmitVectorExtractZx(context, op.Rn, Base + index, op.Size);
  578. EmitVectorExtractZx(context, op.Rm, Base + index, op.Size);
  579. EmitVectorInsertTmp(context, idx + 1, op.Size);
  580. EmitVectorInsertTmp(context, idx, op.Size);
  581. }
  582. context.EmitLdvectmp();
  583. context.EmitStvec(op.Rd);
  584. if (op.RegisterSize == RegisterSize.Simd64)
  585. {
  586. EmitVectorZeroUpper(context, op.Rd);
  587. }
  588. }
  589. }
  590. private static Type[] GetTypesSflUpk(int size)
  591. {
  592. return new Type[] { VectorIntTypesPerSizeLog2[size], VectorIntTypesPerSizeLog2[size] };
  593. }
  594. }
  595. }