blob: ebcb48c15fb20ddeda6c5844e097d7b2835cbd81 [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)
27#ifndef _MSC_VER
niklase@google.com470e71d2011-07-07 08:21:25 +000028// Intrinsic for "cpuid".
29#if defined(__pic__) && defined(__i386__)
ajm@google.comce7c2a22011-08-04 01:50:00 +000030static inline void __cpuid(int cpu_info[4], int info_type) {
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000031 __asm__ volatile(
Karl Wiberg79eb1d92017-11-08 12:26:07 +010032 "mov %%ebx, %%edi\n"
33 "cpuid\n"
34 "xchg %%edi, %%ebx\n"
35 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]),
36 "=d"(cpu_info[3])
37 : "a"(info_type));
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
ajm@google.comce7c2a22011-08-04 01:50:00 +000039#else
40static inline void __cpuid(int cpu_info[4], int info_type) {
Karl Wiberg79eb1d92017-11-08 12:26:07 +010041 __asm__ volatile("cpuid\n"
42 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]),
43 "=d"(cpu_info[3])
Åsa Persson0c9204c2020-07-30 17:31:44 +000044 : "a"(info_type));
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46#endif
ajm@google.comce7c2a22011-08-04 01:50:00 +000047#endif // _MSC_VER
48#endif // WEBRTC_ARCH_X86_FAMILY
niklase@google.com470e71d2011-07-07 08:21:25 +000049
ajm@google.comce7c2a22011-08-04 01:50:00 +000050#if defined(WEBRTC_ARCH_X86_FAMILY)
niklase@google.com470e71d2011-07-07 08:21:25 +000051// Actual feature detection for x86.
52static int GetCPUInfo(CPUFeature feature) {
53 int cpu_info[4];
ajm@google.comce7c2a22011-08-04 01:50:00 +000054 __cpuid(cpu_info, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +000055 if (feature == kSSE2) {
56 return 0 != (cpu_info[3] & 0x04000000);
57 }
58 if (feature == kSSE3) {
59 return 0 != (cpu_info[2] & 0x00000001);
60 }
niklase@google.com470e71d2011-07-07 08:21:25 +000061 return 0;
62}
63#else
64// Default to straight C for other platforms.
65static int GetCPUInfo(CPUFeature feature) {
66 (void)feature;
67 return 0;
68}
69#endif
70
71WebRtc_CPUInfo WebRtc_GetCPUInfo = GetCPUInfo;
72WebRtc_CPUInfo WebRtc_GetCPUInfoNoASM = GetCPUInfoNoASM;