CpuTestSimdCmp.cs 18 KB

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