From 4453280480a019c0fbe6d475deef0ecb918d2806 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 17 Feb 2022 17:11:55 +0100 Subject: m + add tests for optimisations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libar2_decode_params.c | 2 +- libar2_hash.c | 12 +++++++++- test.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/libar2_decode_params.c b/libar2_decode_params.c index 86fae6c..273de55 100644 --- a/libar2_decode_params.c +++ b/libar2_decode_params.c @@ -8,7 +8,7 @@ decode_u32(const char *s, uint_least32_t *outp) uint_least32_t digit; size_t i; - if (s[0] == '0' || !isdigit(s[0])) { + if ((s[0] == '0' && isdigit(s[1])) || !isdigit(s[0])) { errno = EINVAL; return 0; } diff --git a/libar2_hash.c b/libar2_hash.c index 80fb6d3..4dc2e82 100644 --- a/libar2_hash.c +++ b/libar2_hash.c @@ -64,6 +64,7 @@ blockxor_avx2(struct block *a_, const struct block *b_) } +/* $covered{$ (removing from coverage test because my machine does not spport AVX512F) */ LIBAR2_TARGET__("avx512f") static void blockxor_avx512f(struct block *a_, const struct block *b_) @@ -74,6 +75,7 @@ blockxor_avx512f(struct block *a_, const struct block *b_) for (i = 0; i < sizeof(*a_) / (512 / 8); i++) a[i] = _mm512_xor_si512(a[i], b[i]); } +/* $covered}$ */ static void blockxor_vanilla(struct block *a, const struct block *b) @@ -98,6 +100,7 @@ blockxor3_avx2(struct block *a_, const struct block *b_, const struct block *c_) } +/* $covered{$ (removing from coverage test because my machine does not spport AVX512F) */ LIBAR2_TARGET__("avx512f") static void blockxor3_avx512f(struct block *a_, const struct block *b_, const struct block *c_) @@ -109,6 +112,7 @@ blockxor3_avx512f(struct block *a_, const struct block *b_, const struct block * for (i = 0; i < sizeof(*a_) / (512 / 8); i++) a[i] = _mm512_xor_si512(b[i], c[i]); } +/* $covered}$ */ static void blockxor3_vanilla(struct block *a, const struct block *b, const struct block *c) @@ -132,6 +136,7 @@ blockcpy_avx2(struct block *a_, const struct block *b_) a[i] = _mm256_load_si256(&b[i]); } +/* $covered{$ (removing from coverage test because my machine does not spport AVX512F) */ LIBAR2_TARGET__("avx512f") static void blockcpy_avx512f(struct block *a_, const struct block *b_) @@ -142,6 +147,7 @@ blockcpy_avx512f(struct block *a_, const struct block *b_) for (i = 0; i < sizeof(*a_) / (512 / 8); i++) a[i] = _mm512_load_si512(&b[i]); } +/* $covered}$ */ static void blockcpy_vanilla(struct block *a, const struct block *b) @@ -674,6 +680,7 @@ libar2_init(void) if (!initialised) { #if 0 __builtin_cpu_init(); + /* $covered{$ (we know that it works, but the test cannot enter every branch) */ if (__builtin_cpu_supports("avx512f")) libar2_internal_use_avx512f__(); else if (__builtin_cpu_supports("avx2")) @@ -681,10 +688,12 @@ libar2_init(void) else if (__builtin_cpu_supports("sse2")) libar2_internal_use_sse2__(); else - libar2_internal_use_generic__(); + libar2_internal_use_generic__(); + /* $covered}$ */ #else uint32_t x; __asm__ volatile("cpuid" : "=b"(x) : "a"(7), "c"(0) : "edx"); + /* $covered{$ (we know that it works, but the test cannot enter every branch) */ if (x & ((uint32_t)1 << 16)) { libar2_internal_use_avx512f__(); } else if (x & ((uint32_t)1 << 5)) { @@ -696,6 +705,7 @@ libar2_init(void) else libar2_internal_use_generic__(); } + /* $covered}$ */ #endif initialised = 1; } diff --git a/test.c b/test.c index 3aec24c..1e33c05 100644 --- a/test.c +++ b/test.c @@ -4,6 +4,9 @@ #ifndef MEASURE_TIME # define MEASURE_TIME 0 #endif +#ifndef MEASURE_TIME_ONLY +# define MEASURE_TIME_ONLY MEASURE_TIME +#endif #include #if MEASURE_TIME @@ -70,11 +73,11 @@ allocate(size_t num, size_t size, size_t alignment, struct libar2_context *ctx) alignment = sizeof(void *); err = posix_memalign(&ptr, alignment, num * size); if (err) - goto fail; + goto fail; /* $covered$ */ #elif defined(_ISOC11_SOURCE) ptr = aligned_alloc(alignment, num * size); if (!ptr) - goto fail; + goto fail; /* $covered$ */ #else # error No implementation for aligned memory allocation available #endif @@ -994,6 +997,57 @@ check_libar2_hash(void) } +#if defined(__x86_64__) && defined(LIBAR2_TARGET__) && defined(__GNUC__) +static void +run_check_libar2_hash_optimisations(void) +{ + +#define CHECK(PWD, HASH)\ + check_hash(MEM(PWD), HASH, NULL, 0, NULL, 0, &ctx_st, __LINE__) + + CHECK("password", "$argon2i$m=256,t=2,p=2$c29tZXNhbHQ$tsEVYKap1h6scGt5ovl9aLRGOqOth+AMB+KwHpDFZPs"); + CHECK("", "$argon2ds$v=16$m=8,t=1,p=2$ICAgICAgICA$+6+yBnWbuV7mLs6rKMhvi+SLbkzb5CB6Jd2pSWuC/Kw"); + CHECK("", "$argon2d$v=16$m=8,t=1,p=1$ICAgICAgICA$X54KZYxUSfMUihzebb70sKbheabHilo8gsUldrVU4IU"); + CHECK("password", "$argon2i$v=19$m=256,t=2,p=2$c29tZXNhbHQ$T/XOJ2mh1/TIpJHfCdQan76Q5esCFVoT5MAeIM1Oq2E"); + +#undef CHECK +} +#endif + + +static void +check_libar2_hash_optimisations(void) +{ +#if defined(__x86_64__) && defined(LIBAR2_TARGET__) && defined(__GNUC__) + + __builtin_cpu_init(); + + libar2_internal_use_generic__(); + run_check_libar2_hash_optimisations(); + + libar2_internal_use_sse2__(); + if (__builtin_cpu_supports("sse2")) + run_check_libar2_hash_optimisations(); /* $covered$ */ + + libar2_internal_use_avx2__(); + if (__builtin_cpu_supports("avx2")) + run_check_libar2_hash_optimisations(); /* $covered$ */ + + libar2_internal_use_avx512f__(); + if (__builtin_cpu_supports("avx512f")) + run_check_libar2_hash_optimisations(); /* $covered$ */ + /* $covered{$ */ + else if (__builtin_cpu_supports("avx2")) + libar2_internal_use_avx2__(); + else if (__builtin_cpu_supports("sse2")) + libar2_internal_use_sse2__(); + else + libar2_internal_use_generic__(); + /* $covered}$ */ +#endif +} + + #ifdef LIBAR2_WEAKLY_LINKED__ void @@ -1212,7 +1266,7 @@ check_failures(void) int main(void) { -#if 1 +#if !MEASURE_TIME_ONLY check_libar2_type_to_string(); check_libar2_string_to_type(); check_libar2_version_to_string(); @@ -1227,9 +1281,7 @@ main(void) # ifdef LIBAR2_WEAKLY_LINKED__ check_libar2_hash_buf_size(); # endif -#endif -#if 1 check_failures(); #endif @@ -1259,5 +1311,8 @@ main(void) } #endif +#if !MEASURE_TIME_ONLY + check_libar2_hash_optimisations(); +#endif return 0; } -- cgit v1.2.3-70-g09d2