testautomation_intrinsics.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /**
  2. * Intrinsics test suite
  3. */
  4. /* Disable intrinsics that are unsupported by the current compiler */
  5. #include <build_config/SDL_build_config.h>
  6. #include <SDL3/SDL.h>
  7. #include <SDL3/SDL_intrin.h>
  8. #include <SDL3/SDL_test.h>
  9. #include "testautomation_suites.h"
  10. // FIXME: missing tests for loongarch lsx/lasx
  11. // FIXME: missing tests for powerpc altivec
  12. /* ================= Test Case Implementation ================== */
  13. /* Helper functions */
  14. static int allocate_random_int_arrays(Sint32 **dest, Sint32 **a, Sint32 **b, size_t *size) {
  15. size_t i;
  16. *size = (size_t)SDLTest_RandomIntegerInRange(127, 999);
  17. *dest = SDL_malloc(sizeof(Sint32) * *size);
  18. *a = SDL_malloc(sizeof(Sint32) * *size);
  19. *b = SDL_malloc(sizeof(Sint32) * *size);
  20. if (!*dest || !*a || !*b) {
  21. SDLTest_AssertCheck(SDL_FALSE, "SDL_malloc failed");
  22. return -1;
  23. }
  24. for (i = 0; i < *size; ++i) {
  25. (*a)[i] = SDLTest_RandomSint32();
  26. (*b)[i] = SDLTest_RandomSint32();
  27. }
  28. return 0;
  29. }
  30. static int allocate_random_float_arrays(float **dest, float **a, float **b, size_t *size) {
  31. size_t i;
  32. *size = (size_t)SDLTest_RandomIntegerInRange(127, 999);
  33. *dest = SDL_malloc(sizeof(float) * *size);
  34. *a = SDL_malloc(sizeof(float) * *size);
  35. *b = SDL_malloc(sizeof(float) * *size);
  36. if (!*dest || !*a || !*b) {
  37. SDLTest_AssertCheck(SDL_FALSE, "SDL_malloc failed");
  38. return -1;
  39. }
  40. for (i = 0; i < *size; ++i) {
  41. (*a)[i] = SDLTest_RandomUnitFloat();
  42. (*b)[i] = SDLTest_RandomUnitFloat();
  43. }
  44. return 0;
  45. }
  46. static int allocate_random_double_arrays(double **dest, double **a, double **b, size_t *size) {
  47. size_t i;
  48. *size = (size_t)SDLTest_RandomIntegerInRange(127, 999);
  49. *dest = SDL_malloc(sizeof(double) * *size);
  50. *a = SDL_malloc(sizeof(double) * *size);
  51. *b = SDL_malloc(sizeof(double) * *size);
  52. if (!*dest || !*a || !*b) {
  53. SDLTest_AssertCheck(SDL_FALSE, "SDL_malloc failed");
  54. return -1;
  55. }
  56. for (i = 0; i < *size; ++i) {
  57. (*a)[i] = SDLTest_RandomUnitDouble();
  58. (*b)[i] = SDLTest_RandomUnitDouble();
  59. }
  60. return 0;
  61. }
  62. static void free_arrays(void *dest, void *a, void *b) {
  63. SDL_free(dest);
  64. SDL_free(a);
  65. SDL_free(b);
  66. }
  67. /**
  68. * \brief Verify element-wise addition of 2 int arrays.
  69. */
  70. static void verify_ints_addition(const Sint32 *dest, const Sint32 *a, const Sint32 *b, size_t size, const char *desc) {
  71. size_t i;
  72. int all_good = 1;
  73. for (i = 0; i < size; ++i) {
  74. Sint32 expected = a[i] + b[i];
  75. if (dest[i] != expected) {
  76. SDLTest_AssertCheck(SDL_FALSE, "%"SDL_PRIs32" + %"SDL_PRIs32" = %"SDL_PRIs32", expected %"SDL_PRIs32" ([%"SDL_PRIu32"/%"SDL_PRIu32"] %s)",
  77. a[i], b[i], dest[i], expected, (Uint32)i, (Uint32)size, desc);
  78. all_good = 0;
  79. }
  80. }
  81. if (all_good) {
  82. SDLTest_AssertCheck(SDL_TRUE, "All int additions were correct (%s)", desc);
  83. }
  84. }
  85. /**
  86. * \brief Verify element-wise multiplication of 2 int arrays.
  87. */
  88. static void verify_ints_multiplication(const Sint32 *dest, const Sint32 *a, const Sint32 *b, size_t size, const char *desc) {
  89. size_t i;
  90. int all_good = 1;
  91. for (i = 0; i < size; ++i) {
  92. Sint32 expected = a[i] * b[i];
  93. if (dest[i] != expected) {
  94. SDLTest_AssertCheck(SDL_FALSE, "%"SDL_PRIs32" * %"SDL_PRIs32" = %"SDL_PRIs32", expected %"SDL_PRIs32" ([%"SDL_PRIu32"/%"SDL_PRIu32"] %s)",
  95. a[i], b[i], dest[i], expected, (Uint32)i, (Uint32)size, desc);
  96. all_good = 0;
  97. }
  98. }
  99. if (all_good) {
  100. SDLTest_AssertCheck(SDL_TRUE, "All int multiplication were correct (%s)", desc);
  101. }
  102. }
  103. /**
  104. * \brief Verify element-wise addition of 2 float arrays.
  105. */
  106. static void verify_floats_addition(const float *dest, const float *a, const float *b, size_t size, const char *desc) {
  107. size_t i;
  108. int all_good = 1;
  109. for (i = 0; i < size; ++i) {
  110. float expected = a[i] + b[i];
  111. float abs_error = SDL_fabsf(dest[i] - expected);
  112. if (abs_error > 1.0e-5f) {
  113. SDLTest_AssertCheck(SDL_FALSE, "%g + %g = %g, expected %g (error = %g) ([%"SDL_PRIu32"/%"SDL_PRIu32"] %s)",
  114. a[i], b[i], dest[i], expected, abs_error, (Uint32) i, (Uint32) size, desc);
  115. all_good = 0;
  116. }
  117. }
  118. if (all_good) {
  119. SDLTest_AssertCheck(SDL_TRUE, "All float additions were correct (%s)", desc);
  120. }
  121. }
  122. /**
  123. * \brief Verify element-wise addition of 2 double arrays.
  124. */
  125. static void verify_doubles_addition(const double *dest, const double *a, const double *b, size_t size, const char *desc) {
  126. size_t i;
  127. int all_good = 1;
  128. for (i = 0; i < size; ++i) {
  129. double expected = a[i] + b[i];
  130. double abs_error = SDL_fabs(dest[i] - expected);
  131. if (abs_error > 1.0e-5) {
  132. SDLTest_AssertCheck(abs_error < 1.0e-5f, "%g + %g = %g, expected %g (error = %g) ([%"SDL_PRIu32"/%"SDL_PRIu32"] %s)",
  133. a[i], b[i], dest[i], expected, abs_error, (Uint32) i, (Uint32) size, desc);
  134. all_good = SDL_FALSE;
  135. }
  136. }
  137. if (all_good) {
  138. SDLTest_AssertCheck(SDL_TRUE, "All double additions were correct (%s)", desc);
  139. }
  140. }
  141. /* Intrinsic kernels */
  142. static void kernel_ints_add_cpu(Sint32 *dest, const Sint32 *a, const Sint32 *b, size_t size) {
  143. for (; size; --size, ++dest, ++a, ++b) {
  144. *dest = *a + *b;
  145. }
  146. }
  147. static void kernel_ints_mul_cpu(Sint32 *dest, const Sint32 *a, const Sint32 *b, size_t size) {
  148. for (; size; --size, ++dest, ++a, ++b) {
  149. *dest = *a * *b;
  150. }
  151. }
  152. static void kernel_floats_add_cpu(float *dest, const float *a, const float *b, size_t size) {
  153. for (; size; --size, ++dest, ++a, ++b) {
  154. *dest = *a + *b;
  155. }
  156. }
  157. static void kernel_doubles_add_cpu(double *dest, const double *a, const double *b, size_t size) {
  158. for (; size; --size, ++dest, ++a, ++b) {
  159. *dest = *a + *b;
  160. }
  161. }
  162. #if SDL_MMX_INTRINSICS
  163. SDL_TARGETING("mmx") static void kernel_ints_add_mmx(Sint32 *dest, const Sint32 *a, const Sint32 *b, size_t size) {
  164. for (; size >= 2; size -= 2, dest += 2, a += 2, b += 2) {
  165. *(__m64*)dest = _mm_add_pi32(*(__m64*)a, *(__m64*)b);
  166. }
  167. if (size) {
  168. *dest = *a + *b;
  169. }
  170. _mm_empty();
  171. }
  172. #endif
  173. #if SDL_SSE_INTRINSICS
  174. SDL_TARGETING("sse") static void kernel_floats_add_sse(float *dest, const float *a, const float *b, size_t size) {
  175. for (; size >= 4; size -= 4, dest += 4, a += 4, b += 4) {
  176. _mm_storeu_ps(dest, _mm_add_ps(_mm_loadu_ps(a), _mm_loadu_ps (b)));
  177. }
  178. for (; size; size--, ++dest, ++a, ++b) {
  179. *dest = *a + *b;
  180. }
  181. }
  182. #endif
  183. #if SDL_SSE2_INTRINSICS
  184. SDL_TARGETING("sse2") static void kernel_doubles_add_sse2(double *dest, const double *a, const double *b, size_t size) {
  185. for (; size >= 2; size -= 2, dest += 2, a += 2, b += 2) {
  186. _mm_storeu_pd(dest, _mm_add_pd(_mm_loadu_pd(a), _mm_loadu_pd(b)));
  187. }
  188. if (size) {
  189. *dest = *a + *b;
  190. }
  191. }
  192. #endif
  193. #if SDL_SSE3_INTRINSICS
  194. SDL_TARGETING("sse3") static void kernel_ints_add_sse3(Sint32 *dest, const Sint32 *a, const Sint32 *b, size_t size) {
  195. for (; size >= 4; size -= 4, dest += 4, a += 4, b += 4) {
  196. _mm_storeu_si128((__m128i*)dest, _mm_add_epi32(_mm_lddqu_si128((__m128i*)a), _mm_lddqu_si128((__m128i*)b)));
  197. }
  198. for (;size; --size, ++dest, ++a, ++b) {
  199. *dest = *a + *b;
  200. }
  201. }
  202. #endif
  203. #if SDL_SSE4_1_INTRINSICS
  204. SDL_TARGETING("sse4.1") static void kernel_ints_mul_sse4_1(Sint32 *dest, const Sint32 *a, const Sint32 *b, size_t size) {
  205. for (; size >= 4; size -= 4, dest += 4, a += 4, b += 4) {
  206. _mm_storeu_si128((__m128i*)dest, _mm_mullo_epi32(_mm_lddqu_si128((__m128i*)a), _mm_lddqu_si128((__m128i*)b)));
  207. }
  208. for (;size; --size, ++dest, ++a, ++b) {
  209. *dest = *a * *b;
  210. }
  211. }
  212. #endif
  213. #if SDL_SSE4_2_INTRINSICS
  214. SDL_TARGETING("sse4.2") static Uint32 calculate_crc32c_sse4_2(const char *text) {
  215. Uint32 crc32c = ~0;
  216. size_t len = SDL_strlen(text);
  217. #if defined(__x86_64__) || defined(_M_X64)
  218. for (; len >= 8; len -= 8, text += 8) {
  219. crc32c = (Uint32)_mm_crc32_u64(crc32c, *(Sint64*)text);
  220. }
  221. if (len >= 4) {
  222. crc32c = (Uint32)_mm_crc32_u32(crc32c, *(Sint32*)text);
  223. len -= 4;
  224. text += 4;
  225. }
  226. #else
  227. for (; len >= 4; len -= 4, text += 4) {
  228. crc32c = (Uint32)_mm_crc32_u32(crc32c, *(Sint32*)text);
  229. }
  230. #endif
  231. if (len >= 2) {
  232. crc32c = (Uint32)_mm_crc32_u16(crc32c, *(Sint16*)text);
  233. len -= 2;
  234. text += 2;
  235. }
  236. if (len) {
  237. crc32c = (Uint32)_mm_crc32_u8(crc32c, *text);
  238. }
  239. return ~crc32c;
  240. }
  241. #endif
  242. #if SDL_AVX_INTRINSICS
  243. SDL_TARGETING("avx") static void kernel_floats_add_avx(float *dest, const float *a, const float *b, size_t size) {
  244. for (; size >= 8; size -= 8, dest += 8, a += 8, b += 8) {
  245. _mm256_storeu_ps(dest, _mm256_add_ps(_mm256_loadu_ps(a), _mm256_loadu_ps(b)));
  246. }
  247. for (; size; size--, ++dest, ++a, ++b) {
  248. *dest = *a + *b;
  249. }
  250. }
  251. #endif
  252. #if SDL_AVX2_INTRINSICS
  253. SDL_TARGETING("avx2") static void kernel_ints_add_avx2(Sint32 *dest, const Sint32 *a, const Sint32 *b, size_t size) {
  254. for (; size >= 8; size -= 8, dest += 8, a += 8, b += 8) {
  255. _mm256_storeu_si256((__m256i*)dest, _mm256_add_epi32(_mm256_loadu_si256((__m256i*)a), _mm256_loadu_si256((__m256i*)b)));
  256. }
  257. for (; size; size--, ++dest, ++a, ++b) {
  258. *dest = *a + *b;
  259. }
  260. }
  261. #endif
  262. #if SDL_AVX512F_INTRINSICS
  263. SDL_TARGETING("avx512f") static void kernel_floats_add_avx512f(float *dest, const float *a, const float *b, size_t size) {
  264. for (; size >= 16; size -= 16, dest += 16, a += 16, b += 16) {
  265. _mm512_storeu_ps(dest, _mm512_add_ps(_mm512_loadu_ps(a), _mm512_loadu_ps(b)));
  266. }
  267. for (; size; --size) {
  268. *dest++ = *a++ + *b++;
  269. }
  270. }
  271. #endif
  272. /* Test case functions */
  273. static int intrinsics_selftest(void *arg)
  274. {
  275. {
  276. size_t size;
  277. Sint32 *dest, *a, *b;
  278. if (allocate_random_int_arrays(&dest, &a, &b, &size) < 0) {
  279. return TEST_ABORTED;
  280. }
  281. kernel_ints_mul_cpu(dest, a, b, size);
  282. verify_ints_multiplication(dest, a, b, size, "CPU");
  283. free_arrays(dest, a, b);
  284. }
  285. {
  286. size_t size;
  287. Sint32 *dest, *a, *b;
  288. if (allocate_random_int_arrays(&dest, &a, &b, &size) < 0) {
  289. return TEST_ABORTED;
  290. }
  291. kernel_ints_add_cpu(dest, a, b, size);
  292. verify_ints_addition(dest, a, b, size, "CPU");
  293. free_arrays(dest, a, b);
  294. }
  295. {
  296. size_t size;
  297. float *dest, *a, *b;
  298. if (allocate_random_float_arrays(&dest, &a, &b, &size) < 0) {
  299. return TEST_ABORTED;
  300. }
  301. kernel_floats_add_cpu(dest, a, b, size);
  302. verify_floats_addition(dest, a, b, size, "CPU");
  303. free_arrays(dest, a, b);
  304. }
  305. {
  306. size_t size;
  307. double *dest, *a, *b;
  308. if (allocate_random_double_arrays(&dest, &a, &b, &size) < 0) {
  309. return TEST_ABORTED;
  310. }
  311. kernel_doubles_add_cpu(dest, a, b, size);
  312. verify_doubles_addition(dest, a, b, size, "CPU");
  313. free_arrays(dest, a, b);
  314. }
  315. return TEST_COMPLETED;
  316. }
  317. static int intrinsics_testRDTSC(void *arg)
  318. {
  319. if (SDL_HasRDTSC()) {
  320. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has RDTSC support.");
  321. #if SDL_RDTSC_INTRINSICS
  322. {
  323. Sint64 ticks;
  324. #if defined(_MSC_VER) || defined(__clang__)
  325. ticks = __rdtsc();
  326. #else
  327. ticks = _rdtsc();
  328. #endif
  329. SDLTest_AssertCheck(SDL_TRUE, "rdtsc returned: %" SDL_PRIu64 " ticks", ticks);
  330. return TEST_COMPLETED;
  331. }
  332. #else
  333. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use RDTSC intrinsics.");
  334. #endif
  335. } else {
  336. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO RDTSC support.");
  337. }
  338. return TEST_SKIPPED;
  339. }
  340. static int intrinsics_testMMX(void *arg)
  341. {
  342. if (SDL_HasMMX()) {
  343. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has MMX support.");
  344. #if SDL_MMX_INTRINSICS
  345. {
  346. size_t size;
  347. Sint32 *dest, *a, *b;
  348. SDLTest_AssertCheck(SDL_TRUE, "Test executable uses MMX intrinsics.");
  349. if (allocate_random_int_arrays(&dest, &a, &b, &size) < 0) {
  350. return TEST_ABORTED;
  351. }
  352. kernel_ints_add_mmx(dest, a, b, size);
  353. verify_ints_addition(dest, a, b, size, "MMX");
  354. free_arrays(dest, a, b);
  355. return TEST_COMPLETED;
  356. }
  357. #else
  358. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use MMX intrinsics.");
  359. #endif
  360. } else {
  361. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO MMX support.");
  362. }
  363. return TEST_SKIPPED;
  364. }
  365. static int intrinsics_testSSE(void *arg)
  366. {
  367. if (SDL_HasSSE()) {
  368. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has SSE support.");
  369. #if SDL_SSE_INTRINSICS
  370. {
  371. size_t size;
  372. float *dest, *a, *b;
  373. SDLTest_AssertCheck(SDL_TRUE, "Test executable uses SSE intrinsics.");
  374. if (allocate_random_float_arrays(&dest, &a, &b, &size) < 0) {
  375. return TEST_ABORTED;
  376. }
  377. kernel_floats_add_sse(dest, a, b, size);
  378. verify_floats_addition(dest, a, b, size, "SSE");
  379. free_arrays(dest, a, b);
  380. return TEST_COMPLETED;
  381. }
  382. #else
  383. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use SSE intrinsics.");
  384. #endif
  385. } else {
  386. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO SSE support.");
  387. }
  388. return TEST_SKIPPED;
  389. }
  390. static int intrinsics_testSSE2(void *arg)
  391. {
  392. if (SDL_HasSSE2()) {
  393. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has SSE2 support.");
  394. #if SDL_SSE2_INTRINSICS
  395. {
  396. size_t size;
  397. double *dest, *a, *b;
  398. SDLTest_AssertCheck(SDL_TRUE, "Test executable uses SSE2 intrinsics.");
  399. if (allocate_random_double_arrays(&dest, &a, &b, &size) < 0) {
  400. return TEST_ABORTED;
  401. }
  402. kernel_doubles_add_sse2(dest, a, b, size);
  403. verify_doubles_addition(dest, a, b, size, "SSE2");
  404. free_arrays(dest, a, b);
  405. return TEST_COMPLETED;
  406. }
  407. #else
  408. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use SSE2 intrinsics.");
  409. #endif
  410. } else {
  411. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO SSE2 support.");
  412. }
  413. return TEST_SKIPPED;
  414. }
  415. static int intrinsics_testSSE3(void *arg)
  416. {
  417. if (SDL_HasSSE3()) {
  418. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has SSE3 support.");
  419. #if SDL_SSE3_INTRINSICS
  420. {
  421. size_t size;
  422. Sint32 *dest, *a, *b;
  423. SDLTest_AssertCheck(SDL_TRUE, "Test executable uses SSE3 intrinsics.");
  424. if (allocate_random_int_arrays(&dest, &a, &b, &size) < 0) {
  425. return TEST_ABORTED;
  426. }
  427. kernel_ints_add_sse3(dest, a, b, size);
  428. verify_ints_addition(dest, a, b, size, "SSE3");
  429. free_arrays(dest, a, b);
  430. return TEST_COMPLETED;
  431. }
  432. #else
  433. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use SSE3 intrinsics.");
  434. #endif
  435. } else {
  436. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO SSE3 support.");
  437. }
  438. return TEST_SKIPPED;
  439. }
  440. static int intrinsics_testSSE4_1(void *arg)
  441. {
  442. if (SDL_HasSSE41()) {
  443. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has SSE4.1 support.");
  444. #if SDL_SSE4_1_INTRINSICS
  445. {
  446. size_t size;
  447. Sint32 *dest, *a, *b;
  448. SDLTest_AssertCheck(SDL_TRUE, "Test executable uses SSE4.1 intrinsics.");
  449. if (allocate_random_int_arrays(&dest, &a, &b, &size) < 0) {
  450. return TEST_ABORTED;
  451. }
  452. kernel_ints_mul_sse4_1(dest, a, b, size);
  453. verify_ints_multiplication(dest, a, b, size, "SSE4.1");
  454. free_arrays(dest, a, b);
  455. return TEST_COMPLETED;
  456. }
  457. #else
  458. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use SSE4.1 intrinsics.");
  459. #endif
  460. } else {
  461. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO SSE4.1 support.");
  462. }
  463. return TEST_SKIPPED;
  464. }
  465. static int intrinsics_testSSE4_2(void *arg)
  466. {
  467. if (SDL_HasSSE42()) {
  468. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has SSE4.2 support.");
  469. #if SDL_SSE4_2_INTRINSICS
  470. {
  471. struct {
  472. const char *input;
  473. Uint32 crc32c;
  474. } references[] = {
  475. {"", 0x00000000},
  476. {"Hello world", 0x72b51f78},
  477. {"Simple DirectMedia Layer", 0x56f85341, },
  478. };
  479. size_t i;
  480. SDLTest_AssertCheck(SDL_TRUE, "Test executable uses SSE4.2 intrinsics.");
  481. for (i = 0; i < SDL_arraysize(references); ++i) {
  482. Uint32 actual = calculate_crc32c_sse4_2(references[i].input);
  483. SDLTest_AssertCheck(actual == references[i].crc32c, "CRC32-C(\"%s\")=0x%08x, got 0x%08x",
  484. references[i].input, references[i].crc32c, actual);
  485. }
  486. return TEST_COMPLETED;
  487. }
  488. #else
  489. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use SSE4.2 intrinsics.");
  490. #endif
  491. } else {
  492. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO SSE4.2 support.");
  493. }
  494. return TEST_SKIPPED;
  495. }
  496. static int intrinsics_testAVX(void *arg)
  497. {
  498. if (SDL_HasAVX()) {
  499. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has AVX support.");
  500. #if SDL_AVX_INTRINSICS
  501. {
  502. size_t size;
  503. float *dest, *a, *b;
  504. SDLTest_AssertCheck(SDL_TRUE, "Test executable uses AVX intrinsics.");
  505. if (allocate_random_float_arrays(&dest, &a, &b, &size) < 0) {
  506. return TEST_ABORTED;
  507. }
  508. kernel_floats_add_avx(dest, a, b, size);
  509. verify_floats_addition(dest, a, b, size, "AVX");
  510. free_arrays(dest, a, b);
  511. return TEST_COMPLETED;
  512. }
  513. #else
  514. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use AVX intrinsics.");
  515. #endif
  516. } else {
  517. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO AVX support.");
  518. }
  519. return TEST_SKIPPED;
  520. }
  521. static int intrinsics_testAVX2(void *arg)
  522. {
  523. if (SDL_HasAVX2()) {
  524. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has AVX2 support.");
  525. #if SDL_AVX2_INTRINSICS
  526. {
  527. size_t size;
  528. Sint32 *dest, *a, *b;
  529. SDLTest_AssertCheck(SDL_TRUE, "Test executable uses AVX2 intrinsics.");
  530. if (allocate_random_int_arrays(&dest, &a, &b, &size) < 0) {
  531. return TEST_ABORTED;
  532. }
  533. kernel_ints_add_avx2(dest, a, b, size);
  534. verify_ints_addition(dest, a, b, size, "AVX2");
  535. free_arrays(dest, a, b);
  536. return TEST_COMPLETED;
  537. }
  538. #else
  539. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use AVX2 intrinsics.");
  540. #endif
  541. } else {
  542. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO AVX2 support.");
  543. }
  544. return TEST_SKIPPED;
  545. }
  546. static int intrinsics_testAVX512F(void *arg)
  547. {
  548. if (SDL_HasAVX512F()) {
  549. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has AVX512F support.");
  550. #if SDL_AVX512F_INTRINSICS
  551. {
  552. size_t size;
  553. float *dest, *a, *b;
  554. SDLTest_AssertCheck(SDL_TRUE, "Test executable uses AVX512F intrinsics.");
  555. if (allocate_random_float_arrays(&dest, &a, &b, &size) < 0) {
  556. return TEST_ABORTED;
  557. }
  558. kernel_floats_add_avx512f(dest, a, b, size);
  559. verify_floats_addition(dest, a, b, size, "AVX512F");
  560. free_arrays(dest, a, b);
  561. return TEST_COMPLETED;
  562. }
  563. #else
  564. SDLTest_AssertCheck(SDL_TRUE, "Test executable does NOT use AVX512F intrinsics.");
  565. #endif
  566. } else {
  567. SDLTest_AssertCheck(SDL_TRUE, "CPU of test machine has NO AVX512F support.");
  568. }
  569. return TEST_SKIPPED;
  570. }
  571. /* ================= Test References ================== */
  572. /* Intrinsics test cases */
  573. static const SDLTest_TestCaseReference intrinsicsTest1 = {
  574. (SDLTest_TestCaseFp)intrinsics_selftest, "intrinsics_selftest", "Intrinsics testautomation selftest", TEST_ENABLED
  575. };
  576. static const SDLTest_TestCaseReference intrinsicsTest2 = {
  577. (SDLTest_TestCaseFp)intrinsics_testRDTSC, "intrinsics_rdtsc", "Tests RDTC intrinsic", TEST_ENABLED
  578. };
  579. static const SDLTest_TestCaseReference intrinsicsTest3 = {
  580. (SDLTest_TestCaseFp)intrinsics_testMMX, "intrinsics_testMMX", "Tests MMX intrinsics", TEST_ENABLED
  581. };
  582. static const SDLTest_TestCaseReference intrinsicsTest4 = {
  583. (SDLTest_TestCaseFp)intrinsics_testSSE, "intrinsics_testSSE", "Tests SSE intrinsics", TEST_ENABLED
  584. };
  585. static const SDLTest_TestCaseReference intrinsicsTest5 = {
  586. (SDLTest_TestCaseFp)intrinsics_testSSE2, "intrinsics_testSSE2", "Tests SSE2 intrinsics", TEST_ENABLED
  587. };
  588. static const SDLTest_TestCaseReference intrinsicsTest6 = {
  589. (SDLTest_TestCaseFp)intrinsics_testSSE3, "intrinsics_testSSE3", "Tests SSE3 intrinsics", TEST_ENABLED
  590. };
  591. static const SDLTest_TestCaseReference intrinsicsTest7 = {
  592. (SDLTest_TestCaseFp)intrinsics_testSSE4_1, "intrinsics_testSSE4.1", "Tests SSE4.1 intrinsics", TEST_ENABLED
  593. };
  594. static const SDLTest_TestCaseReference intrinsicsTest8 = {
  595. (SDLTest_TestCaseFp)intrinsics_testSSE4_2, "intrinsics_testSSE4.2", "Tests SSE4.2 intrinsics", TEST_ENABLED
  596. };
  597. static const SDLTest_TestCaseReference intrinsicsTest9 = {
  598. (SDLTest_TestCaseFp)intrinsics_testAVX, "intrinsics_testAVX", "Tests AVX intrinsics", TEST_ENABLED
  599. };
  600. static const SDLTest_TestCaseReference intrinsicsTest10 = {
  601. (SDLTest_TestCaseFp)intrinsics_testAVX2, "intrinsics_testAVX2", "Tests AVX2 intrinsics", TEST_ENABLED
  602. };
  603. static const SDLTest_TestCaseReference intrinsicsTest11 = {
  604. (SDLTest_TestCaseFp)intrinsics_testAVX512F, "intrinsics_testAVX512F", "Tests AVX512F intrinsics", TEST_ENABLED
  605. };
  606. /* Sequence of Platform test cases */
  607. static const SDLTest_TestCaseReference *platformTests[] = {
  608. &intrinsicsTest1,
  609. &intrinsicsTest2,
  610. &intrinsicsTest3,
  611. &intrinsicsTest4,
  612. &intrinsicsTest5,
  613. &intrinsicsTest6,
  614. &intrinsicsTest7,
  615. &intrinsicsTest8,
  616. &intrinsicsTest9,
  617. &intrinsicsTest10,
  618. &intrinsicsTest11,
  619. NULL
  620. };
  621. /* Platform test suite (global) */
  622. SDLTest_TestSuiteReference intrinsicsTestSuite = {
  623. "Intrinsics",
  624. NULL,
  625. platformTests,
  626. NULL
  627. };