Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 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) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 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> |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 24 | #else |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 25 | # include <unistd.h> |
| 26 | # include <sched.h> |
| 27 | # include <sys/types.h> |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 28 | #endif |
| 29 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 30 | namespace rr { |
| 31 | |
Nicolas Capens | ba5c656 | 2022-01-31 14:08:34 -0500 | [diff] [blame] | 32 | static void cpuid(int eax_ebx_ecx_edx[4], int eax, int ecx = 0) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 33 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 34 | #if defined(__i386__) || defined(__x86_64__) |
| 35 | # if defined(_WIN32) |
Nicolas Capens | ba5c656 | 2022-01-31 14:08:34 -0500 | [diff] [blame] | 36 | __cpuidex(eax_ebx_ecx_edx, eax, ecx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 37 | # else |
| 38 | __asm volatile("cpuid" |
Nicolas Capens | ba5c656 | 2022-01-31 14:08:34 -0500 | [diff] [blame] | 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)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 41 | # endif |
| 42 | #else |
Nicolas Capens | ba5c656 | 2022-01-31 14:08:34 -0500 | [diff] [blame] | 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; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 47 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Sean Risser | 4088363 | 2021-08-20 15:19:11 -0400 | [diff] [blame] | 50 | bool CPUID::supportsSSE4_1() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 51 | { |
Nicolas Capens | ba5c656 | 2022-01-31 14:08:34 -0500 | [diff] [blame] | 52 | int eax_ebx_ecx_edx[4]; |
| 53 | cpuid(eax_ebx_ecx_edx, 1); |
| 54 | return (eax_ebx_ecx_edx[2] & 0x00080000) != 0; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Nicolas Capens | ad9eba4 | 2022-01-31 13:59:24 -0500 | [diff] [blame] | 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 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 76 | } // namespace rr |