cpu.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 BUTIL_CPU_H_
  5. #define BUTIL_CPU_H_
  6. #include <string>
  7. #include "butil/base_export.h"
  8. namespace butil {
  9. // Query information about the processor.
  10. class BUTIL_EXPORT CPU {
  11. public:
  12. // Constructor
  13. CPU();
  14. enum IntelMicroArchitecture {
  15. PENTIUM,
  16. SSE,
  17. SSE2,
  18. SSE3,
  19. SSSE3,
  20. SSE41,
  21. SSE42,
  22. AVX,
  23. MAX_INTEL_MICRO_ARCHITECTURE
  24. };
  25. // Accessors for CPU information.
  26. const std::string& vendor_name() const { return cpu_vendor_; }
  27. int signature() const { return signature_; }
  28. int stepping() const { return stepping_; }
  29. int model() const { return model_; }
  30. int family() const { return family_; }
  31. int type() const { return type_; }
  32. int extended_model() const { return ext_model_; }
  33. int extended_family() const { return ext_family_; }
  34. bool has_mmx() const { return has_mmx_; }
  35. bool has_sse() const { return has_sse_; }
  36. bool has_sse2() const { return has_sse2_; }
  37. bool has_sse3() const { return has_sse3_; }
  38. bool has_ssse3() const { return has_ssse3_; }
  39. bool has_sse41() const { return has_sse41_; }
  40. bool has_sse42() const { return has_sse42_; }
  41. bool has_avx() const { return has_avx_; }
  42. // has_avx_hardware returns true when AVX is present in the CPU. This might
  43. // differ from the value of |has_avx()| because |has_avx()| also tests for
  44. // operating system support needed to actually call AVX instuctions.
  45. // Note: you should never need to call this function. It was added in order
  46. // to workaround a bug in NSS but |has_avx()| is what you want.
  47. bool has_avx_hardware() const { return has_avx_hardware_; }
  48. bool has_aesni() const { return has_aesni_; }
  49. bool has_non_stop_time_stamp_counter() const {
  50. return has_non_stop_time_stamp_counter_;
  51. }
  52. IntelMicroArchitecture GetIntelMicroArchitecture() const;
  53. const std::string& cpu_brand() const { return cpu_brand_; }
  54. private:
  55. // Query the processor for CPUID information.
  56. void Initialize();
  57. int signature_; // raw form of type, family, model, and stepping
  58. int type_; // process type
  59. int family_; // family of the processor
  60. int model_; // model of processor
  61. int stepping_; // processor revision number
  62. int ext_model_;
  63. int ext_family_;
  64. bool has_mmx_;
  65. bool has_sse_;
  66. bool has_sse2_;
  67. bool has_sse3_;
  68. bool has_ssse3_;
  69. bool has_sse41_;
  70. bool has_sse42_;
  71. bool has_avx_;
  72. bool has_avx_hardware_;
  73. bool has_aesni_;
  74. bool has_non_stop_time_stamp_counter_;
  75. std::string cpu_vendor_;
  76. std::string cpu_brand_;
  77. };
  78. } // namespace butil
  79. #endif // BUTIL_CPU_H_