#!/bin/bash


CPUS="ARM7TDMI-60MHz MIPS-R4400-250MHz MIPS-R5000-200MHz K6II-300MHz Atom-N270-1600MHz Pentium-M-1800MHz \
      Athlon-XP-1530MHz PowerPC-G4-1333MHz Phenom-IIX4-980-3700MHz Core-i7-4710hq-3400MHz"

if [ ."$1" = ."--to" ] ; then
  shift
  to=$1
  shift
fi

if [ ."$1" != . ] ; then
  CPUS=$*
fi


do_plot()
{
  fname=$1 ; shift
  ylabel=$1 ; shift
  what_to_plot=$1 ; shift
  clocks=$1 ; shift
  ( cat <<-
      set grid
      set title "Sieve of Eratosthenes Benchmark"
      min(a,b)=a<b?a:b
      set logscale x
      set mxtics 10
      set mytics 10
      set xlabel "sieve length in bytes (i.e. the max tested number / 8)"
      set key left Left
      set terminal postscript eps color solid "Helvetica-Bold" 
      set output "$fname"
      set ylabel "$ylabel"
      p [:$to] [0:]\\

    ct=0;
    for i in $* ; do 
      freq=$( echo $i | tr 'M-' '\n\n' | tail -2 | head -1 )
      echo -n \"$i\" using '($1):('$what_to_plot
      if $clocks ; then
        echo -n "*1e-9*($freq*1e6)"
      fi
      echo  ')' title \"$i\" with lines, \\
      if [ $ct -eq 4 ] ; then   # to skip yellow color
        echo "log(0) notitle, \\"
      fi
      ct=$(( ct + 1 ))
    done 
    echo "log(0) notitle"
    ) | gnuplot
  gv $fname &
  ps2pdf14 -sPAPERSIZE=a4 $fname &
}

do_plot sieve_iter_times.eps "average inner loop step runtime [ns]" '1e3*min($2,$3)/$4' false $CPUS
# do_plot average_runtime.eps "runtime per sieve length [ns]" '1e3*min($2,$3)/($1*8)' false $CPUS
do_plot sieve_iter_cycles.eps "avg # of CPU cycles of a single inner loop step" '1e3*min($2,$3)/$4' true $CPUS
