blob: b8a67cf98ad02a4ad3ba291ba64ba7eceb8df149 [file] [log] [blame]
Nicolas Capensc07dc4b2018-08-06 14:20:45 -04001// 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)
Ben Clayton713b8d32019-12-17 20:37:56 +000018# 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>
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040024#else
Ben Clayton713b8d32019-12-17 20:37:56 +000025# include <unistd.h>
26# include <sched.h>
27# include <sys/types.h>
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040028#endif
29
Nicolas Capens157ba262019-12-10 17:49:14 -050030namespace rr {
31
Nicolas Capensba5c6562022-01-31 14:08:34 -050032static void cpuid(int eax_ebx_ecx_edx[4], int eax, int ecx = 0)
Nicolas Capens157ba262019-12-10 17:49:14 -050033{
Ben Clayton713b8d32019-12-17 20:37:56 +000034#if defined(__i386__) || defined(__x86_64__)
35# if defined(_WIN32)
Nicolas Capensba5c6562022-01-31 14:08:34 -050036 __cpuidex(eax_ebx_ecx_edx, eax, ecx);
Ben Clayton713b8d32019-12-17 20:37:56 +000037# else
38 __asm volatile("cpuid"
Nicolas Capensba5c6562022-01-31 14:08:34 -050039 : "=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));
Ben Clayton713b8d32019-12-17 20:37:56 +000041# endif
42#else
Nicolas Capensba5c6562022-01-31 14:08:34 -050043 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;
Ben Clayton713b8d32019-12-17 20:37:56 +000047#endif
Nicolas Capens157ba262019-12-10 17:49:14 -050048}
49
Sean Risser40883632021-08-20 15:19:11 -040050bool CPUID::supportsSSE4_1()
Nicolas Capens157ba262019-12-10 17:49:14 -050051{
Nicolas Capensba5c6562022-01-31 14:08:34 -050052 int eax_ebx_ecx_edx[4];
53 cpuid(eax_ebx_ecx_edx, 1);
54 return (eax_ebx_ecx_edx[2] & 0x00080000) != 0;
Nicolas Capens157ba262019-12-10 17:49:14 -050055}
56
Nicolas Capensad9eba42022-01-31 13:59:24 -050057bool 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
Nicolas Capens157ba262019-12-10 17:49:14 -050076} // namespace rr