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 <dsterba@suse.com>
This commit is contained in:
David Sterba 2023-02-16 03:22:58 +01:00
parent 24ec095295
commit d1c366ee42
4 changed files with 21 additions and 8 deletions

View File

@ -188,6 +188,8 @@ extern "C" {
/* This is simply an alias for blake2b */ /* 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 ); 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) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -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_sse41( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] );
void blake2b_compress_avx2( 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 HAVE_AVX2
if (cpu_has_feature(CPU_FLAG_AVX2)) else if (cpu_has_feature(CPU_FLAG_AVX2))
return blake2b_compress_avx2(S, block); blake2b_compress = blake2b_compress_avx2;
#endif #endif
#if HAVE_SSE41 #if HAVE_SSE41
if (cpu_has_feature(CPU_FLAG_SSE41)) else if (cpu_has_feature(CPU_FLAG_SSE41))
return blake2b_compress_sse41(S, block); blake2b_compress = blake2b_compress_sse41;
#endif #endif
#if HAVE_SSE2 #if HAVE_SSE2
if (cpu_has_feature(CPU_FLAG_SSE2)) else if (cpu_has_feature(CPU_FLAG_SSE2))
return blake2b_compress_sse2(S, block); blake2b_compress = blake2b_compress_sse2;
#endif #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 ) int blake2b_update( blake2b_state *S, const void *pin, size_t inlen )

View File

@ -23,6 +23,12 @@
void hash_init_accel(void) void hash_init_accel(void)
{ {
crc32c_optimization_init(); crc32c_optimization_init();
blake2_init_accel();
}
void hash_init_blake2(void)
{
blake2_init_accel();
} }
/* /*

View File

@ -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); int hash_blake2b(const u8 *buf, size_t length, u8 *out);
void hash_init_accel(void); void hash_init_accel(void);
void hash_init_blake2(void);
#endif #endif