Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: added intel assembly for sha algs #208

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Makefile
Makefile.in
aclocal.m4
autom4te.cache/
bench
build-aux/config.guess
build-aux/config.sub
build-aux/depcomp
Expand Down
60 changes: 59 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ endif()

INCLUDE(CTest)
SET(USE_TESTS ${CMAKE_TESTING_ENABLED})
SET(WITH_BENCH TRUE CACHE BOOL "enable bench")
SET(WITH_TOOLS TRUE CACHE BOOL "enable dogecoin tool cli application")
SET(WITH_NET TRUE CACHE BOOL "enable net functions")
SET(WITH_LOGDB TRUE CACHE BOOL "enable logdb")
SET(WITH_UNISTRING TRUE CACHE BOOL "enable unistring functions")
SET(WITH_WALLET TRUE CACHE BOOL "enable wallet")
SET(USE_AVX2 FALSE CACHE BOOL "enable intel avx2")
SET(USE_SSE FALSE CACHE BOOL "enable intel sse")
SET(USE_SSE2 FALSE CACHE BOOL "enable scrypt sse2")
SET(USE_TPM2 TRUE CACHE BOOL "enable tpm2")
SET(USE_OPENENCLAVE FALSE CACHE BOOL "enable openenclave")
Expand Down Expand Up @@ -76,6 +79,41 @@ IF(WITH_NET)
ENDIF()
ENDIF()

# NASM integration for AVX2
if(USE_AVX2 OR USE_SSE)
enable_language(ASM_NASM)
if(NOT CMAKE_ASM_NASM_COMPILER_LOADED)
message(FATAL_ERROR "Can't find NASM assembler")
endif()

set(CMAKE_ASM_NASM_COMPILER nasm)

set(NASM_VERSION_REQUIRED "2.14")
execute_process(
COMMAND ${CMAKE_ASM_NASM_COMPILER} --version
OUTPUT_VARIABLE NASM_VERSION_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REGEX MATCH "NASM version ([0-9]*.[0-9]*)" NASM_VERSION "${NASM_VERSION_OUTPUT}")
if(NOT NASM_VERSION OR NASM_VERSION_REQUIRED VERSION_GREATER ${CMAKE_MATCH_1})
message(FATAL_ERROR "NASM version must be at least ${NASM_VERSION_REQUIRED}!")
endif()

# Define directories with assembly files (modify paths as needed)
set(DIR_ASM_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/intel)

# Define directories with include files (modify paths as needed)
set(DIR_ASM_INCLUDES
${CMAKE_CURRENT_SOURCE_DIR}/src/intel)

# Find all assembly files in the defined directory
file(GLOB_RECURSE ASM_SOURCES "${DIR_ASM_SOURCES}/*.asm")

# Add include directories to the NASM compiler
set_property(SOURCE ${ASM_SOURCES} PROPERTY INCLUDE_DIRECTORIES ${DIR_ASM_INCLUDES})

endif()

ADD_DEFINITIONS(
-DPACKAGE_NAME="${PROJECT_NAME}"
-DPACKAGE_VERSION="${PROJECT_VERSION}"
Expand All @@ -88,6 +126,9 @@ ENDIF()
IF(USE_TESTS)
ADD_DEFINITIONS(-DUSE_TESTS=1)
ENDIF()
IF(WITH_BENCH)
ADD_DEFINITIONS(-DWITH_BENCH=1)
ENDIF()
IF(WITH_TOOLS)
ADD_DEFINITIONS(-DWITH_TOOLS=1)
ENDIF()
Expand All @@ -103,6 +144,12 @@ ENDIF()
IF(WITH_WALLET)
ADD_DEFINITIONS(-DWITH_WALLET=1)
ENDIF()
IF(USE_AVX2)
ADD_DEFINITIONS(-DUSE_AVX2=1)
ENDIF()
IF(USE_SSE)
ADD_DEFINITIONS(-DUSE_SSE=1)
ENDIF()
IF(USE_SSE2)
ADD_DEFINITIONS(-DUSE_SSE2=1)
ENDIF()
Expand All @@ -120,12 +167,15 @@ ENDIF()
MESSAGE(STATUS "")
MESSAGE(STATUS "Options used to compile and link:")
MESSAGE(STATUS " USE_TESTS = ${USE_TESTS}")
MESSAGE(STATUS " WITH_BENCH = ${WITH_BENCH}")
MESSAGE(STATUS " WITH_TOOLS = ${WITH_TOOLS}")
MESSAGE(STATUS " WITH_NET = ${WITH_NET}")
MESSAGE(STATUS " WITH_LOGDB = ${WITH_LOGDB}")
MESSAGE(STATUS " WITH_UNISTRING = ${WITH_UNISTRING}")
MESSAGE(STATUS " WITH_WALLET = ${WITH_WALLET}")
MESSAGE(STATUS "")
MESSAGE(STATUS " USE_AVX2 = ${USE_AVX2}")
MESSAGE(STATUS " USE_SSE = ${USE_SSE}")
MESSAGE(STATUS " USE_SSE2 = ${USE_SSE2}")
MESSAGE(STATUS " USE_TPM2 = ${USE_TPM2}")
MESSAGE(STATUS " TEST_PASSWD = ${TEST_PASSWD}")
Expand All @@ -140,7 +190,7 @@ MESSAGE(STATUS "")

FILE(TOUCH config/libdogecoin-config.h)

ADD_LIBRARY(${LIBDOGECOIN_NAME})
ADD_LIBRARY(${LIBDOGECOIN_NAME} ${ASM_SOURCES})
INSTALL(TARGETS
${LIBDOGECOIN_NAME}
LIBRARY DESTINATION lib
Expand Down Expand Up @@ -296,6 +346,14 @@ ENDIF()
ENDFOREACH()
ENDIF()

IF (WITH_BENCH)
ADD_EXECUTABLE(bench)
TARGET_SOURCES(bench ${visibility}
src/bench.c
)
TARGET_LINK_LIBRARIES(bench ${LIBDOGECOIN_NAME})
ENDIF()

IF(WITH_LOGDB)
TARGET_SOURCES(${LIBDOGECOIN_NAME} ${visibility}
src/logdb/logdb_core.c
Expand Down
74 changes: 68 additions & 6 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,69 @@ libdogecoin_la_SOURCES = \
src/validation.c \
src/vector.c

# NASM assembler setup
NASM = nasm

if USE_AVX2
# Directory containing assembly source files and includes
DIR_ASM_SOURCE = src/intel/avx_t1
DIR_ASM_INCLUDE = src/intel/

# Find all assembly source files and define corresponding object files
ASM_SOURCE_FILES = $(wildcard $(DIR_ASM_SOURCE)/*.asm)
ASM_OBJECT_FILES = $(ASM_SOURCE_FILES:.asm=.o)
ASM_LIB_FILES = $(ASM_SOURCE_FILES:.asm=.a)

# Add the assembly object files to the list of dependencies
libdogecoin_la_DEPENDENCIES = $(ASM_LIB_FILES)

# Pattern rule for building .o files from .asm files
%.o: %.asm
$(NASM) $(NASMFLAGS) -I$(DIR_ASM_INCLUDE) -o $@ $<

# Pattern rule for custom static library from .o files
%.a: %.o
$(AR) $(ARFLAGS) $@ $<

CLEANFILES = $(ASM_OBJECT_FILES)
endif

if USE_SSE
# Directory containing assembly source files and includes
DIR_ASM_SOURCE = src/intel/sse_t1
DIR_ASM_INCLUDE = src/intel/

# Find all assembly source files and define corresponding object files
ASM_SOURCE_FILES = $(wildcard $(DIR_ASM_SOURCE)/*.asm)
ASM_OBJECT_FILES = $(ASM_SOURCE_FILES:.asm=.o)
ASM_LIB_FILES = $(ASM_SOURCE_FILES:.asm=.a)

# Add the assembly object files to the list of dependencies
libdogecoin_la_DEPENDENCIES = $(ASM_LIB_FILES)

# Pattern rule for building .o files from .asm files
%.o: %.asm
$(NASM) $(NASMFLAGS) -I$(DIR_ASM_INCLUDE) -o $@ $<

# Pattern rule for custom static library from .o files
%.a: %.o
$(AR) $(ARFLAGS) $@ $<

CLEANFILES = $(ASM_OBJECT_FILES) $(ASM_LIB_FILES)
endif

if USE_SSE2
libdogecoin_la_SOURCES += \
src/scrypt-sse2.c
endif

libdogecoin_la_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src/logdb/include -fPIC
libdogecoin_la_LIBADD = $(LIBSECP256K1)
libdogecoin_la_LIBADD = $(LIBSECP256K1) $(ASM_LIB_FILES)
noinst_PROGRAMS =

if USE_TESTS
noinst_PROGRAMS = tests
tests_LDADD = libdogecoin.la
noinst_PROGRAMS += tests
tests_LDADD = libdogecoin.la $(ASM_LIB_FILES)
tests_SOURCES = \
test/address_tests.c \
test/aes_tests.c \
Expand Down Expand Up @@ -191,6 +243,16 @@ tests_SOURCES += \
endif
endif

if WITH_BENCH
noinst_PROGRAMS += bench
bench_LDADD = libdogecoin.la $(ASM_LIB_FILES)
bench_SOURCES = \
src/bench.c
bench_CFLAGS = $(libdogecoin_la_CFLAGS)
bench_CPPFLAGS = -I$(top_srcdir)/src
bench_LDFLAGS = -static
endif

if WITH_WALLET
noinst_HEADERS += \
include/dogecoin/wallet.h
Expand Down Expand Up @@ -237,7 +299,7 @@ tests_SOURCES += \
endif
instdir=$(prefix)/bin
inst_PROGRAMS = such
such_LDADD = libdogecoin.la $(LIBSECP256K1)
such_LDADD = libdogecoin.la $(LIBSECP256K1) $(ASM_LIB_FILES)
such_SOURCES = \
src/cli/such.c
such_CFLAGS = $(libdogecoin_la_CFLAGS)
Expand All @@ -246,15 +308,15 @@ such_LDFLAGS = -static

if WITH_NET
inst_PROGRAMS += sendtx
sendtx_LDADD = libdogecoin.la $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(LIBSECP256K1)
sendtx_LDADD = libdogecoin.la $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(LIBSECP256K1) $(ASM_LIB_FILES)
sendtx_SOURCES = \
src/cli/sendtx.c
sendtx_CFLAGS = $(libdogecoin_la_CFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)
sendtx_CPPFLAGS = -I$(top_srcdir)/src
sendtx_LDFLAGS = -static

inst_PROGRAMS += spvnode
spvnode_LDADD = libdogecoin.la
spvnode_LDADD = libdogecoin.la $(ASM_LIB_FILES)
spvnode_SOURCES = \
src/cli/spvnode.c
spvnode_CFLAGS = $(libdogecoin_la_CFLAGS)
Expand Down
45 changes: 45 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ case $host in
*mingw*)
build_windows=yes
TARGET_OS=windows
NASMFLAGS="-Werror -fwin64 -Xvc -gcv8 -DWIN_ABI"
AC_CHECK_LIB([pthread], [main],, AC_MSG_ERROR(lib missing))
AC_CHECK_LIB([winpthread], [main],, AC_MSG_ERROR(lib missing))
AC_CHECK_LIB([shell32], [main],, AC_MSG_ERROR(lib missing))
Expand All @@ -73,6 +74,7 @@ case $host in
;;
*linux*)
TARGET_OS=linux
NASMFLAGS="-Werror -felf64 -Xgnu -gdwarf -DLINUX -D__linux__"
;;
esac
warn_CFLAGS="-std=gnu99 -pedantic -Wall -Wextra -Wno-long-long -Wno-overlength-strings"
Expand All @@ -85,6 +87,18 @@ AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
CFLAGS="$saved_CFLAGS"
])

AC_ARG_ENABLE([intel-avx2],
[AS_HELP_STRING([--enable-intel-avx2],
[Build with Intel AVX2 implementation (default is no)])],
[use_intel_avx2=$enableval],
[use_intel_avx2=no])

AC_ARG_ENABLE([intel-sse],
[AS_HELP_STRING([--enable-intel-sse],
[Build with Intel SSE implementation (default is no)])],
[use_intel_sse=$enableval],
[use_intel_sse=no])

AC_ARG_ENABLE([scrypt-sse2],
[AS_HELP_STRING([--enable-scrypt-sse2],
[Build with scrypt sse2 implementation (default is no)])],
Expand All @@ -98,6 +112,12 @@ AC_ARG_ENABLE([debug],
[enable_debug=$enableval],
[enable_debug=no])

AC_ARG_ENABLE([bench],
[AS_HELP_STRING([--disable-bench],
[disable benchmark functions])],
[with_bench=$enableval],
[with_bench=yes])

AC_ARG_ENABLE([tools],
[AS_HELP_STRING([--disable-tools],
[disable dogecoin tool cli application])],
Expand Down Expand Up @@ -165,9 +185,26 @@ if test "x$random_device" = x"/dev/random"; then
AC_DEFINE([RANDOM_DEVICE],["/dev/random"],[Define to set random file handle])
fi

# Configure Intel AVX2
if test x$use_intel_avx2 = xyes; then
AC_DEFINE(USE_AVX2, 1, [Define this symbol if AVX2 works])
CFLAGS="$CFLAGS -mavx2"
fi

# Configure Intel SSE
if test x$use_intel_sse = xyes; then
AC_DEFINE(USE_SSE, 1, [Define this symbol if SSE works])
CFLAGS="$CFLAGS -msse4.2"
fi

# Configure Scrypt SSE2
if test x$use_scrypt_sse2 = xyes; then
AC_DEFINE(USE_SSE2, 1, [Define this symbol if SSE2 works])
CFLAGS="$CFLAGS -msse4.2"
fi

if test "x$with_bench" = xyes; then
AC_DEFINE_UNQUOTED([WITH_BENCH],[1],[Define to 1 to enable bench compilation])
fi

if test "x$with_tools" = xyes; then
Expand Down Expand Up @@ -224,11 +261,15 @@ AC_SUBST(BUILD_EXEEXT)
AC_SUBST(EVENT_LIBS)
AC_SUBST(EVENT_PTHREADS_LIBS)
AC_SUBST(LIBUNISTRING)
AC_SUBST(NASMFLAGS)
AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
AM_CONDITIONAL([WITH_BENCH], [test "x$with_bench" = "xyes"])
AM_CONDITIONAL([WITH_TOOLS], [test "x$with_tools" = "xyes"])
AM_CONDITIONAL([WITH_NET], [test "x$with_net" = "xyes"])
AM_CONDITIONAL([WITH_LOGDB], [test "x$with_logdb" = "xyes"])
AM_CONDITIONAL([WITH_WALLET], [test "x$with_wallet" = "xyes"])
AM_CONDITIONAL([USE_AVX2], [test "x$use_intel_avx2" = "xyes"])
AM_CONDITIONAL([USE_SSE], [test "x$use_intel_sse" = "xyes"])
AM_CONDITIONAL([USE_SSE2], [test "x$use_scrypt_sse2" = "xyes"])
AM_CONDITIONAL([USE_OPENENCLAVE], [test "x$use_openenclave" = "xyes"])
AC_SUBST(LIB_VERSION_CURRENT, _LIB_VERSION_CURRENT)
Expand All @@ -243,12 +284,15 @@ AC_OUTPUT
echo
echo "Options used to compile and link:"
echo " with tests = $use_tests"
echo " with bench = $with_bench"
echo " with tools = $with_tools"
echo " with net = $with_net"
echo " with logdb = $with_logdb"
echo " with wallet = $with_wallet"
echo " with unistring = $with_unistring"
echo
echo " Intel AVX2 = $use_intel_avx2"
echo " Intel SSE = $use_intel_sse"
echo " SSE2 Scrypt = $use_scrypt_sse2"
echo " test password = $test_passwd"
if test "x$test_passwd" = xyes; then
Expand All @@ -259,6 +303,7 @@ echo " openenclave = $use_openenclave"
echo
echo " host = $host"
echo " target os = $TARGET_OS"
echo " nasm flags = $NASMFLAGS"
echo
echo " CC = $CC"
echo " CFLAGS = $CFLAGS"
Expand Down
Loading