2013-03-15 07:00:03 +00:00
|
|
|
#! /usr/bin/perl -w
|
|
|
|
# Copyright (C) 2013 Free Software Foundation, Inc.
|
|
|
|
# This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
# The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
# The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with the GNU C Library; if not, see
|
|
|
|
# <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
# Generate a benchmark source file for a given input.
|
|
|
|
|
2013-10-07 06:21:24 +00:00
|
|
|
if (@ARGV < 1) {
|
|
|
|
die "Usage: bench.pl <function>"
|
2013-03-15 07:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $func = $ARGV[0];
|
|
|
|
my @args;
|
|
|
|
my $ret = "void";
|
|
|
|
my $getret = "";
|
|
|
|
|
2013-10-07 06:21:24 +00:00
|
|
|
# We create a hash of inputs for each variant of the test.
|
|
|
|
my $variant = "";
|
|
|
|
my @curvals;
|
|
|
|
my %vals;
|
2013-10-08 11:04:10 +00:00
|
|
|
my @include_headers;
|
|
|
|
my @include_sources;
|
2013-10-07 06:21:24 +00:00
|
|
|
my $incl;
|
|
|
|
|
|
|
|
open INPUTS, "<$func-inputs" or die $!;
|
|
|
|
|
|
|
|
LINE:while (<INPUTS>) {
|
|
|
|
chomp;
|
|
|
|
|
|
|
|
# Directives.
|
2013-10-08 11:04:10 +00:00
|
|
|
if (/^## ([\w-]+): (.*)/) {
|
2013-10-07 06:21:24 +00:00
|
|
|
# Function argument types.
|
|
|
|
if ($1 eq "args") {
|
|
|
|
@args = split(":", $2);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function return type.
|
|
|
|
elsif ($1 eq "ret") {
|
|
|
|
$ret = $2;
|
|
|
|
}
|
2013-03-15 07:00:03 +00:00
|
|
|
|
2013-10-07 06:21:24 +00:00
|
|
|
elsif ($1 eq "includes") {
|
2013-10-08 11:04:10 +00:00
|
|
|
@include_headers = split (",", $2);
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif ($1 eq "include-sources") {
|
|
|
|
@include_sources = split (",", $2);
|
2013-10-07 06:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# New variant. This is the only directive allowed in the body of the
|
|
|
|
# inputs to separate inputs into variants. All others should be at the
|
|
|
|
# top or else all hell will break loose.
|
|
|
|
elsif ($1 eq "name") {
|
|
|
|
|
|
|
|
# Save values in the previous variant.
|
|
|
|
my @copy = @curvals;
|
|
|
|
$vals{$variant} = \@copy;
|
|
|
|
|
|
|
|
# Prepare for the next.
|
|
|
|
$variant=$2;
|
|
|
|
undef @curvals;
|
|
|
|
next LINE;
|
|
|
|
}
|
2013-10-08 11:04:10 +00:00
|
|
|
|
|
|
|
else {
|
|
|
|
die "Unknown directive: ".$1;
|
|
|
|
}
|
2013-10-07 06:21:24 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 12:50:32 +00:00
|
|
|
# Skip over comments and blank lines.
|
|
|
|
if (/^#/ || /^$/) {
|
2013-10-07 06:21:24 +00:00
|
|
|
next LINE;
|
|
|
|
}
|
|
|
|
push (@curvals, $_);
|
2013-03-15 07:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-07 06:21:24 +00:00
|
|
|
my $bench_func = "#define CALL_BENCH_FUNC(v, i) $func (";
|
|
|
|
|
|
|
|
|
|
|
|
# Print the definitions and macros.
|
2013-10-08 11:04:10 +00:00
|
|
|
foreach $incl (@include_headers) {
|
2013-10-07 06:21:24 +00:00
|
|
|
print "#include <" . $incl . ">\n";
|
2013-03-15 07:00:03 +00:00
|
|
|
}
|
2013-04-30 08:47:57 +00:00
|
|
|
|
2013-10-08 11:04:10 +00:00
|
|
|
# Print the source files.
|
|
|
|
foreach $incl (@include_sources) {
|
|
|
|
print "#include \"" . $incl . "\"\n";
|
|
|
|
}
|
|
|
|
|
2013-10-07 06:21:24 +00:00
|
|
|
if (@args > 0) {
|
|
|
|
# Save values in the last variant.
|
|
|
|
$vals{$variant} = \@curvals;
|
2013-04-30 08:47:57 +00:00
|
|
|
my $struct =
|
|
|
|
"struct _variants
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
int count;
|
|
|
|
struct args *in;
|
|
|
|
};\n";
|
|
|
|
|
|
|
|
my $arg_struct = "struct args {";
|
2013-03-15 07:00:03 +00:00
|
|
|
|
2013-10-07 06:21:24 +00:00
|
|
|
my $num = 0;
|
|
|
|
my $arg;
|
2013-03-15 07:00:03 +00:00
|
|
|
foreach $arg (@args) {
|
|
|
|
if ($num > 0) {
|
|
|
|
$bench_func = "$bench_func,";
|
|
|
|
}
|
|
|
|
|
2013-05-17 13:40:33 +00:00
|
|
|
$arg_struct = "$arg_struct volatile $arg arg$num;";
|
2013-04-30 08:47:57 +00:00
|
|
|
$bench_func = "$bench_func variants[v].in[i].arg$num";
|
2013-03-15 07:00:03 +00:00
|
|
|
$num = $num + 1;
|
|
|
|
}
|
|
|
|
|
2013-04-30 08:47:57 +00:00
|
|
|
$arg_struct = $arg_struct . "};\n";
|
|
|
|
$bench_func = $bench_func . ");\n";
|
|
|
|
|
|
|
|
print $bench_func;
|
|
|
|
print $arg_struct;
|
|
|
|
print $struct;
|
|
|
|
|
|
|
|
my $c = 0;
|
|
|
|
my $key;
|
|
|
|
|
|
|
|
# Print the input arrays.
|
|
|
|
foreach $key (keys %vals) {
|
|
|
|
my @arr = @{$vals{$key}};
|
|
|
|
|
|
|
|
print "struct args in" . $c . "[" . @arr . "] = {\n";
|
|
|
|
foreach (@arr) {
|
|
|
|
print "{$_},\n";
|
|
|
|
}
|
|
|
|
print "};\n\n";
|
|
|
|
$c += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# The variants. Each variant then points to the appropriate input array we
|
|
|
|
# defined above.
|
|
|
|
print "struct _variants variants[" . (keys %vals) . "] = {\n";
|
|
|
|
$c = 0;
|
|
|
|
foreach $key (keys %vals) {
|
|
|
|
print "{\"$func($key)\", " . @{$vals{$key}} . ", in$c},\n";
|
|
|
|
$c += 1;
|
|
|
|
}
|
|
|
|
print "};\n\n";
|
|
|
|
# Finally, print the last set of macros.
|
|
|
|
print "#define NUM_VARIANTS $c\n";
|
|
|
|
print "#define NUM_SAMPLES(i) (variants[i].count)\n";
|
|
|
|
print "#define VARIANT(i) (variants[i].name)\n";
|
2013-03-15 07:00:03 +00:00
|
|
|
}
|
2013-10-07 06:21:24 +00:00
|
|
|
else {
|
|
|
|
print $bench_func . ");\n";
|
|
|
|
print "#define NUM_VARIANTS (1)\n";
|
|
|
|
print "#define NUM_SAMPLES(v) (1)\n";
|
|
|
|
print "#define VARIANT(v) FUNCNAME \"()\"\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-15 07:00:03 +00:00
|
|
|
|
|
|
|
# In some cases not storing a return value seems to result in the function call
|
|
|
|
# being optimized out.
|
|
|
|
if ($ret ne "void") {
|
2013-10-07 06:21:24 +00:00
|
|
|
print "static volatile $ret ret;\n";
|
2013-03-15 07:00:03 +00:00
|
|
|
$getret = "ret = ";
|
|
|
|
}
|
|
|
|
|
2013-04-30 08:47:57 +00:00
|
|
|
# And we're done.
|
|
|
|
print "#define BENCH_FUNC(i, j) ({$getret CALL_BENCH_FUNC (i, j);})\n";
|
2013-03-15 07:00:03 +00:00
|
|
|
|
|
|
|
print "#define FUNCNAME \"$func\"\n";
|
|
|
|
print "#include \"bench-skeleton.c\"\n";
|