1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "CPUID.hpp"
16
17 #if defined(_WIN32)
18 # ifndef WIN32_LEAN_AND_MEAN
19 # define WIN32_LEAN_AND_MEAN
20 # endif
21 # include <windows.h>
22 # include <intrin.h>
23 # include <float.h>
24 #else
25 # include <unistd.h>
26 # include <sched.h>
27 # include <sys/types.h>
28 #endif
29
30 namespace rr {
31
cpuid(int eax_ebx_ecx_edx[4],int eax,int ecx=0)32 static void cpuid(int eax_ebx_ecx_edx[4], int eax, int ecx = 0)
33 {
34 #if defined(__i386__) || defined(__x86_64__)
35 # if defined(_WIN32)
36 __cpuidex(eax_ebx_ecx_edx, eax, ecx);
37 # else
38 __asm volatile("cpuid"
39 : "=a"(eax_ebx_ecx_edx[0]), "=b"(eax_ebx_ecx_edx[1]), "=c"(eax_ebx_ecx_edx[2]), "=d"(eax_ebx_ecx_edx[3])
40 : "a"(eax), "c"(ecx));
41 # endif
42 #else
43 eax_ebx_ecx_edx[0] = 0;
44 eax_ebx_ecx_edx[1] = 0;
45 eax_ebx_ecx_edx[2] = 0;
46 eax_ebx_ecx_edx[3] = 0;
47 #endif
48 }
49
supportsSSE4_1()50 bool CPUID::supportsSSE4_1()
51 {
52 int eax_ebx_ecx_edx[4];
53 cpuid(eax_ebx_ecx_edx, 1);
54 return (eax_ebx_ecx_edx[2] & 0x00080000) != 0;
55 }
56
supportsAVX2()57 bool CPUID::supportsAVX2()
58 {
59 int eax_ebx_ecx_edx[4];
60 cpuid(eax_ebx_ecx_edx, 1);
61 // Test bits 12 (FMA), 27 (OSXSAVE), and 28 (AVX) of ECX
62 bool osxsave_avx_fma = (eax_ebx_ecx_edx[2] & 0x18001000) == 0x18001000;
63
64 // AVX is a prerequisite of AVX2 and must be checked for first according to the Intel Software Developer's Manual.
65 // OSXSAVE ensures the operating system can save/restore ymm registers on context switches.
66 // FMA support is often considered an integral part of AVX2.
67 if(!osxsave_avx_fma)
68 {
69 return false;
70 }
71
72 cpuid(eax_ebx_ecx_edx, 7, 0); // Valid if AVX is supported
73 return (eax_ebx_ecx_edx[1] & 0x00000020) != 0;
74 }
75
76 } // namespace rr
77