InstEmitSimdCmp32.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using System.Reflection;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.Instructions.InstEmitSimdHelper;
  9. using static ARMeilleure.Instructions.InstEmitSimdHelper32;
  10. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  11. namespace ARMeilleure.Instructions
  12. {
  13. using Func2I = Func<Operand, Operand, Operand>;
  14. static partial class InstEmit32
  15. {
  16. public static void Vceq_V(ArmEmitterContext context)
  17. {
  18. if (Optimizations.FastFP && Optimizations.UseSse2)
  19. {
  20. EmitSse2OrAvxCmpOpF32(context, CmpCondition.Equal, false);
  21. }
  22. else
  23. {
  24. EmitCmpOpF32(context, nameof(SoftFloat32.FPCompareEQFpscr), false);
  25. }
  26. }
  27. public static void Vceq_I(ArmEmitterContext context)
  28. {
  29. EmitCmpOpI32(context, context.ICompareEqual, context.ICompareEqual, false, false);
  30. }
  31. public static void Vceq_Z(ArmEmitterContext context)
  32. {
  33. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  34. if (op.F)
  35. {
  36. if (Optimizations.FastFP && Optimizations.UseSse2)
  37. {
  38. EmitSse2OrAvxCmpOpF32(context, CmpCondition.Equal, true);
  39. }
  40. else
  41. {
  42. EmitCmpOpF32(context, nameof(SoftFloat32.FPCompareEQFpscr), true);
  43. }
  44. }
  45. else
  46. {
  47. EmitCmpOpI32(context, context.ICompareEqual, context.ICompareEqual, true, false);
  48. }
  49. }
  50. public static void Vcge_V(ArmEmitterContext context)
  51. {
  52. if (Optimizations.FastFP && Optimizations.UseAvx)
  53. {
  54. EmitSse2OrAvxCmpOpF32(context, CmpCondition.GreaterThanOrEqual, false);
  55. }
  56. else
  57. {
  58. EmitCmpOpF32(context, nameof(SoftFloat32.FPCompareGEFpscr), false);
  59. }
  60. }
  61. public static void Vcge_I(ArmEmitterContext context)
  62. {
  63. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  64. EmitCmpOpI32(context, context.ICompareGreaterOrEqual, context.ICompareGreaterOrEqualUI, false, !op.U);
  65. }
  66. public static void Vcge_Z(ArmEmitterContext context)
  67. {
  68. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  69. if (op.F)
  70. {
  71. if (Optimizations.FastFP && Optimizations.UseAvx)
  72. {
  73. EmitSse2OrAvxCmpOpF32(context, CmpCondition.GreaterThanOrEqual, true);
  74. }
  75. else
  76. {
  77. EmitCmpOpF32(context, nameof(SoftFloat32.FPCompareGEFpscr), true);
  78. }
  79. }
  80. else
  81. {
  82. EmitCmpOpI32(context, context.ICompareGreaterOrEqual, context.ICompareGreaterOrEqualUI, true, true);
  83. }
  84. }
  85. public static void Vcgt_V(ArmEmitterContext context)
  86. {
  87. if (Optimizations.FastFP && Optimizations.UseAvx)
  88. {
  89. EmitSse2OrAvxCmpOpF32(context, CmpCondition.GreaterThan, false);
  90. }
  91. else
  92. {
  93. EmitCmpOpF32(context, nameof(SoftFloat32.FPCompareGTFpscr), false);
  94. }
  95. }
  96. public static void Vcgt_I(ArmEmitterContext context)
  97. {
  98. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  99. EmitCmpOpI32(context, context.ICompareGreater, context.ICompareGreaterUI, false, !op.U);
  100. }
  101. public static void Vcgt_Z(ArmEmitterContext context)
  102. {
  103. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  104. if (op.F)
  105. {
  106. if (Optimizations.FastFP && Optimizations.UseAvx)
  107. {
  108. EmitSse2OrAvxCmpOpF32(context, CmpCondition.GreaterThan, true);
  109. }
  110. else
  111. {
  112. EmitCmpOpF32(context, nameof(SoftFloat32.FPCompareGTFpscr), true);
  113. }
  114. }
  115. else
  116. {
  117. EmitCmpOpI32(context, context.ICompareGreater, context.ICompareGreaterUI, true, true);
  118. }
  119. }
  120. public static void Vcle_Z(ArmEmitterContext context)
  121. {
  122. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  123. if (op.F)
  124. {
  125. if (Optimizations.FastFP && Optimizations.UseSse2)
  126. {
  127. EmitSse2OrAvxCmpOpF32(context, CmpCondition.LessThanOrEqual, true);
  128. }
  129. else
  130. {
  131. EmitCmpOpF32(context, nameof(SoftFloat32.FPCompareLEFpscr), true);
  132. }
  133. }
  134. else
  135. {
  136. EmitCmpOpI32(context, context.ICompareLessOrEqual, context.ICompareLessOrEqualUI, true, true);
  137. }
  138. }
  139. public static void Vclt_Z(ArmEmitterContext context)
  140. {
  141. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  142. if (op.F)
  143. {
  144. if (Optimizations.FastFP && Optimizations.UseSse2)
  145. {
  146. EmitSse2OrAvxCmpOpF32(context, CmpCondition.LessThan, true);
  147. }
  148. else
  149. {
  150. EmitCmpOpF32(context, nameof(SoftFloat32.FPCompareLTFpscr), true);
  151. }
  152. }
  153. else
  154. {
  155. EmitCmpOpI32(context, context.ICompareLess, context.ICompareLessUI, true, true);
  156. }
  157. }
  158. private static void EmitCmpOpF32(ArmEmitterContext context, string name, bool zero)
  159. {
  160. Operand one = Const(1);
  161. if (zero)
  162. {
  163. EmitVectorUnaryOpF32(context, (m) =>
  164. {
  165. OperandType type = m.Type;
  166. if (type == OperandType.FP64)
  167. {
  168. return context.Call(typeof(SoftFloat64).GetMethod(name), m, ConstF(0.0d), one);
  169. }
  170. else
  171. {
  172. return context.Call(typeof(SoftFloat32).GetMethod(name), m, ConstF(0.0f), one);
  173. }
  174. });
  175. }
  176. else
  177. {
  178. EmitVectorBinaryOpF32(context, (n, m) =>
  179. {
  180. OperandType type = n.Type;
  181. if (type == OperandType.FP64)
  182. {
  183. return context.Call(typeof(SoftFloat64).GetMethod(name), n, m, one);
  184. }
  185. else
  186. {
  187. return context.Call(typeof(SoftFloat32).GetMethod(name), n, m, one);
  188. }
  189. });
  190. }
  191. }
  192. private static Operand ZerosOrOnes(ArmEmitterContext context, Operand fromBool, OperandType baseType)
  193. {
  194. var ones = (baseType == OperandType.I64) ? Const(-1L) : Const(-1);
  195. return context.ConditionalSelect(fromBool, ones, Const(baseType, 0L));
  196. }
  197. private static void EmitCmpOpI32(
  198. ArmEmitterContext context,
  199. Func2I signedOp,
  200. Func2I unsignedOp,
  201. bool zero,
  202. bool signed)
  203. {
  204. if (zero)
  205. {
  206. if (signed)
  207. {
  208. EmitVectorUnaryOpSx32(context, (m) =>
  209. {
  210. OperandType type = m.Type;
  211. Operand zeroV = (type == OperandType.I64) ? Const(0L) : Const(0);
  212. return ZerosOrOnes(context, signedOp(m, zeroV), type);
  213. });
  214. }
  215. else
  216. {
  217. EmitVectorUnaryOpZx32(context, (m) =>
  218. {
  219. OperandType type = m.Type;
  220. Operand zeroV = (type == OperandType.I64) ? Const(0L) : Const(0);
  221. return ZerosOrOnes(context, unsignedOp(m, zeroV), type);
  222. });
  223. }
  224. }
  225. else
  226. {
  227. if (signed)
  228. {
  229. EmitVectorBinaryOpSx32(context, (n, m) => ZerosOrOnes(context, signedOp(n, m), n.Type));
  230. }
  231. else
  232. {
  233. EmitVectorBinaryOpZx32(context, (n, m) => ZerosOrOnes(context, unsignedOp(n, m), n.Type));
  234. }
  235. }
  236. }
  237. public static void Vcmp(ArmEmitterContext context)
  238. {
  239. EmitVcmpOrVcmpe(context, false);
  240. }
  241. public static void Vcmpe(ArmEmitterContext context)
  242. {
  243. EmitVcmpOrVcmpe(context, true);
  244. }
  245. private static void EmitVcmpOrVcmpe(ArmEmitterContext context, bool signalNaNs)
  246. {
  247. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  248. bool cmpWithZero = (op.Opc & 2) != 0;
  249. int sizeF = op.Size & 1;
  250. if (Optimizations.FastFP && (signalNaNs ? Optimizations.UseAvx : Optimizations.UseSse2))
  251. {
  252. CmpCondition cmpOrdered = signalNaNs ? CmpCondition.OrderedS : CmpCondition.OrderedQ;
  253. bool doubleSize = sizeF != 0;
  254. int shift = doubleSize ? 1 : 2;
  255. Operand m = GetVecA32(op.Vm >> shift);
  256. Operand n = GetVecA32(op.Vd >> shift);
  257. n = EmitSwapScalar(context, n, op.Vd, doubleSize);
  258. m = cmpWithZero ? context.VectorZero() : EmitSwapScalar(context, m, op.Vm, doubleSize);
  259. Operand lblNaN = Label();
  260. Operand lblEnd = Label();
  261. if (!doubleSize)
  262. {
  263. Operand ordMask = context.AddIntrinsic(Intrinsic.X86Cmpss, n, m, Const((int)cmpOrdered));
  264. Operand isOrdered = context.AddIntrinsicInt(Intrinsic.X86Cvtsi2si, ordMask);
  265. context.BranchIfFalse(lblNaN, isOrdered);
  266. Operand cf = context.AddIntrinsicInt(Intrinsic.X86Comissge, n, m);
  267. Operand zf = context.AddIntrinsicInt(Intrinsic.X86Comisseq, n, m);
  268. Operand nf = context.AddIntrinsicInt(Intrinsic.X86Comisslt, n, m);
  269. SetFpFlag(context, FPState.VFlag, Const(0));
  270. SetFpFlag(context, FPState.CFlag, cf);
  271. SetFpFlag(context, FPState.ZFlag, zf);
  272. SetFpFlag(context, FPState.NFlag, nf);
  273. }
  274. else
  275. {
  276. Operand ordMask = context.AddIntrinsic(Intrinsic.X86Cmpsd, n, m, Const((int)cmpOrdered));
  277. Operand isOrdered = context.AddIntrinsicLong(Intrinsic.X86Cvtsi2si, ordMask);
  278. context.BranchIfFalse(lblNaN, isOrdered);
  279. Operand cf = context.AddIntrinsicInt(Intrinsic.X86Comisdge, n, m);
  280. Operand zf = context.AddIntrinsicInt(Intrinsic.X86Comisdeq, n, m);
  281. Operand nf = context.AddIntrinsicInt(Intrinsic.X86Comisdlt, n, m);
  282. SetFpFlag(context, FPState.VFlag, Const(0));
  283. SetFpFlag(context, FPState.CFlag, cf);
  284. SetFpFlag(context, FPState.ZFlag, zf);
  285. SetFpFlag(context, FPState.NFlag, nf);
  286. }
  287. context.Branch(lblEnd);
  288. context.MarkLabel(lblNaN);
  289. SetFpFlag(context, FPState.VFlag, Const(1));
  290. SetFpFlag(context, FPState.CFlag, Const(1));
  291. SetFpFlag(context, FPState.ZFlag, Const(0));
  292. SetFpFlag(context, FPState.NFlag, Const(0));
  293. context.MarkLabel(lblEnd);
  294. }
  295. else
  296. {
  297. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  298. Operand ne = ExtractScalar(context, type, op.Vd);
  299. Operand me;
  300. if (cmpWithZero)
  301. {
  302. me = sizeF == 0 ? ConstF(0f) : ConstF(0d);
  303. }
  304. else
  305. {
  306. me = ExtractScalar(context, type, op.Vm);
  307. }
  308. MethodInfo info = sizeF != 0
  309. ? typeof(SoftFloat64).GetMethod(nameof(SoftFloat64.FPCompare))
  310. : typeof(SoftFloat32).GetMethod(nameof(SoftFloat32.FPCompare));
  311. Operand nzcv = context.Call(info, ne, me, Const(signalNaNs));
  312. EmitSetFpscrNzcv(context, nzcv);
  313. }
  314. }
  315. private static void EmitSetFpscrNzcv(ArmEmitterContext context, Operand nzcv)
  316. {
  317. Operand Extract(Operand value, int bit)
  318. {
  319. if (bit != 0)
  320. {
  321. value = context.ShiftRightUI(value, Const(bit));
  322. }
  323. value = context.BitwiseAnd(value, Const(1));
  324. return value;
  325. }
  326. SetFpFlag(context, FPState.VFlag, Extract(nzcv, 0));
  327. SetFpFlag(context, FPState.CFlag, Extract(nzcv, 1));
  328. SetFpFlag(context, FPState.ZFlag, Extract(nzcv, 2));
  329. SetFpFlag(context, FPState.NFlag, Extract(nzcv, 3));
  330. }
  331. private static void EmitSse2OrAvxCmpOpF32(ArmEmitterContext context, CmpCondition cond, bool zero)
  332. {
  333. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  334. int sizeF = op.Size & 1;
  335. Intrinsic inst = (sizeF == 0) ? Intrinsic.X86Cmpps : Intrinsic.X86Cmppd;
  336. if (zero)
  337. {
  338. EmitVectorUnaryOpSimd32(context, (m) =>
  339. {
  340. return context.AddIntrinsic(inst, m, context.VectorZero(), Const((int)cond));
  341. });
  342. }
  343. else
  344. {
  345. EmitVectorBinaryOpSimd32(context, (n, m) =>
  346. {
  347. return context.AddIntrinsic(inst, n, m, Const((int)cond));
  348. });
  349. }
  350. }
  351. }
  352. }