(root)/
glibc-2.38/
benchtests/
bench-strcat.c
       1  /* Measure strcat 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 "strcat"
      22  #else
      23  # define TEST_NAME "wcscat"
      24  # define generic_strcat generic_wcscat
      25  #endif /* WIDE */
      26  #include "bench-string.h"
      27  
      28  #define BIG_CHAR MAX_CHAR
      29  
      30  #ifndef WIDE
      31  # define sfmt "s"
      32  # define SMALL_CHAR 127
      33  #else
      34  # define sfmt "ls"
      35  # define SMALL_CHAR 1273
      36  #endif /* WIDE */
      37  
      38  #include "json-lib.h"
      39  
      40  typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
      41  
      42  CHAR *
      43  generic_strcat (CHAR *dst, const CHAR *src)
      44  {
      45    STRCPY (dst + STRLEN (dst), src);
      46    return dst;
      47  }
      48  
      49  IMPL (STRCAT, 1)
      50  IMPL (generic_strcat, 0)
      51  
      52  static void
      53  do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *dst, const CHAR *src)
      54  {
      55    size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS_LARGE / CHARBYTES;
      56    timing_t start, stop, cur;
      57  
      58    if (CALL (impl, dst, src) != dst)
      59      {
      60        error (0, 0, "Wrong result in function %s %p %p", impl->name,
      61  	     CALL (impl, dst, src), dst);
      62        ret = 1;
      63        return;
      64      }
      65  
      66    if (STRCMP (dst + k, src) != 0)
      67      {
      68        error (0, 0,
      69  	     "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
      70  	     impl->name, dst, src);
      71        ret = 1;
      72        return;
      73      }
      74  
      75    TIMING_NOW (start);
      76    for (i = 0; i < iters; ++i)
      77      {
      78        dst[k] = '\0';
      79        CALL (impl, dst, src);
      80      }
      81    TIMING_NOW (stop);
      82  
      83    TIMING_DIFF (cur, start, stop);
      84  
      85    json_element_double (json_ctx, (double) cur / (double) iters);
      86  }
      87  
      88  static void
      89  do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len1,
      90  	 size_t len2, int max_char)
      91  {
      92    size_t i;
      93    CHAR *s1, *s2;
      94  
      95    align1 &= 7;
      96    if ((align1 + len1) * sizeof (CHAR) >= page_size)
      97      return;
      98  
      99    align2 &= 7;
     100    if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
     101      return;
     102  
     103    s1 = (CHAR *) (buf1) + align1;
     104    s2 = (CHAR *) (buf2) + align2;
     105  
     106    for (i = 0; i < len1; ++i)
     107      s1[i] = 32 + 23 * i % (max_char - 32);
     108    s1[len1] = '\0';
     109  
     110    for (i = 0; i < len2; i++)
     111      s2[i] = 32 + 23 * i % (max_char - 32);
     112  
     113    json_element_object_begin (json_ctx);
     114    json_attr_uint (json_ctx, "align1", align1);
     115    json_attr_uint (json_ctx, "align2", align2);
     116    json_attr_uint (json_ctx, "len1", len1);
     117    json_attr_uint (json_ctx, "len2", len2);
     118    json_attr_uint (json_ctx, "max_char", max_char);
     119  
     120    json_array_begin (json_ctx, "timings");
     121  
     122    FOR_EACH_IMPL (impl, 0)
     123      {
     124        s2[len2] = '\0';
     125        do_one_test (json_ctx, impl, s2, s1);
     126      }
     127  
     128    json_array_end (json_ctx);
     129    json_element_object_end (json_ctx);
     130  }
     131  
     132  int
     133  test_main (void)
     134  {
     135    json_ctx_t json_ctx;
     136    size_t i;
     137  
     138    test_init ();
     139  
     140    test_init ();
     141  
     142    json_init (&json_ctx, 0, stdout);
     143  
     144    json_document_begin (&json_ctx);
     145    json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
     146  
     147    json_attr_object_begin (&json_ctx, "functions");
     148    json_attr_object_begin (&json_ctx, TEST_NAME);
     149    json_attr_string (&json_ctx, "bench-variant", "");
     150  
     151    json_array_begin (&json_ctx, "ifuncs");
     152    FOR_EACH_IMPL (impl, 0)
     153      json_element_string (&json_ctx, impl->name);
     154    json_array_end (&json_ctx);
     155  
     156    json_array_begin (&json_ctx, "results");
     157  
     158    for (i = 0; i < 16; ++i)
     159      {
     160        do_test (&json_ctx, 0, 0, i, i, SMALL_CHAR);
     161        do_test (&json_ctx, 0, 0, i, i, BIG_CHAR);
     162        do_test (&json_ctx, 0, i, i, i, SMALL_CHAR);
     163        do_test (&json_ctx, i, 0, i, i, BIG_CHAR);
     164      }
     165  
     166    for (i = 1; i < 8; ++i)
     167      {
     168        do_test (&json_ctx, 0, 0, 8 << i, 8 << i, SMALL_CHAR);
     169        do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 8 << i, SMALL_CHAR);
     170        do_test (&json_ctx, 0, 0, 8 << i, 2 << i, SMALL_CHAR);
     171        do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 2 << i, SMALL_CHAR);
     172      }
     173  
     174    for (i = 1; i < 8; ++i)
     175      {
     176        do_test (&json_ctx, i, 2 * i, 8 << i, 1, SMALL_CHAR);
     177        do_test (&json_ctx, 2 * i, i, 8 << i, 1, BIG_CHAR);
     178        do_test (&json_ctx, i, i, 8 << i, 10, SMALL_CHAR);
     179        do_test (&json_ctx, i, i, 8 << i, 10, BIG_CHAR);
     180      }
     181  
     182    for (i = 32; i < 256; i += 32)
     183      {
     184        do_test (&json_ctx, 1, 0, i, 31, SMALL_CHAR);
     185        do_test (&json_ctx, 0, i, i, 31, SMALL_CHAR);
     186        do_test (&json_ctx, 0, 0, i, 31, SMALL_CHAR);
     187        do_test (&json_ctx, i, i, i, 31, SMALL_CHAR);
     188      }
     189  
     190    for (; i < 512; i += 64)
     191      {
     192        do_test (&json_ctx, 1, 0, i, 31, SMALL_CHAR);
     193        do_test (&json_ctx, 0, i, i, 31, SMALL_CHAR);
     194        do_test (&json_ctx, 0, 0, i, 31, SMALL_CHAR);
     195        do_test (&json_ctx, i, i, i, 31, SMALL_CHAR);
     196      }
     197  
     198    for (; i < 1024; i += 128)
     199      {
     200        do_test (&json_ctx, 1, 0, i, 31, SMALL_CHAR);
     201        do_test (&json_ctx, 0, i, i, 31, SMALL_CHAR);
     202        do_test (&json_ctx, 0, 0, i, 31, SMALL_CHAR);
     203        do_test (&json_ctx, i, i, i, 31, SMALL_CHAR);
     204      }
     205  
     206    for (; i < 2048; i += 256)
     207      {
     208        do_test (&json_ctx, 1, 0, i, 31, SMALL_CHAR);
     209        do_test (&json_ctx, 0, i, i, 31, SMALL_CHAR);
     210        do_test (&json_ctx, 0, 0, i, 31, SMALL_CHAR);
     211        do_test (&json_ctx, i, i, i, 31, SMALL_CHAR);
     212      }
     213  
     214    json_array_end (&json_ctx);
     215    json_attr_object_end (&json_ctx);
     216    json_attr_object_end (&json_ctx);
     217    json_document_end (&json_ctx);
     218  
     219    return ret;
     220  }
     221  
     222  #include <support/test-driver.c>