DEBUGFLAG=
OPT=-O3
ARCH=$(shell uname -m)
ARCHCCOPT=$(ARCH)-$(CC)$(OPT)
SOURCES=Makefile main.c HEADER.html
BINARIES=stwlf-x86_64-gcc-14-O stwlf-x86_64-gcc-14-O3
CYCLES=cycles:u
MHZ=0.00001
TIME=perf stat --log-fd 3 -x, -e $(CYCLES) 3>&1
#or call with "make CYCLES=apple_firestorm_pmu/cycles/"
#or call with "make TIME="/bin/time -f %U 2>&1" MHZ=3228"

#all: stwlf-$(ARCHCCOPT)

bench-selected: stwlf-x86_64-gcc-14-O stwlf-x86_64-gcc-14-O3
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 0 12 6 9 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "nl>ns wl>ws (maximally independent)"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 2 2 0 12 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "nl>ns=_=>wl>ws"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 3 2 0 12 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "nl>ns=^=>wl>ws"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 2 2 4 0 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "nl>n2=_=>wl>ws=>nl (recurrence)"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 2 0 4 3 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "wl>ws=>wl (recurrence), nl>ns (no recurrence)"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 6 4 0 2 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "nl>ns=>nl (recurrence), wl>ws (no recurrence)"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 6 6 0 2 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "nl>ns=>nl,wl>ws"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 2 2 4 0 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "ws=_=>nl>ns=_=>wl"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 1 2 4 0 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "wl>ws=_=>nl>ns"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 2 1 4 3 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "wl>ws_=^>wl (recurrence) nl>ns (no recurrence)"
	@for i in 14-O 14-O3; do $(TIME) ./stwlf-x86_64-gcc-$$i 2 1 4 0 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'; done
	@echo "ws=_=>nl>ns=^=>wl>ws (recurrence), also ws_=^>wl"

bench: stwlf-$(ARCHCCOPT)
	@$(TIME) ./stwlf-$(ARCHCCOPT) 0 12 6 9 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "nl>ns wl>ws (maximally independent)"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 2 2 0 12 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "nl>ns=_=>wl>ws"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 3 2 0 12 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "nl>ns=^=>wl>ws"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 2 2 4 0 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "nl>n2=_=>wl>ws=>nl (recurrence)"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 2 0 4 3 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "wl>ws=>wl (recurrence), nl>ns (no recurrence)"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 6 4 0 2 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "nl>ns=>nl (recurrence), wl>ws (no recurrence)"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 6 6 0 2 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "nl>ns=>nl,wl>ws"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 2 2 4 0 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "ws=_=>nl>ns=_=>wl"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 1 2 4 0 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "wl>ws=_=>nl>ns"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 2 1 4 3 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "wl>ws_=^>wl (recurrence) nl>ns (no recurrence)"
	@$(TIME) ./stwlf-$(ARCHCCOPT) 2 1 4 0 | awk -F, '{printf("%5.2f ",$$1*'$(MHZ)'/1000)}'
	@echo "ws=_=>nl>ns=^=>wl>ws (recurrence), also ws_=^>wl"

build: stwlf-$(ARCHCCOPT)

stwlf-$(ARCHCCOPT): main.c
	$(CC) -Wall $(OPT) $(DEBUGFLAG) -static -o $@ main.c

main-$(ARCHCCOPT).o: main.c
	$(CC) -Wall -$(OPT) $(DEBUGFLAG) -c $< -o $@

dist:
	rm -rf stwlf
	mkdir stwlf
	cp -p $(SOURCES) $(BINARIES) stwlf
	tar cvfJ stwlf.tar.xz stwlf
	#zip -9r stwlf.zip stwlf
	-rm -rf stwlf

clean:
	rm stwlf-*

