blob: 40110ed87185b3797050e7a376a3bde4397c6023 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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.comce7c2a22011-08-04 01:50:00 +000011// Parts of this file derived from Chromium's base/cpu.cc.
12
Niels Möllera12c42a2018-07-25 16:05:48 +020013#include "rtc_base/system/arch.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "system_wrappers/include/cpu_features_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000016#if defined(WEBRTC_ARCH_X86_FAMILY) && defined(_MSC_VER)
ajm@google.comce7c2a22011-08-04 01:50:00 +000017#include <intrin.h>
18#endif
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000019
niklase@google.com470e71d2011-07-07 08:21:25 +000020// No CPU feature is available => straight C path.
21int GetCPUInfoNoASM(CPUFeature feature) {
22 (void)feature;
23 return 0;
24}
25
ajm@google.comce7c2a22011-08-04 01:50:00 +000026#if defined(WEBRTC_ARCH_X86_FAMILY)
Zhaoliang Ma72e43212020-08-17 17:13:41 +080027
28#if defined(WEBRTC_ENABLE_AVX2)
29// xgetbv returns the value of an Intel Extended Control Register (XCR).
30// Currently only XCR0 is defined by Intel so |xcr| should always be zero.
31static uint64_t xgetbv(uint32_t xcr) {
32#if defined(_MSC_VER)
33 return _xgetbv(xcr);
34#else
35 uint32_t eax, edx;
36
37 __asm__ volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
38 return (static_cast<uint64_t>(edx) << 32) | eax;
39#endif // _MSC_VER
40}
41#endif // WEBRTC_ENABLE_AVX2
42
ajm@google.comce7c2a22011-08-04 01:50:00 +000043#ifndef _MSC_VER
niklase@google.com470e71d2011-07-07 08:21:25 +000044// Intrinsic for "cpuid".
45#if defined(__pic__) && defined(__i386__)
ajm@google.comce7c2a22011-08-04 01:50:00 +000046static inline void __cpuid(int cpu_info[4], int info_type) {
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000047 __asm__ volatile(
Karl Wiberg79eb1d92017-11-08 12:26:07 +010048 "mov %%ebx, %%edi\n"
49 "cpuid\n"
50 "xchg %%edi, %%ebx\n"
51 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]),
52 "=d"(cpu_info[3])
53 : "a"(info_type));
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
ajm@google.comce7c2a22011-08-04 01:50:00 +000055#else
56static inline void __cpuid(int cpu_info[4], int info_type) {
Karl Wiberg79eb1d92017-11-08 12:26:07 +010057 __asm__ volatile("cpuid\n"
58 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]),
59 "=d"(cpu_info[3])
Zhaoliang Ma72e43212020-08-17 17:13:41 +080060 : "a"(info_type), "c"(0));
niklase@google.com470e71d2011-07-07 08:21:25 +000061}
62#endif
ajm@google.comce7c2a22011-08-04 01:50:00 +000063#endif // _MSC_VER
64#endif // WEBRTC_ARCH_X86_FAMILY
niklase@google.com470e71d2011-07-07 08:21:25 +000065
ajm@google.comce7c2a22011-08-04 01:50:00 +000066#if defined(WEBRTC_ARCH_X86_FAMILY)
niklase@google.com470e71d2011-07-07 08:21:25 +000067// Actual feature detection for x86.
68static int GetCPUInfo(CPUFeature feature) {
69 int cpu_info[4];
ajm@google.comce7c2a22011-08-04 01:50:00 +000070 __cpuid(cpu_info, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +000071 if (feature == kSSE2) {
72 return 0 != (cpu_info[3] & 0x04000000);
73 }
74 if (feature == kSSE3) {
75 return 0 != (cpu_info[2] & 0x00000001);
76 }
Zhaoliang Ma72e43212020-08-17 17:13:41 +080077#if defined(WEBRTC_ENABLE_AVX2)
78 if (feature == kAVX2) {
79 int cpu_info7[4];
80 __cpuid(cpu_info7, 0);
81 int num_ids = cpu_info7[0];
82 if (num_ids < 7) {
83 return 0;
84 }
85 // Interpret CPU feature information.
86 __cpuid(cpu_info7, 7);
87
88 // AVX instructions can be used when
89 // a) AVX are supported by the CPU,
90 // b) XSAVE is supported by the CPU,
91 // c) XSAVE is enabled by the kernel.
92 // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled
93 // AVX2 support needs (avx_support && (cpu_info7[1] & 0x00000020) != 0;).
94 return (cpu_info[2] & 0x10000000) != 0 &&
95 (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ &&
96 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
97 (xgetbv(0) & 0x00000006) == 6 /* XSAVE enabled by kernel */ &&
98 (cpu_info7[1] & 0x00000020) != 0;
99 }
100#endif // WEBRTC_ENABLE_AVX2
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 return 0;
102}
103#else
104// Default to straight C for other platforms.
105static int GetCPUInfo(CPUFeature feature) {
106 (void)feature;
107 return 0;
108}
109#endif
110
111WebRtc_CPUInfo WebRtc_GetCPUInfo = GetCPUInfo;
112WebRtc_CPUInfo WebRtc_GetCPUInfoNoASM = GetCPUInfoNoASM;