(root)/
glibc-2.38/
benchtests/
bench-rawmemchr.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 <assert.h>
      20  #include <stdint.h>
      21  
      22  #define TEST_MAIN
      23  #define TEST_NAME "rawmemchr"
      24  #include "bench-string.h"
      25  
      26  #include "json-lib.h"
      27  
      28  typedef char *(*proto_t) (const char *, int);
      29  
      30  char *
      31  generic_rawmemchr (const char *s, int c)
      32  {
      33    if ((unsigned char) c != 0)
      34      return memchr (s, c, PTRDIFF_MAX);
      35    return (char *)s + strlen (s);
      36  }
      37  
      38  IMPL (rawmemchr, 1)
      39  IMPL (generic_rawmemchr, 0)
      40  
      41  static void
      42  do_one_test (json_ctx_t *json_ctx, impl_t *impl, const char *s, int c, char *exp_res)
      43  {
      44    size_t i, iters = INNER_LOOP_ITERS_LARGE * 4;
      45    timing_t start, stop, cur;
      46    char *res = CALL (impl, s, c);
      47    if (res != exp_res)
      48      {
      49        error (0, 0, "Wrong result in function %s %p %p", impl->name,
      50  	     res, exp_res);
      51        ret = 1;
      52        return;
      53      }
      54  
      55    TIMING_NOW (start);
      56    for (i = 0; i < iters; ++i)
      57      {
      58        CALL (impl, s, c);
      59      }
      60    TIMING_NOW (stop);
      61  
      62    TIMING_DIFF (cur, start, stop);
      63  
      64    json_element_double (json_ctx, (double) cur / (double) iters);
      65  }
      66  
      67  static void
      68  do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len, int seek_char)
      69  {
      70    size_t i;
      71    char *result;
      72  
      73    align &= getpagesize () - 1;
      74    if (align + len >= page_size)
      75      return;
      76  
      77    for (i = 0; i < len; ++i)
      78      {
      79        buf1[align + i] = 1 + 23 * i % 127;
      80        if (buf1[align + i] == seek_char)
      81  	buf1[align + i] = seek_char + 1;
      82      }
      83    buf1[align + len] = 0;
      84  
      85    assert (pos < len);
      86  
      87    buf1[align + pos] = seek_char;
      88    buf1[align + len] = -seek_char;
      89    result = (char *) (buf1 + align + pos);
      90  
      91    json_element_object_begin (json_ctx);
      92    json_attr_uint (json_ctx, "length", pos);
      93    json_attr_uint (json_ctx, "alignment", align);
      94    json_attr_uint (json_ctx, "char", seek_char);
      95    json_array_begin (json_ctx, "timings");
      96  
      97    FOR_EACH_IMPL (impl, 0)
      98      do_one_test (json_ctx, impl, (char *) (buf1 + align), seek_char, result);
      99  
     100    json_array_end (json_ctx);
     101    json_element_object_end (json_ctx);
     102  }
     103  
     104  int
     105  test_main (void)
     106  {
     107    json_ctx_t json_ctx;
     108    size_t i;
     109    test_init ();
     110  
     111    json_init (&json_ctx, 0, stdout);
     112  
     113    json_document_begin (&json_ctx);
     114    json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
     115  
     116    json_attr_object_begin (&json_ctx, "functions");
     117    json_attr_object_begin (&json_ctx, TEST_NAME);
     118    json_attr_string (&json_ctx, "bench-variant", "");
     119  
     120    json_array_begin (&json_ctx, "ifuncs");
     121    FOR_EACH_IMPL (impl, 0)
     122      json_element_string (&json_ctx, impl->name);
     123    json_array_end (&json_ctx);
     124  
     125    json_array_begin (&json_ctx, "results");
     126  
     127    for (i = 1; i < 7; ++i)
     128      {
     129        do_test (&json_ctx, 0, 16 << i, 2048, 23);
     130        do_test (&json_ctx, i, 64, 256, 23);
     131        do_test (&json_ctx, 0, 16 << i, 2048, 0);
     132        do_test (&json_ctx, i, 64, 256, 0);
     133      }
     134    for (i = 1; i < 32; ++i)
     135      {
     136        do_test (&json_ctx, 0, i, i + 1, 23);
     137        do_test (&json_ctx, 0, i, i + 1, 0);
     138      }
     139    for (; i < 256; i += 32)
     140      {
     141        do_test (&json_ctx, 0, i, i + 1, 23);
     142        do_test (&json_ctx, 0, i - 1, i, 23);
     143      }
     144    for (; i < 512; i += 64)
     145      {
     146        do_test (&json_ctx, 0, i, i + 1, 23);
     147        do_test (&json_ctx, 0, i - 1, i, 23);
     148      }
     149    for (; i < 1024; i += 128)
     150      {
     151        do_test (&json_ctx, 0, i, i + 1, 23);
     152        do_test (&json_ctx, 0, i - 1, i, 23);
     153      }
     154    for (; i < 2048; i += 256)
     155      {
     156        do_test (&json_ctx, 0, i, i + 1, 23);
     157        do_test (&json_ctx, 0, i - 1, i, 23);
     158      }
     159    for (; i < 4096; i += 512)
     160      {
     161        do_test (&json_ctx, 0, i, i + 1, 23);
     162        do_test (&json_ctx, 0, i - 1, i, 23);
     163      }
     164  
     165    json_array_end (&json_ctx);
     166    json_attr_object_end (&json_ctx);
     167    json_attr_object_end (&json_ctx);
     168    json_document_end (&json_ctx);
     169  
     170    return ret;
     171  }
     172  
     173  #include <support/test-driver.c>