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