Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Classes | Public Types | Static Public Member Functions | List of all members
Botan::CPUID Class Referencefinal

`#include <cpuid.h>`

## Public Types
enum | CPUID_bits : uint64_t { CPUID_INITIALIZED_BIT = (1ULL << 63) }

## Static Public Member Functions
static std::vector< CPUID::CPUID_bits > | bit_from_string (const std::string &tok)
static size_t | cache_line_size ()
static void | clear_cpuid_bit (CPUID_bits bit)
static bool | has_carryless_multiply ()
static bool | has_cpuid_bit (CPUID_bits elem)
static bool | has_simd_32 ()
static bool | has_vperm ()
static void | initialize ()
static bool | is_big_endian ()
static bool | is_little_endian ()
static void | print (std::ostream &o)
static std::string | to_string ()

## Detailed Description

A class handling runtime CPU feature detection. It is limited to just the features necessary to implement CPU specific code in Botan, rather than being a general purpose utility.

This class supports:

- x86 features using CPUID. x86 is also the only processor with accurate cache line detection currently.
- PowerPC AltiVec detection on Linux, NetBSD, OpenBSD, and macOS
- ARM NEON and crypto extensions detection. On Linux and Android systems which support getauxval, that is used to access CPU feature information. Otherwise a relatively portable but thread-unsafe mechanism involving executing probe functions which catching SIGILL signal is used.

Definition at line 38 of file cpuid.h.

## Member Enumeration Documentation

enum Botan::CPUID::CPUID_bits : uint64_t

Enumerator
CPUID_INITIALIZED_BIT |

Definition at line 95 of file cpuid.h.

95 : uint64_t {
96 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
97 // These values have no relation to cpuid bitfields
98
99 // SIMD instruction sets
100 CPUID_SSE2_BIT = (1ULL << 0),
101 CPUID_SSSE3_BIT = (1ULL << 1),
102 CPUID_SSE41_BIT = (1ULL << 2),
103 CPUID_SSE42_BIT = (1ULL << 3),
104 CPUID_AVX2_BIT = (1ULL << 4),
105 CPUID_AVX512F_BIT = (1ULL << 5),
106
107 // Misc useful instructions
108 CPUID_RDTSC_BIT = (1ULL << 10),
109 CPUID_BMI2_BIT = (1ULL << 11),
110 CPUID_ADX_BIT = (1ULL << 12),
111 CPUID_BMI1_BIT = (1ULL << 13),
112
113 // Crypto-specific ISAs
114 CPUID_AESNI_BIT = (1ULL << 16),
115 CPUID_CLMUL_BIT = (1ULL << 17),
116 CPUID_RDRAND_BIT = (1ULL << 18),
117 CPUID_RDSEED_BIT = (1ULL << 19),
118 CPUID_SHA_BIT = (1ULL << 20),
119 #endif
120
121 #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
122 CPUID_ALTIVEC_BIT = (1ULL << 0),
123 CPUID_POWER_CRYPTO_BIT = (1ULL << 1),
124 CPUID_DARN_BIT = (1ULL << 2),
125 #endif
126
127 #if defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
128 CPUID_ARM_NEON_BIT = (1ULL << 0),
129 CPUID_ARM_SVE_BIT = (1ULL << 1),
130 CPUID_ARM_AES_BIT = (1ULL << 16),
131 CPUID_ARM_PMULL_BIT = (1ULL << 17),
132 CPUID_ARM_SHA1_BIT = (1ULL << 18),
133 CPUID_ARM_SHA2_BIT = (1ULL << 19),
134 CPUID_ARM_SHA3_BIT = (1ULL << 20),
135 CPUID_ARM_SHA2_512_BIT = (1ULL << 21),
136 CPUID_ARM_SM3_BIT = (1ULL << 22),
137 CPUID_ARM_SM4_BIT = (1ULL << 23),
138 #endif
139
140 CPUID_INITIALIZED_BIT = (1ULL << 63)
141 };
Botan::CPUID::CPUID_INITIALIZED_BIT
Definition: cpuid.h:140

## Member Function Documentation

std::vector< Botan::CPUID::CPUID_bits > Botan::CPUID::bit_from_string | ( | const std::string & | tok | ) |
| static

Definition at line 145 of file cpuid.cpp.

References BOTAN_UNUSED.

146 {
147 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
148 if(tok == "sse2" || tok == "simd")
149 return {Botan::CPUID::CPUID_SSE2_BIT};
150 if(tok == "ssse3")
151 return {Botan::CPUID::CPUID_SSSE3_BIT};
152 if(tok == "aesni")
153 return {Botan::CPUID::CPUID_AESNI_BIT};
154 if(tok == "clmul")
155 return {Botan::CPUID::CPUID_CLMUL_BIT};
156 if(tok == "avx2")
157 return {Botan::CPUID::CPUID_AVX2_BIT};
158 if(tok == "sha")
159 return {Botan::CPUID::CPUID_SHA_BIT};
160 if(tok == "bmi2")
161 return {Botan::CPUID::CPUID_BMI2_BIT};
162 if(tok == "adx")
163 return {Botan::CPUID::CPUID_ADX_BIT};
164 if(tok == "intel_sha")
165 return {Botan::CPUID::CPUID_SHA_BIT};
166
167 #elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
168 if(tok == "altivec" || tok == "simd")
169 return {Botan::CPUID::CPUID_ALTIVEC_BIT};
170 if(tok == "power_crypto")
171 return {Botan::CPUID::CPUID_POWER_CRYPTO_BIT};
172
173 #elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
174 if(tok == "neon" || tok == "simd")
175 return {Botan::CPUID::CPUID_ARM_NEON_BIT};
176 if(tok == "armv8sha1")
177 return {Botan::CPUID::CPUID_ARM_SHA1_BIT};
178 if(tok == "armv8sha2")
179 return {Botan::CPUID::CPUID_ARM_SHA2_BIT};
180 if(tok == "armv8aes")
181 return {Botan::CPUID::CPUID_ARM_AES_BIT};
182 if(tok == "armv8pmull")
183 return {Botan::CPUID::CPUID_ARM_PMULL_BIT};
184 if(tok == "armv8sha3")
185 return {Botan::CPUID::CPUID_ARM_SHA3_BIT};
186 if(tok == "armv8sha2_512")
187 return {Botan::CPUID::CPUID_ARM_SHA2_512_BIT};
188 if(tok == "armv8sm3")
189 return {Botan::CPUID::CPUID_ARM_SM3_BIT};
190 if(tok == "armv8sm4")
191 return {Botan::CPUID::CPUID_ARM_SM4_BIT};
192
193 #else
194 BOTAN_UNUSED(tok);
195 #endif
196
197 return {};
198 }
BOTAN_UNUSED
#define BOTAN_UNUSED(...)
Definition: assert.h:142

static size_t Botan::CPUID::cache_line_size | ( |  | ) |
| inline static

Return a best guess of the cache line size

Definition at line 68 of file cpuid.h.

Referenced by Botan::prefetch_readonly(), and Botan::prefetch_readwrite().

69 {
70 return state().cache_line_size();
71 }

static void Botan::CPUID::clear_cpuid_bit | ( | CPUID_bits | bit | ) |
| inline static

Definition at line 359 of file cpuid.h.

360 {
361 state().clear_cpuid_bit(static_cast<uint64_t>(bit));
362 }

static bool Botan::CPUID::has_carryless_multiply | ( |  | ) |
| inline static

Check if the processor supports carryless multiply (CLMUL, PMULL)

Definition at line 341 of file cpuid.h.

Referenced by Botan::GHASH::provider().

342 {
343 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
344 return has_clmul();
345 #elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
346 return has_arm_pmull();
347 #else
348 return false;
349 #endif
350 }

static bool Botan::CPUID::has_cpuid_bit | ( | CPUID_bits | elem | ) |
| inline static

Definition at line 368 of file cpuid.h.

369 {
370 const uint64_t elem64 = static_cast<uint64_t>(elem);
371 return state().has_bit(elem64);
372 }

bool Botan::CPUID::has_simd_32 | ( |  | ) |
| static

Definition at line 16 of file cpuid.cpp.

Referenced by Botan::Noekeon::decrypt_n(), Botan::SHACAL2::decrypt_n(), Botan::Serpent::decrypt_n(), Botan::Noekeon::encrypt_n(), Botan::SHACAL2::encrypt_n(), Botan::Serpent::encrypt_n(), Botan::SHACAL2::parallelism(), Botan::Noekeon::parallelism(), Botan::Noekeon::provider(), Botan::SHACAL2::provider(), Botan::Serpent::provider(), and Botan::ChaCha::provider().

17 {
18 #if defined(BOTAN_TARGET_SUPPORTS_SSE2)
19 return CPUID::has_sse2();
20 #elif defined(BOTAN_TARGET_SUPPORTS_ALTIVEC)
21 return CPUID::has_altivec();
22 #elif defined(BOTAN_TARGET_SUPPORTS_NEON)
23 return CPUID::has_neon();
24 #else
25 return true;
26 #endif
27 }

static bool Botan::CPUID::has_vperm | ( |  | ) |
| inline static

Check if the processor supports byte-level vector permutes (SSSE3, NEON, Altivec)

Definition at line 324 of file cpuid.h.

Referenced by Botan::AES_128::decrypt_n(), Botan::AES_192::decrypt_n(), Botan::AES_256::decrypt_n(), Botan::AES_128::encrypt_n(), Botan::AES_192::encrypt_n(), and Botan::AES_256::encrypt_n().

325 {
326 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
327 return has_ssse3();
328 #elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
329 return has_neon();
330 #elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
331 return has_altivec();
332 #else
333 return false;
334 #endif
335 }

void Botan::CPUID::initialize | ( |  | ) |
| static

Probe the CPU and see what extensions are supported

Definition at line 88 of file cpuid.cpp.

89 {
90 state() = CPUID_Data();
91 }

static bool Botan::CPUID::is_big_endian | ( |  | ) |
| inline static

Definition at line 84 of file cpuid.h.

Referenced by Botan::SIMD_4x32::load_le().

85 {
86 #if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
87 return true;
88 #elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
89 return false;
90 #else
91 return state().endian_status() == Endian_Status::Big;
92 #endif
93 }
Botan::CPUID::Endian_Status::Big

static bool Botan::CPUID::is_little_endian | ( |  | ) |
| inline static

Definition at line 73 of file cpuid.h.

Referenced by Botan::XMSS_Tools::concat(), Botan::SIMD_4x32::load_be(), Botan::SIMD_4x32::store_be(), and Botan::SIMD_4x32::store_le().

74 {
75 #if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
76 return true;
77 #elif defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
78 return false;
79 #else
80 return state().endian_status() == Endian_Status::Little;
81 #endif
82 }
Botan::CPUID::Endian_Status::Little

void Botan::CPUID::print | ( | std::ostream & | o | ) |
| static

Deprecated equivalent to o << "CPUID flags: " << CPUID::to_string() << "\n";

Definition at line 82 of file cpuid.cpp.

References to_string().

83 {
84 o << "CPUID flags: " << CPUID::to_string() << "\n";
85 }
Botan::CPUID::to_string
static std::string to_string()
Definition: cpuid.cpp:30

std::string Botan::CPUID::to_string | ( |  | ) |
| static

Return a possibly empty string containing list of known CPU extensions. Each name will be seperated by a space, and the ordering will be arbitrary. This list only contains values that are useful to Botan (for example FMA instructions are not checked).

Example outputs "sse2 ssse3 rdtsc", "neon arm_aes", "altivec"

Definition at line 30 of file cpuid.cpp.

References CPUID_PRINT, Botan::PKCS11::flags(), and Botan::string_join().

Referenced by print().

31 {
32 std::vector<std::string> flags;
33
34 #define CPUID_PRINT(flag) do { if(has_##flag()) { flags.push_back(#flag); } } while(0)
35
36 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
37 CPUID_PRINT(sse2);
38 CPUID_PRINT(ssse3);
39 CPUID_PRINT(sse41);
40 CPUID_PRINT(sse42);
41 CPUID_PRINT(avx2);
42 CPUID_PRINT(avx512f);
43
44 CPUID_PRINT(rdtsc);
45 CPUID_PRINT(bmi1);
46 CPUID_PRINT(bmi2);
47 CPUID_PRINT(adx);
48
49 CPUID_PRINT(aes_ni);
50 CPUID_PRINT(clmul);
51 CPUID_PRINT(rdrand);
52 CPUID_PRINT(rdseed);
53 CPUID_PRINT(intel_sha);
54 #endif
55
56 #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
57 CPUID_PRINT(altivec);
58 CPUID_PRINT(power_crypto);
59 CPUID_PRINT(darn_rng);
60 #endif
61
62 #if defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
63 CPUID_PRINT(neon);
64 CPUID_PRINT(arm_sve);
65
66 CPUID_PRINT(arm_sha1);
67 CPUID_PRINT(arm_sha2);
68 CPUID_PRINT(arm_aes);
69 CPUID_PRINT(arm_pmull);
70 CPUID_PRINT(arm_sha2_512);
71 CPUID_PRINT(arm_sha3);
72 CPUID_PRINT(arm_sm3);
73 CPUID_PRINT(arm_sm4);
74 #endif
75
76 #undef CPUID_PRINT
77
78 return string_join(flags, ' ');
79 }
Botan::PKCS11::flags
Flags flags(Flag flags)
Definition: p11.h:858
CPUID_PRINT
#define CPUID_PRINT(flag)
Botan::string_join
std::string string_join(const std::vector< std::string > &strs, char delim)
Definition: parsing.cpp:182

The documentation for this class was generated from the following files:

- src/lib/utils/cpuid/cpuid.h
- src/lib/utils/cpuid/cpuid.cpp