|
1 | 1 | /*
|
2 |
| -* Copyright (c) 2013-2020, The PurpleI2P Project |
| 2 | +* Copyright (c) 2013-2023, The PurpleI2P Project |
3 | 3 | *
|
4 | 4 | * This file is part of Purple i2pd project and licensed under BSD3
|
5 | 5 | *
|
6 | 6 | * See full license text in LICENSE file at top of project tree
|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | #include "CPU.h"
|
10 |
| -#if defined(__x86_64__) || defined(__i386__) |
11 |
| -#include <cpuid.h> |
12 |
| -#endif |
13 | 10 | #include "Log.h"
|
14 | 11 |
|
| 12 | +#if defined(_MSC_VER) |
| 13 | +#include <intrin.h> |
| 14 | + |
15 | 15 | #ifndef bit_AES
|
16 |
| -#define bit_AES (1 << 25) |
| 16 | + #define bit_AES (1 << 25) |
17 | 17 | #endif
|
18 |
| -#ifndef bit_AVX |
19 |
| -#define bit_AVX (1 << 28) |
20 | 18 | #endif
|
21 | 19 |
|
22 |
| - |
23 | 20 | namespace i2p
|
24 | 21 | {
|
25 | 22 | namespace cpu
|
26 | 23 | {
|
27 | 24 | bool aesni = false;
|
28 |
| - bool avx = false; |
29 | 25 |
|
30 |
| - void Detect(bool AesSwitch, bool AvxSwitch, bool force) |
| 26 | + inline bool cpu_support_aes() |
31 | 27 | {
|
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"); |
39 | 41 | #else
|
40 |
| - if ((info[2] & bit_AES && AesSwitch) || (AesSwitch && force)) { |
| 42 | + return false; |
41 | 43 | #endif
|
42 |
| - aesni = true; |
43 |
| - } |
44 |
| -#if defined (_WIN32) && (WINVER == 0x0501) // WinXP |
45 |
| - if (AvxSwitch && force) { // only if forced |
46 | 44 | #else
|
47 |
| - if ((info[2] & bit_AVX && AvxSwitch) || (AvxSwitch && force)) { |
| 45 | + return false; |
48 | 46 | #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; |
51 | 53 | }
|
52 |
| -#endif // defined(__x86_64__) || defined(__i386__) |
53 | 54 |
|
54 | 55 | LogPrint(eLogInfo, "AESNI ", (aesni ? "enabled" : "disabled"));
|
55 |
| - LogPrint(eLogInfo, "AVX ", (avx ? "enabled" : "disabled")); |
56 | 56 | }
|
57 | 57 | }
|
58 | 58 | }
|
0 commit comments