InstEmitSimdMove.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  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. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  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. EmitLdvecWithUnsignedCast(context, op.Rn, 0);
  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. EmitLdvecWithUnsignedCast(context, op.Rm, 0);
  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. EmitStvecWithUnsignedCast(context, op.Rd, 0);
  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. EmitVectorImmUnaryOp(context, () => { });
  235. }
  236. public static void Mvni_V(ILEmitterCtx context)
  237. {
  238. EmitVectorImmUnaryOp(context, () => context.Emit(OpCodes.Not));
  239. }
  240. public static void Smov_S(ILEmitterCtx context)
  241. {
  242. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  243. EmitVectorExtractSx(context, op.Rn, op.DstIndex, op.Size);
  244. EmitIntZeroUpperIfNeeded(context);
  245. context.EmitStintzr(op.Rd);
  246. }
  247. public static void Tbl_V(ILEmitterCtx context)
  248. {
  249. OpCodeSimdTbl64 op = (OpCodeSimdTbl64)context.CurrOp;
  250. context.EmitLdvec(op.Rm);
  251. for (int index = 0; index < op.Size; index++)
  252. {
  253. context.EmitLdvec((op.Rn + index) & 0x1f);
  254. }
  255. switch (op.Size)
  256. {
  257. case 1: VectorHelper.EmitCall(context,
  258. nameof(VectorHelper.Tbl1_V64),
  259. nameof(VectorHelper.Tbl1_V128)); break;
  260. case 2: VectorHelper.EmitCall(context,
  261. nameof(VectorHelper.Tbl2_V64),
  262. nameof(VectorHelper.Tbl2_V128)); break;
  263. case 3: VectorHelper.EmitCall(context,
  264. nameof(VectorHelper.Tbl3_V64),
  265. nameof(VectorHelper.Tbl3_V128)); break;
  266. case 4: VectorHelper.EmitCall(context,
  267. nameof(VectorHelper.Tbl4_V64),
  268. nameof(VectorHelper.Tbl4_V128)); break;
  269. default: throw new InvalidOperationException();
  270. }
  271. context.EmitStvec(op.Rd);
  272. }
  273. public static void Trn1_V(ILEmitterCtx context)
  274. {
  275. EmitVectorTranspose(context, part: 0);
  276. }
  277. public static void Trn2_V(ILEmitterCtx context)
  278. {
  279. EmitVectorTranspose(context, part: 1);
  280. }
  281. public static void Umov_S(ILEmitterCtx context)
  282. {
  283. OpCodeSimdIns64 op = (OpCodeSimdIns64)context.CurrOp;
  284. EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  285. context.EmitStintzr(op.Rd);
  286. }
  287. public static void Uzp1_V(ILEmitterCtx context)
  288. {
  289. EmitVectorUnzip(context, part: 0);
  290. }
  291. public static void Uzp2_V(ILEmitterCtx context)
  292. {
  293. EmitVectorUnzip(context, part: 1);
  294. }
  295. public static void Xtn_V(ILEmitterCtx context)
  296. {
  297. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  298. if (Optimizations.UseSsse3)
  299. {
  300. Type[] typesSve = new Type[] { typeof(long), typeof(long) };
  301. string nameMov = op.RegisterSize == RegisterSize.Simd128
  302. ? nameof(Sse.MoveLowToHigh)
  303. : nameof(Sse.MoveHighToLow);
  304. context.EmitLdvec(op.Rd);
  305. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorSingleZero));
  306. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MoveLowToHigh)));
  307. EmitLdvecWithSignedCast(context, op.Rn, 0); // value
  308. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // mask
  309. context.Emit(OpCodes.Dup); // mask
  310. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  311. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  312. context.EmitCall(typeof(Sse).GetMethod(nameMov));
  313. context.EmitStvec(op.Rd);
  314. }
  315. else
  316. {
  317. int elems = 8 >> op.Size;
  318. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  319. if (part != 0)
  320. {
  321. context.EmitLdvec(op.Rd);
  322. context.EmitStvectmp();
  323. }
  324. for (int index = 0; index < elems; index++)
  325. {
  326. EmitVectorExtractZx(context, op.Rn, index, op.Size + 1);
  327. EmitVectorInsertTmp(context, part + index, op.Size);
  328. }
  329. context.EmitLdvectmp();
  330. context.EmitStvec(op.Rd);
  331. if (part == 0)
  332. {
  333. EmitVectorZeroUpper(context, op.Rd);
  334. }
  335. }
  336. }
  337. public static void Zip1_V(ILEmitterCtx context)
  338. {
  339. EmitVectorZip(context, part: 0);
  340. }
  341. public static void Zip2_V(ILEmitterCtx context)
  342. {
  343. EmitVectorZip(context, part: 1);
  344. }
  345. private static void EmitIntZeroUpperIfNeeded(ILEmitterCtx context)
  346. {
  347. if (context.CurrOp.RegisterSize == RegisterSize.Int32 ||
  348. context.CurrOp.RegisterSize == RegisterSize.Simd64)
  349. {
  350. context.Emit(OpCodes.Conv_U4);
  351. context.Emit(OpCodes.Conv_U8);
  352. }
  353. }
  354. private static void EmitVectorTranspose(ILEmitterCtx context, int part)
  355. {
  356. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  357. if (Optimizations.UseSsse3)
  358. {
  359. Type[] typesSve = new Type[] { typeof(long), typeof(long) };
  360. string nameUpk = part == 0
  361. ? nameof(Sse2.UnpackLow)
  362. : nameof(Sse2.UnpackHigh);
  363. EmitLdvecWithSignedCast(context, op.Rn, op.Size); // value
  364. if (op.Size < 3)
  365. {
  366. context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1
  367. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0
  368. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  369. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  370. }
  371. EmitLdvecWithSignedCast(context, op.Rm, op.Size); // value
  372. if (op.Size < 3)
  373. {
  374. context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1
  375. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0
  376. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  377. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  378. }
  379. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(op.Size)));
  380. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  381. }
  382. else
  383. {
  384. int words = op.GetBitsCount() >> 4;
  385. int pairs = words >> op.Size;
  386. for (int index = 0; index < pairs; index++)
  387. {
  388. int idx = index << 1;
  389. EmitVectorExtractZx(context, op.Rn, idx + part, op.Size);
  390. EmitVectorExtractZx(context, op.Rm, idx + part, op.Size);
  391. EmitVectorInsertTmp(context, idx + 1, op.Size);
  392. EmitVectorInsertTmp(context, idx, op.Size);
  393. }
  394. context.EmitLdvectmp();
  395. context.EmitStvec(op.Rd);
  396. }
  397. if (op.RegisterSize == RegisterSize.Simd64)
  398. {
  399. EmitVectorZeroUpper(context, op.Rd);
  400. }
  401. }
  402. private static void EmitVectorUnzip(ILEmitterCtx context, int part)
  403. {
  404. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  405. if (Optimizations.UseSsse3)
  406. {
  407. Type[] typesSve = new Type[] { typeof(long), typeof(long) };
  408. string nameUpk = part == 0
  409. ? nameof(Sse2.UnpackLow)
  410. : nameof(Sse2.UnpackHigh);
  411. if (op.RegisterSize == RegisterSize.Simd128)
  412. {
  413. EmitLdvecWithSignedCast(context, op.Rn, op.Size); // value
  414. if (op.Size < 3)
  415. {
  416. context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1
  417. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0
  418. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  419. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  420. }
  421. EmitLdvecWithSignedCast(context, op.Rm, op.Size); // value
  422. if (op.Size < 3)
  423. {
  424. context.EmitLdc_I8(_masksE1_TrnUzp [op.Size]); // maskE1
  425. context.EmitLdc_I8(_masksE0_TrnUzpXtn[op.Size]); // maskE0
  426. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  427. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  428. }
  429. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(3)));
  430. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  431. }
  432. else
  433. {
  434. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  435. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  436. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackLow), GetTypesSflUpk(op.Size))); // value
  437. if (op.Size < 2)
  438. {
  439. context.EmitLdc_I8(_masksE1_Uzp[op.Size]); // maskE1
  440. context.EmitLdc_I8(_masksE0_Uzp[op.Size]); // maskE0
  441. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetVector128), typesSve));
  442. context.EmitCall(typeof(Ssse3).GetMethod(nameof(Ssse3.Shuffle), GetTypesSflUpk(0)));
  443. }
  444. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInt64Zero));
  445. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(3)));
  446. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  447. }
  448. }
  449. else
  450. {
  451. int words = op.GetBitsCount() >> 4;
  452. int pairs = words >> op.Size;
  453. for (int index = 0; index < pairs; index++)
  454. {
  455. int idx = index << 1;
  456. EmitVectorExtractZx(context, op.Rn, idx + part, op.Size);
  457. EmitVectorExtractZx(context, op.Rm, idx + part, op.Size);
  458. EmitVectorInsertTmp(context, pairs + index, op.Size);
  459. EmitVectorInsertTmp(context, index, op.Size);
  460. }
  461. context.EmitLdvectmp();
  462. context.EmitStvec(op.Rd);
  463. if (op.RegisterSize == RegisterSize.Simd64)
  464. {
  465. EmitVectorZeroUpper(context, op.Rd);
  466. }
  467. }
  468. }
  469. private static void EmitVectorZip(ILEmitterCtx context, int part)
  470. {
  471. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  472. if (Optimizations.UseSse2)
  473. {
  474. string nameUpk = part == 0
  475. ? nameof(Sse2.UnpackLow)
  476. : nameof(Sse2.UnpackHigh);
  477. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  478. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  479. if (op.RegisterSize == RegisterSize.Simd128)
  480. {
  481. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(op.Size)));
  482. }
  483. else
  484. {
  485. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.UnpackLow), GetTypesSflUpk(op.Size)));
  486. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorInt64Zero));
  487. context.EmitCall(typeof(Sse2).GetMethod(nameUpk, GetTypesSflUpk(3)));
  488. }
  489. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  490. }
  491. else
  492. {
  493. int words = op.GetBitsCount() >> 4;
  494. int pairs = words >> op.Size;
  495. int Base = part != 0 ? pairs : 0;
  496. for (int index = 0; index < pairs; index++)
  497. {
  498. int idx = index << 1;
  499. EmitVectorExtractZx(context, op.Rn, Base + index, op.Size);
  500. EmitVectorExtractZx(context, op.Rm, Base + index, op.Size);
  501. EmitVectorInsertTmp(context, idx + 1, op.Size);
  502. EmitVectorInsertTmp(context, idx, op.Size);
  503. }
  504. context.EmitLdvectmp();
  505. context.EmitStvec(op.Rd);
  506. if (op.RegisterSize == RegisterSize.Simd64)
  507. {
  508. EmitVectorZeroUpper(context, op.Rd);
  509. }
  510. }
  511. }
  512. private static Type[] GetTypesSflUpk(int size)
  513. {
  514. return new Type[] { VectorIntTypesPerSizeLog2[size], VectorIntTypesPerSizeLog2[size] };
  515. }
  516. }
  517. }