1// [AsmJit]
2// Machine Code Generation for C++.
3//
4// [License]
5// Zlib - See LICENSE.md file in the package.
6
7#ifndef _ASMJIT_CORE_CPUINFO_H
8#define _ASMJIT_CORE_CPUINFO_H
9
10#include "../core/arch.h"
11#include "../core/features.h"
12#include "../core/globals.h"
13#include "../core/string.h"
14
15ASMJIT_BEGIN_NAMESPACE
16
17//! \addtogroup asmjit_support
18//! \{
19
20// ============================================================================
21// [asmjit::CpuInfo]
22// ============================================================================
23
24//! CPU information.
25class CpuInfo {
26public:
27 //! CPU architecture information.
28 ArchInfo _archInfo;
29 //! CPU family ID.
30 uint32_t _familyId;
31 //! CPU model ID.
32 uint32_t _modelId;
33 //! CPU brand ID.
34 uint32_t _brandId;
35 //! CPU stepping.
36 uint32_t _stepping;
37 //! Processor type.
38 uint32_t _processorType;
39 //! Maximum number of addressable IDs for logical processors.
40 uint32_t _maxLogicalProcessors;
41 //! Cache line size (in bytes).
42 uint32_t _cacheLineSize;
43 //! Number of hardware threads.
44 uint32_t _hwThreadCount;
45
46 //! CPU vendor string.
47 FixedString<16> _vendor;
48 //! CPU brand string.
49 FixedString<64> _brand;
50 //! CPU features.
51 BaseFeatures _features;
52
53 //! \name Construction & Destruction
54 //! \{
55
56 inline CpuInfo() noexcept { reset(); }
57 inline CpuInfo(const CpuInfo& other) noexcept = default;
58
59 inline explicit CpuInfo(Globals::NoInit_) noexcept
60 : _archInfo(Globals::NoInit),
61 _features(Globals::NoInit) {};
62
63 //! Returns the host CPU information.
64 ASMJIT_API static const CpuInfo& host() noexcept;
65
66 //! Initializes CpuInfo to the given architecture, see `ArchInfo`.
67 inline void initArch(uint32_t archId, uint32_t archMode = 0) noexcept {
68 _archInfo.init(archId, archMode);
69 }
70
71 inline void reset() noexcept { memset(this, 0, sizeof(*this)); }
72
73 //! \}
74
75 //! \name Overloaded Operators
76 //! \{
77
78 inline CpuInfo& operator=(const CpuInfo& other) noexcept = default;
79
80 //! \}
81
82 //! \name Accessors
83 //! \{
84
85 //! Returns the CPU architecture information.
86 inline const ArchInfo& archInfo() const noexcept { return _archInfo; }
87 //! Returns the CPU architecture id, see `ArchInfo::Id`.
88 inline uint32_t archId() const noexcept { return _archInfo.archId(); }
89 //! Returns the CPU architecture sub-id, see `ArchInfo::SubId`.
90 inline uint32_t archSubId() const noexcept { return _archInfo.archSubId(); }
91
92 //! Returns the CPU family ID.
93 inline uint32_t familyId() const noexcept { return _familyId; }
94 //! Returns the CPU model ID.
95 inline uint32_t modelId() const noexcept { return _modelId; }
96 //! Returns the CPU brand id.
97 inline uint32_t brandId() const noexcept { return _brandId; }
98 //! Returns the CPU stepping.
99 inline uint32_t stepping() const noexcept { return _stepping; }
100 //! Returns the processor type.
101 inline uint32_t processorType() const noexcept { return _processorType; }
102 //! Returns the number of maximum logical processors.
103 inline uint32_t maxLogicalProcessors() const noexcept { return _maxLogicalProcessors; }
104
105 //! Returns the size of a cache line flush.
106 inline uint32_t cacheLineSize() const noexcept { return _cacheLineSize; }
107 //! Returns number of hardware threads available.
108 inline uint32_t hwThreadCount() const noexcept { return _hwThreadCount; }
109
110 //! Returns the CPU vendor.
111 inline const char* vendor() const noexcept { return _vendor.str; }
112 //! Tests whether the CPU vendor is equal to `s`.
113 inline bool isVendor(const char* s) const noexcept { return _vendor.eq(s); }
114
115 //! Returns the CPU brand string.
116 inline const char* brand() const noexcept { return _brand.str; }
117
118 //! Returns all CPU features as `BaseFeatures`, cast to your arch-specific class
119 //! if needed.
120 template<typename T = BaseFeatures>
121 inline const T& features() const noexcept { return _features.as<T>(); }
122
123 //! Tests whether the CPU has the given `feature`.
124 inline bool hasFeature(uint32_t featureId) const noexcept { return _features.has(featureId); }
125 //! Adds the given CPU `feature` to the list of this CpuInfo features.
126 inline CpuInfo& addFeature(uint32_t featureId) noexcept { _features.add(featureId); return *this; }
127
128 //! \}
129};
130
131//! \}
132
133ASMJIT_END_NAMESPACE
134
135#endif // _ASMJIT_CORE_CPUINFO_H
136