#!/bin/sh

CC=${CC:-cc}

# make sure we are in the right directory
cd "`dirname $0`"

# create test directory
rm -rf _test
mkdir -p _test

# Show status info
echo "Floating point precision test suite (version 0.1)"
echo "Author: Christian Seiler <webmaster@christian-seiler.de>"
echo "=============================================================="
echo
echo "Using compiler: $CC"
echo "$CC --version:"
$CC --version
echo "--------------------------------------------------------------"

function compile () {
    FILE=$1
    shift
    MSG=$1
    shift
    OUT=$1
    shift
    
    echo "Compiling $FILE with $MSG"
    $CC "$@" -S -o _test/${FILE%%.c}-$OUT.s $FILE > _test/${FILE%%.c}-$OUT.compile 2>&1 || (
        echo "  Could not compile"
        return
    )
    echo "!!! actual compile:" >> _test/${FILE%%.c}-$OUT.compile
    $CC "$@" -o _test/${FILE%%.c}-$OUT $FILE >> _test/${FILE%%.c}-$OUT.compile 2>&1 && (
        echo "  Running test..."
        _test/${FILE%%.c}-$OUT > _test/${FILE%%.c}-$OUT.out 2> _test/${FILE%%.c}-$OUT.err
        echo "  Removing executable..."
        rm _test/${FILE%%.c}-$OUT
    ) || (
        echo "  Could not compile & link."
    )
}

compile vanilla.c "no optimization, no float store" "O0" -O0
compile vanilla.c "no optimization, float store" "O0f" -O0 -ffloat-store
compile vanilla.c "optimization level 1, no float store" "O1" -O1
compile vanilla.c "optimization level 1, float store" "O1f" -O1 -ffloat-store
compile vanilla.c "optimization level 2, no float store" "O2" -O2
compile vanilla.c "optimization level 2, float store" "O2f" -O2 -ffloat-store
compile vanilla.c "optimization level 3, no float store" "O3" -O3
compile vanilla.c "optimization level 3, float store" "O3f" -O3 -ffloat-store
compile vanilla.c "optimization level size, no float store" "Os" -Os
compile vanilla.c "optimization level size, float store" "Osf" -Os -ffloat-store

# TODO: add additional checks to test suite

cd _test
tar -czf ../results.tar.gz *
cd ..
echo "=============================================================="
echo "Results saved in results.tar.gz in the same directory."
