Skip to content

Commit

Permalink
use builtin __AVX__ and __AES__ macros and reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfl committed Jun 27, 2018
1 parent fc16e76 commit dec848f
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 149 deletions.
2 changes: 1 addition & 1 deletion Makefile.homebrew
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ endif
# Seems like all recent Mac's have AES-NI, after firmware upgrade 2.2
# Found no good way to detect it from command line. TODO: Might be some osx sysinfo magic
ifeq ($(USE_AESNI),yes)
CXXFLAGS += -maes -DAESNI
CXXFLAGS += -maes
endif
ifeq ($(USE_AVX),1)
CXXFLAGS += -mavx
Expand Down
2 changes: 1 addition & 1 deletion Makefile.linux
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ifneq ($(shell $(GREP) -c aes /proc/cpuinfo),0)
ifeq ($(machine), aarch64)
CXXFLAGS += -DARM64AES
else
CPU_FLAGS += -maes -DAESNI
CPU_FLAGS += -maes
endif
endif
endif
Expand Down
2 changes: 1 addition & 1 deletion Makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ endif

# don't change following line to ifeq ($(USE_AESNI),yes) !!!
ifeq ($(USE_AESNI),1)
CPU_FLAGS += -maes -DAESNI
CPU_FLAGS += -maes
else
CPU_FLAGS += -msse
endif
Expand Down
2 changes: 1 addition & 1 deletion Makefile.osx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ifeq ($(USE_UPNP),yes)
endif

ifeq ($(USE_AESNI),1)
CXXFLAGS += -maes -DAESNI
CXXFLAGS += -maes
else
CXXFLAGS += -msse
endif
Expand Down
7 changes: 3 additions & 4 deletions build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ include_directories(${LIBI2PD_CLIENT_SRC_DIR})
set (LIBI2PD_SRC
"${LIBI2PD_SRC_DIR}/BloomFilter.cpp"
"${LIBI2PD_SRC_DIR}/Config.cpp"
"${LIBI2PD_SRC_DIR}/CPU.cpp"
"${LIBI2PD_SRC_DIR}/CPU.cpp"
"${LIBI2PD_SRC_DIR}/Crypto.cpp"
"${LIBI2PD_SRC_DIR}/CryptoKey.cpp"
"${LIBI2PD_SRC_DIR}/Garlic.cpp"
Expand Down Expand Up @@ -77,10 +77,10 @@ set (LIBI2PD_SRC
"${LIBI2PD_SRC_DIR}/api.cpp"
"${LIBI2PD_SRC_DIR}/Event.cpp"
"${LIBI2PD_SRC_DIR}/Gost.cpp"
"${LIBI2PD_SRC_DIR}/ChaCha20.cpp"
"${LIBI2PD_SRC_DIR}/ChaCha20.cpp"
"${LIBI2PD_SRC_DIR}/Poly1305.cpp"
"${LIBI2PD_SRC_DIR}/Ed25519.cpp"
"${LIBI2PD_SRC_DIR}/NTCP2.cpp"
"${LIBI2PD_SRC_DIR}/NTCP2.cpp"
)

if (WITH_WEBSOCKETS)
Expand Down Expand Up @@ -234,7 +234,6 @@ endif ()

if (WITH_AESNI)
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes" )
add_definitions ( -DAESNI )
endif()

if (WITH_AVX)
Expand Down
14 changes: 13 additions & 1 deletion libi2pd/CPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,35 @@ namespace cpu

void Detect()
{
#if defined(__AES__) || defined(__AVX__)

#if defined(__x86_64__) || defined(__i386__)
int info[4];
__cpuid(0, info[0], info[1], info[2], info[3]);
if (info[0] >= 0x00000001) {
__cpuid(0x00000001, info[0], info[1], info[2], info[3]);
#ifdef __AES__
aesni = info[2] & bit_AES; // AESNI
#endif // __AES__
#ifdef __AVX__
avx = info[2] & bit_AVX; // AVX
#endif // __AVX__
}
#endif
#endif // defined(__x86_64__) || defined(__i386__)

#ifdef __AES__
if(aesni)
{
LogPrint(eLogInfo, "AESNI enabled");
}
#endif // __AES__
#ifdef __AVX__
if(avx)
{
LogPrint(eLogInfo, "AVX enabled");
}
#endif // __AVX__
#endif // defined(__AES__) || defined(__AVX__)
}
}
}
Loading

0 comments on commit dec848f

Please sign in to comment.