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.
4 
5 #ifndef BASE_CPU_H_
6 #define BASE_CPU_H_
7 
8 #include <string>
9 #include <tuple>
10 
11 #include "base/base_export.h"
12 #include "build/build_config.h"
13 
14 namespace base {
15 
16 #if defined(ARCH_CPU_X86_FAMILY)
17 namespace internal {
18 
19 // Compute the CPU family and model based on the vendor and CPUID signature.
20 // Returns in order: family, model, extended family, extended model.
21 BASE_EXPORT std::tuple<int, int, int, int> ComputeX86FamilyAndModel(
22     const std::string& vendor,
23     int signature);
24 
25 }  // namespace internal
26 #endif  // defined(ARCH_CPU_X86_FAMILY)
27 
28 // Query information about the processor.
29 class BASE_EXPORT CPU final {
30  public:
31   CPU();
32 
33   enum IntelMicroArchitecture {
34     PENTIUM,
35     SSE,
36     SSE2,
37     SSE3,
38     SSSE3,
39     SSE41,
40     SSE42,
41     AVX,
42     AVX2,
43     MAX_INTEL_MICRO_ARCHITECTURE
44   };
45 
46   // Accessors for CPU information.
vendor_name()47   const std::string& vendor_name() const { return cpu_vendor_; }
signature()48   int signature() const { return signature_; }
stepping()49   int stepping() const { return stepping_; }
model()50   int model() const { return model_; }
family()51   int family() const { return family_; }
type()52   int type() const { return type_; }
extended_model()53   int extended_model() const { return ext_model_; }
extended_family()54   int extended_family() const { return ext_family_; }
has_mmx()55   bool has_mmx() const { return has_mmx_; }
has_sse()56   bool has_sse() const { return has_sse_; }
has_sse2()57   bool has_sse2() const { return has_sse2_; }
has_sse3()58   bool has_sse3() const { return has_sse3_; }
has_ssse3()59   bool has_ssse3() const { return has_ssse3_; }
has_sse41()60   bool has_sse41() const { return has_sse41_; }
has_sse42()61   bool has_sse42() const { return has_sse42_; }
has_popcnt()62   bool has_popcnt() const { return has_popcnt_; }
has_avx()63   bool has_avx() const { return has_avx_; }
has_avx2()64   bool has_avx2() const { return has_avx2_; }
has_aesni()65   bool has_aesni() const { return has_aesni_; }
has_non_stop_time_stamp_counter()66   bool has_non_stop_time_stamp_counter() const {
67     return has_non_stop_time_stamp_counter_;
68   }
is_running_in_vm()69   bool is_running_in_vm() const { return is_running_in_vm_; }
70 
71   IntelMicroArchitecture GetIntelMicroArchitecture() const;
cpu_brand()72   const std::string& cpu_brand() const { return cpu_brand_; }
73 
74  private:
75   // Query the processor for CPUID information.
76   void Initialize();
77 
78   int signature_;  // raw form of type, family, model, and stepping
79   int type_;  // process type
80   int family_;  // family of the processor
81   int model_;  // model of processor
82   int stepping_;  // processor revision number
83   int ext_model_;
84   int ext_family_;
85   bool has_mmx_;
86   bool has_sse_;
87   bool has_sse2_;
88   bool has_sse3_;
89   bool has_ssse3_;
90   bool has_sse41_;
91   bool has_sse42_;
92   bool has_popcnt_;
93   bool has_avx_;
94   bool has_avx2_;
95   bool has_aesni_;
96   bool has_non_stop_time_stamp_counter_;
97   bool is_running_in_vm_;
98   std::string cpu_vendor_;
99   std::string cpu_brand_;
100 };
101 
102 }  // namespace base
103 
104 #endif  // BASE_CPU_H_
105