From d1c366ee42bd3d2abb4fd855ac4a496b720d8bb6 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 16 Feb 2023 03:22:58 +0100 Subject: [PATCH] btrfs-progs: crypto: call blake2 implementations by pointer Change how blake2 implementation is selected. Instead of an if-else check inside blake2b_compress each time, select the best one and assign the function pointer. This is slightly faster. At this point the selection is not implemented properly in hash-speedtest so all results are from the fastest version. This will be fixed once all algorithms are converted. Signed-off-by: David Sterba --- crypto/blake2.h | 2 ++ crypto/blake2b-ref.c | 20 ++++++++++++-------- crypto/hash.c | 6 ++++++ crypto/hash.h | 1 + 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/crypto/blake2.h b/crypto/blake2.h index ad62f260..e51d8696 100644 --- a/crypto/blake2.h +++ b/crypto/blake2.h @@ -188,6 +188,8 @@ extern "C" { /* This is simply an alias for blake2b */ int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + void blake2_init_accel(void); + #if defined(__cplusplus) } #endif diff --git a/crypto/blake2b-ref.c b/crypto/blake2b-ref.c index 19589451..b39cd59d 100644 --- a/crypto/blake2b-ref.c +++ b/crypto/blake2b-ref.c @@ -224,21 +224,25 @@ void blake2b_compress_sse2( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKB void blake2b_compress_sse41( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] ); void blake2b_compress_avx2( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] ); -static void blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] ) +static void (*blake2b_compress)( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] ) = blake2b_compress_ref; + +void blake2_init_accel(void) { + if (0); #if HAVE_AVX2 - if (cpu_has_feature(CPU_FLAG_AVX2)) - return blake2b_compress_avx2(S, block); + else if (cpu_has_feature(CPU_FLAG_AVX2)) + blake2b_compress = blake2b_compress_avx2; #endif #if HAVE_SSE41 - if (cpu_has_feature(CPU_FLAG_SSE41)) - return blake2b_compress_sse41(S, block); + else if (cpu_has_feature(CPU_FLAG_SSE41)) + blake2b_compress = blake2b_compress_sse41; #endif #if HAVE_SSE2 - if (cpu_has_feature(CPU_FLAG_SSE2)) - return blake2b_compress_sse2(S, block); + else if (cpu_has_feature(CPU_FLAG_SSE2)) + blake2b_compress = blake2b_compress_sse2; #endif - return blake2b_compress_ref(S, block); + else + blake2b_compress = blake2b_compress_ref; } int blake2b_update( blake2b_state *S, const void *pin, size_t inlen ) diff --git a/crypto/hash.c b/crypto/hash.c index 4ee65385..0ccdab75 100644 --- a/crypto/hash.c +++ b/crypto/hash.c @@ -23,6 +23,12 @@ void hash_init_accel(void) { crc32c_optimization_init(); + blake2_init_accel(); +} + +void hash_init_blake2(void) +{ + blake2_init_accel(); } /* diff --git a/crypto/hash.h b/crypto/hash.h index 40a579ce..e1e073c3 100644 --- a/crypto/hash.h +++ b/crypto/hash.h @@ -27,5 +27,6 @@ int hash_sha256(const u8 *buf, size_t length, u8 *out); int hash_blake2b(const u8 *buf, size_t length, u8 *out); void hash_init_accel(void); +void hash_init_blake2(void); #endif