1 /* Actual benchmark kernels used by bench-hash-funcs.h
2    Copyright (C) 2022 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 
20 #include "bench-util.h"
21 
22 /* We go through the trouble of using macros here because many of the
23    hash functions are meant to be inlined so its not fair to benchmark
24    them with a function pointer where they won't be inlinable. */
25 #undef RUN_FUNC
26 #undef POSTFIX
27 #ifdef SIMPLE
28 # define RUN_FUNC SIMPLE_TEST_FUNC
29 # define POSTFIX _simple
30 #else
31 # define RUN_FUNC TEST_FUNC
32 # define POSTFIX _optimized
33 #endif
34 
35 #define PRIMITIVE_CAT(x, y) x ## y
36 #define CAT(x, y) PRIMITIVE_CAT (x, y)
37 
38 static double __attribute__ ((noinline, noclone))
CAT(do_one_test_kernel,POSTFIX)39 CAT (do_one_test_kernel, POSTFIX) (const char *s, size_t len)
40 {
41 
42   unsigned int iters;
43   timing_t start, stop, cur;
44 
45   /* Warmup.  */
46   for (iters = NFIXED_ITERS / 32; iters; --iters)
47     DO_NOT_OPTIMIZE_OUT (RUN_FUNC (s, len));
48 
49   TIMING_NOW (start);
50   for (iters = NFIXED_ITERS; iters; --iters)
51     DO_NOT_OPTIMIZE_OUT (RUN_FUNC (s, len));
52 
53   TIMING_NOW (stop);
54 
55   TIMING_DIFF (cur, start, stop);
56 
57   (void) (len);
58   return (double) cur / (double) NFIXED_ITERS;
59 }
60 
61 static double __attribute__ ((noinline, noclone))
CAT(do_rand_test_kernel,POSTFIX)62 CAT (do_rand_test_kernel, POSTFIX) (char const *bufs,
63 				    unsigned int const *sizes)
64 {
65   unsigned int i, iters;
66   size_t offset;
67   timing_t start, stop, cur;
68 
69   /* Warmup.  */
70   for (i = 0, offset = 0; i < NRAND_BUFS; ++i, offset += RAND_BENCH_MAX_LEN)
71     DO_NOT_OPTIMIZE_OUT (RUN_FUNC (bufs + offset, sizes[i]));
72 
73   TIMING_NOW (start);
74   for (iters = NRAND_ITERS; iters; --iters)
75     {
76       for (i = 0, offset = 0; i < NRAND_BUFS;
77 	   ++i, offset += RAND_BENCH_MAX_LEN)
78 	DO_NOT_OPTIMIZE_OUT (RUN_FUNC (bufs + offset, sizes[i]));
79 
80     }
81   TIMING_NOW (stop);
82 
83   TIMING_DIFF (cur, start, stop);
84 
85   (void) (sizes);
86   return (double) cur / (double) (NRAND_ITERS * NRAND_BUFS);
87 }
88