Botan  2.6.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 
102  // Crypto-specific ISAs
103  CPUID_AESNI_BIT = (1ULL << 16),
104  CPUID_CLMUL_BIT = (1ULL << 17),
105  CPUID_RDRAND_BIT = (1ULL << 18),
106  CPUID_RDSEED_BIT = (1ULL << 19),
107  CPUID_SHA_BIT = (1ULL << 20),
108 #endif
109 
110 #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
111  CPUID_ALTIVEC_BIT = (1ULL << 0),
112  CPUID_PPC_CRYPTO_BIT = (1ULL << 1),
113 #endif
114 
115 #if defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
116  CPUID_ARM_NEON_BIT = (1ULL << 0),
117  CPUID_ARM_AES_BIT = (1ULL << 16),
118  CPUID_ARM_PMULL_BIT = (1ULL << 17),
119  CPUID_ARM_SHA1_BIT = (1ULL << 18),
120  CPUID_ARM_SHA2_BIT = (1ULL << 19),
121 #endif
122 
123  CPUID_INITIALIZED_BIT = (1ULL << 63)
124  };

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

References BOTAN_UNUSED.

134  {
135 #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
136  if(tok == "sse2" || tok == "simd")
137  return {Botan::CPUID::CPUID_SSE2_BIT};
138  if(tok == "ssse3")
139  return {Botan::CPUID::CPUID_SSSE3_BIT};
140  if(tok == "aesni")
141  return {Botan::CPUID::CPUID_AESNI_BIT};
142  if(tok == "clmul")
143  return {Botan::CPUID::CPUID_CLMUL_BIT};
144  if(tok == "avx2")
145  return {Botan::CPUID::CPUID_AVX2_BIT};
146  if(tok == "sha")
147  return {Botan::CPUID::CPUID_SHA_BIT};
148 
149 #elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
150  if(tok == "altivec" || tok == "simd")
151  return {Botan::CPUID::CPUID_ALTIVEC_BIT};
152 
153 #elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
154  if(tok == "neon" || tok == "simd")
155  return {Botan::CPUID::CPUID_ARM_NEON_BIT};
156  if(tok == "armv8sha1")
157  return {Botan::CPUID::CPUID_ARM_SHA1_BIT};
158  if(tok == "armv8sha2")
159  return {Botan::CPUID::CPUID_ARM_SHA2_BIT};
160  if(tok == "armv8aes")
161  return {Botan::CPUID::CPUID_ARM_AES_BIT};
162  if(tok == "armv8pmull")
163  return {Botan::CPUID::CPUID_ARM_PMULL_BIT};
164 
165 #else
166  BOTAN_UNUSED(tok);
167 #endif
168 
169  return {};
170  }
#define BOTAN_UNUSED(...)
Definition: assert.h:117

◆ 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:84

◆ clear_cpuid_bit()

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

Definition at line 267 of file cpuid.h.

268  {
269  const uint64_t mask = ~(static_cast<uint64_t>(bit));
270  g_processor_features &= mask;
271  }

◆ has_cpuid_bit()

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

Definition at line 277 of file cpuid.h.

278  {
279  if(g_processor_features == 0)
280  initialize();
281 
282  const uint64_t elem64 = static_cast<uint64_t>(elem);
283  return ((g_processor_features & elem64) == elem64);
284  }
static void initialize()
Definition: cpuid.cpp:84

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

References CPUID_INITIALIZED_BIT.

85  {
86  g_processor_features = 0;
87 
88 #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY) || \
89  defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY) || \
90  defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
91 
92  g_processor_features = CPUID::detect_cpu_features(&g_cache_line_size);
93 
94 #endif
95 
96  g_endian_status = runtime_check_endian();
97  g_processor_features |= CPUID::CPUID_INITIALIZED_BIT;
98  }

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

References to_string().

79  {
80  o << "CPUID flags: " << CPUID::to_string() << "\n";
81  }
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(bmi2);
50  CPUID_PRINT(adx);
51 
52  CPUID_PRINT(aes_ni);
53  CPUID_PRINT(clmul);
54  CPUID_PRINT(rdrand);
55  CPUID_PRINT(rdseed);
56  CPUID_PRINT(intel_sha);
57 #endif
58 
59 #if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
60  CPUID_PRINT(altivec);
61  CPUID_PRINT(ppc_crypto);
62 #endif
63 
64 #if defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
65  CPUID_PRINT(neon);
66  CPUID_PRINT(arm_sha1);
67  CPUID_PRINT(arm_sha2);
68  CPUID_PRINT(arm_aes);
69  CPUID_PRINT(arm_pmull);
70 #endif
71 
72 #undef CPUID_PRINT
73 
74  return string_join(flags, ' ');
75  }
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: