Botan 3.9.0
Crypto and TLS for C&
Botan::CPUID Class Referencefinal

#include <cpuid.h>

Public Types

typedef CPUFeature Feature

Static Public Member Functions

static std::optional< CPUID::Featurebit_from_string (std::string_view tok)
static std::optional< std::string > check (CPUID::Feature feat)
static std::optional< std::string > check (CPUID::Feature feat1, CPUID::Feature feat2)
static void clear_cpuid_bit (CPUID::Feature bit)
static bool has (CPUID::Feature feat)
static bool has (CPUID::Feature feat1, CPUID::Feature feat2)
template<typename T>
static uint32_t if_set (uint64_t cpuid, T flag, CPUID::Feature bit, uint32_t allowed)
static void initialize ()
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.

Definition at line 43 of file cpuid.h.

Member Typedef Documentation

◆ Feature

Definition at line 45 of file cpuid.h.

Member Function Documentation

◆ bit_from_string()

std::optional< CPUFeature > Botan::CPUID::bit_from_string ( std::string_view tok)
static

Definition at line 90 of file cpuid.cpp.

90 {
91 return CPUFeature::from_string(tok);
92}
static std::optional< CPUFeature > from_string(std::string_view)
Definition cpuid.cpp:24

References Botan::CPUFeature::from_string().

◆ check() [1/2]

std::optional< std::string > Botan::CPUID::check ( CPUID::Feature feat)
inlinestatic

Check if a feature is supported returning the associated string if so

This is a helper function used to implement provider()

Definition at line 67 of file cpuid.h.

67 {
68 if(state().has_bit(feat.as_u32())) {
69 return feat.to_string();
70 } else {
71 return {};
72 }
73 }

References Botan::CPUFeature::as_u32(), and Botan::CPUFeature::to_string().

Referenced by Botan::ChaCha::provider(), Botan::GHASH::provider(), Botan::IDEA::provider(), Botan::Keccak_Permutation::provider(), Botan::Noekeon::provider(), Botan::Serpent::provider(), Botan::SHA_1::provider(), Botan::SHACAL2::provider(), Botan::SM4::provider(), and Botan::ZFEC::provider().

◆ check() [2/2]

std::optional< std::string > Botan::CPUID::check ( CPUID::Feature feat1,
CPUID::Feature feat2 )
inlinestatic

Check if a feature is supported returning the associated string if so

This is a helper function used to implement provider()

Definition at line 80 of file cpuid.h.

80 {
81 if(state().has_bit((feat1.as_u32() | feat2.as_u32()))) {
82 // Typically feat2 is a secondary feature that is almost but not
83 // completely implied by feat1 (ex: AVX2 + BMI2) which we have to
84 // check for completness, but don't reflect into the provider name.
85 return feat1.to_string();
86 } else {
87 return {};
88 }
89 }

References Botan::CPUFeature::as_u32(), and Botan::CPUFeature::to_string().

◆ clear_cpuid_bit()

void Botan::CPUID::clear_cpuid_bit ( CPUID::Feature bit)
inlinestatic

Definition at line 109 of file cpuid.h.

109{ state().clear_cpuid_bit(bit.as_u32()); }

References Botan::CPUFeature::as_u32().

◆ has() [1/2]

◆ has() [2/2]

bool Botan::CPUID::has ( CPUID::Feature feat1,
CPUID::Feature feat2 )
inlinestatic

Check if two features are both supported

Definition at line 99 of file cpuid.h.

99 {
100 return state().has_bit(feat1.as_u32() | feat2.as_u32());
101 }

References Botan::CPUFeature::as_u32().

◆ if_set()

template<typename T>
uint32_t Botan::CPUID::if_set ( uint64_t cpuid,
T flag,
CPUID::Feature bit,
uint32_t allowed )
inlinestatic

A common helper for the various CPUID implementations

Definition at line 117 of file cpuid.h.

117 {
118 const uint64_t flag64 = static_cast<uint64_t>(flag);
119 if((cpuid & flag64) == flag64) {
120 return (bit.as_u32() & allowed);
121 } else {
122 return 0;
123 }
124 }

References Botan::CPUFeature::as_u32().

◆ initialize()

void Botan::CPUID::initialize ( )
static

Probe the CPU and see what extensions are supported

Definition at line 51 of file cpuid.cpp.

51 {
52 state() = CPUID_Data();
53}

◆ 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.

34 {
35 std::vector<std::string> flags;
36
37 const uint32_t bitset = state().bitset();
38
39 for(size_t i = 0; i != 32; ++i) {
40 const uint32_t b = static_cast<uint32_t>(1) << i;
41 if((bitset & b) == b) {
42 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
43 flags.push_back(CPUFeature(static_cast<CPUFeature::Bit>(b)).to_string());
44 }
45 }
46
47 return string_join(flags, ' ');
48}
static std::string to_string()
Definition cpuid.cpp:34
Flags flags(Flag flags)
Definition p11.h:848
std::string string_join(const std::vector< std::string > &strs, char delim)
Definition parsing.cpp:140

References Botan::string_join(), and to_string().

Referenced by to_string().


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