CpuTestSimdMemory32.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #define SimdMemory32
  2. using ARMeilleure.State;
  3. using NUnit.Framework;
  4. using System;
  5. namespace Ryujinx.Tests.Cpu
  6. {
  7. [Category("SimdMemory32")]
  8. public sealed class CpuTestSimdMemory32 : CpuTest32
  9. {
  10. #if SimdMemory32
  11. private const int RndCntImm = 2;
  12. private uint[] LDSTModes =
  13. {
  14. // LD1
  15. 0b0111,
  16. 0b1010,
  17. 0b0110,
  18. 0b0010,
  19. // LD2
  20. 0b1000,
  21. 0b1001,
  22. 0b0011,
  23. // LD3
  24. 0b0100,
  25. 0b0101,
  26. // LD4
  27. 0b0000,
  28. 0b0001
  29. };
  30. [Test, Pairwise, Description("VLDn.<size> <list>, [<Rn> {:<align>}]{ /!/, <Rm>} (single n element structure)")]
  31. public void Vldn_Single([Values(0u, 1u, 2u)] uint size,
  32. [Values(0u, 13u)] uint rn,
  33. [Values(1u, 13u, 15u)] uint rm,
  34. [Values(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u)] uint vd,
  35. [Range(0u, 7u)] uint index,
  36. [Range(0u, 3u)] uint n,
  37. [Values(0x0u)] [Random(0u, 0xffu, RndCntImm)] uint offset)
  38. {
  39. var data = GenerateVectorSequence(0x1000);
  40. SetWorkingMemory(0, data);
  41. uint opcode = 0xf4a00000u; // VLD1.8 {D0[0]}, [R0], R0
  42. opcode |= ((size & 3) << 10) | ((rn & 15) << 16) | (rm & 15);
  43. uint index_align = (index << (int)(1 + size)) & 15;
  44. opcode |= (index_align) << 4;
  45. opcode |= ((vd & 0x10) << 18);
  46. opcode |= ((vd & 0xf) << 12);
  47. opcode |= (n & 3) << 8; // LD1 is 0, LD2 is 1 etc.
  48. SingleOpcode(opcode, r0: 0x2500, r1: offset, sp: 0x2500);
  49. CompareAgainstUnicorn();
  50. }
  51. [Test, Pairwise, Description("VLDn.<size> <list>, [<Rn> {:<align>}]{ /!/, <Rm>} (all lanes)")]
  52. public void Vldn_All([Values(0u, 13u)] uint rn,
  53. [Values(1u, 13u, 15u)] uint rm,
  54. [Values(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u)] uint vd,
  55. [Range(0u, 3u)] uint n,
  56. [Range(0u, 2u)] uint size,
  57. [Values] bool t,
  58. [Values(0x0u)] [Random(0u, 0xffu, RndCntImm)] uint offset)
  59. {
  60. var data = GenerateVectorSequence(0x1000);
  61. SetWorkingMemory(0, data);
  62. uint opcode = 0xf4a00c00u; // VLD1.8 {D0[0]}, [R0], R0
  63. opcode |= ((size & 3) << 6) | ((rn & 15) << 16) | (rm & 15);
  64. opcode |= ((vd & 0x10) << 18);
  65. opcode |= ((vd & 0xf) << 12);
  66. opcode |= (n & 3) << 8; // LD1 is 0, LD2 is 1 etc.
  67. if (t) opcode |= 1 << 5;
  68. SingleOpcode(opcode, r0: 0x2500, r1: offset, sp: 0x2500);
  69. CompareAgainstUnicorn();
  70. }
  71. [Test, Pairwise, Description("VLDn.<size> <list>, [<Rn> {:<align>}]{ /!/, <Rm>} (multiple n element structures)")]
  72. public void Vldn_Pair([Values(0u, 1u, 2u, 3u)] uint size,
  73. [Values(0u, 13u)] uint rn,
  74. [Values(1u, 13u, 15u)] uint rm,
  75. [Values(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u)] uint vd,
  76. [Range(0u, 3u)] uint mode,
  77. [Values(0x0u)] [Random(0u, 0xffu, RndCntImm)] uint offset)
  78. {
  79. var data = GenerateVectorSequence(0x1000);
  80. SetWorkingMemory(0, data);
  81. uint opcode = 0xf4200000u; // VLD4.8 {D0, D1, D2, D3}, [R0], R0
  82. opcode |= ((size & 3) << 6) | ((rn & 15) << 16) | (rm & 15) | (LDSTModes[mode] << 8);
  83. opcode |= ((vd & 0x10) << 18);
  84. opcode |= ((vd & 0xf) << 12);
  85. SingleOpcode(opcode, r0: 0x2500, r1: offset, sp: 0x2500);
  86. CompareAgainstUnicorn();
  87. }
  88. [Test, Pairwise, Description("VSTn.<size> <list>, [<Rn> {:<align>}]{ /!/, <Rm>} (single n element structure)")]
  89. public void Vstn_Single([Values(0u, 1u, 2u)] uint size,
  90. [Values(0u, 13u)] uint rn,
  91. [Values(1u, 13u, 15u)] uint rm,
  92. [Values(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u)] uint vd,
  93. [Range(0u, 7u)] uint index,
  94. [Range(0u, 3u)] uint n,
  95. [Values(0x0u)] [Random(0u, 0xffu, RndCntImm)] uint offset)
  96. {
  97. var data = GenerateVectorSequence(0x1000);
  98. SetWorkingMemory(0, data);
  99. (V128 vec1, V128 vec2, V128 vec3, V128 vec4) = GenerateTestVectors();
  100. uint opcode = 0xf4800000u; // VST1.8 {D0[0]}, [R0], R0
  101. opcode |= ((size & 3) << 10) | ((rn & 15) << 16) | (rm & 15);
  102. uint index_align = (index << (int)(1 + size)) & 15;
  103. opcode |= (index_align) << 4;
  104. opcode |= ((vd & 0x10) << 18);
  105. opcode |= ((vd & 0xf) << 12);
  106. opcode |= (n & 3) << 8; // ST1 is 0, ST2 is 1 etc.
  107. SingleOpcode(opcode, r0: 0x2500, r1: offset, v1: vec1, v2: vec2, v3: vec3, v4: vec4, sp: 0x2500);
  108. CompareAgainstUnicorn();
  109. }
  110. [Test, Pairwise, Description("VSTn.<size> <list>, [<Rn> {:<align>}]{ /!/, <Rm>} (multiple n element structures)")]
  111. public void Vstn_Pair([Values(0u, 1u, 2u, 3u)] uint size,
  112. [Values(0u, 13u)] uint rn,
  113. [Values(1u, 13u, 15u)] uint rm,
  114. [Values(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u)] uint vd,
  115. [Range(0u, 3u)] uint mode,
  116. [Values(0x0u)] [Random(0u, 0xffu, RndCntImm)] uint offset)
  117. {
  118. var data = GenerateVectorSequence(0x1000);
  119. SetWorkingMemory(0, data);
  120. (V128 vec1, V128 vec2, V128 vec3, V128 vec4) = GenerateTestVectors();
  121. uint opcode = 0xf4000000u; // VST4.8 {D0, D1, D2, D3}, [R0], R0
  122. opcode |= ((size & 3) << 6) | ((rn & 15) << 16) | (rm & 15) | (LDSTModes[mode] << 8);
  123. opcode |= ((vd & 0x10) << 18);
  124. opcode |= ((vd & 0xf) << 12);
  125. SingleOpcode(opcode, r0: 0x2500, r1: offset, v1: vec1, v2: vec2, v3: vec3, v4: vec4, sp: 0x2500);
  126. CompareAgainstUnicorn();
  127. }
  128. [Test, Pairwise, Description("VLDM.<size> <Rn>{!}, <d/sreglist>")]
  129. public void Vldm([Values(0u, 13u)] uint rn,
  130. [Values(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u)] uint vd,
  131. [Range(0u, 2u)] uint mode,
  132. [Values(0x1u, 0x32u)] [Random(2u, 31u, RndCntImm)] uint regs,
  133. [Values] bool single)
  134. {
  135. var data = GenerateVectorSequence(0x1000);
  136. SetWorkingMemory(0, data);
  137. uint opcode = 0xec100a00u; // VST4.8 {D0, D1, D2, D3}, [R0], R0
  138. uint[] vldmModes = {
  139. // Note: 3rd 0 leaves a space for "D".
  140. 0b0100, // Increment after.
  141. 0b0101, // Increment after. (!)
  142. 0b1001 // Decrement before. (!)
  143. };
  144. opcode |= ((vldmModes[mode] & 15) << 21);
  145. opcode |= ((rn & 15) << 16);
  146. opcode |= ((vd & 0x10) << 18);
  147. opcode |= ((vd & 0xf) << 12);
  148. opcode |= ((uint)(single ? 0 : 1) << 8);
  149. if (!single) regs = (regs << 1); // Low bit must be 0 - must be even number of registers.
  150. uint regSize = single ? 1u : 2u;
  151. if (vd + (regs / regSize) > 32) // Can't address further than S31 or D31.
  152. {
  153. regs -= (vd + (regs / regSize)) - 32;
  154. }
  155. if (regs / regSize > 16) // Can't do more than 16 registers at a time.
  156. {
  157. regs = 16 * regSize;
  158. }
  159. opcode |= regs & 0xff;
  160. SingleOpcode(opcode, r0: 0x2500, sp: 0x2500);
  161. CompareAgainstUnicorn();
  162. }
  163. [Test, Pairwise, Description("VLDR.<size> <Sd>, [<Rn> {, #{+/-}<imm>}]")]
  164. public void Vldr([Values(2u, 3u)] uint size, // FP16 is not supported for now
  165. [Values(0u)] uint rn,
  166. [Values(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u)] uint sd,
  167. [Values(0x0u)] [Random(0u, 0xffu, RndCntImm)] uint imm,
  168. [Values] bool sub)
  169. {
  170. var data = GenerateVectorSequence(0x1000);
  171. SetWorkingMemory(0, data);
  172. uint opcode = 0xed900a00u; // VLDR.32 S0, [R0, #0]
  173. opcode |= ((size & 3) << 8) | ((rn & 15) << 16);
  174. if (sub)
  175. {
  176. opcode &= ~(uint)(1 << 23);
  177. }
  178. if (size == 2)
  179. {
  180. opcode |= ((sd & 0x1) << 22);
  181. opcode |= ((sd & 0x1e) << 11);
  182. }
  183. else
  184. {
  185. opcode |= ((sd & 0x10) << 18);
  186. opcode |= ((sd & 0xf) << 12);
  187. }
  188. opcode |= imm & 0xff;
  189. SingleOpcode(opcode, r0: 0x2500);
  190. CompareAgainstUnicorn();
  191. }
  192. [Test, Pairwise, Description("VSTR.<size> <Sd>, [<Rn> {, #{+/-}<imm>}]")]
  193. public void Vstr([Values(2u, 3u)] uint size, // FP16 is not supported for now
  194. [Values(0u)] uint rn,
  195. [Values(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u)] uint sd,
  196. [Values(0x0u)] [Random(0u, 0xffu, RndCntImm)] uint imm,
  197. [Values] bool sub)
  198. {
  199. var data = GenerateVectorSequence(0x1000);
  200. SetWorkingMemory(0, data);
  201. uint opcode = 0xed800a00u; // VSTR.32 S0, [R0, #0]
  202. opcode |= ((size & 3) << 8) | ((rn & 15) << 16);
  203. if (sub)
  204. {
  205. opcode &= ~(uint)(1 << 23);
  206. }
  207. if (size == 2)
  208. {
  209. opcode |= ((sd & 0x1) << 22);
  210. opcode |= ((sd & 0x1e) << 11);
  211. }
  212. else
  213. {
  214. opcode |= ((sd & 0x10) << 18);
  215. opcode |= ((sd & 0xf) << 12);
  216. }
  217. opcode |= imm & 0xff;
  218. (V128 vec1, V128 vec2, _, _) = GenerateTestVectors();
  219. SingleOpcode(opcode, r0: 0x2500, v0: vec1, v1: vec2);
  220. CompareAgainstUnicorn();
  221. }
  222. private (V128, V128, V128, V128) GenerateTestVectors()
  223. {
  224. return (
  225. new V128(-12.43f, 1872.23f, 4456.23f, -5622.2f),
  226. new V128(0.0f, float.NaN, float.PositiveInfinity, float.NegativeInfinity),
  227. new V128(1.23e10f, -0.0f, -0.123f, 0.123f),
  228. new V128(float.Epsilon, 3.5f, 925.23f, -104.9f)
  229. );
  230. }
  231. private byte[] GenerateVectorSequence(int length)
  232. {
  233. int floatLength = length >> 2;
  234. float[] data = new float[floatLength];
  235. for (int i = 0; i < floatLength; i++)
  236. {
  237. data[i] = i + (i / 9f);
  238. }
  239. var result = new byte[length];
  240. Buffer.BlockCopy(data, 0, result, 0, result.Length);
  241. return result;
  242. }
  243. #endif
  244. }
  245. }