InstEmitSimdMove.cs 26 KB

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