public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Benchtests: Remove simple_str(n)cmp
@ 2023-03-08 10:41 Wilco Dijkstra
  2023-03-08 15:17 ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 2+ messages in thread
From: Wilco Dijkstra @ 2023-03-08 10:41 UTC (permalink / raw)
  To: 'GNU C Library'


Instead of benchmarking slow byte oriented loops, include the optimized generic
strcmp/strncmp implementation. Adjust iteration count to reduce benchmark time.

---

diff --git a/benchtests/bench-strcmp.c b/benchtests/bench-strcmp.c
index 8cd690bac1a612d99ecd11c8dc917f7cc36e19ea..65fd04b43a2aeaab85ee4996f083333a32d7b29f 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 <limits.h>
-
-# 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 <support/test-driver.c>
+
+#ifndef WIDE
+# undef STRCMP
+# define STRCMP generic_strcmp
+# include <string/strcmp.c>
+#endif
diff --git a/benchtests/bench-strncmp.c b/benchtests/bench-strncmp.c
index a714e5a9d42541f8781c3e92f0212cb9ab18e61f..53707e6bca675efe57a16cce3c343153a18894ab 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 <support/test-driver.c>
+
+#ifndef WIDE
+# undef STRNCMP
+# define STRNCMP generic_strncmp
+# define libc_hidden_builtin_def(X)
+# include <string/strncmp.c>
+#endif


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-08 15:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08 10:41 [PATCH] Benchtests: Remove simple_str(n)cmp Wilco Dijkstra
2023-03-08 15:17 ` Adhemerval Zanella Netto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).