CpuTestSimdMemory32.cs 11 KB

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