# Mandelbrot in MaPLe, two versions of the per-pixel inner loop:
#   bin/mandelbrot-ispc   vectorized with ISPC (mandelbrot.ispc)
#   bin/mandelbrot-sml    pure SML (PackByteSml.sml)
#
#   make                       # build both
#   make bin/mandelbrot-sml    # build just the SML version (no ispc needed)
#
#   bin/mandelbrot-ispc @mpl procs 8 -- -repeat 10 -warmup 5
#   bin/mandelbrot-sml  @mpl procs 8 -- -repeat 10 -warmup 5
#   bin/mandelbrot-sml  @mpl procs 8 -- -resolution 1000 -output out.pbm

MPL ?= mpl
SMLPKG ?= smlpkg
ISPC ?= ispc
MPL_FLAGS ?= -default-type int64 -default-type word64
ISPC_FLAGS ?= -O3 --pic

MPLLIB = lib/github.com/mpllang/mpllib/sources.mpl.mlb
COMMON = $(MPLLIB) Params.sml main.sml

.PHONY: all clean realclean

all: bin/mandelbrot-ispc bin/mandelbrot-sml

bin/mandelbrot-ispc: $(COMMON) ispc.mlb PackByteIspc.sml bin/mandelbrot_ispc.o
	@mkdir -p bin
	$(MPL) $(MPL_FLAGS) -output $@ ispc.mlb bin/mandelbrot_ispc.o

bin/mandelbrot-sml: $(COMMON) sml.mlb PackByteSml.sml
	@mkdir -p bin
	$(MPL) $(MPL_FLAGS) -output $@ sml.mlb

bin/mandelbrot_ispc.o: mandelbrot.ispc
	@mkdir -p bin
	$(ISPC) $(ISPC_FLAGS) -o $@ $<

$(MPLLIB): sml.pkg
	$(SMLPKG) sync
	@touch $@

clean:
	rm -rf bin

realclean: clean
	rm -rf lib
