From d1c3c0e4fefb92634fd2583d369cdb71b804e7a6 Mon Sep 17 00:00:00 2001 From: Wilco Dijkstra Date: Fri, 3 Mar 2023 12:40:22 +0000 Subject: [PATCH] Benchtests: Remove simple_str(n)cmp Instead of benchmarking slow byte oriented loops, include the optimized generic strcmp/strncmp implementation. Adjust iteration count to reduce benchmark time. Reviewed-by: Adhemerval Zanella --- benchtests/bench-strcmp.c | 49 +++++++++++----------------------------------- benchtests/bench-strncmp.c | 49 ++++++++++++---------------------------------- 2 files changed, 24 insertions(+), 74 deletions(-) diff --git a/benchtests/bench-strcmp.c b/benchtests/bench-strcmp.c index 8cd690b..65fd04b 100644 --- a/benchtests/bench-strcmp.c +++ b/benchtests/bench-strcmp.c @@ -23,59 +23,26 @@ # define TEST_NAME "strcmp" #endif #include "bench-string.h" +#include "json-lib.h" #ifdef WIDE -# define L(str) L##str -# define SIMPLE_STRCMP simple_wcscmp # define CHARBYTESLOG 2 # define MIDCHAR 0x7fffffff # define LARGECHAR 0xfffffffe - -/* Wcscmp uses signed semantics for comparison, not unsigned */ -/* Avoid using substraction since possible overflow */ - -int -simple_wcscmp (const wchar_t *s1, const wchar_t *s2) -{ - wchar_t c1, c2; - do - { - c1 = *s1++; - c2 = *s2++; - if (c2 == L'\0') - return c1 - c2; - } - while (c1 == c2); - - return c1 < c2 ? -1 : 1; -} - #else -# include - -# define L(str) str -# define SIMPLE_STRCMP simple_strcmp # define CHARBYTESLOG 0 # define MIDCHAR 0x7f # define LARGECHAR 0xfe -/* Strcmp uses unsigned semantics for comparison. */ int -simple_strcmp (const char *s1, const char *s2) -{ - int ret; +generic_strcmp (const char *s1, const char *s2); - while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++); - return ret; -} +IMPL (generic_strcmp, 0) #endif -# include "json-lib.h" - typedef int (*proto_t) (const CHAR *, const CHAR *); -IMPL (SIMPLE_STRCMP, 1) IMPL (STRCMP, 1) static void @@ -83,7 +50,7 @@ do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR *s2, int exp_result) { - size_t i, iters = INNER_LOOP_ITERS8; + size_t i, iters = INNER_LOOP_ITERS; timing_t start, stop, cur; TIMING_NOW (start); @@ -201,7 +168,7 @@ do_test_page_boundary (json_ctx_t *json_ctx) CHAR *s1p = s1 + align1; CHAR *s2p = s2 + align2; len = (page_size / CHARBYTES) - 1 - align1; - exp_result = SIMPLE_STRCMP (s1p, s2p); + exp_result = STRCMP (s1p, s2p); do_one_test_page_boundary (json_ctx, s1p, s2p, align1, align2, len, exp_result); } @@ -324,3 +291,9 @@ test_main (void) } #include + +#ifndef WIDE +# undef STRCMP +# define STRCMP generic_strcmp +# include +#endif diff --git a/benchtests/bench-strncmp.c b/benchtests/bench-strncmp.c index a714e5a..53707e6 100644 --- a/benchtests/bench-strncmp.c +++ b/benchtests/bench-strncmp.c @@ -21,61 +21,31 @@ # define TEST_NAME "wcsncmp" #else # define TEST_NAME "strncmp" -#endif /* !WIDE */ +#endif #include "bench-string.h" #include "json-lib.h" #ifdef WIDE -# define L(str) L##str # define STRDUP wcsdup -# define SIMPLE_STRNCMP simple_wcsncmp - -/* Wcsncmp uses signed semantics for comparison, not unsigned. - Avoid using substraction since possible overflow. */ -int -simple_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n) -{ - wchar_t c1, c2; - - while (n--) - { - c1 = *s1++; - c2 = *s2++; - if (c1 == L ('\0') || c1 != c2) - return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0); - } - return 0; -} - #else -# define L(str) str # define STRDUP strdup -# define SIMPLE_STRNCMP simple_strncmp -/* Strncmp uses unsigned semantics for comparison. */ int -simple_strncmp (const char *s1, const char *s2, size_t n) -{ - int ret = 0; +generic_strncmp (const char *s1, const char *s2, size_t n); - while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0 - && *s1++); - return ret; -} +IMPL (generic_strncmp, 0) -#endif /* !WIDE */ +#endif typedef int (*proto_t) (const CHAR *, const CHAR *, size_t); -IMPL (SIMPLE_STRNCMP, 0) IMPL (STRNCMP, 1) - static void do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR *s2, size_t n, int exp_result) { - size_t i, iters = INNER_LOOP_ITERS8; + size_t i, iters = INNER_LOOP_ITERS; timing_t start, stop, cur; TIMING_NOW (start); @@ -240,7 +210,7 @@ do_test_page_boundary (json_ctx_t *json_ctx) CHAR *s1p = s1 + align1; CHAR *s2p = s2 + align2; len = (page_size / CHARBYTES) - 1 - align1; - exp_result = SIMPLE_STRNCMP (s1p, s2p, s); + exp_result = STRNCMP (s1p, s2p, s); do_one_test_page_boundary (json_ctx, s1p, s2p, align1, align2, len, s, exp_result); } @@ -456,3 +426,10 @@ test_main (void) } #include + +#ifndef WIDE +# undef STRNCMP +# define STRNCMP generic_strncmp +# define libc_hidden_builtin_def(X) +# include +#endif -- 2.7.4