AInstEmitSimdCmp.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 Fcmhi_S(AILEmitterCtx Context)
  106. {
  107. EmitScalarFcmp(Context, OpCodes.Bgt_Un_S);
  108. }
  109. public static void Fcmhi_V(AILEmitterCtx Context)
  110. {
  111. EmitVectorFcmp(Context, OpCodes.Bgt_Un_S);
  112. }
  113. public static void Fcmhs_S(AILEmitterCtx Context)
  114. {
  115. EmitScalarFcmp(Context, OpCodes.Bge_Un_S);
  116. }
  117. public static void Fcmhs_V(AILEmitterCtx Context)
  118. {
  119. EmitVectorFcmp(Context, OpCodes.Bge_Un_S);
  120. }
  121. public static void Fcmle_S(AILEmitterCtx Context)
  122. {
  123. EmitScalarFcmp(Context, OpCodes.Ble_S);
  124. }
  125. public static void Fcmle_V(AILEmitterCtx Context)
  126. {
  127. EmitVectorFcmp(Context, OpCodes.Ble_S);
  128. }
  129. public static void Fcmlt_S(AILEmitterCtx Context)
  130. {
  131. EmitScalarFcmp(Context, OpCodes.Blt_S);
  132. }
  133. public static void Fcmlt_V(AILEmitterCtx Context)
  134. {
  135. EmitVectorFcmp(Context, OpCodes.Blt_S);
  136. }
  137. public static void Fcmp_S(AILEmitterCtx Context)
  138. {
  139. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  140. bool CmpWithZero = !(Op is AOpCodeSimdFcond) ? Op.Bit3 : false;
  141. //Handle NaN case.
  142. //If any number is NaN, then NZCV = 0011.
  143. if (CmpWithZero)
  144. {
  145. EmitNaNCheck(Context, Op.Rn);
  146. }
  147. else
  148. {
  149. EmitNaNCheck(Context, Op.Rn);
  150. EmitNaNCheck(Context, Op.Rm);
  151. Context.Emit(OpCodes.Or);
  152. }
  153. AILLabel LblNaN = new AILLabel();
  154. AILLabel LblEnd = new AILLabel();
  155. Context.Emit(OpCodes.Brtrue_S, LblNaN);
  156. void EmitLoadOpers()
  157. {
  158. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  159. if (CmpWithZero)
  160. {
  161. if (Op.Size == 0)
  162. {
  163. Context.EmitLdc_R4(0);
  164. }
  165. else /* if (SizeF == 1) */
  166. {
  167. Context.EmitLdc_R8(0);
  168. }
  169. }
  170. else
  171. {
  172. EmitVectorExtractF(Context, Op.Rm, 0, Op.Size);
  173. }
  174. }
  175. //Z = Rn == Rm
  176. EmitLoadOpers();
  177. Context.Emit(OpCodes.Ceq);
  178. Context.Emit(OpCodes.Dup);
  179. Context.EmitStflg((int)APState.ZBit);
  180. //C = Rn >= Rm
  181. EmitLoadOpers();
  182. Context.Emit(OpCodes.Cgt);
  183. Context.Emit(OpCodes.Or);
  184. Context.EmitStflg((int)APState.CBit);
  185. //N = Rn < Rm
  186. EmitLoadOpers();
  187. Context.Emit(OpCodes.Clt);
  188. Context.EmitStflg((int)APState.NBit);
  189. //V = 0
  190. Context.EmitLdc_I4(0);
  191. Context.EmitStflg((int)APState.VBit);
  192. Context.Emit(OpCodes.Br_S, LblEnd);
  193. Context.MarkLabel(LblNaN);
  194. EmitSetNZCV(Context, 0b0011);
  195. Context.MarkLabel(LblEnd);
  196. }
  197. public static void Fcmpe_S(AILEmitterCtx Context)
  198. {
  199. Fcmp_S(Context);
  200. }
  201. private static void EmitNaNCheck(AILEmitterCtx Context, int Reg)
  202. {
  203. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  204. EmitVectorExtractF(Context, Reg, 0, Op.Size);
  205. if (Op.Size == 0)
  206. {
  207. Context.EmitCall(typeof(float), nameof(float.IsNaN));
  208. }
  209. else if (Op.Size == 1)
  210. {
  211. Context.EmitCall(typeof(double), nameof(double.IsNaN));
  212. }
  213. else
  214. {
  215. throw new InvalidOperationException();
  216. }
  217. }
  218. private static void EmitVectorCmp(AILEmitterCtx Context, OpCode ILOp)
  219. {
  220. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  221. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  222. ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
  223. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  224. {
  225. EmitVectorExtractSx(Context, Op.Rn, Index, Op.Size);
  226. if (Op is AOpCodeSimdReg BinOp)
  227. {
  228. EmitVectorExtractSx(Context, BinOp.Rm, Index, Op.Size);
  229. }
  230. else
  231. {
  232. Context.EmitLdc_I8(0);
  233. }
  234. AILLabel LblTrue = new AILLabel();
  235. AILLabel LblEnd = new AILLabel();
  236. Context.Emit(ILOp, LblTrue);
  237. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, 0);
  238. Context.Emit(OpCodes.Br_S, LblEnd);
  239. Context.MarkLabel(LblTrue);
  240. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, (long)SzMask);
  241. Context.MarkLabel(LblEnd);
  242. }
  243. if (Op.RegisterSize == ARegisterSize.SIMD64)
  244. {
  245. EmitVectorZeroUpper(Context, Op.Rd);
  246. }
  247. }
  248. private static void EmitScalarFcmp(AILEmitterCtx Context, OpCode ILOp)
  249. {
  250. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  251. int SizeF = Op.Size & 1;
  252. EmitFcmp(Context, ILOp, 0);
  253. EmitScalarSetF(Context, Op.Rd, SizeF);
  254. }
  255. private static void EmitVectorFcmp(AILEmitterCtx Context, OpCode ILOp)
  256. {
  257. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  258. int SizeF = Op.Size & 1;
  259. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  260. for (int Index = 0; Index < Bytes >> SizeF + 2; Index++)
  261. {
  262. EmitFcmp(Context, ILOp, Index);
  263. }
  264. if (Op.RegisterSize == ARegisterSize.SIMD64)
  265. {
  266. EmitVectorZeroUpper(Context, Op.Rd);
  267. }
  268. }
  269. private static void EmitFcmp(AILEmitterCtx Context, OpCode ILOp, int Index)
  270. {
  271. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  272. int SizeF = Op.Size & 1;
  273. ulong SzMask = ulong.MaxValue >> (64 - (32 << SizeF));
  274. EmitVectorExtractF(Context, Op.Rn, Index, SizeF);
  275. if (Op is AOpCodeSimdReg BinOp)
  276. {
  277. EmitVectorExtractF(Context, BinOp.Rm, Index, SizeF);
  278. }
  279. else if (SizeF == 0)
  280. {
  281. Context.EmitLdc_R4(0);
  282. }
  283. else /* if (SizeF == 1) */
  284. {
  285. Context.EmitLdc_R8(0);
  286. }
  287. AILLabel LblTrue = new AILLabel();
  288. AILLabel LblEnd = new AILLabel();
  289. Context.Emit(ILOp, LblTrue);
  290. EmitVectorInsert(Context, Op.Rd, Index, SizeF + 2, 0);
  291. Context.Emit(OpCodes.Br_S, LblEnd);
  292. Context.MarkLabel(LblTrue);
  293. EmitVectorInsert(Context, Op.Rd, Index, SizeF + 2, (long)SzMask);
  294. Context.MarkLabel(LblEnd);
  295. }
  296. }
  297. }