1 // AsmJit - Machine code generation for C++
2 //
3 //  * Official AsmJit Home Page: https://asmjit.com
4 //  * Official Github Repository: https://github.com/asmjit/asmjit
5 //
6 // Copyright (c) 2008-2020 The AsmJit Authors
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 //    claim that you wrote the original software. If you use this software
18 //    in a product, an acknowledgment in the product documentation would be
19 //    appreciated but is not required.
20 // 2. Altered source versions must be plainly marked as such, and must not be
21 //    misrepresented as being the original software.
22 // 3. This notice may not be removed or altered from any source distribution.
23 
24 #ifndef ASMJIT_CORE_CPUINFO_H_INCLUDED
25 #define ASMJIT_CORE_CPUINFO_H_INCLUDED
26 
27 #include "../core/archtraits.h"
28 #include "../core/features.h"
29 #include "../core/globals.h"
30 #include "../core/string.h"
31 
32 ASMJIT_BEGIN_NAMESPACE
33 
34 //! \addtogroup asmjit_core
35 //! \{
36 
37 // ============================================================================
38 // [asmjit::CpuInfo]
39 // ============================================================================
40 
41 //! CPU information.
42 class CpuInfo {
43 public:
44   //! Architecture.
45   uint8_t _arch;
46   //! Sub-architecture.
47   uint8_t _subArch;
48   //! Reserved for future use.
49   uint16_t _reserved;
50   //! CPU family ID.
51   uint32_t _familyId;
52   //! CPU model ID.
53   uint32_t _modelId;
54   //! CPU brand ID.
55   uint32_t _brandId;
56   //! CPU stepping.
57   uint32_t _stepping;
58   //! Processor type.
59   uint32_t _processorType;
60   //! Maximum number of addressable IDs for logical processors.
61   uint32_t _maxLogicalProcessors;
62   //! Cache line size (in bytes).
63   uint32_t _cacheLineSize;
64   //! Number of hardware threads.
65   uint32_t _hwThreadCount;
66 
67   //! CPU vendor string.
68   FixedString<16> _vendor;
69   //! CPU brand string.
70   FixedString<64> _brand;
71   //! CPU features.
72   BaseFeatures _features;
73 
74   //! \name Construction & Destruction
75   //! \{
76 
CpuInfo()77   inline CpuInfo() noexcept { reset(); }
78   inline CpuInfo(const CpuInfo& other) noexcept = default;
79 
CpuInfo(Globals::NoInit_)80   inline explicit CpuInfo(Globals::NoInit_) noexcept
81     : _features(Globals::NoInit) {};
82 
83   //! Returns the host CPU information.
84   ASMJIT_API static const CpuInfo& host() noexcept;
85 
86   //! Initializes CpuInfo to the given architecture, see \ref Environment.
87   inline void initArch(uint32_t arch, uint32_t subArch = 0u) noexcept {
88     _arch = uint8_t(arch);
89     _subArch = uint8_t(subArch);
90   }
91 
reset()92   inline void reset() noexcept { memset(this, 0, sizeof(*this)); }
93 
94   //! \}
95 
96   //! \name Overloaded Operators
97   //! \{
98 
99   inline CpuInfo& operator=(const CpuInfo& other) noexcept = default;
100 
101   //! \}
102 
103   //! \name Accessors
104   //! \{
105 
106   //! Returns the CPU architecture id, see \ref Environment::Arch.
arch()107   inline uint32_t arch() const noexcept { return _arch; }
108   //! Returns the CPU architecture sub-id, see \ref Environment::SubArch.
subArch()109   inline uint32_t subArch() const noexcept { return _subArch; }
110 
111   //! Returns the CPU family ID.
familyId()112   inline uint32_t familyId() const noexcept { return _familyId; }
113   //! Returns the CPU model ID.
modelId()114   inline uint32_t modelId() const noexcept { return _modelId; }
115   //! Returns the CPU brand id.
brandId()116   inline uint32_t brandId() const noexcept { return _brandId; }
117   //! Returns the CPU stepping.
stepping()118   inline uint32_t stepping() const noexcept { return _stepping; }
119   //! Returns the processor type.
processorType()120   inline uint32_t processorType() const noexcept { return _processorType; }
121   //! Returns the number of maximum logical processors.
maxLogicalProcessors()122   inline uint32_t maxLogicalProcessors() const noexcept { return _maxLogicalProcessors; }
123 
124   //! Returns the size of a cache line flush.
cacheLineSize()125   inline uint32_t cacheLineSize() const noexcept { return _cacheLineSize; }
126   //! Returns number of hardware threads available.
hwThreadCount()127   inline uint32_t hwThreadCount() const noexcept { return _hwThreadCount; }
128 
129   //! Returns the CPU vendor.
vendor()130   inline const char* vendor() const noexcept { return _vendor.str; }
131   //! Tests whether the CPU vendor is equal to `s`.
isVendor(const char * s)132   inline bool isVendor(const char* s) const noexcept { return _vendor.eq(s); }
133 
134   //! Returns the CPU brand string.
brand()135   inline const char* brand() const noexcept { return _brand.str; }
136 
137   //! Returns all CPU features as `BaseFeatures`, cast to your arch-specific class
138   //! if needed.
139   template<typename T = BaseFeatures>
features()140   inline const T& features() const noexcept { return _features.as<T>(); }
141 
142   //! Tests whether the CPU has the given `feature`.
hasFeature(uint32_t featureId)143   inline bool hasFeature(uint32_t featureId) const noexcept { return _features.has(featureId); }
144   //! Adds the given CPU `feature` to the list of this CpuInfo features.
addFeature(uint32_t featureId)145   inline CpuInfo& addFeature(uint32_t featureId) noexcept { _features.add(featureId); return *this; }
146 
147   //! \}
148 };
149 
150 //! \}
151 
152 ASMJIT_END_NAMESPACE
153 
154 #endif // ASMJIT_CORE_CPUINFO_H_INCLUDED
155