simd.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright Charly Chevalier 2015
  3. Copyright Joel Falcou 2015
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #include <boost/predef/hardware/simd/x86.h>
  9. #include <boost/predef/hardware/simd/x86_amd.h>
  10. #include <boost/predef/hardware/simd/arm.h>
  11. #include <boost/predef/hardware/simd/ppc.h>
  12. #ifndef BOOST_PREDEF_HARDWARE_SIMD_H
  13. #define BOOST_PREDEF_HARDWARE_SIMD_H
  14. #include <boost/predef/version_number.h>
  15. /* tag::reference[]
  16. = Using the `BOOST_HW_SIMD_*` predefs
  17. SIMD predefs depend on compiler options. For example, you will have to add the
  18. option `-msse3` to clang or gcc to enable SSE3. SIMD predefs are also inclusive.
  19. This means that if SSE3 is enabled, then every other extensions with a lower
  20. version number will implicitly be enabled and detected. However, some extensions
  21. are CPU specific, they may not be detected nor enabled when an upper version is
  22. enabled.
  23. NOTE: SSE(1) and SSE2 are automatically enabled by default when using x86-64
  24. architecture.
  25. To check if any SIMD extension has been enabled, you can use:
  26. [source]
  27. ----
  28. #include <boost/predef/hardware/simd.h>
  29. #include <iostream>
  30. int main()
  31. {
  32. #if defined(BOOST_HW_SIMD_AVAILABLE)
  33. std::cout << "SIMD detected!" << std::endl;
  34. #endif
  35. return 0;
  36. }
  37. ----
  38. When writing SIMD specific code, you may want to check if a particular extension
  39. has been detected. To do so you have to use the right architecture predef and
  40. compare it. Those predef are of the form `BOOST_HW_SIMD_"ARCH"` (where `"ARCH"`
  41. is either `ARM`, `PPC`, or `X86`). For example, if you compile code for x86
  42. architecture, you will have to use `BOOST_HW_SIMD_X86`. Its value will be the
  43. version number of the most recent SIMD extension detected for the architecture.
  44. To check if an extension has been enabled:
  45. [source]
  46. ----
  47. #include <boost/predef/hardware/simd.h>
  48. #include <iostream>
  49. int main()
  50. {
  51. #if BOOST_HW_SIMD_X86 >= BOOST_HW_SIMD_X86_SSE3_VERSION
  52. std::cout << "This is SSE3!" << std::endl;
  53. #endif
  54. return 0;
  55. }
  56. ----
  57. NOTE: The *_VERSION* defines that map version number to actual real
  58. identifiers. This way it is easier to write comparisons without messing up with
  59. version numbers.
  60. To *"strictly"* check the most recent detected extension:
  61. [source]
  62. ----
  63. #include <boost/predef/hardware/simd.h>
  64. #include <iostream>
  65. int main()
  66. {
  67. #if BOOST_HW_SIMD_X86 == BOOST_HW_SIMD_X86_SSE3_VERSION
  68. std::cout << "This is SSE3 and this is the most recent enabled extension!"
  69. << std::endl;
  70. #endif
  71. return 0;
  72. }
  73. ----
  74. Because of the version systems of predefs and of the inclusive property of SIMD
  75. extensions macros, you can easily check for ranges of supported extensions:
  76. [source]
  77. ----
  78. #include <boost/predef/hardware/simd.h>
  79. #include <iostream>
  80. int main()
  81. {
  82. #if BOOST_HW_SIMD_X86 >= BOOST_HW_SIMD_X86_SSE2_VERSION &&\
  83. BOOST_HW_SIMD_X86 <= BOOST_HW_SIMD_X86_SSSE3_VERSION
  84. std::cout << "This is SSE2, SSE3 and SSSE3!" << std::endl;
  85. #endif
  86. return 0;
  87. }
  88. ----
  89. NOTE: Unlike gcc and clang, Visual Studio does not allow you to specify precisely
  90. the SSE variants you want to use, the only detections that will take place are
  91. SSE, SSE2, AVX and AVX2. For more informations,
  92. see [@https://msdn.microsoft.com/en-us/library/b0084kay.aspx here].
  93. */ // end::reference[]
  94. // We check if SIMD extension of multiples architectures have been detected,
  95. // if yes, then this is an error!
  96. //
  97. // NOTE: _X86_AMD implies _X86, so there is no need to check for it here!
  98. //
  99. #if defined(BOOST_HW_SIMD_ARM_AVAILABLE) && defined(BOOST_HW_SIMD_PPC_AVAILABLE) ||\
  100. defined(BOOST_HW_SIMD_ARM_AVAILABLE) && defined(BOOST_HW_SIMD_X86_AVAILABLE) ||\
  101. defined(BOOST_HW_SIMD_PPC_AVAILABLE) && defined(BOOST_HW_SIMD_X86_AVAILABLE)
  102. # error "Multiple SIMD architectures detected, this cannot happen!"
  103. #endif
  104. #if defined(BOOST_HW_SIMD_X86_AVAILABLE) && defined(BOOST_HW_SIMD_X86_AMD_AVAILABLE)
  105. // If both standard _X86 and _X86_AMD are available,
  106. // then take the biggest version of the two!
  107. # if BOOST_HW_SIMD_X86 >= BOOST_HW_SIMD_X86_AMD
  108. # define BOOST_HW_SIMD BOOST_HW_SIMD_X86
  109. # else
  110. # define BOOST_HW_SIMD BOOST_HW_SIMD_X86_AMD
  111. # endif
  112. #endif
  113. #if !defined(BOOST_HW_SIMD)
  114. // At this point, only one of these two is defined
  115. # if defined(BOOST_HW_SIMD_X86_AVAILABLE)
  116. # define BOOST_HW_SIMD BOOST_HW_SIMD_X86
  117. # endif
  118. # if defined(BOOST_HW_SIMD_X86_AMD_AVAILABLE)
  119. # define BOOST_HW_SIMD BOOST_HW_SIMD_X86_AMD
  120. # endif
  121. #endif
  122. #if defined(BOOST_HW_SIMD_ARM_AVAILABLE)
  123. # define BOOST_HW_SIMD BOOST_HW_SIMD_ARM
  124. #endif
  125. #if defined(BOOST_HW_SIMD_PPC_AVAILABLE)
  126. # define BOOST_HW_SIMD BOOST_HW_SIMD_PPC
  127. #endif
  128. #if defined(BOOST_HW_SIMD)
  129. # define BOOST_HW_SIMD_AVAILABLE
  130. #else
  131. # define BOOST_HW_SIMD BOOST_VERSION_NUMBER_NOT_AVAILABLE
  132. #endif
  133. #define BOOST_HW_SIMD_NAME "Hardware SIMD"
  134. #endif
  135. #include <boost/predef/detail/test.h>
  136. BOOST_PREDEF_DECLARE_TEST(BOOST_HW_SIMD, BOOST_HW_SIMD_NAME)