AInstEmitSimdCmp.cs 15 KB

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