Botan 3.14.1
Crypto and TLS for C&
cpuid.h
Go to the documentation of this file.
1/*
2* Runtime CPU detection
3* (C) 2009,2010,2013,2017,2023 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_CPUID_H_
9#define BOTAN_CPUID_H_
10
11#include <botan/types.h>
12#include <optional>
13#include <string>
14
15#if defined(BOTAN_HAS_CPUID_DETECTION)
16 #include <botan/internal/cpuid_features.h>
17#endif
18
19namespace Botan {
20
21#if !defined(BOTAN_HAS_CPUID_DETECTION)
22// A no-op CPUFeature
24 public:
25 enum Bit : uint32_t {};
26
27 uint32_t as_u32() const;
28
30
31 static std::optional<CPUFeature> from_string(std::string_view);
32
33 std::string to_string() const;
34};
35#endif
36
37/**
38* A class handling runtime CPU feature detection. It is limited to
39* just the features necessary to implement CPU specific code in Botan,
40* rather than being a general purpose utility.
41*/
42class BOTAN_TEST_API CPUID final {
43 public:
45
46 /**
47 * Probe the CPU and see what extensions are supported
48 */
49 static void initialize();
50
51 /**
52 * Return a possibly empty string containing list of known CPU
53 * extensions. Each name will be separated by a space, and the ordering
54 * will be arbitrary. This list only contains values that are useful to
55 * Botan (for example FMA instructions are not checked).
56 *
57 * Example outputs "sse2 ssse3 rdtsc", "neon arm_aes", "vsx"
58 */
59 static std::string to_string();
60
61 /**
62 * Check if a feature is supported returning the associated string if so
63 *
64 * This is a helper function used to implement provider()
65 */
66 static std::optional<std::string> check(CPUID::Feature feat) {
67 if(state().has_bit(feat.as_u32())) {
68 return feat.to_string();
69 } else {
70 return {};
71 }
72 }
73
74 /**
75 * Check if a feature is supported returning the associated string if so
76 *
77 * This is a helper function used to implement provider()
78 */
79 static std::optional<std::string> check(CPUID::Feature feat1, CPUID::Feature feat2) {
80 if(state().has_bit((feat1.as_u32() | feat2.as_u32()))) {
81 // Typically feat2 is a secondary feature that is almost but not
82 // completely implied by feat1 (ex: AVX2 + BMI2) which we have to
83 // check for completeness, but don't reflect into the provider name.
84 return feat1.to_string();
85 } else {
86 return {};
87 }
88 }
89
90 /**
91 * Check if a feature is supported
92 */
93 static bool has(CPUID::Feature feat) { return state().has_bit(feat.as_u32()); }
94
95 /**
96 * Check if two features are both supported
97 */
98 static bool has(CPUID::Feature feat1, CPUID::Feature feat2) {
99 return state().has_bit(feat1.as_u32() | feat2.as_u32());
100 }
101
102 /*
103 * Clear a CPUID bit
104 * Call CPUID::initialize to reset
105 *
106 * This is only exposed for testing and should never be called within the library
107 */
108 static void clear_cpuid_bit(CPUID::Feature bit) { state().clear_cpuid_bit(bit.as_u32()); }
109
110 static std::optional<CPUID::Feature> bit_from_string(std::string_view tok);
111
112 /**
113 * A common helper for the various CPUID implementations
114 */
115 template <typename T>
116 static inline uint32_t if_set(uint64_t cpuid, T flag, CPUID::Feature bit, uint32_t allowed) {
117 const uint64_t flag64 = static_cast<uint64_t>(flag);
118 if((cpuid & flag64) == flag64) {
119 return (bit.as_u32() & allowed);
120 } else {
121 return 0;
122 }
123 }
124
125 private:
126 static inline bool is_set(uint32_t allowed, CPUID::Feature bit) {
127 const uint32_t feat_bit = bit.as_u32();
128 return ((allowed & feat_bit) == feat_bit);
129 }
130
131 struct CPUID_Data {
132 public:
133 CPUID_Data();
134
135 CPUID_Data(const CPUID_Data& other) = default;
136 CPUID_Data(CPUID_Data&& other) = default;
137 CPUID_Data& operator=(const CPUID_Data& other) = default;
138 CPUID_Data& operator=(CPUID_Data&& other) = default;
139 ~CPUID_Data() = default;
140
141 void clear_cpuid_bit(uint32_t bit) { m_processor_features &= ~bit; }
142
143 bool has_bit(uint32_t bit) const { return (m_processor_features & bit) == bit; }
144
145 uint32_t bitset() const { return m_processor_features; }
146
147 private:
148#if defined(BOTAN_HAS_CPUID_DETECTION)
149 static uint32_t detect_cpu_features(uint32_t allowed_bits);
150#endif
151
152 uint32_t m_processor_features;
153 };
154
155 static CPUID_Data& state() {
156 static CPUID::CPUID_Data g_cpuid;
157 return g_cpuid;
158 }
159};
160
161} // namespace Botan
162
163#endif
#define BOTAN_TEST_API
Definition api.h:41
uint32_t as_u32() const
Definition cpuid.cpp:20
CPUFeature(Bit)
Definition cpuid.h:29
std::string to_string() const
Definition cpuid.cpp:28
static std::optional< std::string > check(CPUID::Feature feat)
Definition cpuid.h:66
static bool has(CPUID::Feature feat1, CPUID::Feature feat2)
Definition cpuid.h:98
static uint32_t if_set(uint64_t cpuid, T flag, CPUID::Feature bit, uint32_t allowed)
Definition cpuid.h:116
CPUFeature Feature
Definition cpuid.h:44
static void clear_cpuid_bit(CPUID::Feature bit)
Definition cpuid.h:108
static std::string to_string()
Definition cpuid.cpp:34
static void initialize()
Definition cpuid.cpp:51
static std::optional< std::string > check(CPUID::Feature feat1, CPUID::Feature feat2)
Definition cpuid.h:79
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13