AInstEmitSimdCmp.cs 12 KB

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