AInstEmitSimdCmp.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. using System.Runtime.Intrinsics.X86;
  7. using static ChocolArm64.Instruction.AInstEmitAluHelper;
  8. using static ChocolArm64.Instruction.AInstEmitSimdHelper;
  9. namespace ChocolArm64.Instruction
  10. {
  11. static partial class AInstEmit
  12. {
  13. public static void Cmeq_S(AILEmitterCtx Context)
  14. {
  15. EmitCmp(Context, OpCodes.Beq_S, Scalar: true);
  16. }
  17. public static void Cmeq_V(AILEmitterCtx Context)
  18. {
  19. if (AOptimizations.UseSse2 && Context.CurrOp is AOpCodeSimdReg Op && Op.Size < 3)
  20. {
  21. EmitSse2Call(Context, nameof(Sse2.CompareEqual));
  22. }
  23. else
  24. {
  25. EmitCmp(Context, OpCodes.Beq_S, Scalar: false);
  26. }
  27. }
  28. public static void Cmge_S(AILEmitterCtx Context)
  29. {
  30. EmitCmp(Context, OpCodes.Bge_S, Scalar: true);
  31. }
  32. public static void Cmge_V(AILEmitterCtx Context)
  33. {
  34. EmitCmp(Context, OpCodes.Bge_S, Scalar: false);
  35. }
  36. public static void Cmgt_S(AILEmitterCtx Context)
  37. {
  38. EmitCmp(Context, OpCodes.Bgt_S, Scalar: true);
  39. }
  40. public static void Cmgt_V(AILEmitterCtx Context)
  41. {
  42. if (AOptimizations.UseSse2 && Context.CurrOp is AOpCodeSimdReg Op && Op.Size < 3)
  43. {
  44. EmitSse2Call(Context, nameof(Sse2.CompareGreaterThan));
  45. }
  46. else
  47. {
  48. EmitCmp(Context, OpCodes.Bgt_S, Scalar: false);
  49. }
  50. }
  51. public static void Cmhi_S(AILEmitterCtx Context)
  52. {
  53. EmitCmp(Context, OpCodes.Bgt_Un_S, Scalar: true);
  54. }
  55. public static void Cmhi_V(AILEmitterCtx Context)
  56. {
  57. EmitCmp(Context, OpCodes.Bgt_Un_S, Scalar: false);
  58. }
  59. public static void Cmhs_S(AILEmitterCtx Context)
  60. {
  61. EmitCmp(Context, OpCodes.Bge_Un_S, Scalar: true);
  62. }
  63. public static void Cmhs_V(AILEmitterCtx Context)
  64. {
  65. EmitCmp(Context, OpCodes.Bge_Un_S, Scalar: false);
  66. }
  67. public static void Cmle_S(AILEmitterCtx Context)
  68. {
  69. EmitCmp(Context, OpCodes.Ble_S, Scalar: true);
  70. }
  71. public static void Cmle_V(AILEmitterCtx Context)
  72. {
  73. EmitCmp(Context, OpCodes.Ble_S, Scalar: false);
  74. }
  75. public static void Cmlt_S(AILEmitterCtx Context)
  76. {
  77. EmitCmp(Context, OpCodes.Blt_S, Scalar: true);
  78. }
  79. public static void Cmlt_V(AILEmitterCtx Context)
  80. {
  81. EmitCmp(Context, OpCodes.Blt_S, Scalar: false);
  82. }
  83. public static void Cmtst_S(AILEmitterCtx Context)
  84. {
  85. EmitCmtst(Context, Scalar: true);
  86. }
  87. public static void Cmtst_V(AILEmitterCtx Context)
  88. {
  89. EmitCmtst(Context, Scalar: false);
  90. }
  91. public static void Fccmp_S(AILEmitterCtx Context)
  92. {
  93. AOpCodeSimdFcond Op = (AOpCodeSimdFcond)Context.CurrOp;
  94. AILLabel LblTrue = new AILLabel();
  95. AILLabel LblEnd = new AILLabel();
  96. Context.EmitCondBranch(LblTrue, Op.Cond);
  97. EmitSetNZCV(Context, Op.NZCV);
  98. Context.Emit(OpCodes.Br, LblEnd);
  99. Context.MarkLabel(LblTrue);
  100. Fcmp_S(Context);
  101. Context.MarkLabel(LblEnd);
  102. }
  103. public static void Fccmpe_S(AILEmitterCtx Context)
  104. {
  105. Fccmp_S(Context);
  106. }
  107. public static void Fcmeq_S(AILEmitterCtx Context)
  108. {
  109. if (AOptimizations.UseSse2 && Context.CurrOp is AOpCodeSimdReg)
  110. {
  111. EmitSse2CallF(Context, nameof(Sse2.CompareEqualScalar));
  112. }
  113. else
  114. {
  115. EmitScalarFcmp(Context, OpCodes.Beq_S);
  116. }
  117. }
  118. public static void Fcmeq_V(AILEmitterCtx Context)
  119. {
  120. if (AOptimizations.UseSse2 && Context.CurrOp is AOpCodeSimdReg)
  121. {
  122. EmitSse2CallF(Context, nameof(Sse2.CompareEqual));
  123. }
  124. else
  125. {
  126. EmitVectorFcmp(Context, OpCodes.Beq_S);
  127. }
  128. }
  129. public static void Fcmge_S(AILEmitterCtx Context)
  130. {
  131. if (AOptimizations.UseSse2 && Context.CurrOp is AOpCodeSimdReg)
  132. {
  133. EmitSse2CallF(Context, nameof(Sse2.CompareGreaterThanOrEqualScalar));
  134. }
  135. else
  136. {
  137. EmitScalarFcmp(Context, OpCodes.Bge_S);
  138. }
  139. }
  140. public static void Fcmge_V(AILEmitterCtx Context)
  141. {
  142. if (AOptimizations.UseSse2 && Context.CurrOp is AOpCodeSimdReg)
  143. {
  144. EmitSse2CallF(Context, nameof(Sse2.CompareGreaterThanOrEqual));
  145. }
  146. else
  147. {
  148. EmitVectorFcmp(Context, OpCodes.Bge_S);
  149. }
  150. }
  151. public static void Fcmgt_S(AILEmitterCtx Context)
  152. {
  153. if (AOptimizations.UseSse2 && Context.CurrOp is AOpCodeSimdReg)
  154. {
  155. EmitSse2CallF(Context, nameof(Sse2.CompareGreaterThanScalar));
  156. }
  157. else
  158. {
  159. EmitScalarFcmp(Context, OpCodes.Bgt_S);
  160. }
  161. }
  162. public static void Fcmgt_V(AILEmitterCtx Context)
  163. {
  164. if (AOptimizations.UseSse2 && Context.CurrOp is AOpCodeSimdReg)
  165. {
  166. EmitSse2CallF(Context, nameof(Sse2.CompareGreaterThan));
  167. }
  168. else
  169. {
  170. EmitVectorFcmp(Context, OpCodes.Bgt_S);
  171. }
  172. }
  173. public static void Fcmle_S(AILEmitterCtx Context)
  174. {
  175. EmitScalarFcmp(Context, OpCodes.Ble_S);
  176. }
  177. public static void Fcmle_V(AILEmitterCtx Context)
  178. {
  179. EmitVectorFcmp(Context, OpCodes.Ble_S);
  180. }
  181. public static void Fcmlt_S(AILEmitterCtx Context)
  182. {
  183. EmitScalarFcmp(Context, OpCodes.Blt_S);
  184. }
  185. public static void Fcmlt_V(AILEmitterCtx Context)
  186. {
  187. EmitVectorFcmp(Context, OpCodes.Blt_S);
  188. }
  189. public static void Fcmp_S(AILEmitterCtx Context)
  190. {
  191. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  192. bool CmpWithZero = !(Op is AOpCodeSimdFcond) ? Op.Bit3 : false;
  193. //Handle NaN case.
  194. //If any number is NaN, then NZCV = 0011.
  195. if (CmpWithZero)
  196. {
  197. EmitNaNCheck(Context, Op.Rn);
  198. }
  199. else
  200. {
  201. EmitNaNCheck(Context, Op.Rn);
  202. EmitNaNCheck(Context, Op.Rm);
  203. Context.Emit(OpCodes.Or);
  204. }
  205. AILLabel LblNaN = new AILLabel();
  206. AILLabel LblEnd = new AILLabel();
  207. Context.Emit(OpCodes.Brtrue_S, LblNaN);
  208. void EmitLoadOpers()
  209. {
  210. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  211. if (CmpWithZero)
  212. {
  213. if (Op.Size == 0)
  214. {
  215. Context.EmitLdc_R4(0);
  216. }
  217. else /* if (SizeF == 1) */
  218. {
  219. Context.EmitLdc_R8(0);
  220. }
  221. }
  222. else
  223. {
  224. EmitVectorExtractF(Context, Op.Rm, 0, Op.Size);
  225. }
  226. }
  227. //Z = Rn == Rm
  228. EmitLoadOpers();
  229. Context.Emit(OpCodes.Ceq);
  230. Context.Emit(OpCodes.Dup);
  231. Context.EmitStflg((int)APState.ZBit);
  232. //C = Rn >= Rm
  233. EmitLoadOpers();
  234. Context.Emit(OpCodes.Cgt);
  235. Context.Emit(OpCodes.Or);
  236. Context.EmitStflg((int)APState.CBit);
  237. //N = Rn < Rm
  238. EmitLoadOpers();
  239. Context.Emit(OpCodes.Clt);
  240. Context.EmitStflg((int)APState.NBit);
  241. //V = 0
  242. Context.EmitLdc_I4(0);
  243. Context.EmitStflg((int)APState.VBit);
  244. Context.Emit(OpCodes.Br_S, LblEnd);
  245. Context.MarkLabel(LblNaN);
  246. EmitSetNZCV(Context, 0b0011);
  247. Context.MarkLabel(LblEnd);
  248. }
  249. public static void Fcmpe_S(AILEmitterCtx Context)
  250. {
  251. Fcmp_S(Context);
  252. }
  253. private static void EmitNaNCheck(AILEmitterCtx Context, int Reg)
  254. {
  255. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  256. EmitVectorExtractF(Context, Reg, 0, Op.Size);
  257. if (Op.Size == 0)
  258. {
  259. Context.EmitCall(typeof(float), nameof(float.IsNaN));
  260. }
  261. else if (Op.Size == 1)
  262. {
  263. Context.EmitCall(typeof(double), nameof(double.IsNaN));
  264. }
  265. else
  266. {
  267. throw new InvalidOperationException();
  268. }
  269. }
  270. private static void EmitCmp(AILEmitterCtx Context, OpCode ILOp, bool Scalar)
  271. {
  272. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  273. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  274. int Elems = (!Scalar ? Bytes >> Op.Size : 1);
  275. ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
  276. for (int Index = 0; Index < Elems; Index++)
  277. {
  278. EmitVectorExtractSx(Context, Op.Rn, Index, Op.Size);
  279. if (Op is AOpCodeSimdReg BinOp)
  280. {
  281. EmitVectorExtractSx(Context, BinOp.Rm, Index, Op.Size);
  282. }
  283. else
  284. {
  285. Context.EmitLdc_I8(0);
  286. }
  287. AILLabel LblTrue = new AILLabel();
  288. AILLabel LblEnd = new AILLabel();
  289. Context.Emit(ILOp, LblTrue);
  290. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, 0);
  291. Context.Emit(OpCodes.Br_S, LblEnd);
  292. Context.MarkLabel(LblTrue);
  293. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, (long)SzMask);
  294. Context.MarkLabel(LblEnd);
  295. }
  296. if ((Op.RegisterSize == ARegisterSize.SIMD64) || Scalar)
  297. {
  298. EmitVectorZeroUpper(Context, Op.Rd);
  299. }
  300. }
  301. private static void EmitCmtst(AILEmitterCtx Context, bool Scalar)
  302. {
  303. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  304. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  305. int Elems = (!Scalar ? Bytes >> Op.Size : 1);
  306. ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
  307. for (int Index = 0; Index < Elems; Index++)
  308. {
  309. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size);
  310. EmitVectorExtractZx(Context, Op.Rm, Index, Op.Size);
  311. AILLabel LblTrue = new AILLabel();
  312. AILLabel LblEnd = new AILLabel();
  313. Context.Emit(OpCodes.And);
  314. Context.EmitLdc_I8(0);
  315. Context.Emit(OpCodes.Bne_Un_S, LblTrue);
  316. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, 0);
  317. Context.Emit(OpCodes.Br_S, LblEnd);
  318. Context.MarkLabel(LblTrue);
  319. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, (long)SzMask);
  320. Context.MarkLabel(LblEnd);
  321. }
  322. if ((Op.RegisterSize == ARegisterSize.SIMD64) || Scalar)
  323. {
  324. EmitVectorZeroUpper(Context, Op.Rd);
  325. }
  326. }
  327. private static void EmitScalarFcmp(AILEmitterCtx Context, OpCode ILOp)
  328. {
  329. EmitFcmp(Context, ILOp, 0, Scalar: true);
  330. }
  331. private static void EmitVectorFcmp(AILEmitterCtx Context, OpCode ILOp)
  332. {
  333. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  334. int SizeF = Op.Size & 1;
  335. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  336. for (int Index = 0; Index < Bytes >> SizeF + 2; Index++)
  337. {
  338. EmitFcmp(Context, ILOp, Index, Scalar: false);
  339. }
  340. if (Op.RegisterSize == ARegisterSize.SIMD64)
  341. {
  342. EmitVectorZeroUpper(Context, Op.Rd);
  343. }
  344. }
  345. private static void EmitFcmp(AILEmitterCtx Context, OpCode ILOp, int Index, bool Scalar)
  346. {
  347. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  348. int SizeF = Op.Size & 1;
  349. ulong SzMask = ulong.MaxValue >> (64 - (32 << SizeF));
  350. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  351. if (Op is AOpCodeSimdReg BinOp)
  352. {
  353. EmitVectorExtractF(Context, BinOp.Rm, Index, SizeF);
  354. }
  355. else if (SizeF == 0)
  356. {
  357. Context.EmitLdc_R4(0);
  358. }
  359. else /* if (SizeF == 1) */
  360. {
  361. Context.EmitLdc_R8(0);
  362. }
  363. AILLabel LblTrue = new AILLabel();
  364. AILLabel LblEnd = new AILLabel();
  365. Context.Emit(ILOp, LblTrue);
  366. if (Scalar)
  367. {
  368. EmitVectorZeroAll(Context, Op.Rd);
  369. }
  370. else
  371. {
  372. EmitVectorInsert(Context, Op.Rd, Index, SizeF + 2, 0);
  373. }
  374. Context.Emit(OpCodes.Br_S, LblEnd);
  375. Context.MarkLabel(LblTrue);
  376. if (Scalar)
  377. {
  378. EmitVectorInsert(Context, Op.Rd, Index, 3, (long)SzMask);
  379. EmitVectorZeroUpper(Context, Op.Rd);
  380. }
  381. else
  382. {
  383. EmitVectorInsert(Context, Op.Rd, Index, SizeF + 2, (long)SzMask);
  384. }
  385. Context.MarkLabel(LblEnd);
  386. }
  387. }
  388. }