InstEmitSimdMove.cs 28 KB

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