aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile7
-rw-r--r--sha3.c20
-rw-r--r--tests/bench/Makefile6
3 files changed, 23 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index dfc0e0f..bdaf13e 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/sha3.c b/sha3.c
index 329a810..4073287 100644
--- a/sha3.c
+++ b/sha3.c
@@ -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