2 # Copyright (C) 2013 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 # <http://www.gnu.org/licenses/>.
22 # Generate a benchmark source file for a given input.
25 die "Usage: bench.pl <function>"
33 # We create a hash of inputs for each variant of the test.
41 open INPUTS
, "<$func-inputs" or die $!;
43 LINE
:while (<INPUTS
>) {
47 if (/^## ([\w-]+): (.*)/) {
48 # Function argument types.
50 @args = split(":", $2);
53 # Function return type.
58 elsif ($1 eq "includes") {
59 @include_headers = split (",", $2);
62 elsif ($1 eq "include-sources") {
63 @include_sources = split (",", $2);
66 # New variant. This is the only directive allowed in the body of the
67 # inputs to separate inputs into variants. All others should be at the
68 # top or else all hell will break loose.
69 elsif ($1 eq "name") {
71 # Save values in the previous variant.
73 $vals{$variant} = \
@copy;
75 # Prepare for the next.
82 die "Unknown directive: ".$1;
94 my $bench_func = "#define CALL_BENCH_FUNC(v, i) $func (";
97 # Print the definitions and macros.
98 foreach $incl (@include_headers) {
99 print "#include <" . $incl . ">\n";
102 # Print the source files.
103 foreach $incl (@include_sources) {
104 print "#include \"" . $incl . "\"\n";
108 # Save values in the last variant.
109 $vals{$variant} = \
@curvals;
118 my $arg_struct = "struct args {";
122 foreach $arg (@args) {
124 $bench_func = "$bench_func,";
127 $arg_struct = "$arg_struct volatile $arg arg$num;";
128 $bench_func = "$bench_func variants[v].in[i].arg$num";
132 $arg_struct = $arg_struct . "};\n";
133 $bench_func = $bench_func . ");\n";
142 # Print the input arrays.
143 foreach $key (keys %vals) {
144 my @arr = @
{$vals{$key}};
146 print "struct args in" . $c . "[" . @arr . "] = {\n";
154 # The variants. Each variant then points to the appropriate input array we
156 print "struct _variants variants[" . (keys %vals) . "] = {\n";
158 foreach $key (keys %vals) {
159 print "{\"$func($key)\", " . @
{$vals{$key}} . ", in$c},\n";
163 # Finally, print the last set of macros.
164 print "#define NUM_VARIANTS $c\n";
165 print "#define NUM_SAMPLES(i) (variants[i].count)\n";
166 print "#define VARIANT(i) (variants[i].name)\n";
169 print $bench_func . ");\n";
170 print "#define NUM_VARIANTS (1)\n";
171 print "#define NUM_SAMPLES(v) (1)\n";
172 print "#define VARIANT(v) FUNCNAME \"()\"\n"
177 # In some cases not storing a return value seems to result in the function call
178 # being optimized out.
179 if ($ret ne "void") {
180 print "static volatile $ret ret;\n";
185 print "#define BENCH_FUNC(i, j) ({$getret CALL_BENCH_FUNC (i, j);})\n";
187 print "#define FUNCNAME \"$func\"\n";
188 print "#include \"bench-skeleton.c\"\n";