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