Skip to content

Commit 7b6aa41

Browse files
authored
CPU: remove AVX code, switch to __builtin for AES detection (#1959)
* [cpu] remove avx detect and code blocks, try to switch to __builtin * [cpu] use __builtin_* only on x86 systems * [cpu] perform check in separate function * [cpu] set AES definition on MSVC * update x86 and aes support checks at compile time * [cmake] update comment about AES on MSVC
1 parent 32c5ff2 commit 7b6aa41

File tree

11 files changed

+219
-229
lines changed

11 files changed

+219
-229
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ trim_trailing_whitespace = false
3434
[*.yml]
3535
indent_style = space
3636
indent_size = 2
37+
38+
[*.patch]
39+
trim_trailing_whitespace = false

build/CMakeLists.txt

+4-7
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,11 @@ endif()
197197

198198
# Note: AES-NI and AVX is available on x86-based CPU's.
199199
# Here also ARM64 implementation, but currently we don't support it.
200-
# MSVC is not supported.
201-
if(MSVC)
202-
message(STATUS "AES-NI is not supported on MSVC, option was disabled")
203-
set(WITH_AESNI OFF)
204-
endif()
205-
200+
# MSVC is not supported due to different ASM processing, so we hope OpenSSL has its own checks to run optimized code.
206201
if(WITH_AESNI AND (ARCHITECTURE MATCHES "x86_64" OR ARCHITECTURE MATCHES "i386"))
207-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes")
202+
if(NOT MSVC)
203+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes")
204+
endif()
208205
add_definitions(-D__AES__)
209206
endif()
210207

contrib/i2pd.conf

-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ verify = true
280280
[cpuext]
281281
## Use CPU AES-NI instructions set when work with cryptography when available (default: true)
282282
# aesni = true
283-
## Use CPU AVX instructions set when work with cryptography when available (default: true)
284-
# avx = true
285283
## Force usage of CPU instructions set, even if they not found (default: false)
286284
## DO NOT TOUCH that option if you really don't know what are you doing!
287285
# force = false

daemon/Daemon.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,11 @@ namespace util
150150

151151
bool precomputation; i2p::config::GetOption("precomputation.elgamal", precomputation);
152152
bool aesni; i2p::config::GetOption("cpuext.aesni", aesni);
153-
bool avx; i2p::config::GetOption("cpuext.avx", avx);
154153
bool forceCpuExt; i2p::config::GetOption("cpuext.force", forceCpuExt);
155154
bool ssu; i2p::config::GetOption("ssu", ssu);
156155
if (!ssu && i2p::config::IsDefault ("precomputation.elgamal"))
157156
precomputation = false; // we don't elgamal table if no ssu, unless it's specified explicitly
158-
i2p::crypto::InitCrypto (precomputation, aesni, avx, forceCpuExt);
157+
i2p::crypto::InitCrypto (precomputation, aesni, forceCpuExt);
159158

160159
i2p::transport::InitAddressFromIface (); // get address4/6 from interfaces
161160

libi2pd/CPU.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
/*
2-
* Copyright (c) 2013-2020, The PurpleI2P Project
2+
* Copyright (c) 2013-2023, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
66
* See full license text in LICENSE file at top of project tree
77
*/
88

99
#include "CPU.h"
10-
#if defined(__x86_64__) || defined(__i386__)
11-
#include <cpuid.h>
12-
#endif
1310
#include "Log.h"
1411

12+
#if defined(_MSC_VER)
13+
#include <intrin.h>
14+
1515
#ifndef bit_AES
16-
#define bit_AES (1 << 25)
16+
#define bit_AES (1 << 25)
1717
#endif
18-
#ifndef bit_AVX
19-
#define bit_AVX (1 << 28)
2018
#endif
2119

22-
2320
namespace i2p
2421
{
2522
namespace cpu
2623
{
2724
bool aesni = false;
28-
bool avx = false;
2925

30-
void Detect(bool AesSwitch, bool AvxSwitch, bool force)
26+
inline bool cpu_support_aes()
3127
{
32-
#if defined(__x86_64__) || defined(__i386__)
33-
int info[4];
34-
__cpuid(0, info[0], info[1], info[2], info[3]);
35-
if (info[0] >= 0x00000001) {
36-
__cpuid(0x00000001, info[0], info[1], info[2], info[3]);
37-
#if defined (_WIN32) && (WINVER == 0x0501) // WinXP
38-
if (AesSwitch && force) { // only if forced
28+
#if (defined(_M_AMD64) || defined(__x86_64__)) || (defined(_M_IX86) || defined(__i386__))
29+
#if defined(_MSC_VER)
30+
int cpu_info[4];
31+
__cpuid(cpu_info, 1);
32+
return ((cpu_info[2] & bit_AES) != 0);
33+
#elif defined(__clang__)
34+
#if __clang_major__ >= 6
35+
__builtin_cpu_init();
36+
#endif
37+
return __builtin_cpu_supports("aes");
38+
#elif defined(__GNUC__)
39+
__builtin_cpu_init();
40+
return __builtin_cpu_supports("aes");
3941
#else
40-
if ((info[2] & bit_AES && AesSwitch) || (AesSwitch && force)) {
42+
return false;
4143
#endif
42-
aesni = true;
43-
}
44-
#if defined (_WIN32) && (WINVER == 0x0501) // WinXP
45-
if (AvxSwitch && force) { // only if forced
4644
#else
47-
if ((info[2] & bit_AVX && AvxSwitch) || (AvxSwitch && force)) {
45+
return false;
4846
#endif
49-
avx = true;
50-
}
47+
}
48+
49+
void Detect(bool AesSwitch, bool force)
50+
{
51+
if ((cpu_support_aes() && AesSwitch) || (AesSwitch && force)) {
52+
aesni = true;
5153
}
52-
#endif // defined(__x86_64__) || defined(__i386__)
5354

5455
LogPrint(eLogInfo, "AESNI ", (aesni ? "enabled" : "disabled"));
55-
LogPrint(eLogInfo, "AVX ", (avx ? "enabled" : "disabled"));
5656
}
5757
}
5858
}

libi2pd/CPU.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2020, The PurpleI2P Project
2+
* Copyright (c) 2013-2023, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -14,9 +14,8 @@ namespace i2p
1414
namespace cpu
1515
{
1616
extern bool aesni;
17-
extern bool avx;
1817

19-
void Detect(bool AesSwitch, bool AvxSwitch, bool force);
18+
void Detect(bool AesSwitch, bool force);
2019
}
2120
}
2221

libi2pd/Config.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ namespace config {
193193
options_description precomputation("Precomputation options");
194194
precomputation.add_options()
195195
("precomputation.elgamal",
196-
#if defined(__x86_64__)
196+
#if (defined(_M_AMD64) || defined(__x86_64__))
197197
value<bool>()->default_value(false),
198198
#else
199199
value<bool>()->default_value(true),
@@ -308,7 +308,7 @@ namespace config {
308308
options_description cpuext("CPU encryption extensions options");
309309
cpuext.add_options()
310310
("cpuext.aesni", bool_switch()->default_value(true), "Use auto detection for AESNI CPU extensions. If false, AESNI will be not used")
311-
("cpuext.avx", bool_switch()->default_value(true), "Use auto detection for AVX CPU extensions. If false, AVX will be not used")
311+
("cpuext.avx", bool_switch()->default_value(false), "Deprecated option")
312312
("cpuext.force", bool_switch()->default_value(false), "Force usage of CPU extensions. Useful when cpuinfo is not available on virtual machines")
313313
;
314314

0 commit comments

Comments
 (0)