CpuTestMisc.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #define Misc
  2. using ARMeilleure.State;
  3. using NUnit.Framework;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace Ryujinx.Tests.Cpu
  7. {
  8. [Category("Misc")]
  9. public sealed class CpuTestMisc : CpuTest
  10. {
  11. #if Misc
  12. #region "ValueSource (Types)"
  13. private static IEnumerable<ulong> _1S_F_()
  14. {
  15. yield return 0x00000000FF7FFFFFul; // -Max Normal (float.MinValue)
  16. yield return 0x0000000080800000ul; // -Min Normal
  17. yield return 0x00000000807FFFFFul; // -Max Subnormal
  18. yield return 0x0000000080000001ul; // -Min Subnormal (-float.Epsilon)
  19. yield return 0x000000007F7FFFFFul; // +Max Normal (float.MaxValue)
  20. yield return 0x0000000000800000ul; // +Min Normal
  21. yield return 0x00000000007FFFFFul; // +Max Subnormal
  22. yield return 0x0000000000000001ul; // +Min Subnormal (float.Epsilon)
  23. if (!NoZeros)
  24. {
  25. yield return 0x0000000080000000ul; // -Zero
  26. yield return 0x0000000000000000ul; // +Zero
  27. }
  28. if (!NoInfs)
  29. {
  30. yield return 0x00000000FF800000ul; // -Infinity
  31. yield return 0x000000007F800000ul; // +Infinity
  32. }
  33. if (!NoNaNs)
  34. {
  35. yield return 0x00000000FFC00000ul; // -QNaN (all zeros payload) (float.NaN)
  36. yield return 0x00000000FFBFFFFFul; // -SNaN (all ones payload)
  37. yield return 0x000000007FC00000ul; // +QNaN (all zeros payload) (-float.NaN) (DefaultNaN)
  38. yield return 0x000000007FBFFFFFul; // +SNaN (all ones payload)
  39. }
  40. for (int cnt = 1; cnt <= RndCnt; cnt++)
  41. {
  42. ulong grbg = TestContext.CurrentContext.Random.NextUInt();
  43. ulong rnd1 = GenNormalS();
  44. ulong rnd2 = GenSubnormalS();
  45. yield return (grbg << 32) | rnd1;
  46. yield return (grbg << 32) | rnd2;
  47. }
  48. }
  49. #endregion
  50. private const int RndCnt = 2;
  51. private const int RndCntImm = 2;
  52. private static readonly bool NoZeros = false;
  53. private static readonly bool NoInfs = false;
  54. private static readonly bool NoNaNs = false;
  55. #region "AluImm & Csel"
  56. [Test, Pairwise]
  57. public void Adds_Csinc_64bit([Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
  58. 0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] [Random(RndCnt)] ulong xn,
  59. [Values(0u, 4095u)] [Random(0u, 4095u, RndCntImm)] uint imm,
  60. [Values(0b00u, 0b01u)] uint shift, // <LSL #0, LSL #12>
  61. [Values(0b0000u, 0b0001u, 0b0010u, 0b0011u, // <EQ, NE, CS/HS, CC/LO,
  62. 0b0100u, 0b0101u, 0b0110u, 0b0111u, // MI, PL, VS, VC,
  63. 0b1000u, 0b1001u, 0b1010u, 0b1011u, // HI, LS, GE, LT,
  64. 0b1100u, 0b1101u)] uint cond) // GT, LE>
  65. {
  66. uint opCmn = 0xB100001F; // ADDS X31, X0, #0, LSL #0 -> CMN X0, #0, LSL #0
  67. uint opCset = 0x9A9F07E0; // CSINC X0, X31, X31, EQ -> CSET X0, NE
  68. opCmn |= ((shift & 3) << 22) | ((imm & 4095) << 10);
  69. opCset |= ((cond & 15) << 12);
  70. SetContext(x0: xn);
  71. Opcode(opCmn);
  72. Opcode(opCset);
  73. Opcode(0xD65F03C0); // RET
  74. ExecuteOpcodes();
  75. CompareAgainstUnicorn();
  76. }
  77. [Test, Pairwise]
  78. public void Adds_Csinc_32bit([Values(0x00000000u, 0x7FFFFFFFu,
  79. 0x80000000u, 0xFFFFFFFFu)] [Random(RndCnt)] uint wn,
  80. [Values(0u, 4095u)] [Random(0u, 4095u, RndCntImm)] uint imm,
  81. [Values(0b00u, 0b01u)] uint shift, // <LSL #0, LSL #12>
  82. [Values(0b0000u, 0b0001u, 0b0010u, 0b0011u, // <EQ, NE, CS/HS, CC/LO,
  83. 0b0100u, 0b0101u, 0b0110u, 0b0111u, // MI, PL, VS, VC,
  84. 0b1000u, 0b1001u, 0b1010u, 0b1011u, // HI, LS, GE, LT,
  85. 0b1100u, 0b1101u)] uint cond) // GT, LE>
  86. {
  87. uint opCmn = 0x3100001F; // ADDS W31, W0, #0, LSL #0 -> CMN W0, #0, LSL #0
  88. uint opCset = 0x1A9F07E0; // CSINC W0, W31, W31, EQ -> CSET W0, NE
  89. opCmn |= ((shift & 3) << 22) | ((imm & 4095) << 10);
  90. opCset |= ((cond & 15) << 12);
  91. SetContext(x0: wn);
  92. Opcode(opCmn);
  93. Opcode(opCset);
  94. Opcode(0xD65F03C0); // RET
  95. ExecuteOpcodes();
  96. CompareAgainstUnicorn();
  97. }
  98. [Test, Pairwise]
  99. public void Subs_Csinc_64bit([Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
  100. 0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] [Random(RndCnt)] ulong xn,
  101. [Values(0u, 4095u)] [Random(0u, 4095u, RndCntImm)] uint imm,
  102. [Values(0b00u, 0b01u)] uint shift, // <LSL #0, LSL #12>
  103. [Values(0b0000u, 0b0001u, 0b0010u, 0b0011u, // <EQ, NE, CS/HS, CC/LO,
  104. 0b0100u, 0b0101u, 0b0110u, 0b0111u, // MI, PL, VS, VC,
  105. 0b1000u, 0b1001u, 0b1010u, 0b1011u, // HI, LS, GE, LT,
  106. 0b1100u, 0b1101u)] uint cond) // GT, LE>
  107. {
  108. uint opCmp = 0xF100001F; // SUBS X31, X0, #0, LSL #0 -> CMP X0, #0, LSL #0
  109. uint opCset = 0x9A9F07E0; // CSINC X0, X31, X31, EQ -> CSET X0, NE
  110. opCmp |= ((shift & 3) << 22) | ((imm & 4095) << 10);
  111. opCset |= ((cond & 15) << 12);
  112. SetContext(x0: xn);
  113. Opcode(opCmp);
  114. Opcode(opCset);
  115. Opcode(0xD65F03C0); // RET
  116. ExecuteOpcodes();
  117. CompareAgainstUnicorn();
  118. }
  119. [Test, Pairwise]
  120. public void Subs_Csinc_32bit([Values(0x00000000u, 0x7FFFFFFFu,
  121. 0x80000000u, 0xFFFFFFFFu)] [Random(RndCnt)] uint wn,
  122. [Values(0u, 4095u)] [Random(0u, 4095u, RndCntImm)] uint imm,
  123. [Values(0b00u, 0b01u)] uint shift, // <LSL #0, LSL #12>
  124. [Values(0b0000u, 0b0001u, 0b0010u, 0b0011u, // <EQ, NE, CS/HS, CC/LO,
  125. 0b0100u, 0b0101u, 0b0110u, 0b0111u, // MI, PL, VS, VC,
  126. 0b1000u, 0b1001u, 0b1010u, 0b1011u, // HI, LS, GE, LT,
  127. 0b1100u, 0b1101u)] uint cond) // GT, LE>
  128. {
  129. uint opCmp = 0x7100001F; // SUBS W31, W0, #0, LSL #0 -> CMP W0, #0, LSL #0
  130. uint opCset = 0x1A9F07E0; // CSINC W0, W31, W31, EQ -> CSET W0, NE
  131. opCmp |= ((shift & 3) << 22) | ((imm & 4095) << 10);
  132. opCset |= ((cond & 15) << 12);
  133. SetContext(x0: wn);
  134. Opcode(opCmp);
  135. Opcode(opCset);
  136. Opcode(0xD65F03C0); // RET
  137. ExecuteOpcodes();
  138. CompareAgainstUnicorn();
  139. }
  140. #endregion
  141. [Explicit]
  142. [TestCase(0xFFFFFFFDu)] // Roots.
  143. [TestCase(0x00000005u)]
  144. public void Misc1(uint a)
  145. {
  146. // ((a + 3) * (a - 5)) / ((a + 5) * (a - 3)) = 0
  147. /*
  148. ADD W2, W0, 3
  149. SUB W1, W0, #5
  150. MUL W2, W2, W1
  151. ADD W1, W0, 5
  152. SUB W0, W0, #3
  153. MUL W0, W1, W0
  154. SDIV W0, W2, W0
  155. RET
  156. */
  157. SetContext(x0: a);
  158. Opcode(0x11000C02);
  159. Opcode(0x51001401);
  160. Opcode(0x1B017C42);
  161. Opcode(0x11001401);
  162. Opcode(0x51000C00);
  163. Opcode(0x1B007C20);
  164. Opcode(0x1AC00C40);
  165. Opcode(0xD65F03C0);
  166. ExecuteOpcodes();
  167. Assert.That(GetContext().GetX(0), Is.Zero);
  168. }
  169. [Explicit]
  170. [TestCase(-20f, -5f)] // 18 integer solutions.
  171. [TestCase(-12f, -6f)]
  172. [TestCase(-12f, 3f)]
  173. [TestCase( -8f, -8f)]
  174. [TestCase( -6f, -12f)]
  175. [TestCase( -5f, -20f)]
  176. [TestCase( -4f, 2f)]
  177. [TestCase( -3f, 12f)]
  178. [TestCase( -2f, 4f)]
  179. [TestCase( 2f, -4f)]
  180. [TestCase( 3f, -12f)]
  181. [TestCase( 4f, -2f)]
  182. [TestCase( 5f, 20f)]
  183. [TestCase( 6f, 12f)]
  184. [TestCase( 8f, 8f)]
  185. [TestCase( 12f, -3f)]
  186. [TestCase( 12f, 6f)]
  187. [TestCase( 20f, 5f)]
  188. public void Misc2(float a, float b)
  189. {
  190. // 1 / ((1 / a + 1 / b) ^ 2) = 16
  191. /*
  192. FMOV S2, 1.0e+0
  193. FDIV S0, S2, S0
  194. FDIV S1, S2, S1
  195. FADD S0, S0, S1
  196. FDIV S0, S2, S0
  197. FMUL S0, S0, S0
  198. RET
  199. */
  200. SetContext(v0: MakeVectorScalar(a), v1: MakeVectorScalar(b));
  201. Opcode(0x1E2E1002);
  202. Opcode(0x1E201840);
  203. Opcode(0x1E211841);
  204. Opcode(0x1E212800);
  205. Opcode(0x1E201840);
  206. Opcode(0x1E200800);
  207. Opcode(0xD65F03C0);
  208. ExecuteOpcodes();
  209. Assert.That(GetContext().GetV(0).As<float>(), Is.EqualTo(16f));
  210. }
  211. [Explicit]
  212. [TestCase(-20d, -5d)] // 18 integer solutions.
  213. [TestCase(-12d, -6d)]
  214. [TestCase(-12d, 3d)]
  215. [TestCase( -8d, -8d)]
  216. [TestCase( -6d, -12d)]
  217. [TestCase( -5d, -20d)]
  218. [TestCase( -4d, 2d)]
  219. [TestCase( -3d, 12d)]
  220. [TestCase( -2d, 4d)]
  221. [TestCase( 2d, -4d)]
  222. [TestCase( 3d, -12d)]
  223. [TestCase( 4d, -2d)]
  224. [TestCase( 5d, 20d)]
  225. [TestCase( 6d, 12d)]
  226. [TestCase( 8d, 8d)]
  227. [TestCase( 12d, -3d)]
  228. [TestCase( 12d, 6d)]
  229. [TestCase( 20d, 5d)]
  230. public void Misc3(double a, double b)
  231. {
  232. // 1 / ((1 / a + 1 / b) ^ 2) = 16
  233. /*
  234. FMOV D2, 1.0e+0
  235. FDIV D0, D2, D0
  236. FDIV D1, D2, D1
  237. FADD D0, D0, D1
  238. FDIV D0, D2, D0
  239. FMUL D0, D0, D0
  240. RET
  241. */
  242. SetContext(v0: MakeVectorScalar(a), v1: MakeVectorScalar(b));
  243. Opcode(0x1E6E1002);
  244. Opcode(0x1E601840);
  245. Opcode(0x1E611841);
  246. Opcode(0x1E612800);
  247. Opcode(0x1E601840);
  248. Opcode(0x1E600800);
  249. Opcode(0xD65F03C0);
  250. ExecuteOpcodes();
  251. Assert.That(GetContext().GetV(0).As<double>(), Is.EqualTo(16d));
  252. }
  253. [Test, Ignore("The Tester supports only one return point.")]
  254. public void MiscF([Range(0u, 92u, 1u)] uint a)
  255. {
  256. ulong Fn(uint n)
  257. {
  258. ulong x = 0, y = 1, z;
  259. if (n == 0)
  260. {
  261. return x;
  262. }
  263. for (uint i = 2; i <= n; i++)
  264. {
  265. z = x + y;
  266. x = y;
  267. y = z;
  268. }
  269. return y;
  270. }
  271. /*
  272. 0x0000000000001000: MOV W4, W0
  273. 0x0000000000001004: CBZ W0, #0x34
  274. 0x0000000000001008: CMP W0, #1
  275. 0x000000000000100C: B.LS #0x34
  276. 0x0000000000001010: MOVZ W2, #0x2
  277. 0x0000000000001014: MOVZ X1, #0x1
  278. 0x0000000000001018: MOVZ X3, #0
  279. 0x000000000000101C: ADD X0, X3, X1
  280. 0x0000000000001020: ADD W2, W2, #1
  281. 0x0000000000001024: MOV X3, X1
  282. 0x0000000000001028: MOV X1, X0
  283. 0x000000000000102C: CMP W4, W2
  284. 0x0000000000001030: B.HS #-0x14
  285. 0x0000000000001034: RET
  286. 0x0000000000001038: MOVZ X0, #0
  287. 0x000000000000103C: RET
  288. 0x0000000000001040: MOVZ X0, #0x1
  289. 0x0000000000001044: RET
  290. */
  291. SetContext(x0: a);
  292. Opcode(0x2A0003E4);
  293. Opcode(0x340001A0);
  294. Opcode(0x7100041F);
  295. Opcode(0x540001A9);
  296. Opcode(0x52800042);
  297. Opcode(0xD2800021);
  298. Opcode(0xD2800003);
  299. Opcode(0x8B010060);
  300. Opcode(0x11000442);
  301. Opcode(0xAA0103E3);
  302. Opcode(0xAA0003E1);
  303. Opcode(0x6B02009F);
  304. Opcode(0x54FFFF62);
  305. Opcode(0xD65F03C0);
  306. Opcode(0xD2800000);
  307. Opcode(0xD65F03C0);
  308. Opcode(0xD2800020);
  309. Opcode(0xD65F03C0);
  310. ExecuteOpcodes();
  311. Assert.That(GetContext().GetX(0), Is.EqualTo(Fn(a)));
  312. }
  313. [Explicit]
  314. [Test]
  315. public void MiscR()
  316. {
  317. const ulong result = 5;
  318. /*
  319. 0x0000000000001000: MOV X0, #2
  320. 0x0000000000001004: MOV X1, #3
  321. 0x0000000000001008: ADD X0, X0, X1
  322. 0x000000000000100C: RET
  323. */
  324. Opcode(0xD2800040);
  325. Opcode(0xD2800061);
  326. Opcode(0x8B010000);
  327. Opcode(0xD65F03C0);
  328. ExecuteOpcodes();
  329. Assert.That(GetContext().GetX(0), Is.EqualTo(result));
  330. Reset();
  331. /*
  332. 0x0000000000001000: MOV X0, #3
  333. 0x0000000000001004: MOV X1, #2
  334. 0x0000000000001008: ADD X0, X0, X1
  335. 0x000000000000100C: RET
  336. */
  337. Opcode(0xD2800060);
  338. Opcode(0xD2800041);
  339. Opcode(0x8B010000);
  340. Opcode(0xD65F03C0);
  341. ExecuteOpcodes();
  342. Assert.That(GetContext().GetX(0), Is.EqualTo(result));
  343. }
  344. [Explicit]
  345. [TestCase( 0ul)]
  346. [TestCase( 1ul)]
  347. [TestCase( 2ul)]
  348. [TestCase(42ul)]
  349. public void SanityCheck(ulong a)
  350. {
  351. uint opcode = 0xD503201F; // NOP
  352. ExecutionContext context = SingleOpcode(opcode, x0: a);
  353. Assert.That(context.GetX(0), Is.EqualTo(a));
  354. }
  355. [Explicit]
  356. [Test, Pairwise]
  357. public void Misc4([ValueSource("_1S_F_")] ulong a,
  358. [ValueSource("_1S_F_")] ulong b,
  359. [ValueSource("_1S_F_")] ulong c,
  360. [Values(0ul, 1ul, 2ul, 3ul)] ulong displacement)
  361. {
  362. if (!BitConverter.IsLittleEndian)
  363. {
  364. Assert.Ignore();
  365. }
  366. for (ulong gapOffset = 0; gapOffset < displacement; gapOffset++)
  367. {
  368. SetWorkingMemory(gapOffset, TestContext.CurrentContext.Random.NextByte());
  369. }
  370. SetWorkingMemory(0x0 + displacement, BitConverter.GetBytes((uint)b));
  371. SetWorkingMemory(0x4 + displacement, BitConverter.GetBytes((uint)c));
  372. SetWorkingMemory(0x8 + displacement, TestContext.CurrentContext.Random.NextByte());
  373. SetWorkingMemory(0x9 + displacement, TestContext.CurrentContext.Random.NextByte());
  374. SetWorkingMemory(0xA + displacement, TestContext.CurrentContext.Random.NextByte());
  375. SetWorkingMemory(0xB + displacement, TestContext.CurrentContext.Random.NextByte());
  376. SetContext(
  377. x0: DataBaseAddress + displacement,
  378. v0: MakeVectorE0E1(a, TestContext.CurrentContext.Random.NextULong()),
  379. v1: MakeVectorE0E1(TestContext.CurrentContext.Random.NextULong(), TestContext.CurrentContext.Random.NextULong()),
  380. v2: MakeVectorE0E1(TestContext.CurrentContext.Random.NextULong(), TestContext.CurrentContext.Random.NextULong()),
  381. overflow: TestContext.CurrentContext.Random.NextBool(),
  382. carry: TestContext.CurrentContext.Random.NextBool(),
  383. zero: TestContext.CurrentContext.Random.NextBool(),
  384. negative: TestContext.CurrentContext.Random.NextBool());
  385. Opcode(0xBD400001); // LDR S1, [X0,#0]
  386. Opcode(0xBD400402); // LDR S2, [X0,#4]
  387. Opcode(0x1E215801); // FMIN S1, S0, S1
  388. Opcode(0x1E222000); // FCMP S0, S2
  389. Opcode(0x1E214C40); // FCSEL S0, S2, S1, MI
  390. Opcode(0xBD000800); // STR S0, [X0,#8]
  391. Opcode(0xD65F03C0); // RET
  392. ExecuteOpcodes();
  393. CompareAgainstUnicorn();
  394. }
  395. [Explicit]
  396. [Test]
  397. public void Misc5([ValueSource("_1S_F_")] ulong a)
  398. {
  399. SetContext(
  400. v0: MakeVectorE0E1(a, TestContext.CurrentContext.Random.NextULong()),
  401. v1: MakeVectorE0E1(TestContext.CurrentContext.Random.NextULong(), TestContext.CurrentContext.Random.NextULong()),
  402. overflow: TestContext.CurrentContext.Random.NextBool(),
  403. carry: TestContext.CurrentContext.Random.NextBool(),
  404. zero: TestContext.CurrentContext.Random.NextBool(),
  405. negative: TestContext.CurrentContext.Random.NextBool());
  406. Opcode(0x1E202008); // FCMP S0, #0.0
  407. Opcode(0x1E2E1001); // FMOV S1, #1.0
  408. Opcode(0x1E215800); // FMIN S0, S0, S1
  409. Opcode(0x1E2703E1); // FMOV S1, WZR
  410. Opcode(0x1E204C20); // FCSEL S0, S1, S0, MI
  411. Opcode(0xD65F03C0); // RET
  412. ExecuteOpcodes();
  413. CompareAgainstUnicorn();
  414. }
  415. #endif
  416. }
  417. }