InstEmitSimdLogical.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System.Diagnostics;
  5. using static ARMeilleure.Instructions.InstEmitHelper;
  6. using static ARMeilleure.Instructions.InstEmitSimdHelper;
  7. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  8. namespace ARMeilleure.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. public static void And_V(ArmEmitterContext context)
  13. {
  14. if (Optimizations.UseSse2)
  15. {
  16. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  17. Operand n = GetVec(op.Rn);
  18. Operand m = GetVec(op.Rm);
  19. Operand res = context.AddIntrinsic(Intrinsic.X86Pand, n, m);
  20. if (op.RegisterSize == RegisterSize.Simd64)
  21. {
  22. res = context.VectorZeroUpper64(res);
  23. }
  24. context.Copy(GetVec(op.Rd), res);
  25. }
  26. else
  27. {
  28. EmitVectorBinaryOpZx(context, (op1, op2) => context.BitwiseAnd(op1, op2));
  29. }
  30. }
  31. public static void Bic_V(ArmEmitterContext context)
  32. {
  33. if (Optimizations.UseSse2)
  34. {
  35. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  36. Operand n = GetVec(op.Rn);
  37. Operand m = GetVec(op.Rm);
  38. Operand res = context.AddIntrinsic(Intrinsic.X86Pandn, m, n);
  39. if (op.RegisterSize == RegisterSize.Simd64)
  40. {
  41. res = context.VectorZeroUpper64(res);
  42. }
  43. context.Copy(GetVec(op.Rd), res);
  44. }
  45. else
  46. {
  47. EmitVectorBinaryOpZx(context, (op1, op2) =>
  48. {
  49. return context.BitwiseAnd(op1, context.BitwiseNot(op2));
  50. });
  51. }
  52. }
  53. public static void Bic_Vi(ArmEmitterContext context)
  54. {
  55. EmitVectorImmBinaryOp(context, (op1, op2) =>
  56. {
  57. return context.BitwiseAnd(op1, context.BitwiseNot(op2));
  58. });
  59. }
  60. public static void Bif_V(ArmEmitterContext context)
  61. {
  62. EmitBifBit(context, notRm: true);
  63. }
  64. public static void Bit_V(ArmEmitterContext context)
  65. {
  66. EmitBifBit(context, notRm: false);
  67. }
  68. private static void EmitBifBit(ArmEmitterContext context, bool notRm)
  69. {
  70. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  71. if (Optimizations.UseSse2)
  72. {
  73. Operand d = GetVec(op.Rd);
  74. Operand n = GetVec(op.Rn);
  75. Operand m = GetVec(op.Rm);
  76. Operand res = context.AddIntrinsic(Intrinsic.X86Pxor, n, d);
  77. if (notRm)
  78. {
  79. res = context.AddIntrinsic(Intrinsic.X86Pandn, m, res);
  80. }
  81. else
  82. {
  83. res = context.AddIntrinsic(Intrinsic.X86Pand, m, res);
  84. }
  85. res = context.AddIntrinsic(Intrinsic.X86Pxor, d, res);
  86. if (op.RegisterSize == RegisterSize.Simd64)
  87. {
  88. res = context.VectorZeroUpper64(res);
  89. }
  90. context.Copy(d, res);
  91. }
  92. else
  93. {
  94. Operand res = context.VectorZero();
  95. int elems = op.RegisterSize == RegisterSize.Simd128 ? 2 : 1;
  96. for (int index = 0; index < elems; index++)
  97. {
  98. Operand d = EmitVectorExtractZx(context, op.Rd, index, 3);
  99. Operand n = EmitVectorExtractZx(context, op.Rn, index, 3);
  100. Operand m = EmitVectorExtractZx(context, op.Rm, index, 3);
  101. if (notRm)
  102. {
  103. m = context.BitwiseNot(m);
  104. }
  105. Operand e = context.BitwiseExclusiveOr(d, n);
  106. e = context.BitwiseAnd(e, m);
  107. e = context.BitwiseExclusiveOr(e, d);
  108. res = EmitVectorInsert(context, res, e, index, 3);
  109. }
  110. context.Copy(GetVec(op.Rd), res);
  111. }
  112. }
  113. public static void Bsl_V(ArmEmitterContext context)
  114. {
  115. if (Optimizations.UseSse2)
  116. {
  117. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  118. Operand d = GetVec(op.Rd);
  119. Operand n = GetVec(op.Rn);
  120. Operand m = GetVec(op.Rm);
  121. Operand res = context.AddIntrinsic(Intrinsic.X86Pxor, n, m);
  122. res = context.AddIntrinsic(Intrinsic.X86Pand, res, d);
  123. res = context.AddIntrinsic(Intrinsic.X86Pxor, res, m);
  124. if (op.RegisterSize == RegisterSize.Simd64)
  125. {
  126. res = context.VectorZeroUpper64(res);
  127. }
  128. context.Copy(d, res);
  129. }
  130. else
  131. {
  132. EmitVectorTernaryOpZx(context, (op1, op2, op3) =>
  133. {
  134. return context.BitwiseExclusiveOr(
  135. context.BitwiseAnd(op1,
  136. context.BitwiseExclusiveOr(op2, op3)), op3);
  137. });
  138. }
  139. }
  140. public static void Eor_V(ArmEmitterContext context)
  141. {
  142. if (Optimizations.UseSse2)
  143. {
  144. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  145. Operand n = GetVec(op.Rn);
  146. Operand m = GetVec(op.Rm);
  147. Operand res = context.AddIntrinsic(Intrinsic.X86Pxor, n, m);
  148. if (op.RegisterSize == RegisterSize.Simd64)
  149. {
  150. res = context.VectorZeroUpper64(res);
  151. }
  152. context.Copy(GetVec(op.Rd), res);
  153. }
  154. else
  155. {
  156. EmitVectorBinaryOpZx(context, (op1, op2) => context.BitwiseExclusiveOr(op1, op2));
  157. }
  158. }
  159. public static void Not_V(ArmEmitterContext context)
  160. {
  161. if (Optimizations.UseSse2)
  162. {
  163. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  164. Operand n = GetVec(op.Rn);
  165. Operand mask = X86GetAllElements(context, -1L);
  166. Operand res = context.AddIntrinsic(Intrinsic.X86Pandn, n, mask);
  167. if (op.RegisterSize == RegisterSize.Simd64)
  168. {
  169. res = context.VectorZeroUpper64(res);
  170. }
  171. context.Copy(GetVec(op.Rd), res);
  172. }
  173. else
  174. {
  175. EmitVectorUnaryOpZx(context, (op1) => context.BitwiseNot(op1));
  176. }
  177. }
  178. public static void Orn_V(ArmEmitterContext context)
  179. {
  180. if (Optimizations.UseSse2)
  181. {
  182. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  183. Operand n = GetVec(op.Rn);
  184. Operand m = GetVec(op.Rm);
  185. Operand mask = X86GetAllElements(context, -1L);
  186. Operand res = context.AddIntrinsic(Intrinsic.X86Pandn, m, mask);
  187. res = context.AddIntrinsic(Intrinsic.X86Por, res, n);
  188. if (op.RegisterSize == RegisterSize.Simd64)
  189. {
  190. res = context.VectorZeroUpper64(res);
  191. }
  192. context.Copy(GetVec(op.Rd), res);
  193. }
  194. else
  195. {
  196. EmitVectorBinaryOpZx(context, (op1, op2) =>
  197. {
  198. return context.BitwiseOr(op1, context.BitwiseNot(op2));
  199. });
  200. }
  201. }
  202. public static void Orr_V(ArmEmitterContext context)
  203. {
  204. if (Optimizations.UseSse2)
  205. {
  206. OpCodeSimdReg op = (OpCodeSimdReg)context.CurrOp;
  207. Operand n = GetVec(op.Rn);
  208. Operand m = GetVec(op.Rm);
  209. Operand res = context.AddIntrinsic(Intrinsic.X86Por, n, m);
  210. if (op.RegisterSize == RegisterSize.Simd64)
  211. {
  212. res = context.VectorZeroUpper64(res);
  213. }
  214. context.Copy(GetVec(op.Rd), res);
  215. }
  216. else
  217. {
  218. EmitVectorBinaryOpZx(context, (op1, op2) => context.BitwiseOr(op1, op2));
  219. }
  220. }
  221. public static void Orr_Vi(ArmEmitterContext context)
  222. {
  223. EmitVectorImmBinaryOp(context, (op1, op2) => context.BitwiseOr(op1, op2));
  224. }
  225. public static void Rbit_V(ArmEmitterContext context)
  226. {
  227. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  228. Operand res = context.VectorZero();
  229. int elems = op.RegisterSize == RegisterSize.Simd128 ? 16 : 8;
  230. for (int index = 0; index < elems; index++)
  231. {
  232. Operand ne = EmitVectorExtractZx(context, op.Rn, index, 0);
  233. Operand de = EmitReverseBits8Op(context, ne);
  234. res = EmitVectorInsert(context, res, de, index, 0);
  235. }
  236. context.Copy(GetVec(op.Rd), res);
  237. }
  238. private static Operand EmitReverseBits8Op(ArmEmitterContext context, Operand op)
  239. {
  240. Debug.Assert(op.Type == OperandType.I64);
  241. Operand val = context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op, Const(0xaaul)), Const(1)),
  242. context.ShiftLeft (context.BitwiseAnd(op, Const(0x55ul)), Const(1)));
  243. val = context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(val, Const(0xccul)), Const(2)),
  244. context.ShiftLeft (context.BitwiseAnd(val, Const(0x33ul)), Const(2)));
  245. return context.BitwiseOr(context.ShiftRightUI(val, Const(4)),
  246. context.ShiftLeft (context.BitwiseAnd(val, Const(0x0ful)), Const(4)));
  247. }
  248. public static void Rev16_V(ArmEmitterContext context)
  249. {
  250. if (Optimizations.UseSsse3)
  251. {
  252. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  253. Operand n = GetVec(op.Rn);
  254. const long maskE0 = 06L << 56 | 07L << 48 | 04L << 40 | 05L << 32 | 02L << 24 | 03L << 16 | 00L << 8 | 01L << 0;
  255. const long maskE1 = 14L << 56 | 15L << 48 | 12L << 40 | 13L << 32 | 10L << 24 | 11L << 16 | 08L << 8 | 09L << 0;
  256. Operand mask = X86GetScalar(context, maskE0);
  257. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  258. Operand res = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mask);
  259. if (op.RegisterSize == RegisterSize.Simd64)
  260. {
  261. res = context.VectorZeroUpper64(res);
  262. }
  263. context.Copy(GetVec(op.Rd), res);
  264. }
  265. else
  266. {
  267. EmitRev_V(context, containerSize: 1);
  268. }
  269. }
  270. public static void Rev32_V(ArmEmitterContext context)
  271. {
  272. if (Optimizations.UseSsse3)
  273. {
  274. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  275. Operand n = GetVec(op.Rn);
  276. Operand mask;
  277. if (op.Size == 0)
  278. {
  279. const long maskE0 = 04L << 56 | 05L << 48 | 06L << 40 | 07L << 32 | 00L << 24 | 01L << 16 | 02L << 8 | 03L << 0;
  280. const long maskE1 = 12L << 56 | 13L << 48 | 14L << 40 | 15L << 32 | 08L << 24 | 09L << 16 | 10L << 8 | 11L << 0;
  281. mask = X86GetScalar(context, maskE0);
  282. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  283. }
  284. else /* if (op.Size == 1) */
  285. {
  286. const long maskE0 = 05L << 56 | 04L << 48 | 07L << 40 | 06L << 32 | 01L << 24 | 00L << 16 | 03L << 8 | 02L << 0;
  287. const long maskE1 = 13L << 56 | 12L << 48 | 15L << 40 | 14L << 32 | 09L << 24 | 08L << 16 | 11L << 8 | 10L << 0;
  288. mask = X86GetScalar(context, maskE0);
  289. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  290. }
  291. Operand res = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mask);
  292. if (op.RegisterSize == RegisterSize.Simd64)
  293. {
  294. res = context.VectorZeroUpper64(res);
  295. }
  296. context.Copy(GetVec(op.Rd), res);
  297. }
  298. else
  299. {
  300. EmitRev_V(context, containerSize: 2);
  301. }
  302. }
  303. public static void Rev64_V(ArmEmitterContext context)
  304. {
  305. if (Optimizations.UseSsse3)
  306. {
  307. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  308. Operand n = GetVec(op.Rn);
  309. Operand mask;
  310. if (op.Size == 0)
  311. {
  312. const long maskE0 = 00L << 56 | 01L << 48 | 02L << 40 | 03L << 32 | 04L << 24 | 05L << 16 | 06L << 8 | 07L << 0;
  313. const long maskE1 = 08L << 56 | 09L << 48 | 10L << 40 | 11L << 32 | 12L << 24 | 13L << 16 | 14L << 8 | 15L << 0;
  314. mask = X86GetScalar(context, maskE0);
  315. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  316. }
  317. else if (op.Size == 1)
  318. {
  319. const long maskE0 = 01L << 56 | 00L << 48 | 03L << 40 | 02L << 32 | 05L << 24 | 04L << 16 | 07L << 8 | 06L << 0;
  320. const long maskE1 = 09L << 56 | 08L << 48 | 11L << 40 | 10L << 32 | 13L << 24 | 12L << 16 | 15L << 8 | 14L << 0;
  321. mask = X86GetScalar(context, maskE0);
  322. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  323. }
  324. else /* if (op.Size == 2) */
  325. {
  326. const long maskE0 = 03L << 56 | 02L << 48 | 01L << 40 | 00L << 32 | 07L << 24 | 06L << 16 | 05L << 8 | 04L << 0;
  327. const long maskE1 = 11L << 56 | 10L << 48 | 09L << 40 | 08L << 32 | 15L << 24 | 14L << 16 | 13L << 8 | 12L << 0;
  328. mask = X86GetScalar(context, maskE0);
  329. mask = EmitVectorInsert(context, mask, Const(maskE1), 1, 3);
  330. }
  331. Operand res = context.AddIntrinsic(Intrinsic.X86Pshufb, n, mask);
  332. if (op.RegisterSize == RegisterSize.Simd64)
  333. {
  334. res = context.VectorZeroUpper64(res);
  335. }
  336. context.Copy(GetVec(op.Rd), res);
  337. }
  338. else
  339. {
  340. EmitRev_V(context, containerSize: 3);
  341. }
  342. }
  343. private static void EmitRev_V(ArmEmitterContext context, int containerSize)
  344. {
  345. OpCodeSimd op = (OpCodeSimd)context.CurrOp;
  346. Operand res = context.VectorZero();
  347. int elems = op.GetBytesCount() >> op.Size;
  348. int containerMask = (1 << (containerSize - op.Size)) - 1;
  349. for (int index = 0; index < elems; index++)
  350. {
  351. int revIndex = index ^ containerMask;
  352. Operand ne = EmitVectorExtractZx(context, op.Rn, revIndex, op.Size);
  353. res = EmitVectorInsert(context, res, ne, index, op.Size);
  354. }
  355. context.Copy(GetVec(op.Rd), res);
  356. }
  357. }
  358. }