blob: 1667e46c1082f5773fb30f75c4bd7c95f7c2f89a [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 Ma1ca8d872020-07-22 17:34:56 +080027
28// xgetbv returns the value of an Intel Extended Control Register (XCR).
29// Currently only XCR0 is defined by Intel so |xcr| should always be zero.
30uint64_t xgetbv(uint32_t xcr) {
31#if defined(_MSC_VER)
32 return _xgetbv(xcr);
33#else
34 uint32_t eax, edx;
35
36 __asm__ volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
37 return (static_cast<uint64_t>(edx) << 32) | eax;
38#endif // _MSC_VER
39}
40
ajm@google.comce7c2a22011-08-04 01:50:00 +000041#ifndef _MSC_VER
niklase@google.com470e71d2011-07-07 08:21:25 +000042// Intrinsic for "cpuid".
43#if defined(__pic__) && defined(__i386__)
ajm@google.comce7c2a22011-08-04 01:50:00 +000044static inline void __cpuid(int cpu_info[4], int info_type) {
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000045 __asm__ volatile(
Karl Wiberg79eb1d92017-11-08 12:26:07 +010046 "mov %%ebx, %%edi\n"
47 "cpuid\n"
48 "xchg %%edi, %%ebx\n"
49 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]),
50 "=d"(cpu_info[3])
51 : "a"(info_type));
niklase@google.com470e71d2011-07-07 08:21:25 +000052}
ajm@google.comce7c2a22011-08-04 01:50:00 +000053#else
54static inline void __cpuid(int cpu_info[4], int info_type) {
Karl Wiberg79eb1d92017-11-08 12:26:07 +010055 __asm__ volatile("cpuid\n"
56 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]),
57 "=d"(cpu_info[3])
Zhaoliang Ma1ca8d872020-07-22 17:34:56 +080058 : "a"(info_type), "c"(0));
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60#endif
ajm@google.comce7c2a22011-08-04 01:50:00 +000061#endif // _MSC_VER
62#endif // WEBRTC_ARCH_X86_FAMILY
niklase@google.com470e71d2011-07-07 08:21:25 +000063
ajm@google.comce7c2a22011-08-04 01:50:00 +000064#if defined(WEBRTC_ARCH_X86_FAMILY)
niklase@google.com470e71d2011-07-07 08:21:25 +000065// Actual feature detection for x86.
66static int GetCPUInfo(CPUFeature feature) {
67 int cpu_info[4];
Zhaoliang Ma1ca8d872020-07-22 17:34:56 +080068 __cpuid(cpu_info, 0);
69 int num_ids = cpu_info[0];
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 Ma1ca8d872020-07-22 17:34:56 +080077 if (feature == kAVX2) {
78 // Interpret CPU feature information.
79 int cpu_info7[4] = {-1};
80 if (num_ids >= 7) {
81 __cpuid(cpu_info7, 7);
82 }
83
84#if defined(WEBRTC_ENABLE_AVX2)
85 return (cpu_info[2] & 0x10000000) != 0 &&
86 (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ &&
87 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
88 (xgetbv(0) & 0x00000006) == 6 /* XSAVE enabled by kernel */ &&
89 (cpu_info7[1] & 0x00000020) != 0;
90#else
91 return 0;
92#endif // WEBRTC_ENABLE_AVX2
93 }
niklase@google.com470e71d2011-07-07 08:21:25 +000094 return 0;
95}
96#else
97// Default to straight C for other platforms.
98static int GetCPUInfo(CPUFeature feature) {
99 (void)feature;
100 return 0;
101}
102#endif
103
104WebRtc_CPUInfo WebRtc_GetCPUInfo = GetCPUInfo;
105WebRtc_CPUInfo WebRtc_GetCPUInfoNoASM = GetCPUInfoNoASM;