simd_test.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/simd_t.h>
  7. #include <bx/fpumath.h>
  8. #include <string.h>
  9. #if 0
  10. # define SIMD_DBG DBG
  11. #else
  12. # define SIMD_DBG unused
  13. #endif // 0
  14. using namespace bx;
  15. inline void unused(...) {}
  16. union simd_cast
  17. {
  18. bx::simd256_t simd256;
  19. bx::simd128_t simd128;
  20. float f[8];
  21. uint32_t ui[8];
  22. int32_t i[8];
  23. char c[32];
  24. };
  25. void simd_check_bool(const char* _str, bool _a, bool _0)
  26. {
  27. SIMD_DBG("%s %d == %d"
  28. , _str
  29. , _a
  30. , _0
  31. );
  32. REQUIRE(_a == _0);
  33. }
  34. void simd_check_int32(
  35. const char* _str
  36. , bx::simd128_t _a
  37. , int32_t _0
  38. , int32_t _1
  39. , int32_t _2
  40. , int32_t _3
  41. )
  42. {
  43. simd_cast c; c.simd128 = _a;
  44. SIMD_DBG("%s (%d, %d, %d, %d) == (%d, %d, %d, %d)"
  45. , _str
  46. , c.i[0], c.i[1], c.i[2], c.i[3]
  47. , _0, _1, _2, _3
  48. );
  49. REQUIRE(c.i[0] == _0);
  50. REQUIRE(c.i[1] == _1);
  51. REQUIRE(c.i[2] == _2);
  52. REQUIRE(c.i[3] == _3);
  53. }
  54. void simd_check_int32(
  55. const char* _str
  56. , bx::simd256_t _a
  57. , int32_t _0
  58. , int32_t _1
  59. , int32_t _2
  60. , int32_t _3
  61. , int32_t _4
  62. , int32_t _5
  63. , int32_t _6
  64. , int32_t _7
  65. )
  66. {
  67. simd_cast c; c.simd256 = _a;
  68. SIMD_DBG("%s (%d, %d, %d, %d, %d, %d, %d, %d) == (%d, %d, %d, %d, %d, %d, %d, %d)"
  69. , _str
  70. , c.i[0], c.i[1], c.i[2], c.i[3], c.i[4], c.i[5], c.i[6], c.i[7]
  71. , _0, _1, _2, _3, _4, _5, _6, _7
  72. );
  73. REQUIRE(c.i[0] == _0);
  74. REQUIRE(c.i[1] == _1);
  75. REQUIRE(c.i[2] == _2);
  76. REQUIRE(c.i[3] == _3);
  77. REQUIRE(c.i[4] == _4);
  78. REQUIRE(c.i[5] == _5);
  79. REQUIRE(c.i[6] == _6);
  80. REQUIRE(c.i[7] == _7);
  81. }
  82. void simd_check_uint32(
  83. const char* _str
  84. , bx::simd128_t _a
  85. , uint32_t _0
  86. , uint32_t _1
  87. , uint32_t _2
  88. , uint32_t _3
  89. )
  90. {
  91. simd_cast c; c.simd128 = _a;
  92. SIMD_DBG("%s (0x%08x, 0x%08x, 0x%08x, 0x%08x) == (0x%08x, 0x%08x, 0x%08x, 0x%08x)"
  93. , _str
  94. , c.ui[0], c.ui[1], c.ui[2], c.ui[3]
  95. , _0, _1, _2, _3
  96. );
  97. REQUIRE(c.ui[0] == _0);
  98. REQUIRE(c.ui[1] == _1);
  99. REQUIRE(c.ui[2] == _2);
  100. REQUIRE(c.ui[3] == _3);
  101. }
  102. void simd_check_uint32(
  103. const char* _str
  104. , bx::simd256_t _a
  105. , uint32_t _0
  106. , uint32_t _1
  107. , uint32_t _2
  108. , uint32_t _3
  109. , uint32_t _4
  110. , uint32_t _5
  111. , uint32_t _6
  112. , uint32_t _7
  113. )
  114. {
  115. simd_cast c; c.simd256 = _a;
  116. SIMD_DBG("%s (0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x) == (0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x)"
  117. , _str
  118. , c.ui[0], c.ui[1], c.ui[2], c.ui[3], c.ui[4], c.ui[5], c.ui[6], c.ui[7]
  119. , _0, _1, _2, _3, _4, _5, _6, _7
  120. );
  121. REQUIRE(c.ui[0] == _0);
  122. REQUIRE(c.ui[1] == _1);
  123. REQUIRE(c.ui[2] == _2);
  124. REQUIRE(c.ui[3] == _3);
  125. REQUIRE(c.ui[4] == _4);
  126. REQUIRE(c.ui[5] == _5);
  127. REQUIRE(c.ui[6] == _6);
  128. REQUIRE(c.ui[7] == _7);
  129. }
  130. void simd_check_float(
  131. const char* _str
  132. , bx::simd128_t _a
  133. , float _0
  134. , float _1
  135. , float _2
  136. , float _3
  137. )
  138. {
  139. simd_cast c; c.simd128 = _a;
  140. SIMD_DBG("%s (%f, %f, %f, %f) == (%f, %f, %f, %f)"
  141. , _str
  142. , c.f[0], c.f[1], c.f[2], c.f[3]
  143. , _0, _1, _2, _3
  144. );
  145. CHECK(bx::fequal(c.f[0], _0, 0.0001f) );
  146. CHECK(bx::fequal(c.f[1], _1, 0.0001f) );
  147. CHECK(bx::fequal(c.f[2], _2, 0.0001f) );
  148. CHECK(bx::fequal(c.f[3], _3, 0.0001f) );
  149. }
  150. void simd_check_float(
  151. const char* _str
  152. , bx::simd256_t _a
  153. , float _0
  154. , float _1
  155. , float _2
  156. , float _3
  157. , float _4
  158. , float _5
  159. , float _6
  160. , float _7
  161. )
  162. {
  163. simd_cast c; c.simd256 = _a;
  164. SIMD_DBG("%s (%f, %f, %f, %f, %f, %f, %f, %f) == (%f, %f, %f, %f, %f, %f, %f, %f)"
  165. , _str
  166. , c.f[0], c.f[1], c.f[2], c.f[3], c.f[4], c.f[5], c.f[6], c.f[7]
  167. , _0, _1, _2, _3, _4, _5, _6, _7
  168. );
  169. CHECK(bx::fequal(c.f[0], _0, 0.0001f) );
  170. CHECK(bx::fequal(c.f[1], _1, 0.0001f) );
  171. CHECK(bx::fequal(c.f[2], _2, 0.0001f) );
  172. CHECK(bx::fequal(c.f[3], _3, 0.0001f) );
  173. CHECK(bx::fequal(c.f[4], _4, 0.0001f) );
  174. CHECK(bx::fequal(c.f[5], _5, 0.0001f) );
  175. CHECK(bx::fequal(c.f[6], _6, 0.0001f) );
  176. CHECK(bx::fequal(c.f[7], _7, 0.0001f) );
  177. }
  178. void simd_check_string(const char* _str, bx::simd128_t _a)
  179. {
  180. simd_cast c; c.simd128 = _a;
  181. const char test[5] = { c.c[0], c.c[4], c.c[8], c.c[12], '\0' };
  182. SIMD_DBG("%s %s", _str, test);
  183. CHECK(0 == strcmp(_str, test) );
  184. }
  185. TEST_CASE("simd_swizzle", "")
  186. {
  187. const simd128_t xyzw = simd_ild(0x78787878, 0x79797979, 0x7a7a7a7a, 0x77777777);
  188. #define ELEMx 0
  189. #define ELEMy 1
  190. #define ELEMz 2
  191. #define ELEMw 3
  192. #define BX_SIMD128_IMPLEMENT_SWIZZLE(_x, _y, _z, _w) \
  193. simd_check_string("" #_x #_y #_z #_w "", simd_swiz_##_x##_y##_z##_w(xyzw) ); \
  194. #include <bx/simd128_swizzle.inl>
  195. #undef BX_SIMD128_IMPLEMENT_SWIZZLE
  196. #undef ELEMw
  197. #undef ELEMz
  198. #undef ELEMy
  199. #undef ELEMx
  200. }
  201. TEST_CASE("simd_shuffle", "")
  202. {
  203. const simd128_t xyzw = simd_ild(0x78787878, 0x79797979, 0x7a7a7a7a, 0x77777777);
  204. const simd128_t ABCD = simd_ild(0x41414141, 0x42424242, 0x43434343, 0x44444444);
  205. simd_check_string("xyAB", simd_shuf_xyAB(xyzw, ABCD) );
  206. simd_check_string("ABxy", simd_shuf_ABxy(xyzw, ABCD) );
  207. simd_check_string("zwCD", simd_shuf_zwCD(xyzw, ABCD) );
  208. simd_check_string("CDzw", simd_shuf_CDzw(xyzw, ABCD) );
  209. simd_check_string("xAyB", simd_shuf_xAyB(xyzw, ABCD) );
  210. simd_check_string("zCwD", simd_shuf_zCwD(xyzw, ABCD) );
  211. simd_check_string("xAzC", simd_shuf_xAzC(xyzw, ABCD) );
  212. simd_check_string("yBwD", simd_shuf_yBwD(xyzw, ABCD) );
  213. simd_check_string("CzDw", simd_shuf_CzDw(xyzw, ABCD) );
  214. }
  215. TEST_CASE("simd_compare", "")
  216. {
  217. simd_check_uint32("cmpeq"
  218. , simd_cmpeq(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  219. , 0, 0xffffffff, 0, 0
  220. );
  221. simd_check_uint32("cmplt"
  222. , simd_cmplt(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  223. , 0, 0, 0, 0
  224. );
  225. simd_check_uint32("cmple"
  226. , simd_cmple(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  227. , 0, 0xffffffff, 0, 0
  228. );
  229. simd_check_uint32("cmpgt"
  230. , simd_cmpgt(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  231. , 0xffffffff, 0, 0xffffffff, 0xffffffff
  232. );
  233. simd_check_uint32("cmpge"
  234. , simd_cmpge(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  235. , 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
  236. );
  237. simd_check_uint32("icmpeq"
  238. , simd_icmpeq(simd_ild(0, 1, 2, 3), simd_ild(0, uint32_t(-2), 1, 3) )
  239. , 0xffffffff, 0, 0, 0xffffffff
  240. );
  241. simd_check_uint32("icmplt"
  242. , simd_icmplt(simd_ild(0, 1, 2, 3), simd_ild(0, uint32_t(-2), 1, 3) )
  243. , 0, 0, 0, 0
  244. );
  245. simd_check_uint32("icmpgt"
  246. , simd_icmpgt(simd_ild(0, 1, 2, 3), simd_ild(0, uint32_t(-2), 1, 3) )
  247. , 0, 0xffffffff, 0xffffffff, 0
  248. );
  249. }
  250. TEST_CASE("simd_test", "")
  251. {
  252. simd_check_bool("test_any_xyzw"
  253. , simd_test_any_xyzw(simd_ild(0xffffffff, 0, 0, 0) )
  254. , true
  255. );
  256. simd_check_bool("test_all_xyzw"
  257. , simd_test_all_xyzw(simd_ild(0xffffffff, 0, 0xffffffff, 0) )
  258. , false
  259. );
  260. simd_check_bool("test_all_xyzw"
  261. , simd_test_all_xyzw(simd_ild(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff) )
  262. , true
  263. );
  264. simd_check_bool("test_all_xw"
  265. , simd_test_all_xw(simd_ild(0xffffffff, 0, 0, 0xffffffff) )
  266. , true
  267. );
  268. simd_check_bool("test_all_xzw"
  269. , simd_test_all_xzw(simd_ild(0xffffffff, 0, 0, 0xffffffff) )
  270. , false
  271. );
  272. }
  273. TEST_CASE("simd_load", "")
  274. {
  275. simd_check_float("ld"
  276. , simd_ld(0.0f, 1.0f, 2.0f, 3.0f)
  277. , 0.0f, 1.0f, 2.0f, 3.0f
  278. );
  279. simd_check_float("ld"
  280. , simd_ld<simd256_t>(0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f)
  281. , 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f
  282. );
  283. simd_check_int32("ild"
  284. , simd_ild(uint32_t(-1), 0, 1, 2)
  285. , uint32_t(-1), 0, 1, 2
  286. );
  287. simd_check_int32("ild"
  288. , simd_ild<simd256_t>(uint32_t(-1), 0, 1, 2, 3, 4, 5, 6)
  289. , uint32_t(-1), 0, 1, 2, 3, 4, 5, 6
  290. );
  291. simd_check_int32("ild"
  292. , simd_ild(uint32_t(-1), uint32_t(-2), uint32_t(-3), uint32_t(-4) )
  293. , uint32_t(-1), uint32_t(-2), uint32_t(-3), uint32_t(-4)
  294. );
  295. simd_check_uint32("zero", simd_zero()
  296. , 0, 0, 0, 0
  297. );
  298. simd_check_uint32("isplat", simd_isplat<simd128_t>(0x80000001)
  299. , 0x80000001, 0x80000001, 0x80000001, 0x80000001
  300. );
  301. simd_check_float("splat", simd_splat<simd128_t>(1.0f)
  302. , 1.0f, 1.0f, 1.0f, 1.0f
  303. );
  304. simd_check_uint32("isplat", simd_isplat<simd256_t>(0x80000001)
  305. , 0x80000001, 0x80000001, 0x80000001, 0x80000001, 0x80000001, 0x80000001, 0x80000001, 0x80000001
  306. );
  307. simd_check_float("splat", simd_splat<simd256_t>(1.0f)
  308. , 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f
  309. );
  310. }
  311. TEST_CASE("simd_arithmetic", "")
  312. {
  313. simd_check_float("madd"
  314. , simd_madd(simd_ld(0.0f, 1.0f, 2.0f, 3.0f), simd_ld(4.0f, 5.0f, 6.0f, 7.0f), simd_ld(8.0f, 9.0f, 10.0f, 11.0f) )
  315. , 8.0f, 14.0f, 22.0f, 32.0f
  316. );
  317. simd_check_float("cross3"
  318. , simd_cross3(simd_ld(1.0f, 0.0f, 0.0f, 0.0f), simd_ld(0.0f, 1.0f, 0.0f, 0.0f) )
  319. , 0.0f, 0.0f, 1.0f, 0.0f
  320. );
  321. }
  322. TEST_CASE("simd_sqrt", "")
  323. {
  324. simd_check_float("simd_sqrt"
  325. , simd_sqrt(simd_ld(1.0f, 16.0f, 65536.0f, 123456.0f) )
  326. , 1.0f, 4.0f, 256.0f, 351.363060096f
  327. );
  328. simd_check_float("simd_sqrt_nr_ni"
  329. , simd_sqrt_nr_ni(simd_ld(1.0f, 16.0f, 65536.0f, 123456.0f) )
  330. , 1.0f, 4.0f, 256.0f, 351.363060096f
  331. );
  332. simd_check_float("simd_sqrt_nr1_ni"
  333. , simd_sqrt_nr1_ni(simd_ld(1.0f, 16.0f, 65536.0f, 123456.0f) )
  334. , 1.0f, 4.0f, 256.0f, 351.363060096f
  335. );
  336. }
  337. TEST_CASE("simd", "")
  338. {
  339. const simd128_t isplat = simd_isplat(0x80000001);
  340. simd_check_uint32("sll"
  341. , simd_sll(isplat, 1)
  342. , 0x00000002, 0x00000002, 0x00000002, 0x00000002
  343. );
  344. simd_check_uint32("srl"
  345. , simd_srl(isplat, 1)
  346. , 0x40000000, 0x40000000, 0x40000000, 0x40000000
  347. );
  348. simd_check_uint32("sra"
  349. , simd_sra(isplat, 1)
  350. , 0xc0000000, 0xc0000000, 0xc0000000, 0xc0000000
  351. );
  352. simd_check_uint32("and"
  353. , simd_and(simd_isplat(0x55555555), simd_isplat(0xaaaaaaaa) )
  354. , 0, 0, 0, 0
  355. );
  356. simd_check_uint32("or "
  357. , simd_or(simd_isplat(0x55555555), simd_isplat(0xaaaaaaaa) )
  358. , uint32_t(-1), uint32_t(-1), uint32_t(-1), uint32_t(-1)
  359. );
  360. simd_check_uint32("xor"
  361. , simd_or(simd_isplat(0x55555555), simd_isplat(0xaaaaaaaa) )
  362. , uint32_t(-1), uint32_t(-1), uint32_t(-1), uint32_t(-1)
  363. );
  364. simd_check_int32("imin"
  365. , simd_imin(simd_ild(0, 1, 2, 3), simd_ild(uint32_t(-1), 2, uint32_t(-2), 1) )
  366. , uint32_t(-1), 1, uint32_t(-2), 1
  367. );
  368. simd_check_float("min"
  369. , simd_min(simd_ld(0.0f, 1.0f, 2.0f, 3.0f), simd_ld(-1.0f, 2.0f, -2.0f, 1.0f) )
  370. , -1.0f, 1.0f, -2.0f, 1.0f
  371. );
  372. simd_check_int32("imax"
  373. , simd_imax(simd_ild(0, 1, 2, 3), simd_ild(uint32_t(-1), 2, uint32_t(-2), 1) )
  374. , 0, 2, 2, 3
  375. );
  376. simd_check_float("max"
  377. , simd_max(simd_ld(0.0f, 1.0f, 2.0f, 3.0f), simd_ld(-1.0f, 2.0f, -2.0f, 1.0f) )
  378. , 0.0f, 2.0f, 2.0f, 3.0f
  379. );
  380. }