From fc892fc5c0a904e7f08d8e5b2e171503c9e0c955 Mon Sep 17 00:00:00 2001 From: tevador Date: Wed, 9 Oct 2019 21:37:14 +0200 Subject: [PATCH] Select AVX2 if both AVX2 and SSSE3 flags are set --- src/dataset.hpp | 17 +++++------------ src/randomx.cpp | 8 ++++++-- src/tests/benchmark.cpp | 14 +++++++++----- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/dataset.hpp b/src/dataset.hpp index 16a6a84..083a5d7 100644 --- a/src/dataset.hpp +++ b/src/dataset.hpp @@ -83,19 +83,12 @@ namespace randomx { void initDataset(randomx_cache* cache, uint8_t* dataset, uint32_t startBlock, uint32_t endBlock); inline randomx_argon2_impl* selectArgonImpl(randomx_flags flags) { - if ((flags & RANDOMX_FLAG_ARGON2) == 0) { - return &randomx_argon2_fill_segment_ref; + if (flags & RANDOMX_FLAG_ARGON2_AVX2) { + return randomx_argon2_impl_avx2(); } - randomx_argon2_impl* impl = nullptr; - if ((flags & RANDOMX_FLAG_ARGON2) == RANDOMX_FLAG_ARGON2_SSSE3) { - impl = randomx_argon2_impl_ssse3(); + if (flags & RANDOMX_FLAG_ARGON2_SSSE3) { + return randomx_argon2_impl_ssse3(); } - if ((flags & RANDOMX_FLAG_ARGON2) == RANDOMX_FLAG_ARGON2_AVX2) { - impl = randomx_argon2_impl_avx2(); - } - if (impl != nullptr) { - return impl; - } - throw std::runtime_error("Unsupported Argon2 implementation"); + return &randomx_argon2_fill_segment_ref; } } diff --git a/src/randomx.cpp b/src/randomx.cpp index 10eec4d..451ccca 100644 --- a/src/randomx.cpp +++ b/src/randomx.cpp @@ -48,7 +48,7 @@ extern "C" { if (randomx_argon2_impl_avx2() != nullptr && cpu.hasAvx2()) { flags |= RANDOMX_FLAG_ARGON2_AVX2; } - else if (randomx_argon2_impl_ssse3() != nullptr && cpu.hasSsse3()) { + if (randomx_argon2_impl_ssse3() != nullptr && cpu.hasSsse3()) { flags |= RANDOMX_FLAG_ARGON2_SSSE3; } return flags; @@ -56,10 +56,14 @@ extern "C" { randomx_cache *randomx_alloc_cache(randomx_flags flags) { randomx_cache *cache = nullptr; + auto impl = randomx::selectArgonImpl(flags); + if (impl == nullptr) { + return cache; + } try { cache = new randomx_cache(); - cache->argonImpl = randomx::selectArgonImpl(flags); + cache->argonImpl = impl; switch (flags & (RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES)) { case RANDOMX_FLAG_DEFAULT: cache->dealloc = &randomx::deallocCache; diff --git a/src/tests/benchmark.cpp b/src/tests/benchmark.cpp index f00a3e0..e58166e 100644 --- a/src/tests/benchmark.cpp +++ b/src/tests/benchmark.cpp @@ -203,13 +203,15 @@ int main(int argc, char** argv) { flags |= RANDOMX_FLAG_SECURE; } - if (flags & RANDOMX_FLAG_ARGON2_SSSE3) { - std::cout << " - Argon2 implementation: SSSE3" << std::endl; - } - if (flags & RANDOMX_FLAG_ARGON2_AVX2) { std::cout << " - Argon2 implementation: AVX2" << std::endl; } + else if (flags & RANDOMX_FLAG_ARGON2_SSSE3) { + std::cout << " - Argon2 implementation: SSSE3" << std::endl; + } + else { + std::cout << " - Argon2 implementation: reference" << std::endl; + } if (flags & RANDOMX_FLAG_FULL_MEM) { std::cout << " - full memory mode (2080 MiB)" << std::endl; @@ -253,7 +255,9 @@ int main(int argc, char** argv) { std::cout << " ..." << std::endl; try { - randomx::selectArgonImpl(flags); //just to check if flags are valid + if (nullptr == randomx::selectArgonImpl(flags)) { + throw std::runtime_error("Unsupported Argon2 implementation"); + } if ((flags & RANDOMX_FLAG_JIT) && !RANDOMX_HAVE_COMPILER) { throw std::runtime_error("JIT compilation is not supported on this platform. Try without --jit"); }