detect-avx2.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #define CPUID_SSSE3_ECX_BIT 9
  32. #define CPUID_OSXSAVE_ECX_BIT 27
  33. #define CPUID_AVX_ECX_BIT 28
  34. #define CPUID_AVX2_EBX_BIT 5
  35. #define CPUID_SSE_EDX_BIT 25
  36. #define BIT_XMM_STATE (1 << 1)
  37. #define BIT_YMM_STATE (2 << 1)
  38. #define REG_EAX 0
  39. #define REG_EBX 1
  40. #define REG_ECX 2
  41. #define REG_EDX 3
  42. #if defined(__GNUC__) && (defined(__i386__) ||defined(__x86_64))
  43. #include <cpuid.h>
  44. #define CPL_CPUID(level, subfunction, array) __cpuid_count(level, subfunction, array[0], array[1], array[2], array[3])
  45. #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
  46. #include <intrin.h>
  47. #define CPL_CPUID(level, subfunction, array) __cpuidex(array, level, subfunction)
  48. #else
  49. #error "not supported"
  50. #endif
  51. #if defined(__GNUC__) && (defined(__i386__) ||defined(__x86_64))
  52. int CPLHaveRuntimeAVX()
  53. {
  54. int cpuinfo[4] = { 0, 0, 0, 0 };
  55. unsigned int nXCRLow;
  56. unsigned int nXCRHigh;
  57. CPL_CPUID(1, 0, cpuinfo);
  58. // Check OSXSAVE feature.
  59. if ((cpuinfo[REG_ECX] & (1 << CPUID_OSXSAVE_ECX_BIT)) == 0) {
  60. return 0;
  61. }
  62. // Check AVX feature.
  63. if ((cpuinfo[REG_ECX] & (1 << CPUID_AVX_ECX_BIT)) == 0) {
  64. return 0;
  65. }
  66. // Issue XGETBV and check the XMM and YMM state bit.
  67. __asm__("xgetbv" : "=a"(nXCRLow), "=d"(nXCRHigh) : "c"(0));
  68. if ((nXCRLow & (BIT_XMM_STATE | BIT_YMM_STATE)) !=
  69. (BIT_XMM_STATE | BIT_YMM_STATE)) {
  70. return 0;
  71. }
  72. return 1;
  73. }
  74. #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219) && (defined(_M_IX86) || defined(_M_X64))
  75. // _xgetbv available only in Visual Studio 2010 SP1 or later
  76. int CPLHaveRuntimeAVX()
  77. {
  78. int cpuinfo[4] = { 0, 0, 0, 0 };
  79. unsigned __int64 xcrFeatureMask;
  80. CPL_CPUID(1, 0, cpuinfo);
  81. // Check OSXSAVE feature.
  82. if ((cpuinfo[REG_ECX] & (1 << CPUID_OSXSAVE_ECX_BIT)) == 0) {
  83. return 0;
  84. }
  85. // Check AVX feature.
  86. if ((cpuinfo[REG_ECX] & (1 << CPUID_AVX_ECX_BIT)) == 0) {
  87. return 0;
  88. }
  89. // Issue XGETBV and check the XMM and YMM state bit.
  90. xcrFeatureMask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
  91. if ((xcrFeatureMask & (BIT_XMM_STATE | BIT_YMM_STATE)) !=
  92. (BIT_XMM_STATE | BIT_YMM_STATE)) {
  93. return 0;
  94. }
  95. return 1;
  96. }
  97. #endif
  98. int CPLHaveRuntimeAVX2()
  99. {
  100. int cpuinfo[4] = { 0, 0, 0, 0 };
  101. if (!CPLHaveRuntimeAVX()) {
  102. return 0;
  103. }
  104. CPL_CPUID(7, 0, cpuinfo);
  105. // Check AVX2 feature.
  106. if ((cpuinfo[REG_EBX] & (1 << CPUID_AVX2_EBX_BIT)) == 0) {
  107. return 0;
  108. }
  109. return 1;
  110. }
  111. int main()
  112. {
  113. if (CPLHaveRuntimeAVX2()) {
  114. return 0;
  115. }
  116. return 1;
  117. }