Botan 3.9.0
Crypto and TLS for C&
cpuid_features.cpp
Go to the documentation of this file.
1/**
2* (C) 2025 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/cpuid_features.h>
8
9#include <botan/exceptn.h>
10
11namespace Botan {
12
13std::string CPUFeature::to_string() const {
14 switch(m_bit) {
15 case Bit::SSE2:
16 return "sse2";
17 case Bit::SSSE3:
18 return "ssse3";
19 case Bit::AVX2:
20 return "avx2";
21 case Bit::AVX512:
22 return "avx512";
23 case Bit::RDTSC:
24 return "rdtsc";
25 case Bit::ADX:
26 return "adx";
27 case Bit::BMI:
28 return "bmi2";
29 case Bit::GFNI:
30 return "gfni";
31 case Bit::RDRAND:
32 return "rdrand";
33 case Bit::RDSEED:
34 return "rdseed";
35 case Bit::AESNI:
36 return "aesni";
37 case Bit::CLMUL:
38 return "clmul";
39 case Bit::SHA:
40 return "intel_sha";
41 case Bit::SHA512:
42 return "intel_sha512";
43 case Bit::AVX2_AES:
44 return "avx2_aes";
45 case Bit::AVX512_AES:
46 return "avx512_aes";
47 case Bit::AVX2_CLMUL:
48 return "avx2_clmul";
49 case Bit::AVX512_CLMUL:
50 return "avx512_clmul";
51 case Bit::SM3:
52 return "intel_sm3";
53 case Bit::SM4:
54 return "intel_sm4";
55 }
56 throw Invalid_State("CPUFeature invalid bit");
57}
58
59//static
60std::optional<CPUFeature> CPUFeature::from_string(std::string_view tok) {
61 if(tok == "sse2" || tok == "simd") {
62 return CPUFeature(Bit::SSE2);
63 } else if(tok == "ssse3") {
64 return CPUFeature(Bit::SSSE3);
65 } else if(tok == "aesni" || tok == "aes_ni") {
66 // aes_ni is the string printed on the console when running "botan cpuid"
67 return CPUFeature(Bit::AESNI);
68 } else if(tok == "clmul") {
69 return CPUFeature(Bit::CLMUL);
70 } else if(tok == "avx2") {
71 return CPUFeature(Bit::AVX2);
72 } else if(tok == "avx512") {
73 return CPUFeature(Bit::AVX512);
74 } else if(tok == "sha" || tok == "intel_sha") {
75 // TODO(Botan4) remove "sha" match here
76 return CPUFeature(Bit::SHA);
77 } else if(tok == "intel_sha512") {
78 return CPUFeature(Bit::SHA512);
79 } else if(tok == "rdtsc") {
80 return CPUFeature(Bit::RDTSC);
81 } else if(tok == "bmi2") {
82 return CPUFeature(Bit::BMI);
83 } else if(tok == "adx") {
84 return CPUFeature(Bit::ADX);
85 } else if(tok == "gfni") {
86 return CPUFeature(Bit::GFNI);
87 } else if(tok == "rdrand") {
88 return CPUFeature(Bit::RDRAND);
89 } else if(tok == "rdseed") {
90 return CPUFeature(Bit::RDSEED);
91 } else if(tok == "avx512_aes") {
92 return CPUFeature(Bit::AVX512_AES);
93 } else if(tok == "avx512_clmul") {
94 return CPUFeature(Bit::AVX512_CLMUL);
95 } else if(tok == "avx2_vaes" || tok == "avx2_aes") {
96 return CPUFeature(Bit::AVX2_AES);
97 } else if(tok == "avx2_clmul") {
98 return CPUFeature(Bit::AVX2_CLMUL);
99 } else if(tok == "intel_sm3") {
100 return CPUFeature(Bit::SM3);
101 } else if(tok == "intel_sm4") {
102 return CPUFeature(Bit::SM4);
103 } else {
104 return {};
105 }
106}
107
108} // namespace Botan
static std::optional< CPUFeature > from_string(std::string_view)
Definition cpuid.cpp:24
CPUFeature(Bit)
Definition cpuid.h:30
std::string to_string() const
Definition cpuid.cpp:28