hardware_simd.qbk 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. SIMD predefs depend on compiler options. For example, you will have to add the
  2. option `-msse3` to clang or gcc to enable SSE3. SIMD predefs are also inclusive.
  3. This means that if SSE3 is enabled, then every other extensions with a lower
  4. version number will implicitly be enabled and detected. However, some extensions
  5. are CPU specific, they may not be detected nor enabled when an upper version is
  6. enabled.
  7. [note SSE(1) and SSE2 are automatically enabled by default when using x86-64
  8. architecture.]
  9. To check if any SIMD extension has been enabled, you can use:
  10. ``
  11. #include <boost/predef/hardware/simd.h>
  12. #include <iostream>
  13. int main()
  14. {
  15. #if defined(BOOST_HW_SIMD_AVAILABLE)
  16. std::cout << "SIMD detected!" << std::endl;
  17. #endif
  18. return 0;
  19. }
  20. ``
  21. When writing SIMD specific code, you may want to check if a particular extension
  22. has been detected. To do so you have to use the right architecture predef and
  23. compare it. Those predef are of the form `BOOST_HW_SIMD_"ARCH"` (where `"ARCH"`
  24. is either `ARM`, `PPC`, or `X86`). For example, if you compile code for x86
  25. architecture, you will have to use `BOOST_HW_SIMD_X86`. Its value will be the
  26. version number of the most recent SIMD extension detected for the architecture.
  27. To check if an extension has been enabled:
  28. ``
  29. #include <boost/predef/hardware/simd.h>
  30. #include <iostream>
  31. int main()
  32. {
  33. #if BOOST_HW_SIMD_X86 >= BOOST_HW_SIMD_X86_SSE3_VERSION
  34. std::cout << "This is SSE3!" << std::endl;
  35. #endif
  36. return 0;
  37. }
  38. ``
  39. [note The *_VERSION* defines that map version number to actual real
  40. identifiers. This way it is easier to write comparisons without messing up with
  41. version numbers.]
  42. To *"stricly"* check the most recent detected extension:
  43. ``
  44. #include <boost/predef/hardware/simd.h>
  45. #include <iostream>
  46. int main()
  47. {
  48. #if BOOST_HW_SIMD_X86 == BOOST_HW_SIMD_X86_SSE3_VERSION
  49. std::cout << "This is SSE3 and this is the most recent enabled extension!"
  50. << std::endl;
  51. #endif
  52. return 0;
  53. }
  54. ``
  55. Because of the version systems of predefs and of the inclusive property of SIMD
  56. extensions macros, you can easily check for ranges of supported extensions:
  57. ``
  58. #include <boost/predef/hardware/simd.h>
  59. #include <iostream>
  60. int main()
  61. {
  62. #if BOOST_HW_SIMD_X86 >= BOOST_HW_SIMD_X86_SSE2_VERSION &&\
  63. BOOST_HW_SIMD_X86 <= BOOST_HW_SIMD_X86_SSSE3_VERSION
  64. std::cout << "This is SSE2, SSE3 and SSSE3!" << std::endl;
  65. #endif
  66. return 0;
  67. }
  68. ``
  69. [note Unlike gcc and clang, Visual Studio does not allow you to specify precisely
  70. the SSE variants you want to use, the only detections that will take place are
  71. SSE, SSE2, AVX and AVX2. For more informations,
  72. see [@https://msdn.microsoft.com/en-us/library/b0084kay.aspx here].]