simd_test.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright 2010-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/simd_t.h>
  7. #include <bx/math.h>
  8. #include <bx/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::isEqual(c.f[0], _0, 0.0001f) );
  146. CHECK(bx::isEqual(c.f[1], _1, 0.0001f) );
  147. CHECK(bx::isEqual(c.f[2], _2, 0.0001f) );
  148. CHECK(bx::isEqual(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::isEqual(c.f[0], _0, 0.0001f) );
  170. CHECK(bx::isEqual(c.f[1], _1, 0.0001f) );
  171. CHECK(bx::isEqual(c.f[2], _2, 0.0001f) );
  172. CHECK(bx::isEqual(c.f[3], _3, 0.0001f) );
  173. CHECK(bx::isEqual(c.f[4], _4, 0.0001f) );
  174. CHECK(bx::isEqual(c.f[5], _5, 0.0001f) );
  175. CHECK(bx::isEqual(c.f[6], _6, 0.0001f) );
  176. CHECK(bx::isEqual(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 == bx::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/inline/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("CDzw", simd_shuf_CDzw(xyzw, ABCD) );
  208. simd_check_string("zwCD", simd_shuf_zwCD(xyzw, ABCD) );
  209. simd_check_string("xAyB", simd_shuf_xAyB(xyzw, ABCD) );
  210. simd_check_string("AxBy", simd_shuf_AxBy(xyzw, ABCD) );
  211. simd_check_string("zCwD", simd_shuf_zCwD(xyzw, ABCD) );
  212. simd_check_string("CzDw", simd_shuf_CzDw(xyzw, ABCD) );
  213. simd_check_string("xAzC", simd_shuf_xAzC(xyzw, ABCD) );
  214. simd_check_string("yBwD", simd_shuf_yBwD(xyzw, ABCD) );
  215. }
  216. TEST_CASE("simd_compare", "")
  217. {
  218. simd_check_uint32("cmpeq"
  219. , simd_cmpeq(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  220. , 0, 0xffffffff, 0, 0
  221. );
  222. simd_check_uint32("cmplt"
  223. , simd_cmplt(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  224. , 0, 0, 0, 0
  225. );
  226. simd_check_uint32("cmple"
  227. , simd_cmple(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  228. , 0, 0xffffffff, 0, 0
  229. );
  230. simd_check_uint32("cmpgt"
  231. , simd_cmpgt(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  232. , 0xffffffff, 0, 0xffffffff, 0xffffffff
  233. );
  234. simd_check_uint32("cmpge"
  235. , simd_cmpge(simd_ld(1.0f, 2.0f, 3.0f, 4.0f), simd_ld(0.0f, 2.0f, 0.0f, 3.0f) )
  236. , 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
  237. );
  238. simd_check_uint32("icmpeq"
  239. , simd_icmpeq(simd_ild(0, 1, 2, 3), simd_ild(0, uint32_t(-2), 1, 3) )
  240. , 0xffffffff, 0, 0, 0xffffffff
  241. );
  242. simd_check_uint32("icmplt"
  243. , simd_icmplt(simd_ild(0, 1, 2, 3), simd_ild(0, uint32_t(-2), 1, 3) )
  244. , 0, 0, 0, 0
  245. );
  246. simd_check_uint32("icmpgt"
  247. , simd_icmpgt(simd_ild(0, 1, 2, 3), simd_ild(0, uint32_t(-2), 1, 3) )
  248. , 0, 0xffffffff, 0xffffffff, 0
  249. );
  250. }
  251. TEST_CASE("simd_test", "")
  252. {
  253. simd_check_bool("test_any_xyzw"
  254. , simd_test_any_xyzw(simd_ild(0xffffffff, 0, 0, 0) )
  255. , true
  256. );
  257. simd_check_bool("test_all_xyzw"
  258. , simd_test_all_xyzw(simd_ild(0xffffffff, 0, 0xffffffff, 0) )
  259. , false
  260. );
  261. simd_check_bool("test_all_xyzw"
  262. , simd_test_all_xyzw(simd_ild(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff) )
  263. , true
  264. );
  265. simd_check_bool("test_all_xw"
  266. , simd_test_all_xw(simd_ild(0xffffffff, 0, 0, 0xffffffff) )
  267. , true
  268. );
  269. simd_check_bool("test_all_xzw"
  270. , simd_test_all_xzw(simd_ild(0xffffffff, 0, 0, 0xffffffff) )
  271. , false
  272. );
  273. }
  274. TEST_CASE("simd_load", "")
  275. {
  276. simd_check_float("ld"
  277. , simd_ld(0.0f, 1.0f, 2.0f, 3.0f)
  278. , 0.0f, 1.0f, 2.0f, 3.0f
  279. );
  280. simd_check_float("ld"
  281. , simd_ld<simd256_t>(0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f)
  282. , 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f
  283. );
  284. simd_check_int32("ild"
  285. , simd_ild(uint32_t(-1), 0, 1, 2)
  286. , uint32_t(-1), 0, 1, 2
  287. );
  288. simd_check_int32("ild"
  289. , simd_ild<simd256_t>(uint32_t(-1), 0, 1, 2, 3, 4, 5, 6)
  290. , uint32_t(-1), 0, 1, 2, 3, 4, 5, 6
  291. );
  292. simd_check_int32("ild"
  293. , simd_ild(uint32_t(-1), uint32_t(-2), uint32_t(-3), uint32_t(-4) )
  294. , uint32_t(-1), uint32_t(-2), uint32_t(-3), uint32_t(-4)
  295. );
  296. simd_check_uint32("zero", simd_zero()
  297. , 0, 0, 0, 0
  298. );
  299. simd_check_uint32("isplat", simd_isplat<simd128_t>(0x80000001)
  300. , 0x80000001, 0x80000001, 0x80000001, 0x80000001
  301. );
  302. simd_check_float("splat", simd_splat<simd128_t>(1.0f)
  303. , 1.0f, 1.0f, 1.0f, 1.0f
  304. );
  305. simd_check_uint32("isplat", simd_isplat<simd256_t>(0x80000001)
  306. , 0x80000001, 0x80000001, 0x80000001, 0x80000001, 0x80000001, 0x80000001, 0x80000001, 0x80000001
  307. );
  308. simd_check_float("splat", simd_splat<simd256_t>(1.0f)
  309. , 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f
  310. );
  311. }
  312. TEST_CASE("simd_arithmetic", "")
  313. {
  314. simd_check_float("madd"
  315. , 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) )
  316. , 8.0f, 14.0f, 22.0f, 32.0f
  317. );
  318. simd_check_float("cross3"
  319. , simd_cross3(simd_ld(1.0f, 0.0f, 0.0f, 0.0f), simd_ld(0.0f, 1.0f, 0.0f, 0.0f) )
  320. , 0.0f, 0.0f, 1.0f, 0.0f
  321. );
  322. }
  323. TEST_CASE("simd_sqrt", "")
  324. {
  325. simd_check_float("simd_sqrt"
  326. , simd_sqrt(simd_ld(1.0f, 16.0f, 65536.0f, 123456.0f) )
  327. , 1.0f, 4.0f, 256.0f, 351.363060096f
  328. );
  329. simd_check_float("simd_sqrt_nr_ni"
  330. , simd_sqrt_nr_ni(simd_ld(1.0f, 16.0f, 65536.0f, 123456.0f) )
  331. , 1.0f, 4.0f, 256.0f, 351.363060096f
  332. );
  333. simd_check_float("simd_sqrt_nr1_ni"
  334. , simd_sqrt_nr1_ni(simd_ld(1.0f, 16.0f, 65536.0f, 123456.0f) )
  335. , 1.0f, 4.0f, 256.0f, 351.363060096f
  336. );
  337. }
  338. TEST_CASE("simd", "")
  339. {
  340. const simd128_t isplat = simd_isplat(0x80000001);
  341. simd_check_uint32("sll"
  342. , simd_sll(isplat, 1)
  343. , 0x00000002, 0x00000002, 0x00000002, 0x00000002
  344. );
  345. simd_check_uint32("srl"
  346. , simd_srl(isplat, 1)
  347. , 0x40000000, 0x40000000, 0x40000000, 0x40000000
  348. );
  349. simd_check_uint32("sra"
  350. , simd_sra(isplat, 1)
  351. , 0xc0000000, 0xc0000000, 0xc0000000, 0xc0000000
  352. );
  353. simd_check_uint32("and"
  354. , simd_and(simd_isplat(0x55555555), simd_isplat(0xaaaaaaaa) )
  355. , 0, 0, 0, 0
  356. );
  357. simd_check_uint32("or "
  358. , simd_or(simd_isplat(0x55555555), simd_isplat(0xaaaaaaaa) )
  359. , uint32_t(-1), uint32_t(-1), uint32_t(-1), uint32_t(-1)
  360. );
  361. simd_check_uint32("xor"
  362. , simd_or(simd_isplat(0x55555555), simd_isplat(0xaaaaaaaa) )
  363. , uint32_t(-1), uint32_t(-1), uint32_t(-1), uint32_t(-1)
  364. );
  365. simd_check_int32("imin"
  366. , simd_imin(simd_ild(0, 1, 2, 3), simd_ild(uint32_t(-1), 2, uint32_t(-2), 1) )
  367. , uint32_t(-1), 1, uint32_t(-2), 1
  368. );
  369. simd_check_float("min"
  370. , simd_min(simd_ld(0.0f, 1.0f, 2.0f, 3.0f), simd_ld(-1.0f, 2.0f, -2.0f, 1.0f) )
  371. , -1.0f, 1.0f, -2.0f, 1.0f
  372. );
  373. simd_check_int32("imax"
  374. , simd_imax(simd_ild(0, 1, 2, 3), simd_ild(uint32_t(-1), 2, uint32_t(-2), 1) )
  375. , 0, 2, 2, 3
  376. );
  377. simd_check_float("max"
  378. , simd_max(simd_ld(0.0f, 1.0f, 2.0f, 3.0f), simd_ld(-1.0f, 2.0f, -2.0f, 1.0f) )
  379. , 0.0f, 2.0f, 2.0f, 3.0f
  380. );
  381. }