Add OS_IOS defines for the mobile promo.
[chromium-blink-merge.git] / base / cpu.cc
bloba07783478fa65791b9016103d123322d6e2314c9
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/cpu.h"
7 #include <string.h>
9 #include "build/build_config.h"
11 #if defined(ARCH_CPU_X86_FAMILY)
12 #if defined(_MSC_VER)
13 #include <intrin.h>
14 #endif
15 #endif
17 namespace base {
19 CPU::CPU()
20 : type_(0),
21 family_(0),
22 model_(0),
23 stepping_(0),
24 ext_model_(0),
25 ext_family_(0),
26 has_mmx_(false),
27 has_sse_(false),
28 has_sse2_(false),
29 has_sse3_(false),
30 has_ssse3_(false),
31 has_sse41_(false),
32 has_sse42_(false),
33 cpu_vendor_("unknown") {
34 Initialize();
37 #if defined(ARCH_CPU_X86_FAMILY)
38 #ifndef _MSC_VER
40 #if defined(__pic__) && defined(__i386__)
42 void __cpuid(int cpu_info[4], int info_type) {
43 __asm__ volatile (
44 "mov %%ebx, %%edi\n"
45 "cpuid\n"
46 "xchg %%edi, %%ebx\n"
47 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
48 : "a"(info_type)
52 void __cpuidex(int cpu_info[4], int info_type, int info_index) {
53 __asm__ volatile (
54 "mov %%ebx, %%edi\n"
55 "cpuid\n"
56 "xchg %%edi, %%ebx\n"
57 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
58 : "a"(info_type), "c"(info_index)
62 #else
64 void __cpuid(int cpu_info[4], int info_type) {
65 __asm__ volatile (
66 "cpuid \n\t"
67 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
68 : "a"(info_type)
72 void __cpuidex(int cpu_info[4], int info_type, int info_index) {
73 __asm__ volatile (
74 "cpuid \n\t"
75 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
76 : "a"(info_type), "c"(info_index)
80 #endif
81 #endif // _MSC_VER
82 #endif // ARCH_CPU_X86_FAMILY
84 void CPU::Initialize() {
85 #if defined(ARCH_CPU_X86_FAMILY)
86 int cpu_info[4] = {-1};
87 char cpu_string[0x20];
89 // __cpuid with an InfoType argument of 0 returns the number of
90 // valid Ids in CPUInfo[0] and the CPU identification string in
91 // the other three array elements. The CPU identification string is
92 // not in linear order. The code below arranges the information
93 // in a human readable form.
95 // More info can be found here:
96 // http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
97 __cpuid(cpu_info, 0);
98 int num_ids = cpu_info[0];
99 memset(cpu_string, 0, sizeof(cpu_string));
100 *(reinterpret_cast<int*>(cpu_string)) = cpu_info[1];
101 *(reinterpret_cast<int*>(cpu_string+4)) = cpu_info[3];
102 *(reinterpret_cast<int*>(cpu_string+8)) = cpu_info[2];
104 // Interpret CPU feature information.
105 if (num_ids > 0) {
106 __cpuid(cpu_info, 1);
107 stepping_ = cpu_info[0] & 0xf;
108 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
109 family_ = (cpu_info[0] >> 8) & 0xf;
110 type_ = (cpu_info[0] >> 12) & 0x3;
111 ext_model_ = (cpu_info[0] >> 16) & 0xf;
112 ext_family_ = (cpu_info[0] >> 20) & 0xff;
113 cpu_vendor_ = cpu_string;
114 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
115 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
116 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
117 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
118 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
119 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
120 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
122 #endif
125 } // namespace base