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
11// Parts of this file derived from Chromium's base/cpu.cc.
12
13#include "cpu_features_wrapper.h"
14
15#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
23// No CPU feature is available => straight C path.
24int GetCPUInfoNoASM(CPUFeature feature) {
25  (void)feature;
26  return 0;
27}
28
29#if defined(WEBRTC_ARCH_X86_FAMILY)
30#ifndef _MSC_VER
31// Intrinsic for "cpuid".
32#if defined(__pic__) && defined(__i386__)
33static inline void __cpuid(int cpu_info[4], int info_type) {
34  __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}
41#else
42static inline void __cpuid(int cpu_info[4], int info_type) {
43  __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
49#endif  // _MSC_VER
50#endif  // WEBRTC_ARCH_X86_FAMILY
51
52#if defined(WEBRTC_ARCH_X86_FAMILY)
53// Actual feature detection for x86.
54static int GetCPUInfo(CPUFeature feature) {
55  int cpu_info[4];
56  __cpuid(cpu_info, 1);
57  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;
75