# Copyright 2000-2002 Free Software Foundation, Inc. # # This file is part of the GNU MP Library. # # The GNU MP Library is free software; you can redistribute it and/or modify # it under the terms of either: # # * the GNU Lesser General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your # option) any later version. # # or # # * the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any # later version. # # or both in parallel, as here. # # The GNU MP 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 General Public License # for more details. # # You should have received copies of the GNU General Public License and the # GNU Lesser General Public License along with the GNU MP Library. If not, # see https://www.gnu.org/licenses/.
# Usage: cd $builddir/tune # perl $srcdir/tune/many.pl [-t] <files/dirs>... # # Output: speed-many.c # try-many.c # Makefile.many # # Make alternate versions of various mpn routines available for measuring # and testing. # # The $srcdir and $builddir in the invocation above just means the script # lives in the tune source directory, but should be run in the tune build # directory. When not using a separate object directory this just becomes # # cd tune # perl many.pl [-t] <files/dirs>... # # # SINGLE FILES # # Suppose $HOME/newcode/mul_1_experiment.asm is a new implementation of # mpn_mul_1, then # # cd $builddir/tune # perl $srcdir/tune/many.pl $HOME/newcode/mul_1_experiment.asm # # will produce rules and renaming so that a speed program incorporating it # can be built, # # make -f Makefile.many speed-many # # then for example it can be compared to the standard mul_1, # # ./speed-many -s 1-30 mpn_mul_1 mpn_mul_1_experiment # # An expanded try program can be used to check correctness, # # make -f Makefile.many try-many # # and run # # ./try-many mpn_mul_1_experiment # # Files can be ".c", ".S" or ".asm". ".s" files can't be used because they # don't get any preprocessing so there's no way to do renaming of their # functions. # # # WHOLE DIRECTORIES # # If a directory is given, then all files in it will be made available. # For example, # # cd $builddir/tune # perl $srcdir/tune/many.pl $HOME/newcode # # Each file should have a suffix, like "_experiment" above. # # # MPN DIRECTORIES # # mpn directories from the GMP source tree can be included, and this is a # convenient way to compare multiple implementations suiting different chips # in a CPU family. For example the following would make all x86 routines # available, # # cd $builddir/tune # perl $srcdir/tune/many.pl `find $srcdir/mpn/x86 -type d` # # On a new x86 chip a comparison could then be made to see how existing code # runs. For example, # # make -f Makefile.many speed-many # ./speed-many -s 1-30 -c \ # mpn_add_n_x86 mpn_add_n_pentium mpn_add_n_k6 mpn_add_n_k7 # # Files in "mpn" subdirectories don't need the "_experiment" style suffix # described above, instead a suffix is constructed from the subdirectory. # For example "mpn/x86/k7/mmx/mod_1.asm" will generate a function # mpn_mod_1_k7_mmx. The rule is to take the last directory name after the # "mpn", or the last two if there's three or more. (Check the generated # speed-many.c if in doubt.) # # # GENERIC C # # The mpn/generic directory can be included too, just like any processor # specific directory. This is a good way to compare assembler and generic C # implementations. For example, # # cd $builddir/tune # perl $srcdir/tune/many.pl $srcdir/mpn/generic # # or if just a few routines are of interest, then for example # # cd $builddir/tune # perl $srcdir/tune/many.pl \ # $srcdir/mpn/generic/lshift.c \ # $srcdir/mpn/generic/mod_1.c \ # $srcdir/mpn/generic/aorsmul_1.c # # giving mpn_lshift_generic etc. # # # TESTS/DEVEL PROGRAMS # # Makefile.many also has rules to build the tests/devel programs with suitable # renaming, and with some parameters for correctness or speed. This is less # convenient than the speed and try programs, but provides an independent # check. For example, # # make -f Makefile.many tests_mul_1_experimental # ./tests_mul_1_experimental # # and for speed # # make -f Makefile.many tests_mul_1_experimental_sp # ./tests_mul_1_experimental_sp # # Not all the programs support speed measuring, in which case only the # correctness test will be useful. # # The parameters for repetitions and host clock speed are -D defines. Some # defaults are provided at the end of Makefile.many, but probably these will # want to be overridden. For example, # # rm tests_mul_1_experimental.o # make -f Makefile.many \ # CFLAGS_TESTS="-DSIZE=50 -DTIMES=1000 -DRANDOM -DCLOCK=175000000" \ # tests_mul_1_experimental # ./tests_mul_1_experimental # # # OTHER NOTES # # The mappings of file names to functions, and the macros to then use for # speed measuring etc are driven by @table below. The scheme isn't # completely general, it's only got as many variations as have been needed # so far. # # Some functions are only made available in speed-many, or others only in # try-many. An @table entry speed=>none means no speed measuring is # available, or try=>none no try program testing. These can be removed # if/when the respective programs get the necessary support. # # If a file has "1c" or "nc" carry-in entrypoints, they're renamed and made # available too. These are recognised from PROLOGUE or MULFUNC_PROLOGUE in # .S and .asm files, or from a line starting with "mpn_foo_1c" in a .c file # (possibly via a #define), and on that basis are entirely optional. This # entrypoint matching is done for the standard entrypoints too, but it would # be very unusual to have for instance a mul_1c without a mul_1. # # Some mpz files are recognized. For example an experimental copy of # mpz/powm.c could be included as powm_new.c and would be called # mpz_powm_new. So far only speed measuring is available for these. # # For the ".S" and ".asm" files, both PIC and non-PIC objects are built. # The PIC functions have a "_pic" suffix, for example "mpn_mod_1_k7_mmx_pic". # This can be ignored for routines that don't differ for PIC, or for CPUs # where everything is PIC anyway. # # K&R compilers are supported via the same ansi2knr mechanism used by # automake, though it's hard to believe anyone will have much interest in # measuring a compiler so old that it doesn't even have an ANSI mode. # # The "-t" option can be used to print a trace of the files found and what's # done with them. A great deal of obscure output is produced, but it can # indicate where or why some files aren't being recognised etc. For # example, # # cd $builddir/tune # perl $srcdir/tune/many.pl -t $HOME/newcode/add_n_weird.asm # # In general, when including new code, all that's really necessary is that # it will compile or assemble under the current configuration. It's fine if # some code doesn't actually run due to bugs, or to needing a newer CPU or # whatever, simply don't ask for the offending routines when invoking # speed-many or try-many, or don't try to run them on sizes they don't yet # support, or whatever. # # # CPU SPECIFICS # # x86 - All the x86 code will assemble on any system, but code for newer # chips might not run on older chips. Expect SIGILLs from new # instructions on old chips. # # A few "new" instructions, like cmov for instance, are done as macros # and will generate some equivalent plain i386 code when HAVE_HOST_CPU # in config.m4 indicates an old CPU. It won't run fast, but it does # make it possible to test correctness. # # # INTERNALS # # The nonsense involving $ENV is some hooks used during development to add # additional functions temporarily. # # # FUTURE # # Maybe the C files should be compiled pic and non-pic too. Wait until # there's a difference that might be of interest. # # Warn if a file provides no functions. # # Allow mpz and mpn files of the same name. Currently the mpn fib2_ui # matching hides the mpz version of that. Will need to check the file # contents to see which it is. Would be worth allowing an "mpz_" or "mpn_" # prefix on the filenames to have working versions of both in one directory. # # # LIMITATIONS # # Some of the command lines can become very long when a lot of files are # included. If this is a problem on a given system the only suggestion is # to run many.pl for just those that are actually wanted at a particular # time. # # DOS 8.3 or SysV 14 char filesystems won't work, since the long filenames # generated will almost certainly fail to be unique.
use strict;
use File::Basename;
use Getopt::Std;
my %opt;
getopts('t', \%opt);
my @DIRECTORIES = @ARGV; if (defined $ENV{directories}) { push @DIRECTORIES, @{$ENV{directories}} }
# regexp - matched against the start of the filename. If a grouping "(...)" # is present then only the first such part is used. # # mulfunc - filenames to be generated from a multi-function file. # # funs - functions provided by the file, defaulting to the filename with mpn # (or mpX). # # mpX - prefix like "mpz", defaulting to "mpn". # # ret - return value type. # # args, args_<fun> - arguments for the given function. If an args_<fun> is # set then it's used, otherwise plain args is used. "mp_limb_t # carry" is appended for carry-in variants. # # try - try.c TYPE_ to use, defaulting to TYPE_fun with the function name # in upper case. "C" is appended for carry-in variants. Can be # 'none' for no try program entry. # # speed - SPEED_ROUTINE_ to use, handled like "try". # # speed_flags - SPEED_ROUTINE_ to use, handled like "try".
my $builddir = $ENV{builddir};
$builddir = "."if (! defined $builddir);
my $top_builddir = "${builddir}/..";
open(MAKEFILE, "<${builddir}/Makefile")
or die "Cannot open ${builddir}/Makefile: $!\n"
. "Is this a tune build directory?";
my ($srcdir, $top_srcdir); while (<MAKEFILE>) { if (/^srcdir = (.*)/) { $srcdir = $1; } if (/^top_srcdir = (.*)/) { $top_srcdir = $1; }
}
die "Cannot find \$srcdir in Makefile\n"if (! defined $srcdir);
die "Cannot find \$top_srcdir in Makefile\n"if (! defined $top_srcdir);
print "srcdir $srcdir\n"if $opt{'t'};
print "top_srcdir $top_srcdir\n"if $opt{'t'};
close(MAKEFILE);
open(SPEED, ">speed-many.c") or die;
print SPEED "/* speed-many.c generated by many.pl - DO NOT EDIT, CHANGES WILL BE LOST */
";
my $SPEED_EXTRA_ROUTINES = "#define SPEED_EXTRA_ROUTINES \\\n";
my $SPEED_EXTRA_PROTOS = "#define SPEED_EXTRA_PROTOS \\\n";
my $SPEED_CODE = "";
open(TRY, ">try-many.c") or die;
print TRY "/* try-many.c generated by many.pl - DO NOT EDIT, CHANGES WILL BE LOST */\n" . "\n";
my $TRY_EXTRA_ROUTINES = "#define EXTRA_ROUTINES \\\n";
my $TRY_EXTRA_PROTOS = "#define EXTRA_PROTOS \\\n";
open(FD,"<${top_builddir}/libtool") or die "Cannot open \"${top_builddir}/libtool\": $!\n";
my $pic_flag; while (<FD>) { if (/^pic_flag="?([^"]*)"?$/) {
$pic_flag=$1;
last;
}
}
close FD; if (! defined $pic_flag) {
die "Cannot find pic_flag in ${top_builddir}/libtool";
}
open(MAKEFILE, ">Makefile.many") or die;
print MAKEFILE "# Makefile.many generated by many.pl - DO NOT EDIT, CHANGES WILL BE LOST\n" . "\n" . "all: speed-many try-many\n" . "\n" . "#--------- begin included copy of basic Makefile ----------\n" . "\n";
open(FD,"<${builddir}/Makefile") or die "Cannot open \"${builddir}/Makefile\": $!\n";
print MAKEFILE <FD>;
close FD;
print MAKEFILE "\n" . "#--------- end included copy of basic Makefile ----------\n" . "\n" . "CFLAGS_PIC = $CFLAGS_PIC\n" . "ASMFLAGS_PIC = $ASMFLAGS_PIC\n" . "\n";
my $CLEAN="";
my $MANY_OBJS="";
sub print_ansi2knr {
my ($base,$file,$includes) = @_; if (! defined $file) { $file = "$base.c"; } if (! defined $includes) { $includes = ""; }
my ($t, $file_match); foreach my $p (@table) { # print " ",$p->{'regexp'},"\n" if $opt{'t'}; if ($FILE =~ "^($p->{'regexp'})") {
$t = $p;
$file_match = $1;
$file_match = $2 if defined $2;
last;
}
}
next if ! defined $t;
print "match $t->{'regexp'} $FILE ($file_full)\n"if $opt{'t'};
if (! open FD,"<$file_full") { print "Can't open $file_full: $!\n"; next }
my @file_contents = <FD>;
close FD;
my $objs; if (defined $t->{'mulfunc'}) { $objs = $t->{'mulfunc'}; } else { $objs = [$file_match]; }
print "objs @$objs\n"if $opt{'t'};
my $ret = $t->{'ret'}; if (! defined $ret && $lang eq '.h') { $ret = ''; } if (! defined $ret) { die "$FILE return type not defined\n" };
print "ret $ret\n"if $opt{'t'};
my $mpX = $t->{'mpX'}; if (! defined $mpX) { $mpX = ($lang eq '.h' ? '' : 'mpn'); }
$mpX = "${mpX}_"if $mpX ne '';
print "mpX $mpX\n"if $opt{'t'};
# some restriction functions are implemented, but they're not very useful
my $restriction='';
my $suffix; if ($FILE =~ ("${file_match}_(.+)")) {
$suffix = $1;
} elsif ($path =~ /\/mp[zn]\/(.*)$/) { # derive the suffix from the path
$suffix = $1;
$suffix =~ s/\//_/g; # use last directory name, or if there's 3 or more then the last two if ($suffix =~ /([^_]*_)+([^_]+_[^_]+)$/) {
$suffix = $2;
} elsif ($suffix =~ /([^_]*_)*([^_]+)$/) {
$suffix = $2;
}
} else {
die "Can't determine suffix for: $file_full (path $path)\n";
}
print "suffix $suffix\n"if $opt{'t'};
$count_files++;
foreach my $obj (@{$objs}) {
print "obj $obj\n"if $opt{'t'};
my $obj_with_suffix = "${obj}_$suffix"; if (defined $seen_obj{$obj_with_suffix}) {
print "Skipping duplicate object: $obj_with_suffix\n";
print " first from: $seen_obj{$obj_with_suffix}\n";
print " now from: $file_full\n";
next;
}
$seen_obj{$obj_with_suffix} = $file_full;
my $funs = $t->{'funs'};
$funs = [$obj] if ! defined $funs;
print "funs @$funs\n"if $opt{'t'};
if (defined $t->{'pic'}) { @pic_choices = ('no'); }
my $objbase = "${obj}_$suffix$pic->{'suffix'}";
print "objbase $objbase\n"if $opt{'t'};
if ($path !~ "." && -f "${objbase}.c") {
die "Already have ${objbase}.c";
}
my $tmp_file = "tmp-$objbase.c";
my $renaming; foreach my $fun (@{$funs}) { if ($mpX eq 'mpn_' && $lang eq '.c') {
$renaming .= "\t\t-DHAVE_NATIVE_mpn_$fun=1 \\\n";
}
# The carry-in variant is with a "c" appended, unless there's a "_1" # somewhere, eg. "modexact_1_odd", in which case that becomes "_1c".
my $fun_carry = $fun; if (! ($fun_carry =~ s/_1/_1c/)) { $fun_carry = "${fun}c"; }
$CLEAN .= " tmp-$objbase.c";
open(TMP_C,">tmp-$objbase.c")
or die "Can't create tmp-$objbase.c: $!\n";
print TMP_C "/* tmp-$objbase.c generated by many.pl - DO NOT EDIT, CHANGES WILL BE LOST */
foreach my $fun (@{$funs}) {
print "fun $fun\n"if $opt{'t'};
if ($lang eq '.h') {
my $macro_before = $t->{'macro_before'};
$macro_before = ""if ! defined $macro_before;
print TMP_C "$macro_before #undef $fun #include \"$file_full\"
";
}
my $args = $t->{"args_$fun"}; if (! defined $args) { $args = $t->{'args'}; } if (! defined $args) { die "Need args for $fun\n"; }
print "args $args\n"if $opt{'t'};
foreach my $carry (@$carrys) {
print "carry $carry\n"if $opt{'t'};
my $fun_carry = $fun; if (! ($fun_carry =~ s/_1/_1$carry/)) { $fun_carry = "$fun$carry"; }
print "fun_carry $fun_carry\n"if $opt{'t'};
if ($lang =~ /\.(asm|S)/
&& ! grep(m"PROLOGUE\((.* )?$mpX$fun_carry[ ,)]",@file_contents)) {
print "no PROLOGUE $mpX$fun_carry\n"if $opt{'t'};
next;
} if ($lang eq '.c'
&& ! grep(m"^(#define FUNCTION\s+)?$mpX$fun_carry\W", @file_contents)) {
print "no mention of $mpX$fun_carry\n"if $opt{'t'};
next;
} if ($lang eq '.h'
&& ! grep(m"^#define $fun_carry\W", @file_contents)) {
print "no mention of #define $fun_carry\n" if $opt{'t'};
next;
}
if ($try_type ne 'none') {
$TRY_EXTRA_ROUTINES .= " { TRY($mpX${fun_carry}_$suffix$pic->{'suffix'}), $try_type$try_minsize }, \\\n";
}
my $speed_flags = $t->{'speed_flags'};
$speed_flags = '0'if ! defined $speed_flags;
print "speed_flags $speed_flags\n"if $opt{'t'};
my $speed_routine = $t->{'speed'};
$speed_routine = "SPEED_ROUTINE_\U$mpX\U$fun" if !defined $speed_routine; if (! ($speed_routine =~ s/_1/_1\U$carry/)) {
$speed_routine = "$speed_routine\U$carry";
}
print "speed_routine $speed_routine\n"if $opt{'t'};
my @speed_suffixes = ();
push (@speed_suffixes, '') if $speed_routine ne 'none';
push (@speed_suffixes, @{$t->{'speed_suffixes'}}) if defined $t->{'speed_suffixes'};
my $macro_speed = $t->{'macro-speed'};
$macro_speed = "$speed_routine ($fun_carry)"if ! defined $macro_speed;
$macro_speed =~ s/\$fun/$fun_carry/g;
foreach my $S (@speed_suffixes) {
my $Sfunfull="$mpX${fun_carry}${S}_$suffix$pic->{'suffix'}";
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.