AInstEmitSimdCmp.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.AInstEmitSimdHelper;
  7. namespace ChocolArm64.Instruction
  8. {
  9. static partial class AInstEmit
  10. {
  11. public static void Cmeq_V(AILEmitterCtx Context)
  12. {
  13. EmitVectorCmp(Context, OpCodes.Beq_S);
  14. }
  15. public static void Cmge_V(AILEmitterCtx Context)
  16. {
  17. EmitVectorCmp(Context, OpCodes.Bge_S);
  18. }
  19. public static void Cmgt_V(AILEmitterCtx Context)
  20. {
  21. EmitVectorCmp(Context, OpCodes.Bgt_S);
  22. }
  23. public static void Cmhi_V(AILEmitterCtx Context)
  24. {
  25. EmitVectorCmp(Context, OpCodes.Bgt_Un_S);
  26. }
  27. public static void Cmhs_V(AILEmitterCtx Context)
  28. {
  29. EmitVectorCmp(Context, OpCodes.Bge_Un_S);
  30. }
  31. public static void Cmle_V(AILEmitterCtx Context)
  32. {
  33. EmitVectorCmp(Context, OpCodes.Ble_S);
  34. }
  35. public static void Cmlt_V(AILEmitterCtx Context)
  36. {
  37. EmitVectorCmp(Context, OpCodes.Blt_S);
  38. }
  39. public static void Fccmp_S(AILEmitterCtx Context)
  40. {
  41. AOpCodeSimdFcond Op = (AOpCodeSimdFcond)Context.CurrOp;
  42. AILLabel LblTrue = new AILLabel();
  43. AILLabel LblEnd = new AILLabel();
  44. Context.EmitCondBranch(LblTrue, Op.Cond);
  45. //TODO: Share this logic with Ccmp.
  46. Context.EmitLdc_I4((Op.NZCV >> 0) & 1);
  47. Context.EmitStflg((int)APState.VBit);
  48. Context.EmitLdc_I4((Op.NZCV >> 1) & 1);
  49. Context.EmitStflg((int)APState.CBit);
  50. Context.EmitLdc_I4((Op.NZCV >> 2) & 1);
  51. Context.EmitStflg((int)APState.ZBit);
  52. Context.EmitLdc_I4((Op.NZCV >> 3) & 1);
  53. Context.EmitStflg((int)APState.NBit);
  54. Context.Emit(OpCodes.Br_S, LblEnd);
  55. Context.MarkLabel(LblTrue);
  56. Fcmp_S(Context);
  57. Context.MarkLabel(LblEnd);
  58. }
  59. public static void Fcmp_S(AILEmitterCtx Context)
  60. {
  61. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  62. bool CmpWithZero = !(Op is AOpCodeSimdFcond) ? Op.Bit3 : false;
  63. void EmitLoadOpers()
  64. {
  65. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  66. if (CmpWithZero)
  67. {
  68. EmitLdcImmF(Context, 0, Op.Size);
  69. }
  70. else
  71. {
  72. EmitVectorExtractF(Context, Op.Rm, 0, Op.Size);
  73. }
  74. }
  75. //Z = Rn == Rm
  76. EmitLoadOpers();
  77. Context.Emit(OpCodes.Ceq);
  78. Context.Emit(OpCodes.Dup);
  79. Context.EmitStflg((int)APState.ZBit);
  80. //C = Rn >= Rm
  81. EmitLoadOpers();
  82. Context.Emit(OpCodes.Cgt);
  83. Context.Emit(OpCodes.Or);
  84. Context.EmitStflg((int)APState.CBit);
  85. //N = Rn < Rm
  86. EmitLoadOpers();
  87. Context.Emit(OpCodes.Clt);
  88. Context.EmitStflg((int)APState.NBit);
  89. //Handle NaN case. If any number is NaN, then NZCV = 0011.
  90. AILLabel LblNotNaN = new AILLabel();
  91. if (CmpWithZero)
  92. {
  93. EmitNaNCheck(Context, Op.Rn);
  94. }
  95. else
  96. {
  97. EmitNaNCheck(Context, Op.Rn);
  98. EmitNaNCheck(Context, Op.Rm);
  99. Context.Emit(OpCodes.Or);
  100. }
  101. Context.Emit(OpCodes.Brfalse_S, LblNotNaN);
  102. Context.EmitLdc_I4(1);
  103. Context.EmitLdc_I4(1);
  104. Context.EmitStflg((int)APState.CBit);
  105. Context.EmitStflg((int)APState.VBit);
  106. Context.MarkLabel(LblNotNaN);
  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. }