cpu.h 2.8 KB

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