unistd: Improve fortify with clang
[glibc.git] / benchtests / bench-hash-funcs.c
blob8dd88c9e390cac8ac81f67071a29d33ba1414604
1 /* Measure hash functions runtime.
2 Copyright (C) 2022-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 TEST_FUNC
21 # error "No TEST_FUNC provided!"
22 #endif
23 #ifndef SIMPLE_TEST_FUNC
24 # error "No SIMPLE_TEST_FUNC provided!"
25 #endif
27 #ifndef TEST_NAME
28 # define STRINGIFY_PRIMITIVE(x) # x
29 # define STRINGIFY(x) STRINGIFY_PRIMITIVE (x)
31 # define TEST_NAME STRINGIFY (TEST_FUNC)
32 #endif
34 #include "json-lib.h"
35 #include "bench-timing.h"
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 enum
43 NFIXED_ITERS = 1048576,
44 NRAND_BUFS = 16384,
45 NRAND_ITERS = 256,
46 RAND_BENCH_MAX_LEN = 128
49 #include "bench-hash-funcs-kernel.h"
50 #define SIMPLE
51 #include "bench-hash-funcs-kernel.h"
53 static void
54 do_one_test (json_ctx_t *json_ctx, size_t len)
56 char buf[len + 1];
57 memset (buf, -1, len);
58 buf[len] = '\0';
60 json_element_object_begin (json_ctx);
62 json_attr_string (json_ctx, "type", "fixed");
63 json_attr_uint (json_ctx, "length", len);
64 json_attr_double (json_ctx, "time_simple", do_one_test_kernel_simple (buf, len));
65 json_attr_double (json_ctx, "time_optimized", do_one_test_kernel_optimized (buf, len));
67 json_element_object_end (json_ctx);
70 static void __attribute__ ((noinline, noclone))
71 do_rand_test (json_ctx_t *json_ctx)
73 size_t i, sz, offset;
74 char *bufs;
75 unsigned int *sizes;
77 bufs = (char *) calloc (NRAND_BUFS, RAND_BENCH_MAX_LEN);
78 sizes = (unsigned int *) calloc (NRAND_BUFS, sizeof (unsigned int));
79 if (bufs == NULL || sizes == NULL)
81 fprintf (stderr, "Failed to allocate bufs for random test\n");
82 goto done;
85 for (sz = 2; sz <= RAND_BENCH_MAX_LEN; sz += sz)
87 json_element_object_begin (json_ctx);
88 json_attr_string (json_ctx, "type", "random");
89 json_attr_uint (json_ctx, "length", sz);
91 for (i = 0, offset = 0; i < NRAND_BUFS;
92 ++i, offset += RAND_BENCH_MAX_LEN)
94 sizes[i] = random () % sz;
95 memset (bufs + offset, -1, sizes[i]);
96 bufs[offset + sizes[i]] = '\0';
99 json_attr_double (json_ctx, "time_simple",
100 do_rand_test_kernel_simple (bufs, sizes));
101 json_attr_double (json_ctx, "time_optimized",
102 do_rand_test_kernel_optimized (bufs, sizes));
103 json_element_object_end (json_ctx);
106 done:
107 if (bufs)
108 free (bufs);
110 if (sizes)
111 free (sizes);
114 static int
115 do_test (void)
117 int i;
118 json_ctx_t json_ctx;
120 json_init (&json_ctx, 0, stdout);
121 json_document_begin (&json_ctx);
122 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
123 json_attr_object_begin (&json_ctx, "functions");
124 json_attr_object_begin (&json_ctx, TEST_NAME);
125 json_array_begin (&json_ctx, "results");
127 for (i = 0; i < 16; ++i)
128 do_one_test (&json_ctx, i);
130 for (i = 16; i <= 256; i += i)
131 do_one_test (&json_ctx, i);
133 do_rand_test (&json_ctx);
135 json_array_end (&json_ctx);
136 json_attr_object_end (&json_ctx);
137 json_attr_object_end (&json_ctx);
138 json_document_end (&json_ctx);
140 return 0;
143 #include <support/test-driver.c>