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