CpuTestSimdCmp.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using ChocolArm64.State;
  2. using NUnit.Framework;
  3. using System;
  4. using System.Runtime.Intrinsics;
  5. using System.Runtime.Intrinsics.X86;
  6. namespace Ryujinx.Tests.Cpu
  7. {
  8. public class CpuTestSimdCmp : CpuTest
  9. {
  10. #region "ValueSource"
  11. private static float[] _floats_()
  12. {
  13. return new float[] { float.NegativeInfinity, float.MinValue, -1f, -0f,
  14. +0f, +1f, float.MaxValue, float.PositiveInfinity };
  15. }
  16. private static double[] _doubles_()
  17. {
  18. return new double[] { double.NegativeInfinity, double.MinValue, -1d, -0d,
  19. +0d, +1d, double.MaxValue, double.PositiveInfinity };
  20. }
  21. #endregion
  22. [Test, Description("FCMEQ D0, D1, D2 | FCMGE D0, D1, D2 | FCMGT D0, D1, D2")]
  23. public void Fcmeq_Fcmge_Fcmgt_Reg_S_D([ValueSource("_doubles_")] [Random(8)] double A,
  24. [ValueSource("_doubles_")] [Random(8)] double B,
  25. [Values(0u, 1u, 3u)] uint EU) // EQ, GE, GT
  26. {
  27. uint Opcode = 0x5E62E420 | ((EU & 1) << 29) | ((EU >> 1) << 23);
  28. Vector128<float> V0 = Sse.StaticCast<double, float>(Sse2.SetAllVector128(TestContext.CurrentContext.Random.NextDouble()));
  29. Vector128<float> V1 = Sse.StaticCast<double, float>(Sse2.SetScalarVector128(A));
  30. Vector128<float> V2 = Sse.StaticCast<double, float>(Sse2.SetScalarVector128(B));
  31. AThreadState ThreadState = SingleOpcode(Opcode, V0: V0, V1: V1, V2: V2);
  32. byte[] Exp = default(byte[]);
  33. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  34. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  35. switch (EU)
  36. {
  37. case 0: Exp = (A == B ? Ones : Zeros); break;
  38. case 1: Exp = (A >= B ? Ones : Zeros); break;
  39. case 3: Exp = (A > B ? Ones : Zeros); break;
  40. }
  41. Assert.Multiple(() =>
  42. {
  43. Assert.That(BitConverter.GetBytes(VectorExtractDouble(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  44. Assert.That(VectorExtractDouble(ThreadState.V0, (byte)1), Is.Zero);
  45. });
  46. CompareAgainstUnicorn();
  47. }
  48. [Test, Description("FCMEQ S0, S1, S2 | FCMGE S0, S1, S2 | FCMGT S0, S1, S2")]
  49. public void Fcmeq_Fcmge_Fcmgt_Reg_S_S([ValueSource("_floats_")] [Random(8)] float A,
  50. [ValueSource("_floats_")] [Random(8)] float B,
  51. [Values(0u, 1u, 3u)] uint EU) // EQ, GE, GT
  52. {
  53. uint Opcode = 0x5E22E420 | ((EU & 1) << 29) | ((EU >> 1) << 23);
  54. Vector128<float> V0 = Sse.SetAllVector128(TestContext.CurrentContext.Random.NextFloat());
  55. Vector128<float> V1 = Sse.SetScalarVector128(A);
  56. Vector128<float> V2 = Sse.SetScalarVector128(B);
  57. AThreadState ThreadState = SingleOpcode(Opcode, V0: V0, V1: V1, V2: V2);
  58. byte[] Exp = default(byte[]);
  59. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF};
  60. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00};
  61. switch (EU)
  62. {
  63. case 0: Exp = (A == B ? Ones : Zeros); break;
  64. case 1: Exp = (A >= B ? Ones : Zeros); break;
  65. case 3: Exp = (A > B ? Ones : Zeros); break;
  66. }
  67. Assert.Multiple(() =>
  68. {
  69. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  70. Assert.That(Sse41.Extract(ThreadState.V0, (byte)1), Is.Zero);
  71. Assert.That(Sse41.Extract(ThreadState.V0, (byte)2), Is.Zero);
  72. Assert.That(Sse41.Extract(ThreadState.V0, (byte)3), Is.Zero);
  73. });
  74. CompareAgainstUnicorn();
  75. }
  76. [Test, Description("FCMEQ V0.2D, V1.2D, V2.2D | FCMGE V0.2D, V1.2D, V2.2D | FCMGT V0.2D, V1.2D, V2.2D")]
  77. public void Fcmeq_Fcmge_Fcmgt_Reg_V_2D([ValueSource("_doubles_")] [Random(8)] double A,
  78. [ValueSource("_doubles_")] [Random(8)] double B,
  79. [Values(0u, 1u, 3u)] uint EU) // EQ, GE, GT
  80. {
  81. uint Opcode = 0x4E62E420 | ((EU & 1) << 29) | ((EU >> 1) << 23);
  82. Vector128<float> V1 = Sse.StaticCast<double, float>(Sse2.SetAllVector128(A));
  83. Vector128<float> V2 = Sse.StaticCast<double, float>(Sse2.SetAllVector128(B));
  84. AThreadState ThreadState = SingleOpcode(Opcode, V1: V1, V2: V2);
  85. byte[] Exp = default(byte[]);
  86. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  87. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  88. switch (EU)
  89. {
  90. case 0: Exp = (A == B ? Ones : Zeros); break;
  91. case 1: Exp = (A >= B ? Ones : Zeros); break;
  92. case 3: Exp = (A > B ? Ones : Zeros); break;
  93. }
  94. Assert.Multiple(() =>
  95. {
  96. Assert.That(BitConverter.GetBytes(VectorExtractDouble(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  97. Assert.That(BitConverter.GetBytes(VectorExtractDouble(ThreadState.V0, (byte)1)), Is.EquivalentTo(Exp));
  98. });
  99. CompareAgainstUnicorn();
  100. }
  101. [Test, Description("FCMEQ V0.2S, V1.2S, V2.2S | FCMGE V0.2S, V1.2S, V2.2S | FCMGT V0.2S, V1.2S, V2.2S")]
  102. public void Fcmeq_Fcmge_Fcmgt_Reg_V_2S([ValueSource("_floats_")] [Random(8)] float A,
  103. [ValueSource("_floats_")] [Random(8)] float B,
  104. [Values(0u, 1u, 3u)] uint EU) // EQ, GE, GT
  105. {
  106. uint Opcode = 0x0E22E420 | ((EU & 1) << 29) | ((EU >> 1) << 23);
  107. Vector128<float> V0 = Sse.SetAllVector128(TestContext.CurrentContext.Random.NextFloat());
  108. Vector128<float> V1 = Sse.SetVector128(0, 0, A, A);
  109. Vector128<float> V2 = Sse.SetVector128(0, 0, B, B);
  110. AThreadState ThreadState = SingleOpcode(Opcode, V0: V0, V1: V1, V2: V2);
  111. byte[] Exp = default(byte[]);
  112. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF};
  113. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00};
  114. switch (EU)
  115. {
  116. case 0: Exp = (A == B ? Ones : Zeros); break;
  117. case 1: Exp = (A >= B ? Ones : Zeros); break;
  118. case 3: Exp = (A > B ? Ones : Zeros); break;
  119. }
  120. Assert.Multiple(() =>
  121. {
  122. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  123. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)1)), Is.EquivalentTo(Exp));
  124. Assert.That(Sse41.Extract(ThreadState.V0, (byte)2), Is.Zero);
  125. Assert.That(Sse41.Extract(ThreadState.V0, (byte)3), Is.Zero);
  126. });
  127. CompareAgainstUnicorn();
  128. }
  129. [Test, Description("FCMEQ V0.4S, V1.4S, V2.4S | FCMGE V0.4S, V1.4S, V2.4S | FCMGT V0.4S, V1.4S, V2.4S")]
  130. public void Fcmeq_Fcmge_Fcmgt_Reg_V_4S([ValueSource("_floats_")] [Random(8)] float A,
  131. [ValueSource("_floats_")] [Random(8)] float B,
  132. [Values(0u, 1u, 3u)] uint EU) // EQ, GE, GT
  133. {
  134. uint Opcode = 0x4E22E420 | ((EU & 1) << 29) | ((EU >> 1) << 23);
  135. Vector128<float> V1 = Sse.SetAllVector128(A);
  136. Vector128<float> V2 = Sse.SetAllVector128(B);
  137. AThreadState ThreadState = SingleOpcode(Opcode, V1: V1, V2: V2);
  138. byte[] Exp = default(byte[]);
  139. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF};
  140. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00};
  141. switch (EU)
  142. {
  143. case 0: Exp = (A == B ? Ones : Zeros); break;
  144. case 1: Exp = (A >= B ? Ones : Zeros); break;
  145. case 3: Exp = (A > B ? Ones : Zeros); break;
  146. }
  147. Assert.Multiple(() =>
  148. {
  149. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  150. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)1)), Is.EquivalentTo(Exp));
  151. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)2)), Is.EquivalentTo(Exp));
  152. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)3)), Is.EquivalentTo(Exp));
  153. });
  154. CompareAgainstUnicorn();
  155. }
  156. [Test, Description("FCMGT D0, D1, #0.0 | FCMGE D0, D1, #0.0 | FCMEQ D0, D1, #0.0 | FCMLE D0, D1, #0.0 | FCMLT D0, D1, #0.0")]
  157. public void Fcmgt_Fcmge_Fcmeq_Fcmle_Fcmlt_Zero_S_D([ValueSource("_doubles_")] [Random(8)] double A,
  158. [Values(0u, 1u, 2u, 3u)] uint opU, // GT, GE, EQ, LE
  159. [Values(0u, 1u)] uint bit13) // "LT"
  160. {
  161. uint Opcode = 0x5EE0C820 | (((opU & 1) & ~bit13) << 29) | (bit13 << 13) | (((opU >> 1) & ~bit13) << 12);
  162. Vector128<float> V0 = Sse.StaticCast<double, float>(Sse2.SetAllVector128(TestContext.CurrentContext.Random.NextDouble()));
  163. Vector128<float> V1 = Sse.StaticCast<double, float>(Sse2.SetScalarVector128(A));
  164. AThreadState ThreadState = SingleOpcode(Opcode, V0: V0, V1: V1);
  165. double Zero = +0d;
  166. byte[] Exp = default(byte[]);
  167. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  168. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  169. if (bit13 == 0)
  170. {
  171. switch (opU)
  172. {
  173. case 0: Exp = (A > Zero ? Ones : Zeros); break;
  174. case 1: Exp = (A >= Zero ? Ones : Zeros); break;
  175. case 2: Exp = (A == Zero ? Ones : Zeros); break;
  176. case 3: Exp = (Zero >= A ? Ones : Zeros); break;
  177. }
  178. }
  179. else
  180. {
  181. Exp = (Zero > A ? Ones : Zeros);
  182. }
  183. Assert.Multiple(() =>
  184. {
  185. Assert.That(BitConverter.GetBytes(VectorExtractDouble(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  186. Assert.That(VectorExtractDouble(ThreadState.V0, (byte)1), Is.Zero);
  187. });
  188. CompareAgainstUnicorn();
  189. }
  190. [Test, Description("FCMGT S0, S1, #0.0 | FCMGE S0, S1, #0.0 | FCMEQ S0, S1, #0.0 | FCMLE S0, S1, #0.0 | FCMLT S0, S1, #0.0")]
  191. public void Fcmgt_Fcmge_Fcmeq_Fcmle_Fcmlt_Zero_S_S([ValueSource("_floats_")] [Random(8)] float A,
  192. [Values(0u, 1u, 2u, 3u)] uint opU, // GT, GE, EQ, LE
  193. [Values(0u, 1u)] uint bit13) // "LT"
  194. {
  195. uint Opcode = 0x5EA0C820 | (((opU & 1) & ~bit13) << 29) | (bit13 << 13) | (((opU >> 1) & ~bit13) << 12);
  196. Vector128<float> V0 = Sse.SetAllVector128(TestContext.CurrentContext.Random.NextFloat());
  197. Vector128<float> V1 = Sse.SetScalarVector128(A);
  198. AThreadState ThreadState = SingleOpcode(Opcode, V0: V0, V1: V1);
  199. float Zero = +0f;
  200. byte[] Exp = default(byte[]);
  201. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF};
  202. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00};
  203. if (bit13 == 0)
  204. {
  205. switch (opU)
  206. {
  207. case 0: Exp = (A > Zero ? Ones : Zeros); break;
  208. case 1: Exp = (A >= Zero ? Ones : Zeros); break;
  209. case 2: Exp = (A == Zero ? Ones : Zeros); break;
  210. case 3: Exp = (Zero >= A ? Ones : Zeros); break;
  211. }
  212. }
  213. else
  214. {
  215. Exp = (Zero > A ? Ones : Zeros);
  216. }
  217. Assert.Multiple(() =>
  218. {
  219. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  220. Assert.That(Sse41.Extract(ThreadState.V0, (byte)1), Is.Zero);
  221. Assert.That(Sse41.Extract(ThreadState.V0, (byte)2), Is.Zero);
  222. Assert.That(Sse41.Extract(ThreadState.V0, (byte)3), Is.Zero);
  223. });
  224. CompareAgainstUnicorn();
  225. }
  226. [Test, Description("FCMGT V0.2D, V1.2D, #0.0 | FCMGE V0.2D, V1.2D, #0.0 | FCMEQ V0.2D, V1.2D, #0.0 | FCMLE V0.2D, V1.2D, #0.0 | FCMLT V0.2D, V1.2D, #0.0")]
  227. public void Fcmgt_Fcmge_Fcmeq_Fcmle_Fcmlt_Zero_V_2D([ValueSource("_doubles_")] [Random(8)] double A,
  228. [Values(0u, 1u, 2u, 3u)] uint opU, // GT, GE, EQ, LE
  229. [Values(0u, 1u)] uint bit13) // "LT"
  230. {
  231. uint Opcode = 0x4EE0C820 | (((opU & 1) & ~bit13) << 29) | (bit13 << 13) | (((opU >> 1) & ~bit13) << 12);
  232. Vector128<float> V1 = Sse.StaticCast<double, float>(Sse2.SetAllVector128(A));
  233. AThreadState ThreadState = SingleOpcode(Opcode, V1: V1);
  234. double Zero = +0d;
  235. byte[] Exp = default(byte[]);
  236. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  237. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  238. if (bit13 == 0)
  239. {
  240. switch (opU)
  241. {
  242. case 0: Exp = (A > Zero ? Ones : Zeros); break;
  243. case 1: Exp = (A >= Zero ? Ones : Zeros); break;
  244. case 2: Exp = (A == Zero ? Ones : Zeros); break;
  245. case 3: Exp = (Zero >= A ? Ones : Zeros); break;
  246. }
  247. }
  248. else
  249. {
  250. Exp = (Zero > A ? Ones : Zeros);
  251. }
  252. Assert.Multiple(() =>
  253. {
  254. Assert.That(BitConverter.GetBytes(VectorExtractDouble(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  255. Assert.That(BitConverter.GetBytes(VectorExtractDouble(ThreadState.V0, (byte)1)), Is.EquivalentTo(Exp));
  256. });
  257. CompareAgainstUnicorn();
  258. }
  259. [Test, Description("FCMGT V0.2S, V1.2S, #0.0 | FCMGE V0.2S, V1.2S, #0.0 | FCMEQ V0.2S, V1.2S, #0.0 | FCMLE V0.2S, V1.2S, #0.0 | FCMLT V0.2S, V1.2S, #0.0")]
  260. public void Fcmgt_Fcmge_Fcmeq_Fcmle_Fcmlt_Zero_V_2S([ValueSource("_floats_")] [Random(8)] float A,
  261. [Values(0u, 1u, 2u, 3u)] uint opU, // GT, GE, EQ, LE
  262. [Values(0u, 1u)] uint bit13) // "LT"
  263. {
  264. uint Opcode = 0x0EA0C820 | (((opU & 1) & ~bit13) << 29) | (bit13 << 13) | (((opU >> 1) & ~bit13) << 12);
  265. Vector128<float> V0 = Sse.SetAllVector128(TestContext.CurrentContext.Random.NextFloat());
  266. Vector128<float> V1 = Sse.SetVector128(0, 0, A, A);
  267. AThreadState ThreadState = SingleOpcode(Opcode, V0: V0, V1: V1);
  268. float Zero = +0f;
  269. byte[] Exp = default(byte[]);
  270. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF};
  271. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00};
  272. if (bit13 == 0)
  273. {
  274. switch (opU)
  275. {
  276. case 0: Exp = (A > Zero ? Ones : Zeros); break;
  277. case 1: Exp = (A >= Zero ? Ones : Zeros); break;
  278. case 2: Exp = (A == Zero ? Ones : Zeros); break;
  279. case 3: Exp = (Zero >= A ? Ones : Zeros); break;
  280. }
  281. }
  282. else
  283. {
  284. Exp = (Zero > A ? Ones : Zeros);
  285. }
  286. Assert.Multiple(() =>
  287. {
  288. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  289. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)1)), Is.EquivalentTo(Exp));
  290. Assert.That(Sse41.Extract(ThreadState.V0, (byte)2), Is.Zero);
  291. Assert.That(Sse41.Extract(ThreadState.V0, (byte)3), Is.Zero);
  292. });
  293. CompareAgainstUnicorn();
  294. }
  295. [Test, Description("FCMGT V0.4S, V1.4S, #0.0 | FCMGE V0.4S, V1.4S, #0.0 | FCMEQ V0.4S, V1.4S, #0.0 | FCMLE V0.4S, V1.4S, #0.0 | FCMLT V0.4S, V1.4S, #0.0")]
  296. public void Fcmgt_Fcmge_Fcmeq_Fcmle_Fcmlt_Zero_V_4S([ValueSource("_floats_")] [Random(8)] float A,
  297. [Values(0u, 1u, 2u, 3u)] uint opU, // GT, GE, EQ, LE
  298. [Values(0u, 1u)] uint bit13) // "LT"
  299. {
  300. uint Opcode = 0x4EA0C820 | (((opU & 1) & ~bit13) << 29) | (bit13 << 13) | (((opU >> 1) & ~bit13) << 12);
  301. Vector128<float> V1 = Sse.SetAllVector128(A);
  302. AThreadState ThreadState = SingleOpcode(Opcode, V1: V1);
  303. float Zero = +0f;
  304. byte[] Exp = default(byte[]);
  305. byte[] Ones = new byte[] {0xFF, 0xFF, 0xFF, 0xFF};
  306. byte[] Zeros = new byte[] {0x00, 0x00, 0x00, 0x00};
  307. if (bit13 == 0)
  308. {
  309. switch (opU)
  310. {
  311. case 0: Exp = (A > Zero ? Ones : Zeros); break;
  312. case 1: Exp = (A >= Zero ? Ones : Zeros); break;
  313. case 2: Exp = (A == Zero ? Ones : Zeros); break;
  314. case 3: Exp = (Zero >= A ? Ones : Zeros); break;
  315. }
  316. }
  317. else
  318. {
  319. Exp = (Zero > A ? Ones : Zeros);
  320. }
  321. Assert.Multiple(() =>
  322. {
  323. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)0)), Is.EquivalentTo(Exp));
  324. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)1)), Is.EquivalentTo(Exp));
  325. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)2)), Is.EquivalentTo(Exp));
  326. Assert.That(BitConverter.GetBytes(Sse41.Extract(ThreadState.V0, (byte)3)), Is.EquivalentTo(Exp));
  327. });
  328. CompareAgainstUnicorn();
  329. }
  330. }
  331. }