CpuTestAluBinary.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #define AluBinary
  2. using ARMeilleure.State;
  3. using NUnit.Framework;
  4. namespace Ryujinx.Tests.Cpu
  5. {
  6. [Category("AluBinary")]
  7. public sealed class CpuTestAluBinary : CpuTest
  8. {
  9. #if AluBinary
  10. public struct CrcTest
  11. {
  12. public uint Crc;
  13. public ulong Value;
  14. public bool C;
  15. public uint[] Results; // One result for each CRC variant (8, 16, 32)
  16. public CrcTest(uint crc, ulong value, bool c, params uint[] results)
  17. {
  18. Crc = crc;
  19. Value = value;
  20. C = c;
  21. Results = results;
  22. }
  23. }
  24. #region "ValueSource (CRC32)"
  25. private static CrcTest[] _CRC32_Test_Values_()
  26. {
  27. // Created with http://www.sunshine2k.de/coding/javascript/crc/crc_js.html, with:
  28. // - non-reflected polynomials
  29. // - input reflected, result reflected
  30. // - bytes in order of increasing significance
  31. // - xor 0
  32. // Only includes non-C variant, as the other can be tested with unicorn.
  33. return new[]
  34. {
  35. new CrcTest(0x00000000u, 0x00_00_00_00_00_00_00_00u, false, 0x00000000, 0x00000000, 0x00000000, 0x00000000),
  36. new CrcTest(0x00000000u, 0x7f_ff_ff_ff_ff_ff_ff_ffu, false, 0x2d02ef8d, 0xbe2612ff, 0xdebb20e3, 0xa9de8355),
  37. new CrcTest(0x00000000u, 0x80_00_00_00_00_00_00_00u, false, 0x00000000, 0x00000000, 0x00000000, 0xedb88320),
  38. new CrcTest(0x00000000u, 0xff_ff_ff_ff_ff_ff_ff_ffu, false, 0x2d02ef8d, 0xbe2612ff, 0xdebb20e3, 0x44660075),
  39. new CrcTest(0x00000000u, 0xa0_02_f1_ca_52_78_8c_1cu, false, 0x14015c4f, 0x02799256, 0x9063c9e5, 0x8816610a),
  40. new CrcTest(0xffffffffu, 0x00_00_00_00_00_00_00_00u, false, 0x2dfd1072, 0xbe26ed00, 0xdebb20e3, 0x9add2096),
  41. new CrcTest(0xffffffffu, 0x7f_ff_ff_ff_ff_ff_ff_ffu, false, 0x00ffffff, 0x0000ffff, 0x00000000, 0x3303a3c3),
  42. new CrcTest(0xffffffffu, 0x80_00_00_00_00_00_00_00u, false, 0x2dfd1072, 0xbe26ed00, 0xdebb20e3, 0x7765a3b6),
  43. new CrcTest(0xffffffffu, 0xff_ff_ff_ff_ff_ff_ff_ffu, false, 0x00ffffff, 0x0000ffff, 0x00000000, 0xdebb20e3),
  44. new CrcTest(0xffffffffu, 0xa0_02_f1_ca_52_78_8c_1cu, false, 0x39fc4c3d, 0xbc5f7f56, 0x4ed8e906, 0x12cb419c)
  45. };
  46. }
  47. #endregion
  48. [Test, Combinatorial]
  49. public void Crc32_b_h_w_x([Values(0u)] uint rd,
  50. [Values(1u)] uint rn,
  51. [Values(2u)] uint rm,
  52. [Range(0u, 3u)] uint size,
  53. [ValueSource(nameof(_CRC32_Test_Values_))] CrcTest test)
  54. {
  55. uint opcode = 0x1AC04000; // CRC32B W0, W0, W0
  56. opcode |= size << 10;
  57. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  58. if (size == 3)
  59. {
  60. opcode |= 0x80000000;
  61. }
  62. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  63. SingleOpcode(opcode, x1: test.Crc, x2: test.Value, x31: w31, runUnicorn: false);
  64. ExecutionContext context = GetContext();
  65. ulong result = context.GetX((int)rd);
  66. Assert.That(result == test.Results[size]);
  67. }
  68. [Test, Pairwise, Description("CRC32X <Wd>, <Wn>, <Xm>"), Ignore("Unicorn fails.")]
  69. public void Crc32x([Values(0u, 31u)] uint rd,
  70. [Values(1u, 31u)] uint rn,
  71. [Values(2u, 31u)] uint rm,
  72. [Values(0x00000000u, 0xFFFFFFFFu)] uint wn,
  73. [Values((ulong)0x00_00_00_00_00_00_00_00,
  74. (ulong)0x7F_FF_FF_FF_FF_FF_FF_FF,
  75. 0x80_00_00_00_00_00_00_00,
  76. 0xFF_FF_FF_FF_FF_FF_FF_FF)] ulong xm)
  77. {
  78. uint opcode = 0x9AC04C00; // CRC32X W0, W0, X0
  79. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  80. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  81. SingleOpcode(opcode, x1: wn, x2: xm, x31: w31);
  82. CompareAgainstUnicorn();
  83. }
  84. [Test, Pairwise, Description("CRC32W <Wd>, <Wn>, <Wm>"), Ignore("Unicorn fails.")]
  85. public void Crc32w([Values(0u, 31u)] uint rd,
  86. [Values(1u, 31u)] uint rn,
  87. [Values(2u, 31u)] uint rm,
  88. [Values(0x00000000u, 0xFFFFFFFFu)] uint wn,
  89. [Values((uint)0x00_00_00_00, (uint)0x7F_FF_FF_FF,
  90. 0x80_00_00_00, 0xFF_FF_FF_FF)] uint wm)
  91. {
  92. uint opcode = 0x1AC04800; // CRC32W W0, W0, W0
  93. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  94. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  95. SingleOpcode(opcode, x1: wn, x2: wm, x31: w31);
  96. CompareAgainstUnicorn();
  97. }
  98. [Test, Pairwise, Description("CRC32H <Wd>, <Wn>, <Wm>"), Ignore("Unicorn fails.")]
  99. public void Crc32h([Values(0u, 31u)] uint rd,
  100. [Values(1u, 31u)] uint rn,
  101. [Values(2u, 31u)] uint rm,
  102. [Values(0x00000000u, 0xFFFFFFFFu)] uint wn,
  103. [Values((ushort)0x00_00, (ushort)0x7F_FF,
  104. (ushort)0x80_00, (ushort)0xFF_FF)] ushort wm)
  105. {
  106. uint opcode = 0x1AC04400; // CRC32H W0, W0, W0
  107. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  108. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  109. SingleOpcode(opcode, x1: wn, x2: wm, x31: w31);
  110. CompareAgainstUnicorn();
  111. }
  112. [Test, Pairwise, Description("CRC32B <Wd>, <Wn>, <Wm>"), Ignore("Unicorn fails.")]
  113. public void Crc32b([Values(0u, 31u)] uint rd,
  114. [Values(1u, 31u)] uint rn,
  115. [Values(2u, 31u)] uint rm,
  116. [Values(0x00000000u, 0xFFFFFFFFu)] uint wn,
  117. [Values((byte)0x00, (byte)0x7F,
  118. (byte)0x80, (byte)0xFF)] byte wm)
  119. {
  120. uint opcode = 0x1AC04000; // CRC32B W0, W0, W0
  121. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  122. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  123. SingleOpcode(opcode, x1: wn, x2: wm, x31: w31);
  124. CompareAgainstUnicorn();
  125. }
  126. [Test, Pairwise, Description("CRC32CX <Wd>, <Wn>, <Xm>")]
  127. public void Crc32cx([Values(0u, 31u)] uint rd,
  128. [Values(1u, 31u)] uint rn,
  129. [Values(2u, 31u)] uint rm,
  130. [Values(0x00000000u, 0xFFFFFFFFu)] uint wn,
  131. [Values((ulong)0x00_00_00_00_00_00_00_00,
  132. (ulong)0x7F_FF_FF_FF_FF_FF_FF_FF,
  133. 0x80_00_00_00_00_00_00_00,
  134. 0xFF_FF_FF_FF_FF_FF_FF_FF)] ulong xm)
  135. {
  136. uint opcode = 0x9AC05C00; // CRC32CX W0, W0, X0
  137. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  138. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  139. SingleOpcode(opcode, x1: wn, x2: xm, x31: w31);
  140. CompareAgainstUnicorn();
  141. }
  142. [Test, Pairwise, Description("CRC32CW <Wd>, <Wn>, <Wm>")]
  143. public void Crc32cw([Values(0u, 31u)] uint rd,
  144. [Values(1u, 31u)] uint rn,
  145. [Values(2u, 31u)] uint rm,
  146. [Values(0x00000000u, 0xFFFFFFFFu)] uint wn,
  147. [Values((uint)0x00_00_00_00, (uint)0x7F_FF_FF_FF,
  148. 0x80_00_00_00, 0xFF_FF_FF_FF)] uint wm)
  149. {
  150. uint opcode = 0x1AC05800; // CRC32CW W0, W0, W0
  151. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  152. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  153. SingleOpcode(opcode, x1: wn, x2: wm, x31: w31);
  154. CompareAgainstUnicorn();
  155. }
  156. [Test, Pairwise, Description("CRC32CH <Wd>, <Wn>, <Wm>")]
  157. public void Crc32ch([Values(0u, 31u)] uint rd,
  158. [Values(1u, 31u)] uint rn,
  159. [Values(2u, 31u)] uint rm,
  160. [Values(0x00000000u, 0xFFFFFFFFu)] uint wn,
  161. [Values((ushort)0x00_00, (ushort)0x7F_FF,
  162. (ushort)0x80_00, (ushort)0xFF_FF)] ushort wm)
  163. {
  164. uint opcode = 0x1AC05400; // CRC32CH W0, W0, W0
  165. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  166. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  167. SingleOpcode(opcode, x1: wn, x2: wm, x31: w31);
  168. CompareAgainstUnicorn();
  169. }
  170. [Test, Pairwise, Description("CRC32CB <Wd>, <Wn>, <Wm>")]
  171. public void Crc32cb([Values(0u, 31u)] uint rd,
  172. [Values(1u, 31u)] uint rn,
  173. [Values(2u, 31u)] uint rm,
  174. [Values(0x00000000u, 0xFFFFFFFFu)] uint wn,
  175. [Values((byte)0x00, (byte)0x7F,
  176. (byte)0x80, (byte)0xFF)] byte wm)
  177. {
  178. uint opcode = 0x1AC05000; // CRC32CB W0, W0, W0
  179. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  180. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  181. SingleOpcode(opcode, x1: wn, x2: wm, x31: w31);
  182. CompareAgainstUnicorn();
  183. }
  184. [Test, Pairwise, Description("SDIV <Xd>, <Xn>, <Xm>")]
  185. public void Sdiv_64bit([Values(0u, 31u)] uint rd,
  186. [Values(1u, 31u)] uint rn,
  187. [Values(2u, 31u)] uint rm,
  188. [Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
  189. 0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] ulong xn,
  190. [Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
  191. 0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] ulong xm)
  192. {
  193. uint opcode = 0x9AC00C00; // SDIV X0, X0, X0
  194. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  195. ulong x31 = TestContext.CurrentContext.Random.NextULong();
  196. SingleOpcode(opcode, x1: xn, x2: xm, x31: x31);
  197. CompareAgainstUnicorn();
  198. }
  199. [Test, Pairwise, Description("SDIV <Wd>, <Wn>, <Wm>")]
  200. public void Sdiv_32bit([Values(0u, 31u)] uint rd,
  201. [Values(1u, 31u)] uint rn,
  202. [Values(2u, 31u)] uint rm,
  203. [Values(0x00000000u, 0x7FFFFFFFu,
  204. 0x80000000u, 0xFFFFFFFFu)] uint wn,
  205. [Values(0x00000000u, 0x7FFFFFFFu,
  206. 0x80000000u, 0xFFFFFFFFu)] uint wm)
  207. {
  208. uint opcode = 0x1AC00C00; // SDIV W0, W0, W0
  209. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  210. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  211. SingleOpcode(opcode, x1: wn, x2: wm, x31: w31);
  212. CompareAgainstUnicorn();
  213. }
  214. [Test, Pairwise, Description("UDIV <Xd>, <Xn>, <Xm>")]
  215. public void Udiv_64bit([Values(0u, 31u)] uint rd,
  216. [Values(1u, 31u)] uint rn,
  217. [Values(2u, 31u)] uint rm,
  218. [Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
  219. 0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] ulong xn,
  220. [Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
  221. 0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] ulong xm)
  222. {
  223. uint opcode = 0x9AC00800; // UDIV X0, X0, X0
  224. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  225. ulong x31 = TestContext.CurrentContext.Random.NextULong();
  226. SingleOpcode(opcode, x1: xn, x2: xm, x31: x31);
  227. CompareAgainstUnicorn();
  228. }
  229. [Test, Pairwise, Description("UDIV <Wd>, <Wn>, <Wm>")]
  230. public void Udiv_32bit([Values(0u, 31u)] uint rd,
  231. [Values(1u, 31u)] uint rn,
  232. [Values(2u, 31u)] uint rm,
  233. [Values(0x00000000u, 0x7FFFFFFFu,
  234. 0x80000000u, 0xFFFFFFFFu)] uint wn,
  235. [Values(0x00000000u, 0x7FFFFFFFu,
  236. 0x80000000u, 0xFFFFFFFFu)] uint wm)
  237. {
  238. uint opcode = 0x1AC00800; // UDIV W0, W0, W0
  239. opcode |= ((rm & 31) << 16) | ((rn & 31) << 5) | ((rd & 31) << 0);
  240. uint w31 = TestContext.CurrentContext.Random.NextUInt();
  241. SingleOpcode(opcode, x1: wn, x2: wm, x31: w31);
  242. CompareAgainstUnicorn();
  243. }
  244. #endif
  245. }
  246. }