InstEmitSimdMove32.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using static ARMeilleure.Instructions.InstEmitHelper;
  6. using static ARMeilleure.Instructions.InstEmitSimdHelper;
  7. using static ARMeilleure.Instructions.InstEmitSimdHelper32;
  8. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static partial class InstEmit32
  12. {
  13. #region "Masks"
  14. // Same as InstEmitSimdMove, as the instructions do the same thing.
  15. private static readonly long[] _masksE0_Uzp = new long[]
  16. {
  17. 13L << 56 | 09L << 48 | 05L << 40 | 01L << 32 | 12L << 24 | 08L << 16 | 04L << 8 | 00L << 0,
  18. 11L << 56 | 10L << 48 | 03L << 40 | 02L << 32 | 09L << 24 | 08L << 16 | 01L << 8 | 00L << 0
  19. };
  20. private static readonly long[] _masksE1_Uzp = new long[]
  21. {
  22. 15L << 56 | 11L << 48 | 07L << 40 | 03L << 32 | 14L << 24 | 10L << 16 | 06L << 8 | 02L << 0,
  23. 15L << 56 | 14L << 48 | 07L << 40 | 06L << 32 | 13L << 24 | 12L << 16 | 05L << 8 | 04L << 0
  24. };
  25. #endregion
  26. public static void Vmov_I(ArmEmitterContext context)
  27. {
  28. EmitVectorImmUnaryOp32(context, (op1) => op1);
  29. }
  30. public static void Vmvn_I(ArmEmitterContext context)
  31. {
  32. if (Optimizations.UseSse2)
  33. {
  34. EmitVectorUnaryOpSimd32(context, (op1) =>
  35. {
  36. Operand mask = X86GetAllElements(context, -1L);
  37. return context.AddIntrinsic(Intrinsic.X86Pandn, op1, mask);
  38. });
  39. }
  40. else
  41. {
  42. EmitVectorUnaryOpZx32(context, (op1) => context.BitwiseNot(op1));
  43. }
  44. }
  45. public static void Vmvn_II(ArmEmitterContext context)
  46. {
  47. EmitVectorImmUnaryOp32(context, (op1) => context.BitwiseNot(op1));
  48. }
  49. public static void Vmov_GS(ArmEmitterContext context)
  50. {
  51. OpCode32SimdMovGp op = (OpCode32SimdMovGp)context.CurrOp;
  52. Operand vec = GetVecA32(op.Vn >> 2);
  53. if (op.Op == 1)
  54. {
  55. // To general purpose.
  56. Operand value = context.VectorExtract(OperandType.I32, vec, op.Vn & 0x3);
  57. SetIntA32(context, op.Rt, value);
  58. }
  59. else
  60. {
  61. // From general purpose.
  62. Operand value = GetIntA32(context, op.Rt);
  63. context.Copy(vec, context.VectorInsert(vec, value, op.Vn & 0x3));
  64. }
  65. }
  66. public static void Vmov_G1(ArmEmitterContext context)
  67. {
  68. OpCode32SimdMovGpElem op = (OpCode32SimdMovGpElem)context.CurrOp;
  69. int index = op.Index + ((op.Vd & 1) << (3 - op.Size));
  70. if (op.Op == 1)
  71. {
  72. // To general purpose.
  73. Operand value = EmitVectorExtract32(context, op.Vd >> 1, index, op.Size, !op.U);
  74. SetIntA32(context, op.Rt, value);
  75. }
  76. else
  77. {
  78. // From general purpose.
  79. Operand vec = GetVecA32(op.Vd >> 1);
  80. Operand value = GetIntA32(context, op.Rt);
  81. context.Copy(vec, EmitVectorInsert(context, vec, value, index, op.Size));
  82. }
  83. }
  84. public static void Vmov_G2(ArmEmitterContext context)
  85. {
  86. OpCode32SimdMovGpDouble op = (OpCode32SimdMovGpDouble)context.CurrOp;
  87. Operand vec = GetVecA32(op.Vm >> 2);
  88. int vm1 = op.Vm + 1;
  89. bool sameOwnerVec = (op.Vm >> 2) == (vm1 >> 2);
  90. Operand vec2 = sameOwnerVec ? vec : GetVecA32(vm1 >> 2);
  91. if (op.Op == 1)
  92. {
  93. // To general purpose.
  94. Operand lowValue = context.VectorExtract(OperandType.I32, vec, op.Vm & 3);
  95. SetIntA32(context, op.Rt, lowValue);
  96. Operand highValue = context.VectorExtract(OperandType.I32, vec2, vm1 & 3);
  97. SetIntA32(context, op.Rt2, highValue);
  98. }
  99. else
  100. {
  101. // From general purpose.
  102. Operand lowValue = GetIntA32(context, op.Rt);
  103. Operand resultVec = context.VectorInsert(vec, lowValue, op.Vm & 3);
  104. Operand highValue = GetIntA32(context, op.Rt2);
  105. if (sameOwnerVec)
  106. {
  107. context.Copy(vec, context.VectorInsert(resultVec, highValue, vm1 & 3));
  108. }
  109. else
  110. {
  111. context.Copy(vec, resultVec);
  112. context.Copy(vec2, context.VectorInsert(vec2, highValue, vm1 & 3));
  113. }
  114. }
  115. }
  116. public static void Vmov_GD(ArmEmitterContext context)
  117. {
  118. OpCode32SimdMovGpDouble op = (OpCode32SimdMovGpDouble)context.CurrOp;
  119. Operand vec = GetVecA32(op.Vm >> 1);
  120. if (op.Op == 1)
  121. {
  122. // To general purpose.
  123. Operand value = context.VectorExtract(OperandType.I64, vec, op.Vm & 1);
  124. SetIntA32(context, op.Rt, context.ConvertI64ToI32(value));
  125. SetIntA32(context, op.Rt2, context.ConvertI64ToI32(context.ShiftRightUI(value, Const(32))));
  126. }
  127. else
  128. {
  129. // From general purpose.
  130. Operand lowValue = GetIntA32(context, op.Rt);
  131. Operand highValue = GetIntA32(context, op.Rt2);
  132. Operand value = context.BitwiseOr(
  133. context.ZeroExtend32(OperandType.I64, lowValue),
  134. context.ShiftLeft(context.ZeroExtend32(OperandType.I64, highValue), Const(32)));
  135. context.Copy(vec, context.VectorInsert(vec, value, op.Vm & 1));
  136. }
  137. }
  138. public static void Vmovl(ArmEmitterContext context)
  139. {
  140. OpCode32SimdLong op = (OpCode32SimdLong)context.CurrOp;
  141. Operand res = context.VectorZero();
  142. int elems = op.GetBytesCount() >> op.Size;
  143. for (int index = 0; index < elems; index++)
  144. {
  145. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, !op.U);
  146. if (op.Size == 2)
  147. {
  148. if (op.U)
  149. {
  150. me = context.ZeroExtend32(OperandType.I64, me);
  151. }
  152. else
  153. {
  154. me = context.SignExtend32(OperandType.I64, me);
  155. }
  156. }
  157. res = EmitVectorInsert(context, res, me, index, op.Size + 1);
  158. }
  159. context.Copy(GetVecA32(op.Qd), res);
  160. }
  161. public static void Vtbl(ArmEmitterContext context)
  162. {
  163. OpCode32SimdTbl op = (OpCode32SimdTbl)context.CurrOp;
  164. bool extension = op.Opc == 1;
  165. int length = op.Length + 1;
  166. if (Optimizations.UseSsse3)
  167. {
  168. Operand d = GetVecA32(op.Qd);
  169. Operand m = EmitMoveDoubleWordToSide(context, GetVecA32(op.Qm), op.Vm, 0);
  170. Operand res;
  171. Operand mask = X86GetAllElements(context, 0x0707070707070707L);
  172. // Fast path for single register table.
  173. {
  174. Operand n = EmitMoveDoubleWordToSide(context, GetVecA32(op.Qn), op.Vn, 0);
  175. Operand mMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, m, mask);
  176. mMask = context.AddIntrinsic(Intrinsic.X86Por, mMask, m);
  177. res = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mMask);
  178. }
  179. for (int index = 1; index < length; index++)
  180. {
  181. int newVn = (op.Vn + index) & 0x1F;
  182. (int qn, int ind) = GetQuadwordAndSubindex(newVn, op.RegisterSize);
  183. Operand ni = EmitMoveDoubleWordToSide(context, GetVecA32(qn), newVn, 0);
  184. Operand idxMask = X86GetAllElements(context, 0x0808080808080808L * index);
  185. Operand mSubMask = context.AddIntrinsic(Intrinsic.X86Psubb, m, idxMask);
  186. Operand mMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, mSubMask, mask);
  187. mMask = context.AddIntrinsic(Intrinsic.X86Por, mMask, mSubMask);
  188. Operand res2 = context.AddIntrinsic(Intrinsic.X86Pshufb, ni, mMask);
  189. res = context.AddIntrinsic(Intrinsic.X86Por, res, res2);
  190. }
  191. if (extension)
  192. {
  193. Operand idxMask = X86GetAllElements(context, (0x0808080808080808L * length) - 0x0101010101010101L);
  194. Operand zeroMask = context.VectorZero();
  195. Operand mPosMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, m, idxMask);
  196. Operand mNegMask = context.AddIntrinsic(Intrinsic.X86Pcmpgtb, zeroMask, m);
  197. Operand mMask = context.AddIntrinsic(Intrinsic.X86Por, mPosMask, mNegMask);
  198. Operand dMask = context.AddIntrinsic(Intrinsic.X86Pand, EmitMoveDoubleWordToSide(context, d, op.Vd, 0), mMask);
  199. res = context.AddIntrinsic(Intrinsic.X86Por, res, dMask);
  200. }
  201. res = EmitMoveDoubleWordToSide(context, res, 0, op.Vd);
  202. context.Copy(d, EmitDoubleWordInsert(context, d, res, op.Vd));
  203. }
  204. else
  205. {
  206. int elems = op.GetBytesCount() >> op.Size;
  207. (int Qx, int Ix)[] tableTuples = new (int, int)[length];
  208. for (int i = 0; i < length; i++)
  209. {
  210. tableTuples[i] = GetQuadwordAndSubindex(op.Vn + i, op.RegisterSize);
  211. }
  212. int byteLength = length * 8;
  213. Operand res = GetVecA32(op.Qd);
  214. Operand m = GetVecA32(op.Qm);
  215. for (int index = 0; index < elems; index++)
  216. {
  217. Operand selectedIndex = context.ZeroExtend8(OperandType.I32, context.VectorExtract8(m, index + op.Im));
  218. Operand inRange = context.ICompareLess(selectedIndex, Const(byteLength));
  219. Operand elemRes = default; // Note: This is I64 for ease of calculation.
  220. // TODO: Branching rather than conditional select.
  221. // Get indexed byte.
  222. // To simplify (ha) the il, we get bytes from every vector and use a nested conditional select to choose the right result.
  223. // This does have to extract `length` times for every element but certainly not as bad as it could be.
  224. // Which vector number is the index on.
  225. Operand vecIndex = context.ShiftRightUI(selectedIndex, Const(3));
  226. // What should we shift by to extract it.
  227. Operand subVecIndexShift = context.ShiftLeft(context.BitwiseAnd(selectedIndex, Const(7)), Const(3));
  228. for (int i = 0; i < length; i++)
  229. {
  230. (int qx, int ix) = tableTuples[i];
  231. // Get the whole vector, we'll get a byte out of it.
  232. Operand lookupResult;
  233. if (qx == op.Qd)
  234. {
  235. // Result contains the current state of the vector.
  236. lookupResult = context.VectorExtract(OperandType.I64, res, ix);
  237. }
  238. else
  239. {
  240. lookupResult = EmitVectorExtract32(context, qx, ix, 3, false); // I64
  241. }
  242. lookupResult = context.ShiftRightUI(lookupResult, subVecIndexShift); // Get the relevant byte from this vector.
  243. if (i == 0)
  244. {
  245. elemRes = lookupResult; // First result is always default.
  246. }
  247. else
  248. {
  249. Operand isThisElem = context.ICompareEqual(vecIndex, Const(i));
  250. elemRes = context.ConditionalSelect(isThisElem, lookupResult, elemRes);
  251. }
  252. }
  253. Operand fallback = (extension) ? context.ZeroExtend32(OperandType.I64, EmitVectorExtract32(context, op.Qd, index + op.Id, 0, false)) : Const(0L);
  254. res = EmitVectorInsert(context, res, context.ConditionalSelect(inRange, elemRes, fallback), index + op.Id, 0);
  255. }
  256. context.Copy(GetVecA32(op.Qd), res);
  257. }
  258. }
  259. public static void Vtrn(ArmEmitterContext context)
  260. {
  261. OpCode32SimdCmpZ op = (OpCode32SimdCmpZ)context.CurrOp;
  262. if (Optimizations.UseSsse3)
  263. {
  264. EmitVectorShuffleOpSimd32(context, (m, d) =>
  265. {
  266. Operand mask = default;
  267. if (op.Size < 3)
  268. {
  269. long maskE0 = EvenMasks[op.Size];
  270. long maskE1 = OddMasks[op.Size];
  271. mask = X86GetScalar(context, maskE0);
  272. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  273. }
  274. if (op.Size < 3)
  275. {
  276. d = context.AddIntrinsic(Intrinsic.X86Pshufb, d, mask);
  277. m = context.AddIntrinsic(Intrinsic.X86Pshufb, m, mask);
  278. }
  279. Operand resD = context.AddIntrinsic(X86PunpcklInstruction[op.Size], d, m);
  280. Operand resM = context.AddIntrinsic(X86PunpckhInstruction[op.Size], d, m);
  281. return (resM, resD);
  282. });
  283. }
  284. else
  285. {
  286. int elems = op.GetBytesCount() >> op.Size;
  287. int pairs = elems >> 1;
  288. bool overlap = op.Qm == op.Qd;
  289. Operand resD = GetVecA32(op.Qd);
  290. Operand resM = GetVecA32(op.Qm);
  291. for (int index = 0; index < pairs; index++)
  292. {
  293. int pairIndex = index << 1;
  294. Operand d2 = EmitVectorExtract32(context, op.Qd, pairIndex + 1 + op.Id, op.Size, false);
  295. Operand m1 = EmitVectorExtract32(context, op.Qm, pairIndex + op.Im, op.Size, false);
  296. resD = EmitVectorInsert(context, resD, m1, pairIndex + 1 + op.Id, op.Size);
  297. if (overlap)
  298. {
  299. resM = resD;
  300. }
  301. resM = EmitVectorInsert(context, resM, d2, pairIndex + op.Im, op.Size);
  302. if (overlap)
  303. {
  304. resD = resM;
  305. }
  306. }
  307. context.Copy(GetVecA32(op.Qd), resD);
  308. if (!overlap)
  309. {
  310. context.Copy(GetVecA32(op.Qm), resM);
  311. }
  312. }
  313. }
  314. public static void Vzip(ArmEmitterContext context)
  315. {
  316. OpCode32SimdCmpZ op = (OpCode32SimdCmpZ)context.CurrOp;
  317. if (Optimizations.UseSse2)
  318. {
  319. EmitVectorShuffleOpSimd32(context, (m, d) =>
  320. {
  321. if (op.RegisterSize == RegisterSize.Simd128)
  322. {
  323. Operand resD = context.AddIntrinsic(X86PunpcklInstruction[op.Size], d, m);
  324. Operand resM = context.AddIntrinsic(X86PunpckhInstruction[op.Size], d, m);
  325. return (resM, resD);
  326. }
  327. else
  328. {
  329. Operand res = context.AddIntrinsic(X86PunpcklInstruction[op.Size], d, m);
  330. Operand resD = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, res, context.VectorZero());
  331. Operand resM = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, res, context.VectorZero());
  332. return (resM, resD);
  333. }
  334. });
  335. }
  336. else
  337. {
  338. int elems = op.GetBytesCount() >> op.Size;
  339. int pairs = elems >> 1;
  340. bool overlap = op.Qm == op.Qd;
  341. Operand resD = GetVecA32(op.Qd);
  342. Operand resM = GetVecA32(op.Qm);
  343. for (int index = 0; index < pairs; index++)
  344. {
  345. int pairIndex = index << 1;
  346. Operand dRowD = EmitVectorExtract32(context, op.Qd, index + op.Id, op.Size, false);
  347. Operand mRowD = EmitVectorExtract32(context, op.Qm, index + op.Im, op.Size, false);
  348. Operand dRowM = EmitVectorExtract32(context, op.Qd, index + op.Id + pairs, op.Size, false);
  349. Operand mRowM = EmitVectorExtract32(context, op.Qm, index + op.Im + pairs, op.Size, false);
  350. resD = EmitVectorInsert(context, resD, dRowD, pairIndex + op.Id, op.Size);
  351. resD = EmitVectorInsert(context, resD, mRowD, pairIndex + 1 + op.Id, op.Size);
  352. if (overlap)
  353. {
  354. resM = resD;
  355. }
  356. resM = EmitVectorInsert(context, resM, dRowM, pairIndex + op.Im, op.Size);
  357. resM = EmitVectorInsert(context, resM, mRowM, pairIndex + 1 + op.Im, op.Size);
  358. if (overlap)
  359. {
  360. resD = resM;
  361. }
  362. }
  363. context.Copy(GetVecA32(op.Qd), resD);
  364. if (!overlap)
  365. {
  366. context.Copy(GetVecA32(op.Qm), resM);
  367. }
  368. }
  369. }
  370. public static void Vuzp(ArmEmitterContext context)
  371. {
  372. OpCode32SimdCmpZ op = (OpCode32SimdCmpZ)context.CurrOp;
  373. if (Optimizations.UseSsse3)
  374. {
  375. EmitVectorShuffleOpSimd32(context, (m, d) =>
  376. {
  377. if (op.RegisterSize == RegisterSize.Simd128)
  378. {
  379. Operand mask = default;
  380. if (op.Size < 3)
  381. {
  382. long maskE0 = EvenMasks[op.Size];
  383. long maskE1 = OddMasks[op.Size];
  384. mask = X86GetScalar(context, maskE0);
  385. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  386. d = context.AddIntrinsic(Intrinsic.X86Pshufb, d, mask);
  387. m = context.AddIntrinsic(Intrinsic.X86Pshufb, m, mask);
  388. }
  389. Operand resD = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, d, m);
  390. Operand resM = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, d, m);
  391. return (resM, resD);
  392. }
  393. else
  394. {
  395. Intrinsic punpcklInst = X86PunpcklInstruction[op.Size];
  396. Operand res = context.AddIntrinsic(punpcklInst, d, m);
  397. if (op.Size < 2)
  398. {
  399. long maskE0 = _masksE0_Uzp[op.Size];
  400. long maskE1 = _masksE1_Uzp[op.Size];
  401. Operand mask = X86GetScalar(context, maskE0);
  402. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  403. res = context.AddIntrinsic(Intrinsic.X86Pshufb, res, mask);
  404. }
  405. Operand resD = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, res, context.VectorZero());
  406. Operand resM = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, res, context.VectorZero());
  407. return (resM, resD);
  408. }
  409. });
  410. }
  411. else
  412. {
  413. int elems = op.GetBytesCount() >> op.Size;
  414. int pairs = elems >> 1;
  415. bool overlap = op.Qm == op.Qd;
  416. Operand resD = GetVecA32(op.Qd);
  417. Operand resM = GetVecA32(op.Qm);
  418. for (int index = 0; index < elems; index++)
  419. {
  420. Operand dIns, mIns;
  421. if (index >= pairs)
  422. {
  423. int pairIndex = index - pairs;
  424. dIns = EmitVectorExtract32(context, op.Qm, (pairIndex << 1) + op.Im, op.Size, false);
  425. mIns = EmitVectorExtract32(context, op.Qm, ((pairIndex << 1) | 1) + op.Im, op.Size, false);
  426. }
  427. else
  428. {
  429. dIns = EmitVectorExtract32(context, op.Qd, (index << 1) + op.Id, op.Size, false);
  430. mIns = EmitVectorExtract32(context, op.Qd, ((index << 1) | 1) + op.Id, op.Size, false);
  431. }
  432. resD = EmitVectorInsert(context, resD, dIns, index + op.Id, op.Size);
  433. if (overlap)
  434. {
  435. resM = resD;
  436. }
  437. resM = EmitVectorInsert(context, resM, mIns, index + op.Im, op.Size);
  438. if (overlap)
  439. {
  440. resD = resM;
  441. }
  442. }
  443. context.Copy(GetVecA32(op.Qd), resD);
  444. if (!overlap)
  445. {
  446. context.Copy(GetVecA32(op.Qm), resM);
  447. }
  448. }
  449. }
  450. private static void EmitVectorShuffleOpSimd32(ArmEmitterContext context, Func<Operand, Operand, (Operand, Operand)> shuffleFunc)
  451. {
  452. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  453. Operand m = GetVecA32(op.Qm);
  454. Operand d = GetVecA32(op.Qd);
  455. Operand initialM = m;
  456. Operand initialD = d;
  457. if (!op.Q) // Register swap: move relevant doubleword to side 0, for consistency.
  458. {
  459. m = EmitMoveDoubleWordToSide(context, m, op.Vm, 0);
  460. d = EmitMoveDoubleWordToSide(context, d, op.Vd, 0);
  461. }
  462. (Operand resM, Operand resD) = shuffleFunc(m, d);
  463. bool overlap = op.Qm == op.Qd;
  464. if (!op.Q) // Register insert.
  465. {
  466. resM = EmitDoubleWordInsert(context, initialM, EmitMoveDoubleWordToSide(context, resM, 0, op.Vm), op.Vm);
  467. resD = EmitDoubleWordInsert(context, overlap ? resM : initialD, EmitMoveDoubleWordToSide(context, resD, 0, op.Vd), op.Vd);
  468. }
  469. if (!overlap)
  470. {
  471. context.Copy(initialM, resM);
  472. }
  473. context.Copy(initialD, resD);
  474. }
  475. }
  476. }