blob: 41a86e367d69bf3ae228c26a90d09dd2600e9f9b [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
niklase@google.com470e71d2011-07-07 08:21:25 +000013#include "cpu_features_wrapper.h"
14
ajm@google.comce7c2a22011-08-04 01:50:00 +000015#include "typedefs.h"
16
17#if defined(WEBRTC_ARCH_X86_FAMILY)
18#if defined(_MSC_VER)
19#include <intrin.h>
20#endif
21#endif
22
niklase@google.com470e71d2011-07-07 08:21:25 +000023// No CPU feature is available => straight C path.
24int GetCPUInfoNoASM(CPUFeature feature) {
25 (void)feature;
26 return 0;
27}
28
ajm@google.comce7c2a22011-08-04 01:50:00 +000029#if defined(WEBRTC_ARCH_X86_FAMILY)
30#ifndef _MSC_VER
niklase@google.com470e71d2011-07-07 08:21:25 +000031// Intrinsic for "cpuid".
32#if defined(__pic__) && defined(__i386__)
ajm@google.comce7c2a22011-08-04 01:50:00 +000033static inline void __cpuid(int cpu_info[4], int info_type) {
niklase@google.com470e71d2011-07-07 08:21:25 +000034 __asm__ volatile (
35 "mov %%ebx, %%edi\n"
36 "cpuid\n"
37 "xchg %%edi, %%ebx\n"
38 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
39 : "a"(info_type));
40}
ajm@google.comce7c2a22011-08-04 01:50:00 +000041#else
42static inline void __cpuid(int cpu_info[4], int info_type) {
niklase@google.com470e71d2011-07-07 08:21:25 +000043 __asm__ volatile (
44 "cpuid\n"
45 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
46 : "a"(info_type));
47}
48#endif
ajm@google.comce7c2a22011-08-04 01:50:00 +000049#endif // _MSC_VER
50#endif // WEBRTC_ARCH_X86_FAMILY
niklase@google.com470e71d2011-07-07 08:21:25 +000051
ajm@google.comce7c2a22011-08-04 01:50:00 +000052#if defined(WEBRTC_ARCH_X86_FAMILY)
niklase@google.com470e71d2011-07-07 08:21:25 +000053// Actual feature detection for x86.
54static int GetCPUInfo(CPUFeature feature) {
55 int cpu_info[4];
ajm@google.comce7c2a22011-08-04 01:50:00 +000056 __cpuid(cpu_info, 1);
niklase@google.com470e71d2011-07-07 08:21:25 +000057 if (feature == kSSE2) {
58 return 0 != (cpu_info[3] & 0x04000000);
59 }
60 if (feature == kSSE3) {
61 return 0 != (cpu_info[2] & 0x00000001);
62 }
63 return 0;
64}
65#else
66// Default to straight C for other platforms.
67static int GetCPUInfo(CPUFeature feature) {
68 (void)feature;
69 return 0;
70}
71#endif
72
73WebRtc_CPUInfo WebRtc_GetCPUInfo = GetCPUInfo;
74WebRtc_CPUInfo WebRtc_GetCPUInfoNoASM = GetCPUInfoNoASM;