simdchecker.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include<simdfuncs.h>
  2. #include<stdio.h>
  3. /*
  4. * A function that checks at runtime which simd accelerations are
  5. * available and calls the best one. Falls
  6. * back to plain C implementation if SIMD is not available.
  7. */
  8. int main(int argc, char **argv) {
  9. float four[4] = {2.0, 3.0, 4.0, 5.0};
  10. const float expected[4] = {3.0, 4.0, 5.0, 6.0};
  11. void (*fptr)(float[4]) = NULL;
  12. const char *type;
  13. int i;
  14. /* Add here. The first matched one is used so put "better" instruction
  15. * sets at the top.
  16. */
  17. #if HAVE_NEON
  18. if(fptr == NULL && neon_available()) {
  19. fptr = increment_neon;
  20. type = "NEON";
  21. }
  22. #endif
  23. #if HAVE_AVX2
  24. if(fptr == NULL && avx2_available()) {
  25. fptr = increment_avx2;
  26. type = "AVX2";
  27. }
  28. #endif
  29. #if HAVE_AVX
  30. if(fptr == NULL && avx_available()) {
  31. fptr = increment_avx;
  32. type = "AVX";
  33. }
  34. #endif
  35. #if HAVE_SSE42
  36. if(fptr == NULL && sse42_available()) {
  37. fptr = increment_sse42;
  38. type = "SSE42";
  39. }
  40. #endif
  41. #if HAVE_SSE41
  42. if(fptr == NULL && sse41_available()) {
  43. fptr = increment_sse41;
  44. type = "SSE41";
  45. }
  46. #endif
  47. #if HAVE_SSSE3
  48. if(fptr == NULL && ssse3_available()) {
  49. fptr = increment_ssse3;
  50. type = "SSSE3";
  51. }
  52. #endif
  53. #if HAVE_SSE3
  54. if(fptr == NULL && sse3_available()) {
  55. fptr = increment_sse3;
  56. type = "SSE3";
  57. }
  58. #endif
  59. #if HAVE_SSE2
  60. if(fptr == NULL && sse2_available()) {
  61. fptr = increment_sse2;
  62. type = "SSE2";
  63. }
  64. #endif
  65. #if HAVE_SSE
  66. if(fptr == NULL && sse_available()) {
  67. fptr = increment_sse;
  68. type = "SSE";
  69. }
  70. #endif
  71. #if HAVE_MMX
  72. if(fptr == NULL && mmx_available()) {
  73. fptr = increment_mmx;
  74. type = "MMX";
  75. }
  76. #endif
  77. if(fptr == NULL) {
  78. fptr = increment_fallback;
  79. type = "fallback";
  80. }
  81. printf("Using %s.\n", type);
  82. fptr(four);
  83. for(i=0; i<4; i++) {
  84. if(four[i] != expected[i]) {
  85. printf("Increment function failed, got %f expected %f.\n", four[i], expected[i]);
  86. return 1;
  87. }
  88. }
  89. return 0;
  90. }