#! /bin/sh
#
# Attempt to time all available assembly variants for a given arch
# First we build gmp in the current working directory, so make sure
# to cd into a good place for that.

echo "******************************************************************************"
echo "Set SRCDIR to point to the gmp source directory (defaults to ./)"
echo "Set CC to your compiler, including any ABI or ISA options (defaults to gcc)"
echo "Set MPNDIR to point to the proper mpn subdirectory (e.g., x86, sparc64, alpha)"
echo "   (defaults to x86)"
echo ""
echo "Ignore errors for some tests; they are unavoidable"
echo "******************************************************************************"


if [ -z "$SRCDIR" ]
  then
    SRCDIR=.
  fi

if [ -z "$CC" ]
  then
    CC=gcc
  fi
if [ -z "$MPNDIR" ]
  then
    MPNDIR=x86
  fi

$SRCDIR/configure --disable-shared >cfg.log 2>&1
make >make.log 2>&1
(cd tune; make speed) >makespeed.log 2>&1

mul_1="mul_1"
addmul_1="addmul_1 aorsmul_1"
submul_1="submul_1 aorsmul_1"
add_n="add_n aors_n"
sub_n="sub_n aors_n"
lshift="lshift lorrshift"
rshift="rshift lorrshift"
divrem_1="divrem_1"

ops="mul_1 addmul_1 submul_1 add_n sub_n lshift rshift divrem_1"

for i in $ops
  do
    eval "v=\$$i"
    findpat="-name nosuchfile"
    for j in $v
      do
	findpat="$findpat -o -name $j.asm"
      done

    files=`find $SRCDIR/mpn/$MPNDIR $findpat`

    for f in $files
      do
	echo "operation=$i file=$f"
	(cd mpn; m4 -DOPERATION_$i $f) >x.s
	$CC $CFLAGS -I$SRCDIR -I. x.s tune/speed.o tune/.libs/libspeed.a .libs/libgmp.a -lm
	./a.out -C -s1-500 -f1.4 mpn_$i.3
      done
  done

rm -f a.out x.s
