InstEmitSimdCmp32.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using static ARMeilleure.Instructions.InstEmitHelper;
  7. using static ARMeilleure.Instructions.InstEmitSimdHelper32;
  8. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  9. namespace ARMeilleure.Instructions
  10. {
  11. using Func2I = Func<Operand, Operand, Operand>;
  12. static partial class InstEmit32
  13. {
  14. public static void Vceq_V(ArmEmitterContext context)
  15. {
  16. EmitCmpOpF32(context, SoftFloat32.FPCompareEQFpscr, SoftFloat64.FPCompareEQFpscr, false);
  17. }
  18. public static void Vceq_I(ArmEmitterContext context)
  19. {
  20. EmitCmpOpI32(context, context.ICompareEqual, context.ICompareEqual, false, false);
  21. }
  22. public static void Vceq_Z(ArmEmitterContext context)
  23. {
  24. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  25. if (op.F)
  26. {
  27. EmitCmpOpF32(context, SoftFloat32.FPCompareEQFpscr, SoftFloat64.FPCompareEQFpscr, true);
  28. }
  29. else
  30. {
  31. EmitCmpOpI32(context, context.ICompareEqual, context.ICompareEqual, true, false);
  32. }
  33. }
  34. public static void Vcge_V(ArmEmitterContext context)
  35. {
  36. EmitCmpOpF32(context, SoftFloat32.FPCompareGEFpscr, SoftFloat64.FPCompareGEFpscr, false);
  37. }
  38. public static void Vcge_I(ArmEmitterContext context)
  39. {
  40. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  41. EmitCmpOpI32(context, context.ICompareGreaterOrEqual, context.ICompareGreaterOrEqualUI, false, !op.U);
  42. }
  43. public static void Vcge_Z(ArmEmitterContext context)
  44. {
  45. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  46. if (op.F)
  47. {
  48. EmitCmpOpF32(context, SoftFloat32.FPCompareGEFpscr, SoftFloat64.FPCompareGEFpscr, true);
  49. }
  50. else
  51. {
  52. EmitCmpOpI32(context, context.ICompareGreaterOrEqual, context.ICompareGreaterOrEqualUI, true, true);
  53. }
  54. }
  55. public static void Vcgt_V(ArmEmitterContext context)
  56. {
  57. EmitCmpOpF32(context, SoftFloat32.FPCompareGTFpscr, SoftFloat64.FPCompareGTFpscr, false);
  58. }
  59. public static void Vcgt_I(ArmEmitterContext context)
  60. {
  61. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  62. EmitCmpOpI32(context, context.ICompareGreater, context.ICompareGreaterUI, false, !op.U);
  63. }
  64. public static void Vcgt_Z(ArmEmitterContext context)
  65. {
  66. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  67. if (op.F)
  68. {
  69. EmitCmpOpF32(context, SoftFloat32.FPCompareGTFpscr, SoftFloat64.FPCompareGTFpscr, true);
  70. }
  71. else
  72. {
  73. EmitCmpOpI32(context, context.ICompareGreater, context.ICompareGreaterUI, true, true);
  74. }
  75. }
  76. public static void Vcle_Z(ArmEmitterContext context)
  77. {
  78. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  79. if (op.F)
  80. {
  81. EmitCmpOpF32(context, SoftFloat32.FPCompareLEFpscr, SoftFloat64.FPCompareLEFpscr, true);
  82. }
  83. else
  84. {
  85. EmitCmpOpI32(context, context.ICompareLessOrEqual, context.ICompareLessOrEqualUI, true, true);
  86. }
  87. }
  88. public static void Vclt_Z(ArmEmitterContext context)
  89. {
  90. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  91. if (op.F)
  92. {
  93. EmitCmpOpF32(context, SoftFloat32.FPCompareLTFpscr, SoftFloat64.FPCompareLTFpscr, true);
  94. }
  95. else
  96. {
  97. EmitCmpOpI32(context, context.ICompareLess, context.ICompareLessUI, true, true);
  98. }
  99. }
  100. private static void EmitCmpOpF32(
  101. ArmEmitterContext context,
  102. _F32_F32_F32_Bool f32,
  103. _F64_F64_F64_Bool f64,
  104. bool zero)
  105. {
  106. Operand one = Const(1);
  107. if (zero)
  108. {
  109. EmitVectorUnaryOpF32(context, (m) =>
  110. {
  111. OperandType type = m.Type;
  112. if (type == OperandType.FP64)
  113. {
  114. return context.Call(f64, m, ConstF(0.0), one);
  115. }
  116. else
  117. {
  118. return context.Call(f32, m, ConstF(0.0f), one);
  119. }
  120. });
  121. }
  122. else
  123. {
  124. EmitVectorBinaryOpF32(context, (n, m) =>
  125. {
  126. OperandType type = n.Type;
  127. if (type == OperandType.FP64)
  128. {
  129. return context.Call(f64, n, m, one);
  130. }
  131. else
  132. {
  133. return context.Call(f32, n, m, one);
  134. }
  135. });
  136. }
  137. }
  138. private static Operand ZerosOrOnes(ArmEmitterContext context, Operand fromBool, OperandType baseType)
  139. {
  140. var ones = (baseType == OperandType.I64) ? Const(-1L) : Const(-1);
  141. return context.ConditionalSelect(fromBool, ones, Const(baseType, 0L));
  142. }
  143. private static void EmitCmpOpI32(
  144. ArmEmitterContext context,
  145. Func2I signedOp,
  146. Func2I unsignedOp,
  147. bool zero,
  148. bool signed)
  149. {
  150. if (zero)
  151. {
  152. if (signed)
  153. {
  154. EmitVectorUnaryOpSx32(context, (m) =>
  155. {
  156. OperandType type = m.Type;
  157. Operand zeroV = (type == OperandType.I64) ? Const(0L) : Const(0);
  158. return ZerosOrOnes(context, signedOp(m, zeroV), type);
  159. });
  160. }
  161. else
  162. {
  163. EmitVectorUnaryOpZx32(context, (m) =>
  164. {
  165. OperandType type = m.Type;
  166. Operand zeroV = (type == OperandType.I64) ? Const(0L) : Const(0);
  167. return ZerosOrOnes(context, unsignedOp(m, zeroV), type);
  168. });
  169. }
  170. }
  171. else
  172. {
  173. if (signed)
  174. {
  175. EmitVectorBinaryOpSx32(context, (n, m) => ZerosOrOnes(context, signedOp(n, m), n.Type));
  176. }
  177. else
  178. {
  179. EmitVectorBinaryOpZx32(context, (n, m) => ZerosOrOnes(context, unsignedOp(n, m), n.Type));
  180. }
  181. }
  182. }
  183. public static void Vcmp(ArmEmitterContext context)
  184. {
  185. EmitVcmpOrVcmpe(context, false);
  186. }
  187. public static void Vcmpe(ArmEmitterContext context)
  188. {
  189. EmitVcmpOrVcmpe(context, true);
  190. }
  191. private static void EmitVcmpOrVcmpe(ArmEmitterContext context, bool signalNaNs)
  192. {
  193. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  194. bool cmpWithZero = (op.Opc & 2) != 0;
  195. {
  196. int fSize = op.Size & 1;
  197. OperandType type = fSize != 0 ? OperandType.FP64 : OperandType.FP32;
  198. Operand ne = ExtractScalar(context, type, op.Vd);
  199. Operand me;
  200. if (cmpWithZero)
  201. {
  202. me = fSize == 0 ? ConstF(0f) : ConstF(0d);
  203. }
  204. else
  205. {
  206. me = ExtractScalar(context, type, op.Vm);
  207. }
  208. Delegate dlg = fSize != 0
  209. ? (Delegate)new _S32_F64_F64_Bool(SoftFloat64.FPCompare)
  210. : (Delegate)new _S32_F32_F32_Bool(SoftFloat32.FPCompare);
  211. Operand nzcv = context.Call(dlg, ne, me, Const(signalNaNs));
  212. EmitSetFPSCRFlags(context, nzcv);
  213. }
  214. }
  215. private static void EmitSetFPSCRFlags(ArmEmitterContext context, Operand nzcv)
  216. {
  217. Operand Extract(Operand value, int bit)
  218. {
  219. if (bit != 0)
  220. {
  221. value = context.ShiftRightUI(value, Const(bit));
  222. }
  223. value = context.BitwiseAnd(value, Const(1));
  224. return value;
  225. }
  226. SetFpFlag(context, FPState.VFlag, Extract(nzcv, 0));
  227. SetFpFlag(context, FPState.CFlag, Extract(nzcv, 1));
  228. SetFpFlag(context, FPState.ZFlag, Extract(nzcv, 2));
  229. SetFpFlag(context, FPState.NFlag, Extract(nzcv, 3));
  230. }
  231. }
  232. }