#!/bin/sh

M1=`make path`


time()
{
	sed '/^.*of \([0-9.]*\) seconds.*/s//\1/p;d' <data/$1/prof/$2
}


size()
{
	sed '/^0*\([1-9][0-9]*\): VECTOUT.*/s//\1/p;d' <data/$1/out/$2 |
	    tail -n 1
}


eff()
{
	sed '/^Efficiency: \([0-9.]*\)%/s//\1/p;d' <data/$1/out/$2 | tail -n 1
}


regs()
{
	./eval.pl -r data/$1/out/$2 | sed '1d;s|/.*||'
#	sed '/^regs: 0\//s///p;d' <data/$1/out/$2 | tail -n 1
}


sum()
{
	md5sum <data/$1/expr/$2 | sed 's/ .*//'
}


eq()
{
	if [ "$1" = "$2" ]; then
		echo Y
	else
		echo N
	fi
}


trim()
{
	sed 's/[^0-9].*//'
}


cfield()
{
	echo "<TD bgcolor=\"$1\">$2"
}


rank()
{
	op=$1
	txt=`$2 $3 $6`
	a=`echo "$txt" | trim`
	b=`$2 $4 $6 | trim`
	c=`$2 $5 $6 | trim`

	if [ $a $op $b -a $a $op $c ]; then
		col=$green
	elif [ $b $op $a -a $c $op $a ]; then
		col=$red
	else
		col=white
	fi
	echo "<TD bgcolor=\"$col\" align="right">$txt$7"
}


red="#ffb0b0"
green="#a0ffa0"

html=false
if [ $1 = -h ]; then
	html=true
	shift
fi

if $html; then
	cat <<EOF
<HTML>
<TITLE>Scheduler comparison</TITLE>
<BODY>
<TABLE bgcolor="#f0f0f0">
  <TR>
    <TH colspan="5" align="left">Original</TH>
    <TH colspan="5" align="left">New (no optimizer)</TH>
    <TH colspan="5" align="left">New (LCPF)</TH>
    <TH align="left">Equiv</TH>
    <TH align="left">Name</TH>
  <TR>
     <TH>Time</TH><TH>Size</TH><TH>Eff</TH><TH>Regs</TH>
     <TH>&nbsp;</TH>
     <TH>Time</TH><TH>Size</TH><TH>Eff</TH><TH>Regs</TH>
     <TH>&nbsp;</TH>
     <TH>Time</TH><TH>Size</TH><TH>Eff</TH><TH>Regs</TH>
     <TH>&nbsp;</TH>
EOF
	for n in `ls -1 data/ref/out`; do
		ref=`sum ref $n`
		new=`sum new $n`
		opt=`sum opt $n`
		same=`eq $ref $new`/`eq $ref $opt`/`eq $new $opt`

		echo "<TR>"
		rank -lt time ref new opt $n
		rank -lt size ref new opt $n
		rank -gt eff ref new opt $n %
		rank -lt regs ref new opt $n

		echo "<TD>"
		rank -lt time new ref opt $n
		rank -lt size new ref opt $n
		rank -gt eff new ref opt $n %
		rank -lt regs new ref opt $n

		echo "<TD>"
		rank -lt time opt ref new $n
		rank -lt size opt ref new $n
		rank -gt eff opt ref new $n %
		rank -lt regs opt ref new $n

		echo "<TD>"
		if [ $same = Y/Y/Y ]; then
			cfield $green $same
		else
			cfield $red $same
		fi
		cfield white "$n"
	done
	cat <<EOF
</TABLE>
</BODY>
</HTML>
EOF
	exit
fi


echo "Original            New sched (no opt)  New sched (LCPF)    Equiv Name"
echo "Time  Size Eff Regs Time  Size Eff Regs Time  Size Eff Regs"

tref=0
tnew=0
topt=0

for n in `ls -1 data/ref/out`; do
	ref=`sum ref $n`
	new=`sum new $n`
	opt=`sum opt $n`
	printf "%5.1f %4d%3d%% %4d %5.1f %4d%3d%% %4d %5.1f %4d%3d%% %4d %s " \
	  `time ref $n` `size ref $n` `eff ref $n` `regs ref $n` \
	  `time new $n` `size new $n` `eff new $n` `regs new $n` \
	  `time opt $n` `size opt $n` `eff opt $n` `regs opt $n` \
	  `eq $ref $new`/`eq $ref $opt`/`eq $new $opt`
	echo $n
	tref="$tref `time ref $n` +"
	tnew="$tnew `time new $n` +"
	topt="$topt `time opt $n` +"
done

printf "Original time: %9.1f s\n" `dc -e "$tref p"`
printf "New (unopt) time: %6.1f s\n" `dc -e "$tnew p"`
printf "New (opt) time: %8.1f s\n" `dc -e "$topt p"`
