Use __glibc_block in public headers.
[glibc.git] / scripts / bench.pl
blob492ab816ed867b018f73e1d63b27b7e7f4ad748d
1 #! /usr/bin/perl -w
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/>.
20 use strict;
21 use warnings;
22 # Generate a benchmark source file for a given input.
24 if (@ARGV < 1) {
25 die "Usage: bench.pl <function>"
28 my $func = $ARGV[0];
29 my @args;
30 my $ret = "void";
31 my $getret = "";
33 # We create a hash of inputs for each variant of the test.
34 my $variant = "";
35 my @curvals;
36 my %vals;
37 my @include_headers;
38 my @include_sources;
39 my $incl;
41 open INPUTS, "<$func-inputs" or die $!;
43 LINE:while (<INPUTS>) {
44 chomp;
46 # Directives.
47 if (/^## ([\w-]+): (.*)/) {
48 # Function argument types.
49 if ($1 eq "args") {
50 @args = split(":", $2);
53 # Function return type.
54 elsif ($1 eq "ret") {
55 $ret = $2;
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.
72 my @copy = @curvals;
73 $vals{$variant} = \@copy;
75 # Prepare for the next.
76 $variant=$2;
77 undef @curvals;
78 next LINE;
81 else {
82 die "Unknown directive: ".$1;
86 # Skip over comments.
87 if (/^#/) {
88 next LINE;
90 push (@curvals, $_);
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";
107 if (@args > 0) {
108 # Save values in the last variant.
109 $vals{$variant} = \@curvals;
110 my $struct =
111 "struct _variants
113 const char *name;
114 int count;
115 struct args *in;
116 };\n";
118 my $arg_struct = "struct args {";
120 my $num = 0;
121 my $arg;
122 foreach $arg (@args) {
123 if ($num > 0) {
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";
129 $num = $num + 1;
132 $arg_struct = $arg_struct . "};\n";
133 $bench_func = $bench_func . ");\n";
135 print $bench_func;
136 print $arg_struct;
137 print $struct;
139 my $c = 0;
140 my $key;
142 # Print the input arrays.
143 foreach $key (keys %vals) {
144 my @arr = @{$vals{$key}};
146 print "struct args in" . $c . "[" . @arr . "] = {\n";
147 foreach (@arr) {
148 print "{$_},\n";
150 print "};\n\n";
151 $c += 1;
154 # The variants. Each variant then points to the appropriate input array we
155 # defined above.
156 print "struct _variants variants[" . (keys %vals) . "] = {\n";
157 $c = 0;
158 foreach $key (keys %vals) {
159 print "{\"$func($key)\", " . @{$vals{$key}} . ", in$c},\n";
160 $c += 1;
162 print "};\n\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";
168 else {
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";
181 $getret = "ret = ";
184 # And we're done.
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";