AInstEmitSimdCmp.cs 10 KB

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