Fix cbrtl for ldbl-96
[glibc.git] / scripts / bench.pl
blobdcf13552820eba64381f3ee23bb5167e749dbe83
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 < 2) {
25 die "Usage: bench.pl <function> [parameter types] [return type]"
28 my $arg;
29 my $func = $ARGV[0];
30 my @args;
31 my $ret = "void";
32 my $getret = "";
33 my $retval = "";
35 if (@ARGV >= 2) {
36 @args = split(':', $ARGV[1]);
39 if (@ARGV == 3) {
40 $ret = $ARGV[2];
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.
54 else {
55 my $num = 0;
56 my $bench_func = "#define CALL_BENCH_FUNC(v, i) $func (";
58 my $struct =
59 "struct _variants
61 const char *name;
62 int count;
63 struct args *in;
64 };\n";
66 my $arg_struct = "struct args {";
68 foreach $arg (@args) {
69 if ($num > 0) {
70 $bench_func = "$bench_func,";
71 $decl = "$decl,";
74 $arg_struct = "$arg_struct volatile $arg arg$num;";
75 $bench_func = "$bench_func variants[v].in[i].arg$num";
76 $decl = "$decl $arg";
77 $num = $num + 1;
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.
85 my $variant = "";
86 my @curvals;
87 my %vals;
89 open INPUTS, "<$func-inputs" or die $!;
91 LINE:while (<INPUTS>) {
92 chomp;
94 # New variant.
95 if (/^## (\w+): (\w+)/) {
96 #We only identify Name for now.
97 if ($1 ne "name") {
98 next LINE;
101 # Save values in the last variant.
102 my @copy = @curvals;
103 $vals{$variant} = \@copy;
105 # Prepare for the next.
106 $variant=$2;
107 undef @curvals;
108 next LINE;
111 # Skip over comments.
112 if (/^#/) {
113 next LINE;
115 push (@curvals, $_);
118 $vals{$variant} = \@curvals;
120 # Print the definitions and macros.
121 print $decl;
122 print $bench_func;
123 print $arg_struct;
124 print $struct;
126 my $c = 0;
127 my $key;
129 # Print the input arrays.
130 foreach $key (keys %vals) {
131 my @arr = @{$vals{$key}};
133 print "struct args in" . $c . "[" . @arr . "] = {\n";
134 foreach (@arr) {
135 print "{$_},\n";
137 print "};\n\n";
138 $c += 1;
141 # The variants. Each variant then points to the appropriate input array we
142 # defined above.
143 print "struct _variants variants[" . (keys %vals) . "] = {\n";
144 $c = 0;
145 foreach $key (keys %vals) {
146 print "{\"$func($key)\", " . @{$vals{$key}} . ", in$c},\n";
147 $c += 1;
149 print "};\n\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";
161 $getret = "ret = ";
164 # And we're done.
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";