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