(root)/
glibc-2.38/
benchtests/
bench-memchr.c
       1  /* Measure memchr functions.
       2     Copyright (C) 2013-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include "json-lib.h"
      20  
      21  #ifndef WIDE
      22  # define SMALL_CHAR 127
      23  #else
      24  # define SMALL_CHAR 1273
      25  #endif /* WIDE */
      26  
      27  #ifndef USE_AS_MEMRCHR
      28  # define TEST_MAIN
      29  # ifndef WIDE
      30  #  define TEST_NAME "memchr"
      31  # else
      32  #  define TEST_NAME "wmemchr"
      33  # endif /* WIDE */
      34  # include "bench-string.h"
      35  
      36  typedef void *(*proto_t) (const void *, int, size_t);
      37  
      38  void *
      39  generic_memchr (const void *, int, size_t);
      40  
      41  IMPL (MEMCHR, 1)
      42  
      43  # ifndef WIDE
      44  IMPL (generic_memchr, 0)
      45  # endif
      46  
      47  #endif /* !USE_AS_MEMRCHR */
      48  
      49  
      50  static void
      51  do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, int c,
      52  	     size_t n)
      53  {
      54    size_t i, iters = INNER_LOOP_ITERS8 / 2;
      55    timing_t start, stop, cur;
      56  
      57    TIMING_NOW (start);
      58    for (i = 0; i < iters; ++i)
      59      {
      60        CALL (impl, s, c, n);
      61      }
      62    TIMING_NOW (stop);
      63  
      64    TIMING_DIFF (cur, start, stop);
      65  
      66    json_element_double (json_ctx, (double) cur / (double) iters);
      67  }
      68  
      69  static void
      70  do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len,
      71  	 int seek_char, int invert_pos)
      72  {
      73    size_t i;
      74  
      75    align &= getpagesize () - 1;
      76    if ((align + len) * sizeof (CHAR) >= page_size)
      77      return;
      78  
      79    CHAR *buf = (CHAR *) (buf1);
      80  
      81    for (i = 0; i < len; ++i)
      82      {
      83        buf[align + i] = 1 + 23 * i % SMALL_CHAR;
      84        if (buf[align + i] == seek_char)
      85  	buf[align + i] = seek_char + 1;
      86      }
      87    buf[align + len] = 0;
      88  
      89    if (pos < len)
      90      {
      91        if (invert_pos)
      92  	buf[align + len - pos] = seek_char;
      93        else
      94  	buf[align + pos] = seek_char;
      95        buf[align + len] = -seek_char;
      96      }
      97    else
      98      {
      99        buf[align + len] = seek_char;
     100      }
     101  
     102    json_element_object_begin (json_ctx);
     103    json_attr_uint (json_ctx, "align", align);
     104    json_attr_uint (json_ctx, "pos", pos);
     105    json_attr_uint (json_ctx, "len", len);
     106    json_attr_uint (json_ctx, "seek_char", seek_char);
     107    json_attr_uint (json_ctx, "invert_pos", invert_pos);
     108  
     109    json_array_begin (json_ctx, "timings");
     110  
     111    FOR_EACH_IMPL (impl, 0)
     112      do_one_test (json_ctx, impl, (CHAR *) (buf + align), seek_char, len);
     113  
     114    json_array_end (json_ctx);
     115    json_element_object_end (json_ctx);
     116  }
     117  
     118  int
     119  test_main (void)
     120  {
     121    size_t i, j, al, al_max;
     122    int repeats;
     123    json_ctx_t json_ctx;
     124    test_init ();
     125  
     126    json_init (&json_ctx, 0, stdout);
     127  
     128    json_document_begin (&json_ctx);
     129    json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
     130  
     131    json_attr_object_begin (&json_ctx, "functions");
     132    json_attr_object_begin (&json_ctx, TEST_NAME);
     133    json_attr_string (&json_ctx, "bench-variant", "");
     134  
     135    json_array_begin (&json_ctx, "ifuncs");
     136    FOR_EACH_IMPL (impl, 0)
     137      json_element_string (&json_ctx, impl->name);
     138    json_array_end (&json_ctx);
     139  
     140    json_array_begin (&json_ctx, "results");
     141  
     142    al_max = 0;
     143  #ifdef USE_AS_MEMRCHR
     144    al_max = getpagesize () / 2;
     145  #endif
     146  
     147    for (repeats = 0; repeats < 2; ++repeats)
     148      {
     149        for (al = 0; al <= al_max; al += getpagesize () / 2)
     150  	{
     151  	  for (i = 1; i < 8; ++i)
     152  	    {
     153  	      do_test (&json_ctx, al, 16 << i, 2048, 23, repeats);
     154  	      do_test (&json_ctx, al + i, 64, 256, 23, repeats);
     155  	      do_test (&json_ctx, al, 16 << i, 2048, 0, repeats);
     156  	      do_test (&json_ctx, al + i, 64, 256, 0, repeats);
     157  
     158  	      do_test (&json_ctx, al + getpagesize () - 15, 64, 256, 0,
     159  		       repeats);
     160  #ifdef USE_AS_MEMRCHR
     161  	      /* Also test the position close to the beginning for memrchr.  */
     162  	      do_test (&json_ctx, al, i, 256, 23, repeats);
     163  	      do_test (&json_ctx, al, i, 256, 0, repeats);
     164  	      do_test (&json_ctx, al + i, i, 256, 23, repeats);
     165  	      do_test (&json_ctx, al + i, i, 256, 0, repeats);
     166  #endif
     167  	    }
     168  	  for (i = 1; i < 8; ++i)
     169  	    {
     170  	      do_test (&json_ctx, al + i, i << 5, 192, 23, repeats);
     171  	      do_test (&json_ctx, al + i, i << 5, 192, 0, repeats);
     172  	      do_test (&json_ctx, al + i, i << 5, 256, 23, repeats);
     173  	      do_test (&json_ctx, al + i, i << 5, 256, 0, repeats);
     174  	      do_test (&json_ctx, al + i, i << 5, 512, 23, repeats);
     175  	      do_test (&json_ctx, al + i, i << 5, 512, 0, repeats);
     176  
     177  	      do_test (&json_ctx, al + getpagesize () - 15, i << 5, 256, 23,
     178  		       repeats);
     179  	    }
     180  	}
     181  
     182        for (i = 1; i < 32; ++i)
     183  	{
     184  	  do_test (&json_ctx, 0, i, i + 1, 23, repeats);
     185  	  do_test (&json_ctx, 0, i, i + 1, 0, repeats);
     186  	  do_test (&json_ctx, i, i, i + 1, 23, repeats);
     187  	  do_test (&json_ctx, i, i, i + 1, 0, repeats);
     188  	  do_test (&json_ctx, 0, i, i - 1, 23, repeats);
     189  	  do_test (&json_ctx, 0, i, i - 1, 0, repeats);
     190  	  do_test (&json_ctx, i, i, i - 1, 23, repeats);
     191  	  do_test (&json_ctx, i, i, i - 1, 0, repeats);
     192  
     193  	  do_test (&json_ctx, getpagesize () / 2, i, i + 1, 23, repeats);
     194  	  do_test (&json_ctx, getpagesize () / 2, i, i + 1, 0, repeats);
     195  	  do_test (&json_ctx, getpagesize () / 2 + i, i, i + 1, 23, repeats);
     196  	  do_test (&json_ctx, getpagesize () / 2 + i, i, i + 1, 0, repeats);
     197  	  do_test (&json_ctx, getpagesize () / 2, i, i - 1, 23, repeats);
     198  	  do_test (&json_ctx, getpagesize () / 2, i, i - 1, 0, repeats);
     199  	  do_test (&json_ctx, getpagesize () / 2 + i, i, i - 1, 23, repeats);
     200  	  do_test (&json_ctx, getpagesize () / 2 + i, i, i - 1, 0, repeats);
     201  
     202  	  do_test (&json_ctx, getpagesize () - 15, i, i - 1, 23, repeats);
     203  	  do_test (&json_ctx, getpagesize () - 15, i, i - 1, 0, repeats);
     204  
     205  	  do_test (&json_ctx, getpagesize () - 15, i, i + 1, 23, repeats);
     206  	  do_test (&json_ctx, getpagesize () - 15, i, i + 1, 0, repeats);
     207  
     208  #ifdef USE_AS_MEMRCHR
     209  	  do_test (&json_ctx, 0, 1, i + 1, 23, repeats);
     210  	  do_test (&json_ctx, 0, 2, i + 1, 0, repeats);
     211  #endif
     212  	}
     213        for (al = 0; al <= al_max; al += getpagesize () / 2)
     214  	{
     215  	  for (i = (16 / sizeof (CHAR)); i <= (8192 / sizeof (CHAR)); i += i)
     216  	    {
     217  	      for (j = 0; j <= (384 / sizeof (CHAR));
     218  		   j += (32 / sizeof (CHAR)))
     219  		{
     220  		  do_test (&json_ctx, al, i + j, i, 23, repeats);
     221  		  do_test (&json_ctx, al, i, i + j, 23, repeats);
     222  		  if (j < i)
     223  		    {
     224  		      do_test (&json_ctx, al, i - j, i, 23, repeats);
     225  		      do_test (&json_ctx, al, i, i - j, 23, repeats);
     226  		    }
     227  		}
     228  	    }
     229  	}
     230  
     231  #ifndef USE_AS_MEMRCHR
     232        break;
     233  #endif
     234      }
     235  
     236    json_array_end (&json_ctx);
     237    json_attr_object_end (&json_ctx);
     238    json_attr_object_end (&json_ctx);
     239    json_document_end (&json_ctx);
     240  
     241    return ret;
     242  }
     243  
     244  #include <support/test-driver.c>
     245  
     246  #ifndef WIDE
     247  # ifndef USE_AS_MEMRCHR
     248  #  undef MEMCHR
     249  #  define MEMCHR generic_memchr
     250  #  include <string/memchr.c>
     251  # else
     252  #  undef MEMRCHR
     253  #  define MEMRCHR generic_memrchr
     254  #  include <string/memrchr.c>
     255  # endif
     256  #endif