AInstEmitSimdCmp.cs 12 KB

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