AInstEmitSimdCmp.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 Fcmp_S(AILEmitterCtx Context)
  82. {
  83. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  84. bool CmpWithZero = !(Op is AOpCodeSimdFcond) ? Op.Bit3 : false;
  85. //Handle NaN case. If any number is NaN, then NZCV = 0011.
  86. if (CmpWithZero)
  87. {
  88. EmitNaNCheck(Context, Op.Rn);
  89. }
  90. else
  91. {
  92. EmitNaNCheck(Context, Op.Rn);
  93. EmitNaNCheck(Context, Op.Rm);
  94. Context.Emit(OpCodes.Or);
  95. }
  96. AILLabel LblNaN = new AILLabel();
  97. AILLabel LblEnd = new AILLabel();
  98. Context.Emit(OpCodes.Brtrue_S, LblNaN);
  99. void EmitLoadOpers()
  100. {
  101. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  102. if (CmpWithZero)
  103. {
  104. EmitLdcImmF(Context, 0, Op.Size);
  105. }
  106. else
  107. {
  108. EmitVectorExtractF(Context, Op.Rm, 0, Op.Size);
  109. }
  110. }
  111. //Z = Rn == Rm
  112. EmitLoadOpers();
  113. Context.Emit(OpCodes.Ceq);
  114. Context.Emit(OpCodes.Dup);
  115. Context.EmitStflg((int)APState.ZBit);
  116. //C = Rn >= Rm
  117. EmitLoadOpers();
  118. Context.Emit(OpCodes.Cgt);
  119. Context.Emit(OpCodes.Or);
  120. Context.EmitStflg((int)APState.CBit);
  121. //N = Rn < Rm
  122. EmitLoadOpers();
  123. Context.Emit(OpCodes.Clt);
  124. Context.EmitStflg((int)APState.NBit);
  125. //V = 0
  126. Context.EmitLdc_I4(0);
  127. Context.EmitStflg((int)APState.VBit);
  128. Context.Emit(OpCodes.Br_S, LblEnd);
  129. Context.MarkLabel(LblNaN);
  130. EmitSetNZCV(Context, 0b0011);
  131. Context.MarkLabel(LblEnd);
  132. }
  133. public static void Fcmpe_S(AILEmitterCtx Context)
  134. {
  135. Fcmp_S(Context);
  136. }
  137. private static void EmitLdcImmF(AILEmitterCtx Context, double ImmF, int Size)
  138. {
  139. if (Size == 0)
  140. {
  141. Context.EmitLdc_R4((float)ImmF);
  142. }
  143. else if (Size == 1)
  144. {
  145. Context.EmitLdc_R8(ImmF);
  146. }
  147. else
  148. {
  149. throw new ArgumentOutOfRangeException(nameof(Size));
  150. }
  151. }
  152. private static void EmitNaNCheck(AILEmitterCtx Context, int Reg)
  153. {
  154. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  155. EmitVectorExtractF(Context, Reg, 0, Op.Size);
  156. if (Op.Size == 0)
  157. {
  158. Context.EmitCall(typeof(float), nameof(float.IsNaN));
  159. }
  160. else if (Op.Size == 1)
  161. {
  162. Context.EmitCall(typeof(double), nameof(double.IsNaN));
  163. }
  164. else
  165. {
  166. throw new InvalidOperationException();
  167. }
  168. }
  169. private static void EmitVectorCmp(AILEmitterCtx Context, OpCode ILOp)
  170. {
  171. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  172. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  173. ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
  174. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  175. {
  176. EmitVectorExtractSx(Context, Op.Rn, Index, Op.Size);
  177. if (Op is AOpCodeSimdReg BinOp)
  178. {
  179. EmitVectorExtractSx(Context, BinOp.Rm, Index, Op.Size);
  180. }
  181. else
  182. {
  183. Context.EmitLdc_I8(0);
  184. }
  185. AILLabel LblTrue = new AILLabel();
  186. AILLabel LblEnd = new AILLabel();
  187. Context.Emit(ILOp, LblTrue);
  188. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, 0);
  189. Context.Emit(OpCodes.Br_S, LblEnd);
  190. Context.MarkLabel(LblTrue);
  191. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, (long)SzMask);
  192. Context.MarkLabel(LblEnd);
  193. }
  194. if (Op.RegisterSize == ARegisterSize.SIMD64)
  195. {
  196. EmitVectorZeroUpper(Context, Op.Rd);
  197. }
  198. }
  199. }
  200. }