CpuTestSimd32.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #define Simd32
  2. using ARMeilleure.State;
  3. using NUnit.Framework;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Tests.Cpu
  6. {
  7. [Category("Simd32")]
  8. public sealed class CpuTestSimd32 : CpuTest32
  9. {
  10. #if Simd32
  11. #region "ValueSource (Opcodes)"
  12. private static uint[] _Vabs_Vneg_V_()
  13. {
  14. return new uint[]
  15. {
  16. 0xf3b10300u, // VABS.S8 D0, D0
  17. 0xf3b10380u // VNEG.S8 D0, D0
  18. };
  19. }
  20. #endregion
  21. #region "ValueSource (Types)"
  22. private static ulong[] _8B4H2S_()
  23. {
  24. return new ulong[] { 0x0000000000000000ul, 0x7F7F7F7F7F7F7F7Ful,
  25. 0x8080808080808080ul, 0x7FFF7FFF7FFF7FFFul,
  26. 0x8000800080008000ul, 0x7FFFFFFF7FFFFFFFul,
  27. 0x8000000080000000ul, 0xFFFFFFFFFFFFFFFFul };
  28. }
  29. private static IEnumerable<ulong> _1S_F_()
  30. {
  31. yield return 0x00000000FF7FFFFFul; // -Max Normal (float.MinValue)
  32. yield return 0x0000000080800000ul; // -Min Normal
  33. yield return 0x00000000807FFFFFul; // -Max Subnormal
  34. yield return 0x0000000080000001ul; // -Min Subnormal (-float.Epsilon)
  35. yield return 0x000000007F7FFFFFul; // +Max Normal (float.MaxValue)
  36. yield return 0x0000000000800000ul; // +Min Normal
  37. yield return 0x00000000007FFFFFul; // +Max Subnormal
  38. yield return 0x0000000000000001ul; // +Min Subnormal (float.Epsilon)
  39. if (!NoZeros)
  40. {
  41. yield return 0x0000000080000000ul; // -Zero
  42. yield return 0x0000000000000000ul; // +Zero
  43. }
  44. if (!NoInfs)
  45. {
  46. yield return 0x00000000FF800000ul; // -Infinity
  47. yield return 0x000000007F800000ul; // +Infinity
  48. }
  49. if (!NoNaNs)
  50. {
  51. yield return 0x00000000FFC00000ul; // -QNaN (all zeros payload) (float.NaN)
  52. yield return 0x00000000FFBFFFFFul; // -SNaN (all ones payload)
  53. yield return 0x000000007FC00000ul; // +QNaN (all zeros payload) (-float.NaN) (DefaultNaN)
  54. yield return 0x000000007FBFFFFFul; // +SNaN (all ones payload)
  55. }
  56. for (int cnt = 1; cnt <= RndCnt; cnt++)
  57. {
  58. ulong grbg = TestContext.CurrentContext.Random.NextUInt();
  59. ulong rnd1 = GenNormalS();
  60. ulong rnd2 = GenSubnormalS();
  61. yield return (grbg << 32) | rnd1;
  62. yield return (grbg << 32) | rnd2;
  63. }
  64. }
  65. private static IEnumerable<ulong> _2S_F_()
  66. {
  67. yield return 0xFF7FFFFFFF7FFFFFul; // -Max Normal (float.MinValue)
  68. yield return 0x8080000080800000ul; // -Min Normal
  69. yield return 0x807FFFFF807FFFFFul; // -Max Subnormal
  70. yield return 0x8000000180000001ul; // -Min Subnormal (-float.Epsilon)
  71. yield return 0x7F7FFFFF7F7FFFFFul; // +Max Normal (float.MaxValue)
  72. yield return 0x0080000000800000ul; // +Min Normal
  73. yield return 0x007FFFFF007FFFFFul; // +Max Subnormal
  74. yield return 0x0000000100000001ul; // +Min Subnormal (float.Epsilon)
  75. if (!NoZeros)
  76. {
  77. yield return 0x8000000080000000ul; // -Zero
  78. yield return 0x0000000000000000ul; // +Zero
  79. }
  80. if (!NoInfs)
  81. {
  82. yield return 0xFF800000FF800000ul; // -Infinity
  83. yield return 0x7F8000007F800000ul; // +Infinity
  84. }
  85. if (!NoNaNs)
  86. {
  87. yield return 0xFFC00000FFC00000ul; // -QNaN (all zeros payload) (float.NaN)
  88. yield return 0xFFBFFFFFFFBFFFFFul; // -SNaN (all ones payload)
  89. yield return 0x7FC000007FC00000ul; // +QNaN (all zeros payload) (-float.NaN) (DefaultNaN)
  90. yield return 0x7FBFFFFF7FBFFFFFul; // +SNaN (all ones payload)
  91. }
  92. for (int cnt = 1; cnt <= RndCnt; cnt++)
  93. {
  94. ulong rnd1 = GenNormalS();
  95. ulong rnd2 = GenSubnormalS();
  96. yield return (rnd1 << 32) | rnd1;
  97. yield return (rnd2 << 32) | rnd2;
  98. }
  99. }
  100. private static IEnumerable<ulong> _1D_F_()
  101. {
  102. yield return 0xFFEFFFFFFFFFFFFFul; // -Max Normal (double.MinValue)
  103. yield return 0x8010000000000000ul; // -Min Normal
  104. yield return 0x800FFFFFFFFFFFFFul; // -Max Subnormal
  105. yield return 0x8000000000000001ul; // -Min Subnormal (-double.Epsilon)
  106. yield return 0x7FEFFFFFFFFFFFFFul; // +Max Normal (double.MaxValue)
  107. yield return 0x0010000000000000ul; // +Min Normal
  108. yield return 0x000FFFFFFFFFFFFFul; // +Max Subnormal
  109. yield return 0x0000000000000001ul; // +Min Subnormal (double.Epsilon)
  110. if (!NoZeros)
  111. {
  112. yield return 0x8000000000000000ul; // -Zero
  113. yield return 0x0000000000000000ul; // +Zero
  114. }
  115. if (!NoInfs)
  116. {
  117. yield return 0xFFF0000000000000ul; // -Infinity
  118. yield return 0x7FF0000000000000ul; // +Infinity
  119. }
  120. if (!NoNaNs)
  121. {
  122. yield return 0xFFF8000000000000ul; // -QNaN (all zeros payload) (double.NaN)
  123. yield return 0xFFF7FFFFFFFFFFFFul; // -SNaN (all ones payload)
  124. yield return 0x7FF8000000000000ul; // +QNaN (all zeros payload) (-double.NaN) (DefaultNaN)
  125. yield return 0x7FF7FFFFFFFFFFFFul; // +SNaN (all ones payload)
  126. }
  127. for (int cnt = 1; cnt <= RndCnt; cnt++)
  128. {
  129. ulong rnd1 = GenNormalD();
  130. ulong rnd2 = GenSubnormalD();
  131. yield return rnd1;
  132. yield return rnd2;
  133. }
  134. }
  135. private static IEnumerable<ulong> _GenPopCnt8B_()
  136. {
  137. for (ulong cnt = 0ul; cnt <= 255ul; cnt++)
  138. {
  139. yield return (cnt << 56) | (cnt << 48) | (cnt << 40) | (cnt << 32) |
  140. (cnt << 24) | (cnt << 16) | (cnt << 08) | cnt;
  141. }
  142. }
  143. #endregion
  144. private const int RndCnt = 2;
  145. private static readonly bool NoZeros = false;
  146. private static readonly bool NoInfs = false;
  147. private static readonly bool NoNaNs = false;
  148. [Test, Pairwise, Description("SHA256SU0.32 <Qd>, <Qm>")]
  149. public void Sha256su0_V([Values(0xF3BA03C0u)] uint opcode,
  150. [Values(0u)] uint rd,
  151. [Values(2u)] uint rm,
  152. [Values(0x9BCBBF7443FB4F91ul)] ulong z0,
  153. [Values(0x482C58A58CBCBD59ul)] ulong z1,
  154. [Values(0xA0099B803625F82Aul)] ulong a0,
  155. [Values(0x1AA3B0B4E1AB4C8Cul)] ulong a1,
  156. [Values(0x29A44D72598F15F3ul)] ulong resultL,
  157. [Values(0x74CED221E2793F07ul)] ulong resultH)
  158. {
  159. opcode |= ((rd & 0xf) << 12) | ((rd & 0x10) << 18);
  160. opcode |= ((rm & 0xf) << 0) | ((rm & 0x10) << 1);
  161. V128 v0 = MakeVectorE0E1(z0, z1);
  162. V128 v1 = MakeVectorE0E1(a0, a1);
  163. ExecutionContext context = SingleOpcode(opcode, v0: v0, v1: v1, runUnicorn: false);
  164. Assert.Multiple(() =>
  165. {
  166. Assert.That(GetVectorE0(context.GetV(0)), Is.EqualTo(resultL));
  167. Assert.That(GetVectorE1(context.GetV(0)), Is.EqualTo(resultH));
  168. });
  169. // Unicorn does not yet support hash instructions in A32.
  170. // CompareAgainstUnicorn();
  171. }
  172. [Test, Pairwise]
  173. public void Vabs_Vneg_V_S8_S16_S32([ValueSource("_Vabs_Vneg_V_")] uint opcode,
  174. [Range(0u, 3u)] uint rd,
  175. [Range(0u, 3u)] uint rm,
  176. [ValueSource("_8B4H2S_")] [Random(RndCnt)] ulong z,
  177. [ValueSource("_8B4H2S_")] [Random(RndCnt)] ulong b,
  178. [Values(0u, 1u, 2u)] uint size, // <S8, S16, S32>
  179. [Values] bool q)
  180. {
  181. const bool f = false;
  182. Vabs_Vneg_V(opcode, rd, rm, z, b, size, f, q);
  183. }
  184. [Test, Pairwise]
  185. public void Vabs_Vneg_V_F32([ValueSource("_Vabs_Vneg_V_")] uint opcode,
  186. [Range(0u, 3u)] uint rd,
  187. [Range(0u, 3u)] uint rm,
  188. [ValueSource("_2S_F_")] ulong z,
  189. [ValueSource("_2S_F_")] ulong b,
  190. [Values] bool q)
  191. {
  192. const uint size = 0b10; // <F32>
  193. const bool f = true;
  194. Vabs_Vneg_V(opcode, rd, rm, z, b, size, f, q);
  195. }
  196. private void Vabs_Vneg_V(uint opcode, uint rd, uint rm, ulong z, ulong b, uint size, bool f, bool q)
  197. {
  198. if (f)
  199. {
  200. opcode |= 1 << 10;
  201. }
  202. if (q)
  203. {
  204. opcode |= 1 << 6;
  205. rd >>= 1; rd <<= 1;
  206. rm >>= 1; rm <<= 1;
  207. }
  208. opcode |= ((rd & 0xf) << 12) | ((rd & 0x10) << 18);
  209. opcode |= ((rm & 0xf) << 0) | ((rm & 0x10) << 1);
  210. opcode |= (size & 0x3) << 18;
  211. V128 v0 = MakeVectorE0E1(z, ~z);
  212. V128 v1 = MakeVectorE0E1(b, ~b);
  213. SingleOpcode(opcode, v0: v0, v1: v1);
  214. CompareAgainstUnicorn();
  215. }
  216. [Test, Pairwise, Description("VCNT.8 D0, D0 | VCNT.8 Q0, Q0")]
  217. public void Vcnt([Values(0u, 1u)] uint rd,
  218. [Values(0u, 1u)] uint rm,
  219. [ValueSource(nameof(_GenPopCnt8B_))] [Random(RndCnt)] ulong d0,
  220. [Values] bool q)
  221. {
  222. ulong d1 = ~d0; // It's expensive to have a second generator.
  223. uint opcode = 0xf3b00500u; // VCNT.8 D0, D0
  224. if (q)
  225. {
  226. opcode |= 1u << 6;
  227. rd &= ~1u;
  228. rm &= ~1u;
  229. }
  230. opcode |= ((rd & 0xf) << 12) | ((rd & 0x10) << 18);
  231. opcode |= ((rm & 0xf) << 0) | ((rm & 0x10) << 1);
  232. V128 v0 = MakeVectorE0E1(d0, d1);
  233. SingleOpcode(opcode, v0: v0);
  234. CompareAgainstUnicorn();
  235. }
  236. #endif
  237. }
  238. }