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> [parameter types] [return type]"
36 @args = split(':', $ARGV[1]);
43 my $decl = "extern $ret $func (";
45 # Function has no arguments.
46 if (@args == 0 || $args[0] eq "void") {
47 print "$decl void);\n";
48 print "#define CALL_BENCH_FUNC(i,j) $func();\n";
49 print "#define NUM_VARIANTS (1)\n";
50 print "#define NUM_SAMPLES(v) (1)\n";
51 print "#define VARIANT(v) FUNCNAME \"()\"\n"
53 # The function has arguments, so parse them and populate the inputs.
56 my $bench_func = "#define CALL_BENCH_FUNC(v, i) $func (";
66 my $arg_struct = "struct args {";
68 foreach $arg (@args) {
70 $bench_func = "$bench_func,";
74 $arg_struct = "$arg_struct volatile $arg arg$num;";
75 $bench_func = "$bench_func variants[v].in[i].arg$num";
80 $arg_struct = $arg_struct . "};\n";
81 $decl = $decl . ");\n";
82 $bench_func = $bench_func . ");\n";
84 # We create a hash of inputs for each variant of the test.
89 open INPUTS
, "<$func-inputs" or die $!;
91 LINE
:while (<INPUTS
>) {
95 if (/^## (\w+): (\w+)/) {
96 #We only identify Name for now.
101 # Save values in the last variant.
103 $vals{$variant} = \
@copy;
105 # Prepare for the next.
111 # Skip over comments.
118 $vals{$variant} = \
@curvals;
120 # Print the definitions and macros.
129 # Print the input arrays.
130 foreach $key (keys %vals) {
131 my @arr = @
{$vals{$key}};
133 print "struct args in" . $c . "[" . @arr . "] = {\n";
141 # The variants. Each variant then points to the appropriate input array we
143 print "struct _variants variants[" . (keys %vals) . "] = {\n";
145 foreach $key (keys %vals) {
146 print "{\"$func($key)\", " . @
{$vals{$key}} . ", in$c},\n";
151 # Finally, print the last set of macros.
152 print "#define NUM_VARIANTS $c\n";
153 print "#define NUM_SAMPLES(i) (variants[i].count)\n";
154 print "#define VARIANT(i) (variants[i].name)\n";
157 # In some cases not storing a return value seems to result in the function call
158 # being optimized out.
159 if ($ret ne "void") {
160 print "static volatile $ret ret = 0.0;\n";
165 print "#define BENCH_FUNC(i, j) ({$getret CALL_BENCH_FUNC (i, j);})\n";
167 print "#define FUNCNAME \"$func\"\n";
168 print "#include \"bench-skeleton.c\"\n";