vector.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. // Copyright © 2018-2019 Ariadne Devos
  3. /* sHT -- select a proessor-supported vector size */
  4. #include <sHT/vector.h>
  5. #include <sHT/bugs.h>
  6. #ifndef sHT_single_vector
  7. /** The binary logarithm of the largest vector size */
  8. static int sHT_vector_order;
  9. size_t
  10. sHT_select_simd(int elem_order)
  11. {
  12. int relative = sHT_vector_order - elem_order;
  13. sHT_require(relative >= 0);
  14. return relative;
  15. }
  16. /* Compare with <sHT/vector.h> */
  17. # ifdef __x86_64__
  18. void
  19. sHT_detect_simd(void)
  20. {
  21. __builtin_cpu_init();
  22. if (__builtin_cpu_supports("avx512f"))
  23. sHT_vector_order = 6;
  24. else if (__builtin_cpu_supports("avx2"))
  25. sHT_vector_order = 5;
  26. else sHT_vector_order = 4;
  27. }
  28. # elif defined(__i386__)
  29. void
  30. sHT_detect_simd(void)
  31. {
  32. __builtin_cpu_init();
  33. if (__builtin_cpu_supports("avx512f"))
  34. sHT_vector_order = 6;
  35. else if (__builtin_cpu_supports("avx2"))
  36. sHT_vector_order = 5;
  37. else if (__builtin_cpu_supports("sse2"))
  38. sHT_vector_order = 4;
  39. else if (__builtin_cpu_supports("mmx"))
  40. sHT_vector_order = 3;
  41. else sHT_vector_order = 2;
  42. }
  43. # elif defined(__arm__)
  44. # include <sys/auxv.h>
  45. void
  46. sHT_detect_simd(void)
  47. {
  48. if (getauxval(AT_HWCAP) & HWCAP_ARM_NEON)
  49. sHT_vector_order = 4;
  50. else sHT_vector_order = 2;
  51. }
  52. # else
  53. # error unsupported system, inconsistency with <sHT/vector.h>
  54. # endif
  55. #endif