build-many-glibcs.py: Add openrisc hard float glibc variant
[glibc.git] / benchtests / bench-strspn.c
bloba9638d00b48e0de73c9ef453f04321714241abfd
1 /* Measure strspn functions.
2 Copyright (C) 2013-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #define TEST_MAIN
20 #ifndef WIDE
21 # define TEST_NAME "strspn"
22 #else
23 # define TEST_NAME "wcsspn"
24 #endif /* WIDE */
25 #include "bench-string.h"
26 #include "json-lib.h"
28 #define BIG_CHAR MAX_CHAR
30 #ifndef WIDE
31 # define SMALL_CHAR 127
32 #else
33 # define SMALL_CHAR 1273
34 #endif /* WIDE */
36 typedef size_t (*proto_t) (const CHAR *, const CHAR *);
38 IMPL (STRSPN, 1)
40 static void
41 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s,
42 const CHAR *acc, size_t exp_res)
44 size_t res = CALL (impl, s, acc), i, iters = INNER_LOOP_ITERS8 / CHARBYTES;
45 timing_t start, stop, cur;
47 if (res != exp_res)
49 error (0, 0, "Wrong result in function %s %p %p", impl->name,
50 (void *) res, (void *) exp_res);
51 ret = 1;
52 return;
55 TIMING_NOW (start);
56 for (i = 0; i < iters; ++i)
58 CALL (impl, s, acc);
60 TIMING_NOW (stop);
62 TIMING_DIFF (cur, start, stop);
64 json_element_double (json_ctx, (double)cur / (double)iters);
67 static void
68 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t pos,
69 size_t len)
71 size_t i;
72 CHAR *acc, *s;
74 align1 &= 7;
75 if ((align1 + pos + 10) * sizeof (CHAR) >= page_size || len > 240 || !len)
76 return;
77 if ((align2 + len) * sizeof (CHAR) >= page_size)
78 return;
80 acc = (CHAR *) (buf2) + align2;
81 s = (CHAR *) (buf1) + align1;
83 for (i = 0; i < len; ++i)
85 acc[i] = random () & BIG_CHAR;
86 if (!acc[i])
87 acc[i] = random () & BIG_CHAR;
88 if (!acc[i])
89 acc[i] = 1 + (random () & SMALL_CHAR);
91 acc[len] = '\0';
93 for (i = 0; i < pos; ++i)
94 s[i] = acc[random () % len];
95 s[pos] = random () & BIG_CHAR;
96 if (STRCHR (acc, s[pos]))
97 s[pos] = '\0';
98 else
100 for (i = pos + 1; i < pos + 10; ++i)
101 s[i] = random () & BIG_CHAR;
102 s[i] = '\0';
105 json_element_object_begin (json_ctx);
106 json_attr_uint (json_ctx, "len", len);
107 json_attr_uint (json_ctx, "pos", pos);
108 json_attr_uint (json_ctx, "align1", align1);
109 json_attr_uint (json_ctx, "align2", align2);
110 json_array_begin (json_ctx, "timings");
112 FOR_EACH_IMPL (impl, 0)
113 do_one_test (json_ctx, impl, s, acc, pos);
115 json_array_end (json_ctx);
116 json_element_object_end (json_ctx);
120 test_main (void)
122 json_ctx_t json_ctx;
123 size_t i;
125 test_init ();
127 json_init (&json_ctx, 0, stdout);
129 json_document_begin (&json_ctx);
130 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
132 json_attr_object_begin (&json_ctx, "functions");
133 json_attr_object_begin (&json_ctx, TEST_NAME);
134 json_attr_string (&json_ctx, "bench-variant", "");
136 json_array_begin (&json_ctx, "ifuncs");
137 FOR_EACH_IMPL (impl, 0)
138 json_element_string (&json_ctx, impl->name);
139 json_array_end (&json_ctx);
141 json_array_begin (&json_ctx, "results");
143 for (i = 0; i < 32; ++i)
145 do_test (&json_ctx, 0, 0, 512, i);
146 do_test (&json_ctx, i, 0, 512, i);
147 do_test (&json_ctx, 0, i, 512, i);
148 do_test (&json_ctx, i, i, 512, i);
151 for (i = 1; i < 8; ++i)
153 do_test (&json_ctx, 0, 0, 16 << i, 4);
154 do_test (&json_ctx, i, 0, 16 << i, 4);
155 do_test (&json_ctx, 0, i, 16 << i, 4);
156 do_test (&json_ctx, i, i, 16 << i, 4);
159 for (i = 1; i < 8; ++i)
161 do_test (&json_ctx, i, 0, 64, 10);
162 do_test (&json_ctx, i, i, 64, 10);
165 for (i = 0; i < 64; ++i)
167 do_test (&json_ctx, 0, 0, i, 6);
168 do_test (&json_ctx, 0, i, i, 6);
171 json_array_end (&json_ctx);
172 json_attr_object_end (&json_ctx);
173 json_attr_object_end (&json_ctx);
174 json_document_end (&json_ctx);
176 return ret;
179 #include <support/test-driver.c>