scripts/glibcelf.py: Add PT_AARCH64_MEMTAG_MTE constant
[glibc.git] / benchtests / bench-hash-funcs.c
blob578c5cbae249263686b5941300d63a67cf539ce6
1 /* Measure hash functions runtime.
2 Copyright (C) 2022 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 #define DO_NOT_OPTIMIZE_OUT(x) __asm__ volatile("" : : "r,m"(x) : "memory")
43 enum
45 NFIXED_ITERS = 1048576,
46 NRAND_BUFS = 16384,
47 NRAND_ITERS = 2048,
48 RAND_BENCH_MAX_LEN = 128
51 #include "bench-hash-funcs-kernel.h"
52 #define SIMPLE
53 #include "bench-hash-funcs-kernel.h"
55 static void
56 do_one_test (json_ctx_t *json_ctx, size_t len)
58 char buf[len + 1];
59 memset (buf, -1, len);
60 buf[len] = '\0';
62 json_element_object_begin (json_ctx);
64 json_attr_string (json_ctx, "type", "fixed");
65 json_attr_uint (json_ctx, "length", len);
66 json_attr_double (json_ctx, "time_simple", do_one_test_kernel_simple (buf, len));
67 json_attr_double (json_ctx, "time_optimized", do_one_test_kernel_optimized (buf, len));
69 json_element_object_end (json_ctx);
72 static void __attribute__ ((noinline, noclone))
73 do_rand_test (json_ctx_t *json_ctx)
75 size_t i, sz, offset;
76 char *bufs;
77 unsigned int *sizes;
79 bufs = (char *) calloc (NRAND_BUFS, RAND_BENCH_MAX_LEN);
80 sizes = (unsigned int *) calloc (NRAND_BUFS, sizeof (unsigned int));
81 if (bufs == NULL || sizes == NULL)
83 fprintf (stderr, "Failed to allocate bufs for random test\n");
84 goto done;
87 for (sz = 2; sz <= RAND_BENCH_MAX_LEN; sz += sz)
89 json_element_object_begin (json_ctx);
90 json_attr_string (json_ctx, "type", "random");
91 json_attr_uint (json_ctx, "length", sz);
93 for (i = 0, offset = 0; i < NRAND_BUFS;
94 ++i, offset += RAND_BENCH_MAX_LEN)
96 sizes[i] = random () % sz;
97 memset (bufs + offset, -1, sizes[i]);
98 bufs[offset + sizes[i]] = '\0';
101 json_attr_double (json_ctx, "time_simple",
102 do_rand_test_kernel_simple (bufs, sizes));
103 json_attr_double (json_ctx, "time_optimized",
104 do_rand_test_kernel_optimized (bufs, sizes));
105 json_element_object_end (json_ctx);
108 done:
109 if (bufs)
110 free (bufs);
112 if (sizes)
113 free (sizes);
116 static int
117 do_test (void)
119 int i;
120 json_ctx_t json_ctx;
122 json_init (&json_ctx, 0, stdout);
123 json_document_begin (&json_ctx);
124 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
125 json_attr_object_begin (&json_ctx, "functions");
126 json_attr_object_begin (&json_ctx, TEST_NAME);
127 json_array_begin (&json_ctx, "results");
129 for (i = 0; i < 16; ++i)
130 do_one_test (&json_ctx, i);
132 for (i = 16; i <= 256; i += i)
133 do_one_test (&json_ctx, i);
135 do_rand_test (&json_ctx);
137 json_array_end (&json_ctx);
138 json_attr_object_end (&json_ctx);
139 json_attr_object_end (&json_ctx);
140 json_document_end (&json_ctx);
142 return 0;
145 #include <support/test-driver.c>