Botan  2.8.0
Crypto and TLS for C++11
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_bitsbit_from_string (const std::string &tok)
 
static size_t cache_line_size ()
 
static void clear_cpuid_bit (CPUID_bits bit)
 
static bool has_cpuid_bit (CPUID_bits elem)
 
static bool has_simd_32 ()
 
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:

Definition at line 36 of file cpuid.h.

Member Enumeration Documentation

◆ CPUID_bits

enum Botan::CPUID::CPUID_bits : uint64_t
Enumerator
CPUID_INITIALIZED_BIT 

Definition at line 85 of file cpuid.h.

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

Member Function Documentation

◆ bit_from_string()

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

Definition at line 140 of file cpuid.cpp.

References BOTAN_UNUSED.

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

◆ cache_line_size()

static size_t Botan::CPUID::cache_line_size ( )
inlinestatic

Return a best guess of the cache line size

Definition at line 66 of file cpuid.h.

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

67  {
68  if(g_processor_features == 0)
69  {
70  initialize();
71  }
72  return g_cache_line_size;
73  }
static void initialize()
Definition: cpuid.cpp:91

◆ clear_cpuid_bit()

static void Botan::CPUID::clear_cpuid_bit ( CPUID_bits  bit)
inlinestatic

Definition at line 310 of file cpuid.h.

311  {
312  const uint64_t mask = ~(static_cast<uint64_t>(bit));
313  g_processor_features &= mask;
314  }

◆ has_cpuid_bit()

static bool Botan::CPUID::has_cpuid_bit ( CPUID_bits  elem)
inlinestatic

Definition at line 320 of file cpuid.h.

321  {
322  if(g_processor_features == 0)
323  initialize();
324 
325  const uint64_t elem64 = static_cast<uint64_t>(elem);
326  return ((g_processor_features & elem64) == elem64);
327  }
static void initialize()
Definition: cpuid.cpp:91

◆ has_simd_32()

bool Botan::CPUID::has_simd_32 ( )
static

Definition at line 20 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::Noekeon::parallelism(), Botan::SHACAL2::parallelism(), Botan::SHACAL2::provider(), Botan::Noekeon::provider(), and Botan::Serpent::provider().

21  {
22 #if defined(BOTAN_TARGET_SUPPORTS_SSE2)
23  return CPUID::has_sse2();
24 #elif defined(BOTAN_TARGET_SUPPORTS_ALTIVEC)
25  return CPUID::has_altivec();
26 #elif defined(BOTAN_TARGET_SUPPORTS_NEON)
27  return CPUID::has_neon();
28 #else
29  return true;
30 #endif
31  }

◆ initialize()

void Botan::CPUID::initialize ( )
static

Probe the CPU and see what extensions are supported

Definition at line 91 of file cpuid.cpp.

References CPUID_INITIALIZED_BIT.

92  {
93  g_processor_features = 0;
94 
95 #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) || \
96  defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY) || \
97  defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
98 
99  g_processor_features = CPUID::detect_cpu_features(&g_cache_line_size);
100 
101 #endif
102 
103  g_endian_status = runtime_check_endian();
104  g_processor_features |= CPUID::CPUID_INITIALIZED_BIT;
105  }

◆ is_big_endian()

static bool Botan::CPUID::is_big_endian ( )
inlinestatic

Definition at line 80 of file cpuid.h.

Referenced by Botan::SIMD_4x32::load_le(), and Botan::SIMD_4x32::store_le().

81  {
82  return endian_status() == ENDIAN_BIG;
83  }

◆ is_little_endian()

static bool Botan::CPUID::is_little_endian ( )
inlinestatic

Definition at line 75 of file cpuid.h.

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

76  {
77  return endian_status() == ENDIAN_LITTLE;
78  }

◆ print()

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

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

Definition at line 85 of file cpuid.cpp.

References to_string().

86  {
87  o << "CPUID flags: " << CPUID::to_string() << "\n";
88  }
static std::string to_string()
Definition: cpuid.cpp:34

◆ to_string()

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 34 of file cpuid.cpp.

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

Referenced by print().

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

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