niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 | [diff] [blame] | 11 | // Parts of this file derived from Chromium's base/cpu.cc. |
| 12 | |
Niels Möller | a12c42a | 2018-07-25 14:05:48 | [diff] [blame] | 13 | #include "rtc_base/system/arch.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 14 | #include "system_wrappers/include/cpu_features_wrapper.h" |
Per Åhgren | a43178c | 2020-09-25 10:02:32 | [diff] [blame] | 15 | #include "system_wrappers/include/field_trial.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 16 | |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 | [diff] [blame] | 17 | #if defined(WEBRTC_ARCH_X86_FAMILY) && defined(_MSC_VER) |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 | [diff] [blame] | 18 | #include <intrin.h> |
| 19 | #endif |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 | [diff] [blame] | 20 | |
Mirko Bonadei | bef022b | 2020-09-06 14:07:15 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 23 | // No CPU feature is available => straight C path. |
| 24 | int GetCPUInfoNoASM(CPUFeature feature) { |
| 25 | (void)feature; |
| 26 | return 0; |
| 27 | } |
| 28 | |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 | [diff] [blame] | 29 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
Zhaoliang Ma | 72e4321 | 2020-08-17 09:13:41 | [diff] [blame] | 30 | |
| 31 | #if defined(WEBRTC_ENABLE_AVX2) |
| 32 | // xgetbv returns the value of an Intel Extended Control Register (XCR). |
Artem Titov | f067192 | 2021-07-27 10:40:17 | [diff] [blame] | 33 | // Currently only XCR0 is defined by Intel so `xcr` should always be zero. |
Zhaoliang Ma | 72e4321 | 2020-08-17 09:13:41 | [diff] [blame] | 34 | static uint64_t xgetbv(uint32_t xcr) { |
| 35 | #if defined(_MSC_VER) |
| 36 | return _xgetbv(xcr); |
| 37 | #else |
| 38 | uint32_t eax, edx; |
| 39 | |
| 40 | __asm__ volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); |
| 41 | return (static_cast<uint64_t>(edx) << 32) | eax; |
| 42 | #endif // _MSC_VER |
| 43 | } |
| 44 | #endif // WEBRTC_ENABLE_AVX2 |
| 45 | |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 | [diff] [blame] | 46 | #ifndef _MSC_VER |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 47 | // Intrinsic for "cpuid". |
| 48 | #if defined(__pic__) && defined(__i386__) |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 | [diff] [blame] | 49 | static inline void __cpuid(int cpu_info[4], int info_type) { |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 | [diff] [blame] | 50 | __asm__ volatile( |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 51 | "mov %%ebx, %%edi\n" |
| 52 | "cpuid\n" |
| 53 | "xchg %%edi, %%ebx\n" |
| 54 | : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), |
| 55 | "=d"(cpu_info[3]) |
| 56 | : "a"(info_type)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 57 | } |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 | [diff] [blame] | 58 | #else |
| 59 | static inline void __cpuid(int cpu_info[4], int info_type) { |
Karl Wiberg | 79eb1d9 | 2017-11-08 11:26:07 | [diff] [blame] | 60 | __asm__ volatile("cpuid\n" |
| 61 | : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), |
| 62 | "=d"(cpu_info[3]) |
Zhaoliang Ma | 72e4321 | 2020-08-17 09:13:41 | [diff] [blame] | 63 | : "a"(info_type), "c"(0)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 64 | } |
| 65 | #endif |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 | [diff] [blame] | 66 | #endif // _MSC_VER |
| 67 | #endif // WEBRTC_ARCH_X86_FAMILY |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 68 | |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 | [diff] [blame] | 69 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 70 | // Actual feature detection for x86. |
Mirko Bonadei | bef022b | 2020-09-06 14:07:15 | [diff] [blame] | 71 | int GetCPUInfo(CPUFeature feature) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 72 | int cpu_info[4]; |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 | [diff] [blame] | 73 | __cpuid(cpu_info, 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 74 | if (feature == kSSE2) { |
| 75 | return 0 != (cpu_info[3] & 0x04000000); |
| 76 | } |
| 77 | if (feature == kSSE3) { |
| 78 | return 0 != (cpu_info[2] & 0x00000001); |
| 79 | } |
Zhaoliang Ma | 72e4321 | 2020-08-17 09:13:41 | [diff] [blame] | 80 | #if defined(WEBRTC_ENABLE_AVX2) |
Per Åhgren | a43178c | 2020-09-25 10:02:32 | [diff] [blame] | 81 | if (feature == kAVX2 && |
| 82 | !webrtc::field_trial::IsEnabled("WebRTC-Avx2SupportKillSwitch")) { |
Zhaoliang Ma | 72e4321 | 2020-08-17 09:13:41 | [diff] [blame] | 83 | int cpu_info7[4]; |
| 84 | __cpuid(cpu_info7, 0); |
| 85 | int num_ids = cpu_info7[0]; |
| 86 | if (num_ids < 7) { |
| 87 | return 0; |
| 88 | } |
| 89 | // Interpret CPU feature information. |
| 90 | __cpuid(cpu_info7, 7); |
| 91 | |
| 92 | // AVX instructions can be used when |
| 93 | // a) AVX are supported by the CPU, |
| 94 | // b) XSAVE is supported by the CPU, |
| 95 | // c) XSAVE is enabled by the kernel. |
Gustaf Ullberg | 8b8d0b5 | 2022-04-29 06:54:29 | [diff] [blame] | 96 | // Compiling with MSVC and /arch:AVX2 surprisingly generates BMI2 |
| 97 | // instructions (see crbug.com/1315519). |
| 98 | return (cpu_info[2] & 0x10000000) != 0 /* AVX */ && |
Zhaoliang Ma | 72e4321 | 2020-08-17 09:13:41 | [diff] [blame] | 99 | (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ && |
| 100 | (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ && |
| 101 | (xgetbv(0) & 0x00000006) == 6 /* XSAVE enabled by kernel */ && |
Gustaf Ullberg | 8b8d0b5 | 2022-04-29 06:54:29 | [diff] [blame] | 102 | (cpu_info7[1] & 0x00000020) != 0 /* AVX2 */ && |
| 103 | (cpu_info7[1] & 0x00000100) != 0 /* BMI2 */; |
Zhaoliang Ma | 72e4321 | 2020-08-17 09:13:41 | [diff] [blame] | 104 | } |
| 105 | #endif // WEBRTC_ENABLE_AVX2 |
Henrik Lundin | cb4b0a6 | 2023-01-31 15:49:55 | [diff] [blame] | 106 | if (feature == kFMA3) { |
| 107 | return 0 != (cpu_info[2] & 0x00001000); |
| 108 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | #else |
| 112 | // Default to straight C for other platforms. |
Mirko Bonadei | bef022b | 2020-09-06 14:07:15 | [diff] [blame] | 113 | int GetCPUInfo(CPUFeature feature) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 114 | (void)feature; |
| 115 | return 0; |
| 116 | } |
| 117 | #endif |
| 118 | |
Mirko Bonadei | bef022b | 2020-09-06 14:07:15 | [diff] [blame] | 119 | } // namespace webrtc |