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