niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [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 +0000 | [diff] [blame] | 11 | // Parts of this file derived from Chromium's base/cpu.cc. |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "system_wrappers/include/cpu_features_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 15 | #if defined(WEBRTC_ARCH_X86_FAMILY) && defined(_MSC_VER) |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 +0000 | [diff] [blame] | 16 | #include <intrin.h> |
| 17 | #endif |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 19 | #include "typedefs.h" // NOLINT(build/include) |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 +0000 | [diff] [blame] | 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | // No CPU feature is available => straight C path. |
| 22 | int GetCPUInfoNoASM(CPUFeature feature) { |
| 23 | (void)feature; |
| 24 | return 0; |
| 25 | } |
| 26 | |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 +0000 | [diff] [blame] | 27 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 28 | #ifndef _MSC_VER |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | // Intrinsic for "cpuid". |
| 30 | #if defined(__pic__) && defined(__i386__) |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 +0000 | [diff] [blame] | 31 | static inline void __cpuid(int cpu_info[4], int info_type) { |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 32 | __asm__ volatile( |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 33 | "mov %%ebx, %%edi\n" |
| 34 | "cpuid\n" |
| 35 | "xchg %%edi, %%ebx\n" |
| 36 | : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), |
| 37 | "=d"(cpu_info[3]) |
| 38 | : "a"(info_type)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 +0000 | [diff] [blame] | 40 | #else |
| 41 | static inline void __cpuid(int cpu_info[4], int info_type) { |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 42 | __asm__ volatile("cpuid\n" |
| 43 | : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), |
| 44 | "=d"(cpu_info[3]) |
| 45 | : "a"(info_type)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
| 47 | #endif |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 +0000 | [diff] [blame] | 48 | #endif // _MSC_VER |
| 49 | #endif // WEBRTC_ARCH_X86_FAMILY |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 +0000 | [diff] [blame] | 51 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | // Actual feature detection for x86. |
| 53 | static int GetCPUInfo(CPUFeature feature) { |
| 54 | int cpu_info[4]; |
ajm@google.com | ce7c2a2 | 2011-08-04 01:50:00 +0000 | [diff] [blame] | 55 | __cpuid(cpu_info, 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | if (feature == kSSE2) { |
| 57 | return 0 != (cpu_info[3] & 0x04000000); |
| 58 | } |
| 59 | if (feature == kSSE3) { |
| 60 | return 0 != (cpu_info[2] & 0x00000001); |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | #else |
| 65 | // Default to straight C for other platforms. |
| 66 | static int GetCPUInfo(CPUFeature feature) { |
| 67 | (void)feature; |
| 68 | return 0; |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | WebRtc_CPUInfo WebRtc_GetCPUInfo = GetCPUInfo; |
| 73 | WebRtc_CPUInfo WebRtc_GetCPUInfoNoASM = GetCPUInfoNoASM; |