AInstEmitSimdCmp.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 Fccmp_S(AILEmitterCtx Context)
  41. {
  42. AOpCodeSimdFcond Op = (AOpCodeSimdFcond)Context.CurrOp;
  43. AILLabel LblTrue = new AILLabel();
  44. AILLabel LblEnd = new AILLabel();
  45. Context.EmitCondBranch(LblTrue, Op.Cond);
  46. EmitSetNZCV(Context, Op.NZCV);
  47. Context.Emit(OpCodes.Br, LblEnd);
  48. Context.MarkLabel(LblTrue);
  49. Fcmp_S(Context);
  50. Context.MarkLabel(LblEnd);
  51. }
  52. public static void Fccmpe_S(AILEmitterCtx Context)
  53. {
  54. Fccmp_S(Context);
  55. }
  56. public static void Fcmp_S(AILEmitterCtx Context)
  57. {
  58. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  59. bool CmpWithZero = !(Op is AOpCodeSimdFcond) ? Op.Bit3 : false;
  60. //Handle NaN case. If any number is NaN, then NZCV = 0011.
  61. if (CmpWithZero)
  62. {
  63. EmitNaNCheck(Context, Op.Rn);
  64. }
  65. else
  66. {
  67. EmitNaNCheck(Context, Op.Rn);
  68. EmitNaNCheck(Context, Op.Rm);
  69. Context.Emit(OpCodes.Or);
  70. }
  71. AILLabel LblNaN = new AILLabel();
  72. AILLabel LblEnd = new AILLabel();
  73. Context.Emit(OpCodes.Brtrue_S, LblNaN);
  74. void EmitLoadOpers()
  75. {
  76. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  77. if (CmpWithZero)
  78. {
  79. EmitLdcImmF(Context, 0, Op.Size);
  80. }
  81. else
  82. {
  83. EmitVectorExtractF(Context, Op.Rm, 0, Op.Size);
  84. }
  85. }
  86. //Z = Rn == Rm
  87. EmitLoadOpers();
  88. Context.Emit(OpCodes.Ceq);
  89. Context.Emit(OpCodes.Dup);
  90. Context.EmitStflg((int)APState.ZBit);
  91. //C = Rn >= Rm
  92. EmitLoadOpers();
  93. Context.Emit(OpCodes.Cgt);
  94. Context.Emit(OpCodes.Or);
  95. Context.EmitStflg((int)APState.CBit);
  96. //N = Rn < Rm
  97. EmitLoadOpers();
  98. Context.Emit(OpCodes.Clt);
  99. Context.EmitStflg((int)APState.NBit);
  100. //V = 0
  101. Context.EmitLdc_I4(0);
  102. Context.EmitStflg((int)APState.VBit);
  103. Context.Emit(OpCodes.Br_S, LblEnd);
  104. Context.MarkLabel(LblNaN);
  105. EmitSetNZCV(Context, 0b0011);
  106. Context.MarkLabel(LblEnd);
  107. }
  108. public static void Fcmpe_S(AILEmitterCtx Context)
  109. {
  110. Fcmp_S(Context);
  111. }
  112. private static void EmitLdcImmF(AILEmitterCtx Context, double ImmF, int Size)
  113. {
  114. if (Size == 0)
  115. {
  116. Context.EmitLdc_R4((float)ImmF);
  117. }
  118. else if (Size == 1)
  119. {
  120. Context.EmitLdc_R8(ImmF);
  121. }
  122. else
  123. {
  124. throw new ArgumentOutOfRangeException(nameof(Size));
  125. }
  126. }
  127. private static void EmitNaNCheck(AILEmitterCtx Context, int Reg)
  128. {
  129. IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
  130. EmitVectorExtractF(Context, Reg, 0, Op.Size);
  131. if (Op.Size == 0)
  132. {
  133. Context.EmitCall(typeof(float), nameof(float.IsNaN));
  134. }
  135. else if (Op.Size == 1)
  136. {
  137. Context.EmitCall(typeof(double), nameof(double.IsNaN));
  138. }
  139. else
  140. {
  141. throw new InvalidOperationException();
  142. }
  143. }
  144. private static void EmitVectorCmp(AILEmitterCtx Context, OpCode ILOp)
  145. {
  146. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  147. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  148. ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
  149. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  150. {
  151. EmitVectorExtractSx(Context, Op.Rn, Index, Op.Size);
  152. if (Op is AOpCodeSimdReg BinOp)
  153. {
  154. EmitVectorExtractSx(Context, BinOp.Rm, Index, Op.Size);
  155. }
  156. else
  157. {
  158. Context.EmitLdc_I8(0);
  159. }
  160. AILLabel LblTrue = new AILLabel();
  161. AILLabel LblEnd = new AILLabel();
  162. Context.Emit(ILOp, LblTrue);
  163. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, 0);
  164. Context.Emit(OpCodes.Br_S, LblEnd);
  165. Context.MarkLabel(LblTrue);
  166. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, (long)SzMask);
  167. Context.MarkLabel(LblEnd);
  168. }
  169. if (Op.RegisterSize == ARegisterSize.SIMD64)
  170. {
  171. EmitVectorZeroUpper(Context, Op.Rd);
  172. }
  173. }
  174. }
  175. }