(root)/
glibc-2.38/
benchtests/
bench-strspn.c
       1  /* Measure strspn 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  #define TEST_MAIN
      20  #ifndef WIDE
      21  # define TEST_NAME "strspn"
      22  #else
      23  # define TEST_NAME "wcsspn"
      24  #endif /* WIDE */
      25  #include "bench-string.h"
      26  #include "json-lib.h"
      27  
      28  #define BIG_CHAR MAX_CHAR
      29  
      30  #ifndef WIDE
      31  # define SMALL_CHAR 127
      32  #else
      33  # define SMALL_CHAR 1273
      34  #endif /* WIDE */
      35  
      36  typedef size_t (*proto_t) (const CHAR *, const CHAR *);
      37  
      38  IMPL (STRSPN, 1)
      39  
      40  static void
      41  do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s,
      42               const CHAR *acc, size_t exp_res)
      43  {
      44    size_t res = CALL (impl, s, acc), i, iters = INNER_LOOP_ITERS8 / CHARBYTES;
      45    timing_t start, stop, cur;
      46  
      47    if (res != exp_res)
      48      {
      49        error (0, 0, "Wrong result in function %s %p %p", impl->name,
      50  	     (void *) res, (void *) 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, acc);
      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 align1, size_t align2, size_t pos,
      69           size_t len)
      70  {
      71    size_t i;
      72    CHAR *acc, *s;
      73  
      74    align1 &= 7;
      75    if ((align1 + pos + 10) * sizeof (CHAR) >= page_size || len > 240 || !len)
      76      return;
      77    if ((align2 + len) * sizeof (CHAR) >= page_size)
      78      return;
      79  
      80    acc = (CHAR *) (buf2) + align2;
      81    s = (CHAR *) (buf1) + align1;
      82  
      83    for (i = 0; i < len; ++i)
      84      {
      85        acc[i] = random () & BIG_CHAR;
      86        if (!acc[i])
      87  	acc[i] = random () & BIG_CHAR;
      88        if (!acc[i])
      89  	acc[i] = 1 + (random () & SMALL_CHAR);
      90      }
      91    acc[len] = '\0';
      92  
      93    for (i = 0; i < pos; ++i)
      94      s[i] = acc[random () % len];
      95    s[pos] = random () & BIG_CHAR;
      96    if (STRCHR (acc, s[pos]))
      97      s[pos] = '\0';
      98    else
      99      {
     100        for (i = pos + 1; i < pos + 10; ++i)
     101  	s[i] = random () & BIG_CHAR;
     102        s[i] = '\0';
     103      }
     104  
     105    json_element_object_begin (json_ctx);
     106    json_attr_uint (json_ctx, "len", len);
     107    json_attr_uint (json_ctx, "pos", pos);
     108    json_attr_uint (json_ctx, "align1", align1);
     109    json_attr_uint (json_ctx, "align2", align2);
     110    json_array_begin (json_ctx, "timings");
     111  
     112    FOR_EACH_IMPL (impl, 0)
     113      do_one_test (json_ctx, impl, s, acc, pos);
     114  
     115    json_array_end (json_ctx);
     116    json_element_object_end (json_ctx);
     117  }
     118  
     119  int
     120  test_main (void)
     121  {
     122    json_ctx_t json_ctx;
     123    size_t i;
     124  
     125    test_init ();
     126  
     127    json_init (&json_ctx, 0, stdout);
     128  
     129    json_document_begin (&json_ctx);
     130    json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
     131  
     132    json_attr_object_begin (&json_ctx, "functions");
     133    json_attr_object_begin (&json_ctx, TEST_NAME);
     134    json_attr_string (&json_ctx, "bench-variant", "");
     135  
     136    json_array_begin (&json_ctx, "ifuncs");
     137    FOR_EACH_IMPL (impl, 0)
     138      json_element_string (&json_ctx, impl->name);
     139    json_array_end (&json_ctx);
     140  
     141    json_array_begin (&json_ctx, "results");
     142  
     143    for (i = 0; i < 32; ++i)
     144      {
     145        do_test (&json_ctx, 0, 0, 512, i);
     146        do_test (&json_ctx, i, 0, 512, i);
     147        do_test (&json_ctx, 0, i, 512, i);
     148        do_test (&json_ctx, i, i, 512, i);
     149      }
     150  
     151    for (i = 1; i < 8; ++i)
     152      {
     153        do_test (&json_ctx, 0, 0, 16 << i, 4);
     154        do_test (&json_ctx, i, 0, 16 << i, 4);
     155        do_test (&json_ctx, 0, i, 16 << i, 4);
     156        do_test (&json_ctx, i, i, 16 << i, 4);
     157      }
     158  
     159    for (i = 1; i < 8; ++i)
     160      {
     161        do_test (&json_ctx, i, 0, 64, 10);
     162        do_test (&json_ctx, i, i, 64, 10);
     163      }
     164  
     165    for (i = 0; i < 64; ++i)
     166      {
     167        do_test (&json_ctx, 0, 0, i, 6);
     168        do_test (&json_ctx, 0, i, i, 6);
     169      }
     170  
     171    json_array_end (&json_ctx);
     172    json_attr_object_end (&json_ctx);
     173    json_attr_object_end (&json_ctx);
     174    json_document_end (&json_ctx);
     175  
     176    return ret;
     177  }
     178  
     179  #include <support/test-driver.c>