CC=gcc
FLAGS=-Wall -Wextra -std=c99 -pedantic -O0 -lm
SPEED_TYPES=float double LD
SUM_TYPES=float double
OPS=ADD DIV MULT SQRT

all: fp speed highlight report.pdf sum kahan pr

highlight: floating_point.tex extra_precision.tex \
	speed.tex sum.tex kahan_sum.tex Makefile.tex

s%.tex: s%.c
	pygmentize -O style=colorful -o $@ $^

kahan_sum.tex: kahan_sum.c
	pygmentize -O style=colorful -o $@ $^

Makefile.tex: Makefile
	pygmentize -O style=colorful -o $@ $^

extra_precision.tex: extra_precision.c
	pygmentize -O style=colorful -o $@ $^

floating_point.tex: floating_point.c
	pygmentize -O style=colorful -o $@ $^

%.pdf: %.tex
	pdflatex $^
	pdflatex $^

speed: speed.c
	for t in $(SPEED_TYPES); do \
	    for o in $(OPS); do \
	        sed "s#{TYPE}#$$t#" $^ | sed "s#{OP}#$$o#" > speed.$$t.$$o.c; \
	        $(CC) $(FLAGS) -o speed.$$t.$$o speed.$$t.$$o.c; \
	        rm speed.$$t.$$o.c; \
	    done; \
	done;
	touch $@

pr: extra_precision.o
	$(CC) $(FLAGS) -mfpmath=387 -O2 -o $@ $^

fp: floating_point.o
	$(CC) $(FLAGS) -o $@ $^

sum: sum.c
	for t in $(SUM_TYPES); do \
		sed "s#{TYPE}#$$t#" $^ > sum.$$t.c; \
		$(CC) $(FLAGS) -o sum.$$t sum.$$t.c; \
		rm sum.$$t.c; \
	done;
	touch $@

kahan: kahan_sum.o
	$(CC) $(FLAGS) -o $@ $^

%.o: %.c
	$(CC) $(FLAGS) -o $@ -c $^

%.s: %.c
	$(CC) $(FLAGS) -o $* $^

clean:
	rm -vf *.o *.i *.s fp pr fd* speed speed.*.* floating_point \
		report.pdf *.aux *.log *.toc sum sum.float sum.double kahan
