Skip to content

Commit

Permalink
[cpu] perform check in separate function
Browse files Browse the repository at this point in the history
Signed-off-by: r4sas <[email protected]>
  • Loading branch information
r4sas committed Aug 10, 2023
1 parent 239a934 commit 968c6fe
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions libi2pd/CPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,49 @@
#include "CPU.h"
#include "Log.h"

#if defined(_MSC_VER)
#include <intrin.h>

#ifndef bit_AES
#define bit_AES (1 << 25)
#endif
#endif

namespace i2p
{
namespace cpu
{
bool aesni = false;

void Detect(bool AesSwitch, bool force)
inline bool cpu_support_aes()
{
#if defined(__x86_64__) || defined(__i386__)
__builtin_cpu_init ();
#if defined (_WIN32) && (WINVER == 0x0501) // WinXP
if (AesSwitch && force) { // only if forced
#if (_M_AMD64 || __x86_64__) || (_M_IX86 || __i386__)
#if defined(_MSC_VER)
int cpu_info[4];
__cpuid(cpu_info, 1);
return ((cpu_info[2] & bit_AES) != 0)
#elif defined(__clang__)
#if __clang_major__ >= 6
__builtin_cpu_init();
#endif
return __builtin_cpu_supports("aes");
#elif defined(__GNUC__)
__builtin_cpu_init();
return __builtin_cpu_supports("aes");
#else
return false;
#endif
#else
if ((__builtin_cpu_supports ("aes") && AesSwitch) || (AesSwitch && force)) {
return false;
#endif
}

void Detect(bool AesSwitch, bool force)
{
if ((cpu_support_aes() && AesSwitch) || (AesSwitch && force)) {
aesni = true;
}
#endif

LogPrint(eLogInfo, "AESNI ", (aesni ? "enabled" : "disabled"));
}
}
Expand Down

0 comments on commit 968c6fe

Please sign in to comment.