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: 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(-) (limited to 'src') diff --git a/src/crypto/sha256.cpp b/src/crypto/sha256.cpp index a4eef36480..5eacaa44e1 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 7625508665..b1348631d3 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 -- cgit v1.2.3 From ce6df7df9bab2405cfe7d6e382f5682cf30de476 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Tue, 16 May 2023 17:25:58 +0100 Subject: bench: Add SHA256 implementation specific benchmarks --- src/bench/crypto_hash.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 121 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/bench/crypto_hash.cpp b/src/bench/crypto_hash.cpp index cf8d807d7b..1685a120b4 100644 --- a/src/bench/crypto_hash.cpp +++ b/src/bench/crypto_hash.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include /* Number of bytes to hash per iteration */ @@ -36,13 +37,48 @@ static void SHA1(benchmark::Bench& bench) }); } -static void SHA256(benchmark::Bench& bench) +static void SHA256_STANDARD(benchmark::Bench& bench) { + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::STANDARD))); uint8_t hash[CSHA256::OUTPUT_SIZE]; std::vector in(BUFFER_SIZE,0); bench.batch(in.size()).unit("byte").run([&] { CSHA256().Write(in.data(), in.size()).Finalize(hash); }); + SHA256AutoDetect(); +} + +static void SHA256_SSE4(benchmark::Bench& bench) +{ + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4))); + uint8_t hash[CSHA256::OUTPUT_SIZE]; + std::vector in(BUFFER_SIZE,0); + bench.batch(in.size()).unit("byte").run([&] { + CSHA256().Write(in.data(), in.size()).Finalize(hash); + }); + SHA256AutoDetect(); +} + +static void SHA256_AVX2(benchmark::Bench& bench) +{ + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_AVX2))); + uint8_t hash[CSHA256::OUTPUT_SIZE]; + std::vector in(BUFFER_SIZE,0); + bench.batch(in.size()).unit("byte").run([&] { + CSHA256().Write(in.data(), in.size()).Finalize(hash); + }); + SHA256AutoDetect(); +} + +static void SHA256_SHANI(benchmark::Bench& bench) +{ + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI))); + uint8_t hash[CSHA256::OUTPUT_SIZE]; + std::vector in(BUFFER_SIZE,0); + bench.batch(in.size()).unit("byte").run([&] { + CSHA256().Write(in.data(), in.size()).Finalize(hash); + }); + SHA256AutoDetect(); } static void SHA3_256_1M(benchmark::Bench& bench) @@ -54,22 +90,92 @@ static void SHA3_256_1M(benchmark::Bench& bench) }); } -static void SHA256_32b(benchmark::Bench& bench) +static void SHA256_32b_STANDARD(benchmark::Bench& bench) +{ + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::STANDARD))); + std::vector in(32,0); + bench.batch(in.size()).unit("byte").run([&] { + CSHA256() + .Write(in.data(), in.size()) + .Finalize(in.data()); + }); + SHA256AutoDetect(); +} + +static void SHA256_32b_SSE4(benchmark::Bench& bench) +{ + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4))); + std::vector in(32,0); + bench.batch(in.size()).unit("byte").run([&] { + CSHA256() + .Write(in.data(), in.size()) + .Finalize(in.data()); + }); + SHA256AutoDetect(); +} + +static void SHA256_32b_AVX2(benchmark::Bench& bench) +{ + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_AVX2))); + std::vector in(32,0); + bench.batch(in.size()).unit("byte").run([&] { + CSHA256() + .Write(in.data(), in.size()) + .Finalize(in.data()); + }); + SHA256AutoDetect(); +} + +static void SHA256_32b_SHANI(benchmark::Bench& bench) { + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI))); std::vector in(32,0); bench.batch(in.size()).unit("byte").run([&] { CSHA256() .Write(in.data(), in.size()) .Finalize(in.data()); }); + SHA256AutoDetect(); +} + +static void SHA256D64_1024_STANDARD(benchmark::Bench& bench) +{ + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::STANDARD))); + std::vector in(64 * 1024, 0); + bench.batch(in.size()).unit("byte").run([&] { + SHA256D64(in.data(), in.data(), 1024); + }); + SHA256AutoDetect(); +} + +static void SHA256D64_1024_SSE4(benchmark::Bench& bench) +{ + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4))); + std::vector in(64 * 1024, 0); + bench.batch(in.size()).unit("byte").run([&] { + SHA256D64(in.data(), in.data(), 1024); + }); + SHA256AutoDetect(); +} + +static void SHA256D64_1024_AVX2(benchmark::Bench& bench) +{ + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_AVX2))); + std::vector in(64 * 1024, 0); + bench.batch(in.size()).unit("byte").run([&] { + SHA256D64(in.data(), in.data(), 1024); + }); + SHA256AutoDetect(); } -static void SHA256D64_1024(benchmark::Bench& bench) +static void SHA256D64_1024_SHANI(benchmark::Bench& bench) { + bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI))); std::vector in(64 * 1024, 0); bench.batch(in.size()).unit("byte").run([&] { SHA256D64(in.data(), in.data(), 1024); }); + SHA256AutoDetect(); } static void SHA512(benchmark::Bench& bench) @@ -152,13 +258,22 @@ static void MuHashPrecompute(benchmark::Bench& bench) BENCHMARK(BenchRIPEMD160, benchmark::PriorityLevel::HIGH); BENCHMARK(SHA1, benchmark::PriorityLevel::HIGH); -BENCHMARK(SHA256, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256_STANDARD, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256_SSE4, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256_AVX2, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256_SHANI, benchmark::PriorityLevel::HIGH); BENCHMARK(SHA512, benchmark::PriorityLevel::HIGH); BENCHMARK(SHA3_256_1M, benchmark::PriorityLevel::HIGH); -BENCHMARK(SHA256_32b, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256_32b_STANDARD, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256_32b_SSE4, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256_32b_AVX2, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256_32b_SHANI, benchmark::PriorityLevel::HIGH); BENCHMARK(SipHash_32b, benchmark::PriorityLevel::HIGH); -BENCHMARK(SHA256D64_1024, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256D64_1024_STANDARD, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256D64_1024_SSE4, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256D64_1024_AVX2, benchmark::PriorityLevel::HIGH); +BENCHMARK(SHA256D64_1024_SHANI, benchmark::PriorityLevel::HIGH); BENCHMARK(FastRandom_32bit, benchmark::PriorityLevel::HIGH); BENCHMARK(FastRandom_1bit, benchmark::PriorityLevel::HIGH); -- cgit v1.2.3