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  <adhemerval.zanella@linaro.org>
This commit is contained in:
Wilco Dijkstra 2023-03-03 12:40:22 +00:00
parent 6a734e62f1
commit d1c3c0e4fe
2 changed files with 24 additions and 74 deletions

View file

@ -23,59 +23,26 @@
# define TEST_NAME "strcmp" # define TEST_NAME "strcmp"
#endif #endif
#include "bench-string.h" #include "bench-string.h"
#include "json-lib.h"
#ifdef WIDE #ifdef WIDE
# define L(str) L##str
# define SIMPLE_STRCMP simple_wcscmp
# define CHARBYTESLOG 2 # define CHARBYTESLOG 2
# define MIDCHAR 0x7fffffff # define MIDCHAR 0x7fffffff
# define LARGECHAR 0xfffffffe # 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 #else
# include <limits.h>
# define L(str) str
# define SIMPLE_STRCMP simple_strcmp
# define CHARBYTESLOG 0 # define CHARBYTESLOG 0
# define MIDCHAR 0x7f # define MIDCHAR 0x7f
# define LARGECHAR 0xfe # define LARGECHAR 0xfe
/* Strcmp uses unsigned semantics for comparison. */
int int
simple_strcmp (const char *s1, const char *s2) generic_strcmp (const char *s1, const char *s2);
{
int ret;
while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++); IMPL (generic_strcmp, 0)
return ret;
}
#endif #endif
# include "json-lib.h"
typedef int (*proto_t) (const CHAR *, const CHAR *); typedef int (*proto_t) (const CHAR *, const CHAR *);
IMPL (SIMPLE_STRCMP, 1)
IMPL (STRCMP, 1) IMPL (STRCMP, 1)
static void static void
@ -83,7 +50,7 @@ do_one_test (json_ctx_t *json_ctx, impl_t *impl,
const CHAR *s1, const CHAR *s2, const CHAR *s1, const CHAR *s2,
int exp_result) int exp_result)
{ {
size_t i, iters = INNER_LOOP_ITERS8; size_t i, iters = INNER_LOOP_ITERS;
timing_t start, stop, cur; timing_t start, stop, cur;
TIMING_NOW (start); TIMING_NOW (start);
@ -201,7 +168,7 @@ do_test_page_boundary (json_ctx_t *json_ctx)
CHAR *s1p = s1 + align1; CHAR *s1p = s1 + align1;
CHAR *s2p = s2 + align2; CHAR *s2p = s2 + align2;
len = (page_size / CHARBYTES) - 1 - align1; 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, do_one_test_page_boundary (json_ctx, s1p, s2p, align1, align2,
len, exp_result); len, exp_result);
} }
@ -324,3 +291,9 @@ test_main (void)
} }
#include <support/test-driver.c> #include <support/test-driver.c>
#ifndef WIDE
# undef STRCMP
# define STRCMP generic_strcmp
# include <string/strcmp.c>
#endif

View file

@ -21,61 +21,31 @@
# define TEST_NAME "wcsncmp" # define TEST_NAME "wcsncmp"
#else #else
# define TEST_NAME "strncmp" # define TEST_NAME "strncmp"
#endif /* !WIDE */ #endif
#include "bench-string.h" #include "bench-string.h"
#include "json-lib.h" #include "json-lib.h"
#ifdef WIDE #ifdef WIDE
# define L(str) L##str
# define STRDUP wcsdup # 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 #else
# define L(str) str
# define STRDUP strdup # define STRDUP strdup
# define SIMPLE_STRNCMP simple_strncmp
/* Strncmp uses unsigned semantics for comparison. */
int int
simple_strncmp (const char *s1, const char *s2, size_t n) generic_strncmp (const char *s1, const char *s2, size_t n);
{
int ret = 0;
while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0 IMPL (generic_strncmp, 0)
&& *s1++);
return ret;
}
#endif /* !WIDE */ #endif
typedef int (*proto_t) (const CHAR *, const CHAR *, size_t); typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
IMPL (SIMPLE_STRNCMP, 0)
IMPL (STRNCMP, 1) IMPL (STRNCMP, 1)
static void static void
do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR
*s2, size_t n, int exp_result) *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_t start, stop, cur;
TIMING_NOW (start); TIMING_NOW (start);
@ -240,7 +210,7 @@ do_test_page_boundary (json_ctx_t *json_ctx)
CHAR *s1p = s1 + align1; CHAR *s1p = s1 + align1;
CHAR *s2p = s2 + align2; CHAR *s2p = s2 + align2;
len = (page_size / CHARBYTES) - 1 - align1; 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, do_one_test_page_boundary (json_ctx, s1p, s2p, align1, align2,
len, s, exp_result); len, s, exp_result);
} }
@ -456,3 +426,10 @@ test_main (void)
} }
#include <support/test-driver.c> #include <support/test-driver.c>
#ifndef WIDE
# undef STRNCMP
# define STRNCMP generic_strncmp
# define libc_hidden_builtin_def(X)
# include <string/strncmp.c>
#endif