InstEmitSimdMove.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using static ARMeilleure.Instructions.InstEmitHelper;
  7. using static ARMeilleure.Instructions.InstEmitSimdHelper;
  8. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static partial class InstEmit
  12. {
  13. #region "Masks"
  14. private static readonly long[] _masksE0_Uzp = new long[]
  15. {
  16. 13L << 56 | 09L << 48 | 05L << 40 | 01L << 32 | 12L << 24 | 08L << 16 | 04L << 8 | 00L << 0,
  17. 11L << 56 | 10L << 48 | 03L << 40 | 02L << 32 | 09L << 24 | 08L << 16 | 01L << 8 | 00L << 0
  18. };
  19. private static readonly long[] _masksE1_Uzp = new long[]
  20. {
  21. 15L << 56 | 11L << 48 | 07L << 40 | 03L << 32 | 14L << 24 | 10L << 16 | 06L << 8 | 02L << 0,
  22. 15L << 56 | 14L << 48 | 07L << 40 | 06L << 32 | 13L << 24 | 12L << 16 | 05L << 8 | 04L << 0
  23. };
  24. #endregion
  25. public static void Dup_Gp(ArmEmitterContext context)
  26. {
  27. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  28. Operand n = GetIntOrZR(context, op.Rn);
  29. if (Optimizations.UseSse2)
  30. {
  31. switch (op.Size)
  32. {
  33. case 0: n = context.ZeroExtend8 (n.Type, n); n = context.Multiply(n, Const(n.Type, 0x01010101)); break;
  34. case 1: n = context.ZeroExtend16(n.Type, n); n = context.Multiply(n, Const(n.Type, 0x00010001)); break;
  35. case 2: n = context.ZeroExtend32(n.Type, n); break;
  36. }
  37. Operand res = context.VectorInsert(context.VectorZero(), n, 0);
  38. if (op.Size < 3)
  39. {
  40. if (op.RegisterSize == RegisterSize.Simd64)
  41. {
  42. res = context.AddIntrinsic(Intrinsic.X86Shufps, res, res, Const(0xf0));
  43. }
  44. else
  45. {
  46. res = context.AddIntrinsic(Intrinsic.X86Shufps, res, res, Const(0));
  47. }
  48. }
  49. else
  50. {
  51. res = context.AddIntrinsic(Intrinsic.X86Movlhps, res, res);
  52. }
  53. context.Copy(GetVec(op.Rd), res);
  54. }
  55. else
  56. {
  57. Operand res = context.VectorZero();
  58. int elems = op.GetBytesCount() >> op.Size;
  59. for (int index = 0; index < elems; index++)
  60. {
  61. res = EmitVectorInsert(context, res, n, index, op.Size);
  62. }
  63. context.Copy(GetVec(op.Rd), res);
  64. }
  65. }
  66. public static void Dup_S(ArmEmitterContext context)
  67. {
  68. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  69. Operand ne = EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  70. context.Copy(GetVec(op.Rd), EmitVectorInsert(context, context.VectorZero(), ne, 0, op.Size));
  71. }
  72. public static void Dup_V(ArmEmitterContext context)
  73. {
  74. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  75. if (Optimizations.UseSse2)
  76. {
  77. Operand res = GetVec(op.Rn);
  78. if (op.Size == 0)
  79. {
  80. if (op.DstIndex != 0)
  81. {
  82. res = context.AddIntrinsic(Intrinsic.X86Psrldq, res, Const(op.DstIndex));
  83. }
  84. res = context.AddIntrinsic(Intrinsic.X86Punpcklbw, res, res);
  85. res = context.AddIntrinsic(Intrinsic.X86Punpcklwd, res, res);
  86. res = context.AddIntrinsic(Intrinsic.X86Shufps, res, res, Const(0));
  87. }
  88. else if (op.Size == 1)
  89. {
  90. if (op.DstIndex != 0)
  91. {
  92. res = context.AddIntrinsic(Intrinsic.X86Psrldq, res, Const(op.DstIndex * 2));
  93. }
  94. res = context.AddIntrinsic(Intrinsic.X86Punpcklwd, res, res);
  95. res = context.AddIntrinsic(Intrinsic.X86Shufps, res, res, Const(0));
  96. }
  97. else if (op.Size == 2)
  98. {
  99. int mask = op.DstIndex * 0b01010101;
  100. res = context.AddIntrinsic(Intrinsic.X86Shufps, res, res, Const(mask));
  101. }
  102. else if (op.DstIndex == 0 && op.RegisterSize != RegisterSize.Simd64)
  103. {
  104. res = context.AddIntrinsic(Intrinsic.X86Movlhps, res, res);
  105. }
  106. else if (op.DstIndex == 1)
  107. {
  108. res = context.AddIntrinsic(Intrinsic.X86Movhlps, res, res);
  109. }
  110. if (op.RegisterSize == RegisterSize.Simd64)
  111. {
  112. res = context.VectorZeroUpper64(res);
  113. }
  114. context.Copy(GetVec(op.Rd), res);
  115. }
  116. else
  117. {
  118. Operand ne = EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  119. Operand res = context.VectorZero();
  120. int elems = op.GetBytesCount() >> op.Size;
  121. for (int index = 0; index < elems; index++)
  122. {
  123. res = EmitVectorInsert(context, res, ne, index, op.Size);
  124. }
  125. context.Copy(GetVec(op.Rd), res);
  126. }
  127. }
  128. public static void Ext_V(ArmEmitterContext context)
  129. {
  130. OpCodeSimdExt op = (OpCodeSimdExt)context.CurrOp;
  131. if (Optimizations.UseSse2)
  132. {
  133. Operand nShifted = GetVec(op.Rn);
  134. if (op.RegisterSize == RegisterSize.Simd64)
  135. {
  136. nShifted = context.VectorZeroUpper64(nShifted);
  137. }
  138. nShifted = context.AddIntrinsic(Intrinsic.X86Psrldq, nShifted, Const(op.Imm4));
  139. Operand mShifted = GetVec(op.Rm);
  140. mShifted = context.AddIntrinsic(Intrinsic.X86Pslldq, mShifted, Const(op.GetBytesCount() - op.Imm4));
  141. if (op.RegisterSize == RegisterSize.Simd64)
  142. {
  143. mShifted = context.VectorZeroUpper64(mShifted);
  144. }
  145. Operand res = context.AddIntrinsic(Intrinsic.X86Por, nShifted, mShifted);
  146. context.Copy(GetVec(op.Rd), res);
  147. }
  148. else
  149. {
  150. Operand res = context.VectorZero();
  151. int bytes = op.GetBytesCount();
  152. int position = op.Imm4 & (bytes - 1);
  153. for (int index = 0; index < bytes; index++)
  154. {
  155. int reg = op.Imm4 + index < bytes ? op.Rn : op.Rm;
  156. Operand e = EmitVectorExtractZx(context, reg, position, 0);
  157. position = (position + 1) & (bytes - 1);
  158. res = EmitVectorInsert(context, res, e, index, 0);
  159. }
  160. context.Copy(GetVec(op.Rd), res);
  161. }
  162. }
  163. public static void Fcsel_S(ArmEmitterContext context)
  164. {
  165. OpCodeSimdFcond op = (OpCodeSimdFcond)context.CurrOp;
  166. Operand lblTrue = Label();
  167. Operand lblEnd = Label();
  168. Operand isTrue = InstEmitFlowHelper.GetCondTrue(context, op.Cond);
  169. context.BranchIfTrue(lblTrue, isTrue);
  170. OperandType type = op.Size == 0 ? OperandType.FP32 : OperandType.FP64;
  171. Operand me = context.VectorExtract(type, GetVec(op.Rm), 0);
  172. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), me, 0));
  173. context.Branch(lblEnd);
  174. context.MarkLabel(lblTrue);
  175. Operand ne = context.VectorExtract(type, GetVec(op.Rn), 0);
  176. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), ne, 0));
  177. context.MarkLabel(lblEnd);
  178. }
  179. public static void Fmov_Ftoi(ArmEmitterContext context)
  180. {
  181. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  182. Operand ne = EmitVectorExtractZx(context, op.Rn, 0, op.Size + 2);
  183. SetIntOrZR(context, op.Rd, ne);
  184. }
  185. public static void Fmov_Ftoi1(ArmEmitterContext context)
  186. {
  187. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  188. Operand ne = EmitVectorExtractZx(context, op.Rn, 1, 3);
  189. SetIntOrZR(context, op.Rd, ne);
  190. }
  191. public static void Fmov_Itof(ArmEmitterContext context)
  192. {
  193. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  194. Operand n = GetIntOrZR(context, op.Rn);
  195. context.Copy(GetVec(op.Rd), EmitVectorInsert(context, context.VectorZero(), n, 0, op.Size + 2));
  196. }
  197. public static void Fmov_Itof1(ArmEmitterContext context)
  198. {
  199. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  200. Operand d = GetVec(op.Rd);
  201. Operand n = GetIntOrZR(context, op.Rn);
  202. context.Copy(d, EmitVectorInsert(context, d, n, 1, 3));
  203. }
  204. public static void Fmov_S(ArmEmitterContext context)
  205. {
  206. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  207. OperandType type = op.Size == 0 ? OperandType.FP32 : OperandType.FP64;
  208. Operand ne = context.VectorExtract(type, GetVec(op.Rn), 0);
  209. context.Copy(GetVec(op.Rd), context.VectorInsert(context.VectorZero(), ne, 0));
  210. }
  211. public static void Fmov_Si(ArmEmitterContext context)
  212. {
  213. OpCodeSimdFmov op = (OpCodeSimdFmov)context.CurrOp;
  214. if (Optimizations.UseSse2)
  215. {
  216. if (op.Size == 0)
  217. {
  218. context.Copy(GetVec(op.Rd), X86GetScalar(context, (int)op.Immediate));
  219. }
  220. else
  221. {
  222. context.Copy(GetVec(op.Rd), X86GetScalar(context, op.Immediate));
  223. }
  224. }
  225. else
  226. {
  227. Operand e = Const(op.Immediate);
  228. Operand res = context.VectorZero();
  229. res = EmitVectorInsert(context, res, e, 0, op.Size + 2);
  230. context.Copy(GetVec(op.Rd), res);
  231. }
  232. }
  233. public static void Fmov_Vi(ArmEmitterContext context)
  234. {
  235. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  236. if (Optimizations.UseSse2)
  237. {
  238. if (op.RegisterSize == RegisterSize.Simd128)
  239. {
  240. context.Copy(GetVec(op.Rd), X86GetAllElements(context, op.Immediate));
  241. }
  242. else
  243. {
  244. context.Copy(GetVec(op.Rd), X86GetScalar(context, op.Immediate));
  245. }
  246. }
  247. else
  248. {
  249. Operand e = Const(op.Immediate);
  250. Operand res = context.VectorZero();
  251. int elems = op.RegisterSize == RegisterSize.Simd128 ? 2 : 1;
  252. for (int index = 0; index < elems; index++)
  253. {
  254. res = EmitVectorInsert(context, res, e, index, 3);
  255. }
  256. context.Copy(GetVec(op.Rd), res);
  257. }
  258. }
  259. public static void Ins_Gp(ArmEmitterContext context)
  260. {
  261. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  262. Operand d = GetVec(op.Rd);
  263. Operand n = GetIntOrZR(context, op.Rn);
  264. context.Copy(d, EmitVectorInsert(context, d, n, op.DstIndex, op.Size));
  265. }
  266. public static void Ins_V(ArmEmitterContext context)
  267. {
  268. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  269. Operand d = GetVec(op.Rd);
  270. Operand ne = EmitVectorExtractZx(context, op.Rn, op.SrcIndex, op.Size);
  271. context.Copy(d, EmitVectorInsert(context, d, ne, op.DstIndex, op.Size));
  272. }
  273. public static void Movi_V(ArmEmitterContext context)
  274. {
  275. if (Optimizations.UseSse2)
  276. {
  277. EmitSse2VectorMoviMvniOp(context, not: false);
  278. }
  279. else
  280. {
  281. EmitVectorImmUnaryOp(context, (op1) => op1);
  282. }
  283. }
  284. public static void Mvni_V(ArmEmitterContext context)
  285. {
  286. if (Optimizations.UseSse2)
  287. {
  288. EmitSse2VectorMoviMvniOp(context, not: true);
  289. }
  290. else
  291. {
  292. EmitVectorImmUnaryOp(context, (op1) => context.BitwiseNot(op1));
  293. }
  294. }
  295. public static void Smov_S(ArmEmitterContext context)
  296. {
  297. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  298. Operand ne = EmitVectorExtractSx(context, op.Rn, op.DstIndex, op.Size);
  299. if (op.RegisterSize == RegisterSize.Simd64)
  300. {
  301. ne = context.ZeroExtend32(OperandType.I64, ne);
  302. }
  303. SetIntOrZR(context, op.Rd, ne);
  304. }
  305. public static void Tbl_V(ArmEmitterContext context)
  306. {
  307. EmitTableVectorLookup(context, isTbl: true);
  308. }
  309. public static void Tbx_V(ArmEmitterContext context)
  310. {
  311. EmitTableVectorLookup(context, isTbl: false);
  312. }
  313. public static void Trn1_V(ArmEmitterContext context)
  314. {
  315. EmitVectorTranspose(context, part: 0);
  316. }
  317. public static void Trn2_V(ArmEmitterContext context)
  318. {
  319. EmitVectorTranspose(context, part: 1);
  320. }
  321. public static void Umov_S(ArmEmitterContext context)
  322. {
  323. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  324. Operand ne = EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  325. SetIntOrZR(context, op.Rd, ne);
  326. }
  327. public static void Uzp1_V(ArmEmitterContext context)
  328. {
  329. EmitVectorUnzip(context, part: 0);
  330. }
  331. public static void Uzp2_V(ArmEmitterContext context)
  332. {
  333. EmitVectorUnzip(context, part: 1);
  334. }
  335. public static void Xtn_V(ArmEmitterContext context)
  336. {
  337. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  338. if (Optimizations.UseSsse3)
  339. {
  340. Operand d = GetVec(op.Rd);
  341. Operand res = context.VectorZeroUpper64(d);
  342. Operand mask = X86GetAllElements(context, EvenMasks[op.Size]);
  343. Operand res2 = context.AddIntrinsic(Intrinsic.X86Pshufb, GetVec(op.Rn), mask);
  344. Intrinsic movInst = op.RegisterSize == RegisterSize.Simd128
  345. ? Intrinsic.X86Movlhps
  346. : Intrinsic.X86Movhlps;
  347. res = context.AddIntrinsic(movInst, res, res2);
  348. context.Copy(d, res);
  349. }
  350. else
  351. {
  352. int elems = 8 >> op.Size;
  353. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  354. Operand d = GetVec(op.Rd);
  355. Operand res = part == 0 ? context.VectorZero() : context.Copy(d);
  356. for (int index = 0; index < elems; index++)
  357. {
  358. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size + 1);
  359. res = EmitVectorInsert(context, res, ne, part + index, op.Size);
  360. }
  361. context.Copy(d, res);
  362. }
  363. }
  364. public static void Zip1_V(ArmEmitterContext context)
  365. {
  366. EmitVectorZip(context, part: 0);
  367. }
  368. public static void Zip2_V(ArmEmitterContext context)
  369. {
  370. EmitVectorZip(context, part: 1);
  371. }
  372. private static void EmitSse2VectorMoviMvniOp(ArmEmitterContext context, bool not)
  373. {
  374. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  375. long imm = op.Immediate;
  376. switch (op.Size)
  377. {
  378. case 0: imm *= 0x01010101; break;
  379. case 1: imm *= 0x00010001; break;
  380. }
  381. if (not)
  382. {
  383. imm = ~imm;
  384. }
  385. Operand mask;
  386. if (op.Size < 3)
  387. {
  388. mask = X86GetAllElements(context, (int)imm);
  389. }
  390. else
  391. {
  392. mask = X86GetAllElements(context, imm);
  393. }
  394. if (op.RegisterSize == RegisterSize.Simd64)
  395. {
  396. mask = context.VectorZeroUpper64(mask);
  397. }
  398. context.Copy(GetVec(op.Rd), mask);
  399. }
  400. private static void EmitTableVectorLookup(ArmEmitterContext context, bool isTbl)
  401. {
  402. OpCodeSimdTbl op = (OpCodeSimdTbl)context.CurrOp;
  403. if (Optimizations.UseSsse3)
  404. {
  405. Operand d = GetVec(op.Rd);
  406. Operand m = GetVec(op.Rm);
  407. Operand res;
  408. Operand mask = X86GetAllElements(context, 0x0F0F0F0F0F0F0F0FL);
  409. // Fast path for single register table.
  410. {
  411. Operand n = GetVec(op.Rn);
  412. Operand mMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, m, mask);
  413. mMask = context.AddIntrinsic(Intrinsic.X86Por, mMask, m);
  414. res = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mMask);
  415. }
  416. for (int index = 1; index < op.Size; index++)
  417. {
  418. Operand ni = GetVec((op.Rn + index) & 0x1F);
  419. Operand idxMask = X86GetAllElements(context, 0x1010101010101010L * index);
  420. Operand mSubMask = context.AddIntrinsic(Intrinsic.X86Psubb, m, idxMask);
  421. Operand mMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, mSubMask, mask);
  422. mMask = context.AddIntrinsic(Intrinsic.X86Por, mMask, mSubMask);
  423. Operand res2 = context.AddIntrinsic(Intrinsic.X86Pshufb, ni, mMask);
  424. res = context.AddIntrinsic(Intrinsic.X86Por, res, res2);
  425. }
  426. if (!isTbl)
  427. {
  428. Operand idxMask = X86GetAllElements(context, (0x1010101010101010L * op.Size) - 0x0101010101010101L);
  429. Operand zeroMask = context.VectorZero();
  430. Operand mPosMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, m, idxMask);
  431. Operand mNegMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, zeroMask, m);
  432. Operand mMask = context.AddIntrinsic(Intrinsic.X86Por, mPosMask, mNegMask);
  433. Operand dMask = context.AddIntrinsic(Intrinsic.X86Pand, d, mMask);
  434. res = context.AddIntrinsic(Intrinsic.X86Por, res, dMask);
  435. }
  436. if (op.RegisterSize == RegisterSize.Simd64)
  437. {
  438. res = context.VectorZeroUpper64(res);
  439. }
  440. context.Copy(d, res);
  441. }
  442. else
  443. {
  444. Operand d = GetVec(op.Rd);
  445. List<Operand> args = new List<Operand>();
  446. if (!isTbl)
  447. {
  448. args.Add(d);
  449. }
  450. args.Add(GetVec(op.Rm));
  451. args.Add(Const(op.RegisterSize == RegisterSize.Simd64 ? 8 : 16));
  452. for (int index = 0; index < op.Size; index++)
  453. {
  454. args.Add(GetVec((op.Rn + index) & 0x1F));
  455. }
  456. MethodInfo info = null;
  457. if (isTbl)
  458. {
  459. switch (op.Size)
  460. {
  461. case 1: info = typeof(SoftFallback).GetMethod(nameof(SoftFallback.Tbl1)); break;
  462. case 2: info = typeof(SoftFallback).GetMethod(nameof(SoftFallback.Tbl2)); break;
  463. case 3: info = typeof(SoftFallback).GetMethod(nameof(SoftFallback.Tbl3)); break;
  464. case 4: info = typeof(SoftFallback).GetMethod(nameof(SoftFallback.Tbl4)); break;
  465. }
  466. }
  467. else
  468. {
  469. switch (op.Size)
  470. {
  471. case 1: info = typeof(SoftFallback).GetMethod(nameof(SoftFallback.Tbx1)); break;
  472. case 2: info = typeof(SoftFallback).GetMethod(nameof(SoftFallback.Tbx2)); break;
  473. case 3: info = typeof(SoftFallback).GetMethod(nameof(SoftFallback.Tbx3)); break;
  474. case 4: info = typeof(SoftFallback).GetMethod(nameof(SoftFallback.Tbx4)); break;
  475. }
  476. }
  477. context.Copy(d, context.Call(info, args.ToArray()));
  478. }
  479. }
  480. private static void EmitVectorTranspose(ArmEmitterContext context, int part)
  481. {
  482. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  483. if (Optimizations.UseSsse3)
  484. {
  485. Operand mask = null;
  486. if (op.Size < 3)
  487. {
  488. long maskE0 = EvenMasks[op.Size];
  489. long maskE1 = OddMasks [op.Size];
  490. mask = X86GetScalar(context, maskE0);
  491. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  492. }
  493. Operand n = GetVec(op.Rn);
  494. if (op.Size < 3)
  495. {
  496. n = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mask);
  497. }
  498. Operand m = GetVec(op.Rm);
  499. if (op.Size < 3)
  500. {
  501. m = context.AddIntrinsic(Intrinsic.X86Pshufb, m, mask);
  502. }
  503. Intrinsic punpckInst = part == 0
  504. ? X86PunpcklInstruction[op.Size]
  505. : X86PunpckhInstruction[op.Size];
  506. Operand res = context.AddIntrinsic(punpckInst, n, m);
  507. if (op.RegisterSize == RegisterSize.Simd64)
  508. {
  509. res = context.VectorZeroUpper64(res);
  510. }
  511. context.Copy(GetVec(op.Rd), res);
  512. }
  513. else
  514. {
  515. Operand res = context.VectorZero();
  516. int pairs = op.GetPairsCount() >> op.Size;
  517. for (int index = 0; index < pairs; index++)
  518. {
  519. int pairIndex = index << 1;
  520. Operand ne = EmitVectorExtractZx(context, op.Rn, pairIndex + part, op.Size);
  521. Operand me = EmitVectorExtractZx(context, op.Rm, pairIndex + part, op.Size);
  522. res = EmitVectorInsert(context, res, ne, pairIndex, op.Size);
  523. res = EmitVectorInsert(context, res, me, pairIndex + 1, op.Size);
  524. }
  525. context.Copy(GetVec(op.Rd), res);
  526. }
  527. }
  528. private static void EmitVectorUnzip(ArmEmitterContext context, int part)
  529. {
  530. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  531. if (Optimizations.UseSsse3)
  532. {
  533. if (op.RegisterSize == RegisterSize.Simd128)
  534. {
  535. Operand mask = null;
  536. if (op.Size < 3)
  537. {
  538. long maskE0 = EvenMasks[op.Size];
  539. long maskE1 = OddMasks [op.Size];
  540. mask = X86GetScalar(context, maskE0);
  541. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  542. }
  543. Operand n = GetVec(op.Rn);
  544. if (op.Size < 3)
  545. {
  546. n = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mask);
  547. }
  548. Operand m = GetVec(op.Rm);
  549. if (op.Size < 3)
  550. {
  551. m = context.AddIntrinsic(Intrinsic.X86Pshufb, m, mask);
  552. }
  553. Intrinsic punpckInst = part == 0
  554. ? Intrinsic.X86Punpcklqdq
  555. : Intrinsic.X86Punpckhqdq;
  556. Operand res = context.AddIntrinsic(punpckInst, n, m);
  557. context.Copy(GetVec(op.Rd), res);
  558. }
  559. else
  560. {
  561. Operand n = GetVec(op.Rn);
  562. Operand m = GetVec(op.Rm);
  563. Intrinsic punpcklInst = X86PunpcklInstruction[op.Size];
  564. Operand res = context.AddIntrinsic(punpcklInst, n, m);
  565. if (op.Size < 2)
  566. {
  567. long maskE0 = _masksE0_Uzp[op.Size];
  568. long maskE1 = _masksE1_Uzp[op.Size];
  569. Operand mask = X86GetScalar(context, maskE0);
  570. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  571. res = context.AddIntrinsic(Intrinsic.X86Pshufb, res, mask);
  572. }
  573. Intrinsic punpckInst = part == 0
  574. ? Intrinsic.X86Punpcklqdq
  575. : Intrinsic.X86Punpckhqdq;
  576. res = context.AddIntrinsic(punpckInst, res, context.VectorZero());
  577. context.Copy(GetVec(op.Rd), res);
  578. }
  579. }
  580. else
  581. {
  582. Operand res = context.VectorZero();
  583. int pairs = op.GetPairsCount() >> op.Size;
  584. for (int index = 0; index < pairs; index++)
  585. {
  586. int idx = index << 1;
  587. Operand ne = EmitVectorExtractZx(context, op.Rn, idx + part, op.Size);
  588. Operand me = EmitVectorExtractZx(context, op.Rm, idx + part, op.Size);
  589. res = EmitVectorInsert(context, res, ne, index, op.Size);
  590. res = EmitVectorInsert(context, res, me, pairs + index, op.Size);
  591. }
  592. context.Copy(GetVec(op.Rd), res);
  593. }
  594. }
  595. private static void EmitVectorZip(ArmEmitterContext context, int part)
  596. {
  597. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  598. if (Optimizations.UseSse2)
  599. {
  600. Operand n = GetVec(op.Rn);
  601. Operand m = GetVec(op.Rm);
  602. if (op.RegisterSize == RegisterSize.Simd128)
  603. {
  604. Intrinsic punpckInst = part == 0
  605. ? X86PunpcklInstruction[op.Size]
  606. : X86PunpckhInstruction[op.Size];
  607. Operand res = context.AddIntrinsic(punpckInst, n, m);
  608. context.Copy(GetVec(op.Rd), res);
  609. }
  610. else
  611. {
  612. Operand res = context.AddIntrinsic(X86PunpcklInstruction[op.Size], n, m);
  613. Intrinsic punpckInst = part == 0
  614. ? Intrinsic.X86Punpcklqdq
  615. : Intrinsic.X86Punpckhqdq;
  616. res = context.AddIntrinsic(punpckInst, res, context.VectorZero());
  617. context.Copy(GetVec(op.Rd), res);
  618. }
  619. }
  620. else
  621. {
  622. Operand res = context.VectorZero();
  623. int pairs = op.GetPairsCount() >> op.Size;
  624. int baseIndex = part != 0 ? pairs : 0;
  625. for (int index = 0; index < pairs; index++)
  626. {
  627. int pairIndex = index << 1;
  628. Operand ne = EmitVectorExtractZx(context, op.Rn, baseIndex + index, op.Size);
  629. Operand me = EmitVectorExtractZx(context, op.Rm, baseIndex + index, op.Size);
  630. res = EmitVectorInsert(context, res, ne, pairIndex, op.Size);
  631. res = EmitVectorInsert(context, res, me, pairIndex + 1, op.Size);
  632. }
  633. context.Copy(GetVec(op.Rd), res);
  634. }
  635. }
  636. }
  637. }