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.
7 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #define CPUID_SSSE3_ECX_BIT 9
33 #define CPUID_OSXSAVE_ECX_BIT 27
34 #define CPUID_AVX_ECX_BIT 28
36 #define CPUID_AVX2_EBX_BIT 5
38 #define CPUID_SSE_EDX_BIT 25
40 #define BIT_XMM_STATE (1 << 1)
41 #define BIT_YMM_STATE (2 << 1)
48 #if defined(__GNUC__) && (defined(__i386__) ||defined(__x86_64))
52 #define CPL_CPUID(level, subfunction, array) __cpuid_count(level, subfunction, array[0], array[1], array[2], array[3])
54 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
58 #define CPL_CPUID(level, subfunction, array) __cpuidex(array, level, subfunction)
62 #error "not supported"
66 #if defined(__GNUC__) && (defined(__i386__) ||defined(__x86_64))
68 int CPLHaveRuntimeAVX()
70 int cpuinfo[4] = { 0, 0, 0, 0 };
72 unsigned int nXCRHigh;
74 CPL_CPUID(1, 0, cpuinfo);
76 // Check OSXSAVE feature.
77 if ((cpuinfo[REG_ECX] & (1 << CPUID_OSXSAVE_ECX_BIT)) == 0) {
82 if ((cpuinfo[REG_ECX] & (1 << CPUID_AVX_ECX_BIT)) == 0) {
86 // Issue XGETBV and check the XMM and YMM state bit.
88 __asm__("xgetbv" : "=a"(nXCRLow), "=d"(nXCRHigh) : "c"(0));
89 if ((nXCRLow & (BIT_XMM_STATE | BIT_YMM_STATE)) !=
90 (BIT_XMM_STATE | BIT_YMM_STATE)) {
97 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219) && (defined(_M_IX86) || defined(_M_X64))
98 // _xgetbv available only in Visual Studio 2010 SP1 or later
100 int CPLHaveRuntimeAVX()
102 int cpuinfo[4] = { 0, 0, 0, 0 };
103 unsigned __int64 xcrFeatureMask;
105 CPL_CPUID(1, 0, cpuinfo);
107 // Check OSXSAVE feature.
108 if ((cpuinfo[REG_ECX] & (1 << CPUID_OSXSAVE_ECX_BIT)) == 0) {
112 // Check AVX feature.
113 if ((cpuinfo[REG_ECX] & (1 << CPUID_AVX_ECX_BIT)) == 0) {
117 // Issue XGETBV and check the XMM and YMM state bit.
118 xcrFeatureMask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
119 if ((xcrFeatureMask & (BIT_XMM_STATE | BIT_YMM_STATE)) !=
120 (BIT_XMM_STATE | BIT_YMM_STATE)) {
129 int CPLHaveRuntimeAVX2()
131 int cpuinfo[4] = { 0, 0, 0, 0 };
132 if (!CPLHaveRuntimeAVX()) {
136 CPL_CPUID(7, 0, cpuinfo);
138 // Check AVX2 feature.
139 if ((cpuinfo[REG_EBX] & (1 << CPUID_AVX2_EBX_BIT)) == 0) {
148 if (CPLHaveRuntimeAVX2()) {