diff options
author | Paul Duncan <pabs@pablotron.org> | 2024-05-05 11:41:46 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2024-05-05 11:41:46 -0400 |
commit | 6678b734ef7e91e0cf91d748910b778891a53e88 (patch) | |
tree | cd94781cb48f54d31e45a59337512d2263617245 | |
parent | c8536f5094f58db837e5d30f5c24aa1aaabe8eef (diff) | |
download | sha3-6678b734ef7e91e0cf91d748910b778891a53e88.tar.bz2 sha3-6678b734ef7e91e0cf91d748910b778891a53e88.zip |
sha3.c, Makefile, tests/bench/Makefile: allow overriding SHA3_BACKEND via command-line argument
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | sha3.c | 20 | ||||
-rw-r--r-- | tests/bench/Makefile | 6 |
3 files changed, 23 insertions, 10 deletions
@@ -1,5 +1,8 @@ +# backend (0 to auto-detect) +SHA3_BACKEND ?= 0 + # compiler flags used for sample application and shared library -CFLAGS=-W -Wall -Wextra -Werror -pedantic -std=c11 -fPIC -O3 -march=native -mtune=native +CFLAGS=-W -Wall -Wextra -Werror -pedantic -std=c11 -fPIC -O3 -march=native -mtune=native -DSHA3_BACKEND=$(SHA3_BACKEND) # sample application APP=./sha3 @@ -10,7 +13,7 @@ LIB=libsha3.so LIB_OBJS=sha3.o # test app (test suite and sanitizers) -TEST_CFLAGS=-g -fsanitize=address,pointer-compare,pointer-subtract,undefined,leak -W -Wall -Wextra -Werror -pedantic -std=c11 -march=native -mtune=native +TEST_CFLAGS=-g -fsanitize=address,pointer-compare,pointer-subtract,undefined,leak -W -Wall -Wextra -Werror -pedantic -std=c11 -march=native -mtune=native -DSHA3_BACKEND=$(SHA3_BACKEND) TEST_APP=./test-sha3 .PHONY=test all @@ -26,20 +26,28 @@ /** @cond INTERNAL */ // available backends -#define BACKEND_AVX512 8 // AVX-512 backend -#define BACKEND_NEON 4 // A64 Neon backend -#define BACKEND_SCALAR 0 // scalar (default) backend - -// auto-detect backend +#define BACKEND_AUTO 0 // auto-detect (default) +#define BACKEND_SCALAR 1 // scalar backend +#define BACKEND_AVX512 2 // AVX-512 backend +#define BACKEND_NEON 3 // Neon backend + +// if SHA3_BACKEND is defined and set to 0 (the default), then unset it +// and auto-detect the appropriate backend +#if defined(SHA3_BACKEND) && SHA3_BACKEND == BACKEND_AUTO +#undef SHA3_BACKEND +#endif /* defined(SHA3_BACKEND) && SHA3_BACKEND == 0 */ + +// detect backend #ifndef SHA3_BACKEND #if defined(__AVX512F__) #define SHA3_BACKEND BACKEND_AVX512 #elif defined(__ARM_NEON) #define SHA3_BACKEND BACKEND_NEON #else +// no optimized backend detected, fall back to scalar #define SHA3_BACKEND BACKEND_SCALAR #endif -#endif /* SHA3_BACKEND */ +#endif /* !SHA3_BACKEND */ // 64-bit rotate left #define ROL(v, n) (((v) << (n)) | ((v) >> (64-(n)))) diff --git a/tests/bench/Makefile b/tests/bench/Makefile index d1106bf..307cb23 100644 --- a/tests/bench/Makefile +++ b/tests/bench/Makefile @@ -1,5 +1,7 @@ -# CFLAGS=-std=c11 -W -Wall -Wextra -Wpedantic -Werror -g -O3 -march=native -mtune=native -DSHA3_BACKEND=0 -CFLAGS=-std=c11 -W -Wall -Wextra -Wpedantic -Werror -g -O3 -march=native -mtune=native +# get backend from environment, or fall back to 0 if unspecified +SHA3_BACKEND ?= 0 + +CFLAGS=-std=c11 -W -Wall -Wextra -Wpedantic -Werror -g -O3 -march=native -mtune=native -DSHA3_BACKEND=$(SHA3_BACKEND) APP=./bench OBJS=sha3.o bench.o |