Blame benchtests/bench-memmem.c

Packit Service 82fcde
/* Measure memmem functions.
Packit Service 82fcde
   Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#define TEST_MAIN
Packit Service 82fcde
#define TEST_NAME "memmem"
Packit Service 82fcde
#define BUF1PAGES 20
Packit Service 82fcde
#define ITERATIONS 500
Packit Service 82fcde
#include "bench-string.h"
Packit Service 82fcde
Packit Service 82fcde
typedef char *(*proto_t) (const void *, size_t, const void *, size_t);
Packit Service 82fcde
void *simple_memmem (const void *, size_t, const void *, size_t);
Packit Service 82fcde
Packit Service 82fcde
IMPL (simple_memmem, 0)
Packit Service 82fcde
IMPL (memmem, 1)
Packit Service 82fcde
Packit Service 82fcde
void *
Packit Service 82fcde
simple_memmem (const void *haystack, size_t haystack_len, const void *needle,
Packit Service 82fcde
	       size_t needle_len)
Packit Service 82fcde
{
Packit Service 82fcde
  const char *begin;
Packit Service 82fcde
  const char *const last_possible
Packit Service 82fcde
    = (const char *) haystack + haystack_len - needle_len;
Packit Service 82fcde
Packit Service 82fcde
  if (needle_len == 0)
Packit Service 82fcde
    /* The first occurrence of the empty string is deemed to occur at
Packit Service 82fcde
       the beginning of the string.  */
Packit Service 82fcde
    return (void *) haystack;
Packit Service 82fcde
Packit Service 82fcde
  /* Sanity check, otherwise the loop might search through the whole
Packit Service 82fcde
     memory.  */
Packit Service 82fcde
  if (__glibc_unlikely (haystack_len < needle_len))
Packit Service 82fcde
    return NULL;
Packit Service 82fcde
Packit Service 82fcde
  for (begin = (const char *) haystack; begin <= last_possible; ++begin)
Packit Service 82fcde
    if (begin[0] == ((const char *) needle)[0] &&
Packit Service 82fcde
	!memcmp ((const void *) &begin[1],
Packit Service 82fcde
		 (const void *) ((const char *) needle + 1),
Packit Service 82fcde
		 needle_len - 1))
Packit Service 82fcde
      return (void *) begin;
Packit Service 82fcde
Packit Service 82fcde
  return NULL;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
do_one_test (impl_t *impl, const void *haystack, size_t haystack_len,
Packit Service 82fcde
	     const void *needle, size_t needle_len, const void *expected)
Packit Service 82fcde
{
Packit Service 82fcde
  size_t i, iters = INNER_LOOP_ITERS;
Packit Service 82fcde
  timing_t start, stop, cur;
Packit Service 82fcde
Packit Service 82fcde
  TIMING_NOW (start);
Packit Service 82fcde
  for (i = 0; i < iters; ++i)
Packit Service 82fcde
    {
Packit Service 82fcde
      CALL (impl, haystack, haystack_len, needle, needle_len);
Packit Service 82fcde
    }
Packit Service 82fcde
  TIMING_NOW (stop);
Packit Service 82fcde
Packit Service 82fcde
  TIMING_DIFF (cur, start, stop);
Packit Service 82fcde
Packit Service 82fcde
  TIMING_PRINT_MEAN ((double) cur, (double) iters);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
do_test (const char *str, size_t len, size_t idx)
Packit Service 82fcde
{
Packit Service 82fcde
  char tmpbuf[len];
Packit Service 82fcde
Packit Service 82fcde
  memcpy (tmpbuf, buf1 + idx, len);
Packit Service 82fcde
  memcpy (buf1 + idx, str, len);
Packit Service 82fcde
Packit Service 82fcde
  printf ("String %s, offset %zd:", str, idx);
Packit Service 82fcde
Packit Service 82fcde
  FOR_EACH_IMPL (impl, 0)
Packit Service 82fcde
    do_one_test (impl, buf1, BUF1PAGES * page_size, str, len, buf1 + idx);
Packit Service 82fcde
Packit Service 82fcde
  memcpy (buf1 + idx, tmpbuf, len);
Packit Service 82fcde
Packit Service 82fcde
  putchar ('\n');
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
do_random_tests (void)
Packit Service 82fcde
{
Packit Service 82fcde
  for (size_t n = 0; n < ITERATIONS; ++n)
Packit Service 82fcde
    {
Packit Service 82fcde
      char tmpbuf[32];
Packit Service 82fcde
Packit Service 82fcde
      size_t shift = random () % 11;
Packit Service 82fcde
      size_t rel = random () % ((2 << (shift + 1)) * 64);
Packit Service 82fcde
      size_t idx = MIN ((2 << shift) * 64 + rel, BUF1PAGES * page_size - 2);
Packit Service 82fcde
      size_t len = random () % (sizeof (tmpbuf) - 1) + 1;
Packit Service 82fcde
      len = MIN (len, BUF1PAGES * page_size - idx - 1);
Packit Service 82fcde
      memcpy (tmpbuf, buf1 + idx, len);
Packit Service 82fcde
      for (size_t i = random () % len / 2 + 1; i > 0; --i)
Packit Service 82fcde
	{
Packit Service 82fcde
	  size_t off = random () % len;
Packit Service 82fcde
	  char ch = '0' + random () % 10;
Packit Service 82fcde
Packit Service 82fcde
	  buf1[idx + off] = ch;
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
      printf ("String %.*s, offset %zd:", (int) len, buf1 + idx, idx);
Packit Service 82fcde
Packit Service 82fcde
      FOR_EACH_IMPL (impl, 0)
Packit Service 82fcde
	do_one_test (impl, buf1, BUF1PAGES * page_size, buf1 + idx, len,
Packit Service 82fcde
		     buf1 + idx);
Packit Service 82fcde
Packit Service 82fcde
      putchar ('\n');
Packit Service 82fcde
Packit Service 82fcde
      memcpy (buf1 + idx, tmpbuf, len);
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static const char *const strs[] =
Packit Service 82fcde
  {
Packit Service 82fcde
    "00000", "00112233", "0123456789", "0000111100001111",
Packit Service 82fcde
    "00000111110000022222", "012345678901234567890",
Packit Service 82fcde
    "abc0", "aaaa0", "abcabc0"
Packit Service 82fcde
  };
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
test_main (void)
Packit Service 82fcde
{
Packit Service 82fcde
  size_t i;
Packit Service 82fcde
Packit Service 82fcde
  test_init ();
Packit Service 82fcde
Packit Service 82fcde
  printf ("%23s", "");
Packit Service 82fcde
  FOR_EACH_IMPL (impl, 0)
Packit Service 82fcde
    printf ("\t%s", impl->name);
Packit Service 82fcde
  putchar ('\n');
Packit Service 82fcde
Packit Service 82fcde
  for (i = 0; i < BUF1PAGES * page_size; ++i)
Packit Service 82fcde
    buf1[i] = 60 + random () % 32;
Packit Service 82fcde
Packit Service 82fcde
  for (i = 0; i < sizeof (strs) / sizeof (strs[0]); ++i)
Packit Service 82fcde
    for (size_t j = 0; j < 120; j += 7)
Packit Service 82fcde
      {
Packit Service 82fcde
	size_t len = strlen (strs[i]);
Packit Service 82fcde
Packit Service 82fcde
	do_test (strs[i], len, j);
Packit Service 82fcde
      }
Packit Service 82fcde
Packit Service 82fcde
  do_random_tests ();
Packit Service 82fcde
  return ret;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#include <support/test-driver.c>