CpuTestMisc.cs 17 KB

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