InstEmitSimdMove.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using System.Collections.Generic;
  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 (op.Size == 0)
  215. {
  216. context.Copy(GetVec(op.Rd), X86GetScalar(context, (int)op.Immediate));
  217. }
  218. else
  219. {
  220. context.Copy(GetVec(op.Rd), X86GetScalar(context, op.Immediate));
  221. }
  222. }
  223. public static void Fmov_Vi(ArmEmitterContext context)
  224. {
  225. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  226. if (Optimizations.UseSse2)
  227. {
  228. if (op.RegisterSize == RegisterSize.Simd128)
  229. {
  230. context.Copy(GetVec(op.Rd), X86GetAllElements(context, op.Immediate));
  231. }
  232. else
  233. {
  234. context.Copy(GetVec(op.Rd), X86GetScalar(context, op.Immediate));
  235. }
  236. }
  237. else
  238. {
  239. Operand e = Const(op.Immediate);
  240. Operand res = context.VectorZero();
  241. int elems = op.RegisterSize == RegisterSize.Simd128 ? 2 : 1;
  242. for (int index = 0; index < elems; index++)
  243. {
  244. res = EmitVectorInsert(context, res, e, index, 3);
  245. }
  246. context.Copy(GetVec(op.Rd), res);
  247. }
  248. }
  249. public static void Ins_Gp(ArmEmitterContext context)
  250. {
  251. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  252. Operand d = GetVec(op.Rd);
  253. Operand n = GetIntOrZR(context, op.Rn);
  254. context.Copy(d, EmitVectorInsert(context, d, n, op.DstIndex, op.Size));
  255. }
  256. public static void Ins_V(ArmEmitterContext context)
  257. {
  258. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  259. Operand d = GetVec(op.Rd);
  260. Operand ne = EmitVectorExtractZx(context, op.Rn, op.SrcIndex, op.Size);
  261. context.Copy(d, EmitVectorInsert(context, d, ne, op.DstIndex, op.Size));
  262. }
  263. public static void Movi_V(ArmEmitterContext context)
  264. {
  265. if (Optimizations.UseSse2)
  266. {
  267. EmitSse2MoviMvni(context, not: false);
  268. }
  269. else
  270. {
  271. EmitVectorImmUnaryOp(context, (op1) => op1);
  272. }
  273. }
  274. public static void Mvni_V(ArmEmitterContext context)
  275. {
  276. if (Optimizations.UseSse2)
  277. {
  278. EmitSse2MoviMvni(context, not: true);
  279. }
  280. else
  281. {
  282. EmitVectorImmUnaryOp(context, (op1) => context.BitwiseNot(op1));
  283. }
  284. }
  285. public static void Smov_S(ArmEmitterContext context)
  286. {
  287. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  288. Operand ne = EmitVectorExtractSx(context, op.Rn, op.DstIndex, op.Size);
  289. if (op.RegisterSize == RegisterSize.Simd64)
  290. {
  291. ne = context.ZeroExtend32(OperandType.I64, ne);
  292. }
  293. SetIntOrZR(context, op.Rd, ne);
  294. }
  295. public static void Tbl_V(ArmEmitterContext context)
  296. {
  297. EmitTableVectorLookup(context, isTbl: true);
  298. }
  299. public static void Tbx_V(ArmEmitterContext context)
  300. {
  301. EmitTableVectorLookup(context, isTbl: false);
  302. }
  303. public static void Trn1_V(ArmEmitterContext context)
  304. {
  305. EmitVectorTranspose(context, part: 0);
  306. }
  307. public static void Trn2_V(ArmEmitterContext context)
  308. {
  309. EmitVectorTranspose(context, part: 1);
  310. }
  311. public static void Umov_S(ArmEmitterContext context)
  312. {
  313. OpCodeSimdIns op = (OpCodeSimdIns)context.CurrOp;
  314. Operand ne = EmitVectorExtractZx(context, op.Rn, op.DstIndex, op.Size);
  315. SetIntOrZR(context, op.Rd, ne);
  316. }
  317. public static void Uzp1_V(ArmEmitterContext context)
  318. {
  319. EmitVectorUnzip(context, part: 0);
  320. }
  321. public static void Uzp2_V(ArmEmitterContext context)
  322. {
  323. EmitVectorUnzip(context, part: 1);
  324. }
  325. public static void Xtn_V(ArmEmitterContext context)
  326. {
  327. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  328. if (Optimizations.UseSsse3)
  329. {
  330. Operand d = GetVec(op.Rd);
  331. Operand res = context.VectorZeroUpper64(d);
  332. Operand mask = X86GetAllElements(context, EvenMasks[op.Size]);
  333. Operand res2 = context.AddIntrinsic(Intrinsic.X86Pshufb, GetVec(op.Rn), mask);
  334. Intrinsic movInst = op.RegisterSize == RegisterSize.Simd128
  335. ? Intrinsic.X86Movlhps
  336. : Intrinsic.X86Movhlps;
  337. res = context.AddIntrinsic(movInst, res, res2);
  338. context.Copy(d, res);
  339. }
  340. else
  341. {
  342. int elems = 8 >> op.Size;
  343. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  344. Operand d = GetVec(op.Rd);
  345. Operand res = part == 0 ? context.VectorZero() : context.Copy(d);
  346. for (int index = 0; index < elems; index++)
  347. {
  348. Operand ne = EmitVectorExtractZx(context, op.Rn, index, op.Size + 1);
  349. res = EmitVectorInsert(context, res, ne, part + index, op.Size);
  350. }
  351. context.Copy(d, res);
  352. }
  353. }
  354. public static void Zip1_V(ArmEmitterContext context)
  355. {
  356. EmitVectorZip(context, part: 0);
  357. }
  358. public static void Zip2_V(ArmEmitterContext context)
  359. {
  360. EmitVectorZip(context, part: 1);
  361. }
  362. private static void EmitSse2MoviMvni(ArmEmitterContext context, bool not)
  363. {
  364. OpCodeSimdImm op = (OpCodeSimdImm)context.CurrOp;
  365. long imm = op.Immediate;
  366. switch (op.Size)
  367. {
  368. case 0: imm *= 0x01010101; break;
  369. case 1: imm *= 0x00010001; break;
  370. }
  371. if (not)
  372. {
  373. imm = ~imm;
  374. }
  375. Operand mask;
  376. if (op.Size < 3)
  377. {
  378. mask = X86GetAllElements(context, (int)imm);
  379. }
  380. else
  381. {
  382. mask = X86GetAllElements(context, imm);
  383. }
  384. if (op.RegisterSize == RegisterSize.Simd64)
  385. {
  386. mask = context.VectorZeroUpper64(mask);
  387. }
  388. context.Copy(GetVec(op.Rd), mask);
  389. }
  390. private static void EmitTableVectorLookup(ArmEmitterContext context, bool isTbl)
  391. {
  392. OpCodeSimdTbl op = (OpCodeSimdTbl)context.CurrOp;
  393. if (Optimizations.UseSsse3)
  394. {
  395. Operand d = GetVec(op.Rd);
  396. Operand m = GetVec(op.Rm);
  397. Operand res;
  398. Operand mask = X86GetAllElements(context, 0x0F0F0F0F0F0F0F0FL);
  399. // Fast path for single register table.
  400. {
  401. Operand n = GetVec(op.Rn);
  402. Operand mMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, m, mask);
  403. mMask = context.AddIntrinsic(Intrinsic.X86Por, mMask, m);
  404. res = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mMask);
  405. }
  406. for (int index = 1; index < op.Size; index++)
  407. {
  408. Operand ni = GetVec((op.Rn + index) & 0x1F);
  409. Operand idxMask = X86GetAllElements(context, 0x1010101010101010L * index);
  410. Operand mSubMask = context.AddIntrinsic(Intrinsic.X86Psubb, m, idxMask);
  411. Operand mMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, mSubMask, mask);
  412. mMask = context.AddIntrinsic(Intrinsic.X86Por, mMask, mSubMask);
  413. Operand res2 = context.AddIntrinsic(Intrinsic.X86Pshufb, ni, mMask);
  414. res = context.AddIntrinsic(Intrinsic.X86Por, res, res2);
  415. }
  416. if (!isTbl)
  417. {
  418. Operand idxMask = X86GetAllElements(context, (0x1010101010101010L * op.Size) - 0x0101010101010101L);
  419. Operand zeroMask = context.VectorZero();
  420. Operand mPosMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, m, idxMask);
  421. Operand mNegMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, zeroMask, m);
  422. Operand mMask = context.AddIntrinsic(Intrinsic.X86Por, mPosMask, mNegMask);
  423. Operand dMask = context.AddIntrinsic(Intrinsic.X86Pand, d, mMask);
  424. res = context.AddIntrinsic(Intrinsic.X86Por, res, dMask);
  425. }
  426. if (op.RegisterSize == RegisterSize.Simd64)
  427. {
  428. res = context.VectorZeroUpper64(res);
  429. }
  430. context.Copy(d, res);
  431. }
  432. else
  433. {
  434. Operand d = GetVec(op.Rd);
  435. List<Operand> args = new List<Operand>();
  436. if (!isTbl)
  437. {
  438. args.Add(d);
  439. }
  440. args.Add(GetVec(op.Rm));
  441. args.Add(Const(op.RegisterSize == RegisterSize.Simd64 ? 8 : 16));
  442. for (int index = 0; index < op.Size; index++)
  443. {
  444. args.Add(GetVec((op.Rn + index) & 0x1F));
  445. }
  446. Delegate dlg = null;
  447. switch (op.Size)
  448. {
  449. case 1: dlg = isTbl
  450. ? (Delegate)new _V128_V128_S32_V128 (SoftFallback.Tbl1)
  451. : (Delegate)new _V128_V128_V128_S32_V128(SoftFallback.Tbx1);
  452. break;
  453. case 2: dlg = isTbl
  454. ? (Delegate)new _V128_V128_S32_V128_V128 (SoftFallback.Tbl2)
  455. : (Delegate)new _V128_V128_V128_S32_V128_V128(SoftFallback.Tbx2);
  456. break;
  457. case 3: dlg = isTbl
  458. ? (Delegate)new _V128_V128_S32_V128_V128_V128 (SoftFallback.Tbl3)
  459. : (Delegate)new _V128_V128_V128_S32_V128_V128_V128(SoftFallback.Tbx3);
  460. break;
  461. case 4: dlg = isTbl
  462. ? (Delegate)new _V128_V128_S32_V128_V128_V128_V128 (SoftFallback.Tbl4)
  463. : (Delegate)new _V128_V128_V128_S32_V128_V128_V128_V128(SoftFallback.Tbx4);
  464. break;
  465. }
  466. context.Copy(d, context.Call(dlg, args.ToArray()));
  467. }
  468. }
  469. private static void EmitVectorTranspose(ArmEmitterContext context, int part)
  470. {
  471. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  472. if (Optimizations.UseSsse3)
  473. {
  474. Operand mask = null;
  475. if (op.Size < 3)
  476. {
  477. long maskE0 = EvenMasks[op.Size];
  478. long maskE1 = OddMasks [op.Size];
  479. mask = X86GetScalar(context, maskE0);
  480. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  481. }
  482. Operand n = GetVec(op.Rn);
  483. if (op.Size < 3)
  484. {
  485. n = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mask);
  486. }
  487. Operand m = GetVec(op.Rm);
  488. if (op.Size < 3)
  489. {
  490. m = context.AddIntrinsic(Intrinsic.X86Pshufb, m, mask);
  491. }
  492. Intrinsic punpckInst = part == 0
  493. ? X86PunpcklInstruction[op.Size]
  494. : X86PunpckhInstruction[op.Size];
  495. Operand res = context.AddIntrinsic(punpckInst, n, m);
  496. if (op.RegisterSize == RegisterSize.Simd64)
  497. {
  498. res = context.VectorZeroUpper64(res);
  499. }
  500. context.Copy(GetVec(op.Rd), res);
  501. }
  502. else
  503. {
  504. Operand res = context.VectorZero();
  505. int pairs = op.GetPairsCount() >> op.Size;
  506. for (int index = 0; index < pairs; index++)
  507. {
  508. int pairIndex = index << 1;
  509. Operand ne = EmitVectorExtractZx(context, op.Rn, pairIndex + part, op.Size);
  510. Operand me = EmitVectorExtractZx(context, op.Rm, pairIndex + part, op.Size);
  511. res = EmitVectorInsert(context, res, ne, pairIndex, op.Size);
  512. res = EmitVectorInsert(context, res, me, pairIndex + 1, op.Size);
  513. }
  514. context.Copy(GetVec(op.Rd), res);
  515. }
  516. }
  517. private static void EmitVectorUnzip(ArmEmitterContext context, int part)
  518. {
  519. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  520. if (Optimizations.UseSsse3)
  521. {
  522. if (op.RegisterSize == RegisterSize.Simd128)
  523. {
  524. Operand mask = null;
  525. if (op.Size < 3)
  526. {
  527. long maskE0 = EvenMasks[op.Size];
  528. long maskE1 = OddMasks [op.Size];
  529. mask = X86GetScalar(context, maskE0);
  530. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  531. }
  532. Operand n = GetVec(op.Rn);
  533. if (op.Size < 3)
  534. {
  535. n = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mask);
  536. }
  537. Operand m = GetVec(op.Rm);
  538. if (op.Size < 3)
  539. {
  540. m = context.AddIntrinsic(Intrinsic.X86Pshufb, m, mask);
  541. }
  542. Intrinsic punpckInst = part == 0
  543. ? Intrinsic.X86Punpcklqdq
  544. : Intrinsic.X86Punpckhqdq;
  545. Operand res = context.AddIntrinsic(punpckInst, n, m);
  546. context.Copy(GetVec(op.Rd), res);
  547. }
  548. else
  549. {
  550. Operand n = GetVec(op.Rn);
  551. Operand m = GetVec(op.Rm);
  552. Intrinsic punpcklInst = X86PunpcklInstruction[op.Size];
  553. Operand res = context.AddIntrinsic(punpcklInst, n, m);
  554. if (op.Size < 2)
  555. {
  556. long maskE0 = _masksE0_Uzp[op.Size];
  557. long maskE1 = _masksE1_Uzp[op.Size];
  558. Operand mask = X86GetScalar(context, maskE0);
  559. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  560. res = context.AddIntrinsic(Intrinsic.X86Pshufb, res, mask);
  561. }
  562. Intrinsic punpckInst = part == 0
  563. ? Intrinsic.X86Punpcklqdq
  564. : Intrinsic.X86Punpckhqdq;
  565. res = context.AddIntrinsic(punpckInst, res, context.VectorZero());
  566. context.Copy(GetVec(op.Rd), res);
  567. }
  568. }
  569. else
  570. {
  571. Operand res = context.VectorZero();
  572. int pairs = op.GetPairsCount() >> op.Size;
  573. for (int index = 0; index < pairs; index++)
  574. {
  575. int idx = index << 1;
  576. Operand ne = EmitVectorExtractZx(context, op.Rn, idx + part, op.Size);
  577. Operand me = EmitVectorExtractZx(context, op.Rm, idx + part, op.Size);
  578. res = EmitVectorInsert(context, res, ne, index, op.Size);
  579. res = EmitVectorInsert(context, res, me, pairs + index, op.Size);
  580. }
  581. context.Copy(GetVec(op.Rd), res);
  582. }
  583. }
  584. private static void EmitVectorZip(ArmEmitterContext context, int part)
  585. {
  586. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  587. if (Optimizations.UseSse2)
  588. {
  589. Operand n = GetVec(op.Rn);
  590. Operand m = GetVec(op.Rm);
  591. if (op.RegisterSize == RegisterSize.Simd128)
  592. {
  593. Intrinsic punpckInst = part == 0
  594. ? X86PunpcklInstruction[op.Size]
  595. : X86PunpckhInstruction[op.Size];
  596. Operand res = context.AddIntrinsic(punpckInst, n, m);
  597. context.Copy(GetVec(op.Rd), res);
  598. }
  599. else
  600. {
  601. Operand res = context.AddIntrinsic(X86PunpcklInstruction[op.Size], n, m);
  602. Intrinsic punpckInst = part == 0
  603. ? Intrinsic.X86Punpcklqdq
  604. : Intrinsic.X86Punpckhqdq;
  605. res = context.AddIntrinsic(punpckInst, res, context.VectorZero());
  606. context.Copy(GetVec(op.Rd), res);
  607. }
  608. }
  609. else
  610. {
  611. Operand res = context.VectorZero();
  612. int pairs = op.GetPairsCount() >> op.Size;
  613. int baseIndex = part != 0 ? pairs : 0;
  614. for (int index = 0; index < pairs; index++)
  615. {
  616. int pairIndex = index << 1;
  617. Operand ne = EmitVectorExtractZx(context, op.Rn, baseIndex + index, op.Size);
  618. Operand me = EmitVectorExtractZx(context, op.Rm, baseIndex + index, op.Size);
  619. res = EmitVectorInsert(context, res, ne, pairIndex, op.Size);
  620. res = EmitVectorInsert(context, res, me, pairIndex + 1, op.Size);
  621. }
  622. context.Copy(GetVec(op.Rd), res);
  623. }
  624. }
  625. }
  626. }