From 5f72417176cfffece9a5aa11e97d5a6599c51e7a Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Tue, 16 May 2023 16:29:58 +0100 Subject: [PATCH] Add ability to specify SHA256 implementation for benchmark purposes --- src/crypto/sha256.cpp | 45 ++++++++++++++++++++++++++++--------------- src/crypto/sha256.h | 14 +++++++++++++- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/crypto/sha256.cpp b/src/crypto/sha256.cpp index a4eef36480d..5eacaa44e18 100644 --- a/src/crypto/sha256.cpp +++ b/src/crypto/sha256.cpp @@ -579,9 +579,15 @@ bool AVXEnabled() } // namespace -std::string SHA256AutoDetect() +std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implementation) { std::string ret = "standard"; + Transform = sha256::Transform; + TransformD64 = sha256::TransformD64; + TransformD64_2way = nullptr; + TransformD64_4way = nullptr; + TransformD64_8way = nullptr; + #if defined(USE_ASM) && defined(HAVE_GETCPUID) bool have_sse4 = false; bool have_xsave = false; @@ -592,7 +598,9 @@ std::string SHA256AutoDetect() uint32_t eax, ebx, ecx, edx; GetCPUID(1, 0, eax, ebx, ecx, edx); - have_sse4 = (ecx >> 19) & 1; + if (use_implementation & sha256_implementation::USE_SSE4) { + have_sse4 = (ecx >> 19) & 1; + } have_xsave = (ecx >> 27) & 1; have_avx = (ecx >> 28) & 1; if (have_xsave && have_avx) { @@ -600,8 +608,12 @@ std::string SHA256AutoDetect() } if (have_sse4) { GetCPUID(7, 0, eax, ebx, ecx, edx); - have_avx2 = (ebx >> 5) & 1; - have_x86_shani = (ebx >> 29) & 1; + if (use_implementation & sha256_implementation::USE_AVX2) { + have_avx2 = (ebx >> 5) & 1; + } + if (use_implementation & sha256_implementation::USE_SHANI) { + have_x86_shani = (ebx >> 29) & 1; + } } #if defined(ENABLE_X86_SHANI) && !defined(BUILD_BITCOIN_INTERNAL) @@ -637,27 +649,28 @@ std::string SHA256AutoDetect() #if defined(ENABLE_ARM_SHANI) && !defined(BUILD_BITCOIN_INTERNAL) bool have_arm_shani = false; - + if (use_implementation & sha256_implementation::USE_SHANI) { #if defined(__linux__) #if defined(__arm__) // 32-bit - if (getauxval(AT_HWCAP2) & HWCAP2_SHA2) { - have_arm_shani = true; - } + if (getauxval(AT_HWCAP2) & HWCAP2_SHA2) { + have_arm_shani = true; + } #endif #if defined(__aarch64__) // 64-bit - if (getauxval(AT_HWCAP) & HWCAP_SHA2) { - have_arm_shani = true; - } + if (getauxval(AT_HWCAP) & HWCAP_SHA2) { + have_arm_shani = true; + } #endif #endif #if defined(MAC_OSX) - int val = 0; - size_t len = sizeof(val); - if (sysctlbyname("hw.optional.arm.FEAT_SHA256", &val, &len, nullptr, 0) == 0) { - have_arm_shani = val != 0; - } + int val = 0; + size_t len = sizeof(val); + if (sysctlbyname("hw.optional.arm.FEAT_SHA256", &val, &len, nullptr, 0) == 0) { + have_arm_shani = val != 0; + } #endif + } if (have_arm_shani) { Transform = sha256_arm_shani::Transform; diff --git a/src/crypto/sha256.h b/src/crypto/sha256.h index 76255086654..b1348631d32 100644 --- a/src/crypto/sha256.h +++ b/src/crypto/sha256.h @@ -26,10 +26,22 @@ public: CSHA256& Reset(); }; +namespace sha256_implementation { +enum UseImplementation : uint8_t { + STANDARD = 0, + USE_SSE4 = 1 << 0, + USE_AVX2 = 1 << 1, + USE_SHANI = 1 << 2, + USE_SSE4_AND_AVX2 = USE_SSE4 | USE_AVX2, + USE_SSE4_AND_SHANI = USE_SSE4 | USE_SHANI, + USE_ALL = USE_SSE4 | USE_AVX2 | USE_SHANI, +}; +} + /** Autodetect the best available SHA256 implementation. * Returns the name of the implementation. */ -std::string SHA256AutoDetect(); +std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implementation = sha256_implementation::USE_ALL); /** Compute multiple double-SHA256's of 64-byte blobs. * output: pointer to a blocks*32 byte output buffer