# Makefile for cksieve. (C) Mark Rodenkirch, April 2016.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.

MAJOR_VER=1
MINOR_VER=1
PATCH_VER=4

# Set DEBUG=yes to compile with debugging information and internal checks.
#
DEBUG=no

# Set ASSEMBLER=yes to use assembler code where available.
#
ASSEMBLER=yes

# Uncomment one of the following ARCH= lines:
#
ARCH=sse            # Uses double precision with SSE2 instructions, max p=2^51
#ARCH=x86-64         # Uses extended double precision (80-bit floats), max p=2^62

# If ARCH not set above then these defauts will be used:
#
ifeq ($(strip $(ARCH)),)
DEBUG_CFLAGS=-g
OPT_CFLAGS=-O2
CPPFLAGS=-Wall
ASSEMBLER=no
endif

# Uses cvtsi2sdq/mulsd/cvtsd2siq for mulmod/powmod. (Max p=2^51)
ifeq ($(strip $(ARCH)),sse)
DEBUG_CFLAGS=-g -march=native -m64
OPT_CFLAGS=-O2 -fomit-frame-pointer -ffast-math -march=native -m64
CPPFLAGS=-Wall -DUSE_INLINE_MULMOD
endif

# Uses fildll/fmul/fistpll for mulmod/powmod.  (Max p=2^62)
ifeq ($(strip $(ARCH)),x86-64)
DEBUG_CFLAGS=-g -march=native -m64
OPT_CFLAGS=-g -O2 -fomit-frame-pointer -ffast-math -march=native -m64
CPPFLAGS=-Wall -DUSE_INLINE_MULMOD -DUSE_FPU_MULMOD
endif

# Add ARCH-specific assembler objects and flags.
#
ifeq ($(strip $(ASSEMBLER)),yes)
CPPFLAGS+= -DUSE_ASM
ASM_OBJS=powmod-x86-64.o powmod-x87-64.o mulmod-x86-64.o mulmod-x87-64.o \
	 misc-x86-64.o
CHECK_OBJS=powmod-x86-64.o powmod-x87-64.o mulmod-x86-64.o mulmod-x87-64.o
endif

# Append any user-supplied CFLAGS.
ifeq ($(strip $(DEBUG)),yes)
override CFLAGS:=$(DEBUG_CFLAGS) $(CFLAGS)
else
CPPFLAGS+= -DNDEBUG
override CFLAGS:=$(OPT_CFLAGS) $(CFLAGS)
endif

CC=gcc
LDLIBS=-lm

# No changes should be needed below here.

.PHONY: all check dist clean realclean

FULL_VER=$(MAJOR_VER).$(MINOR_VER).$(PATCH_VER)

PROGS=cksieve

SIEVE_OBJS=cksieve.o arithmetic.o bitmap.o bsgs.o clock.o \
	events.o factors.o files.o legendre.o primes.o priority.o \
	hashtable.o terms.o util.o $(ASM_OBJS) $(MULTI_OBJS)

SOURCES=Makefile arithmetic.h arithmetic.c bitmap.h bitmap.c bsgs.c choose.c \
	clock.c config.h events.c factors.c files.c \
	legendre.c memset_fast32.h mm_malloc.h primes.c \
	priority.c cksieve.h cksieve.c \
	hashtable.c util.c terms.c misc-x86-64.S \
	asm-x86-64-gcc.h [owmod-sse2.S mulmod-sse2.S \
	powmod-x86-64.S powmod-x87-64.S mulmod-x86-64.S mulmod-x87-64.S \
	ash-x86-64.S giant-x86-64.S giant-x87-64.S \
	README.txt.in

DOCS=CHANGES COPYING INSTALL README

all: $(PROGS)

README.txt: README.txt.in
	$(SED) -e s/VERSION/$(FULL_VER)/g $< | $(FMT) > $@

cksieve: $(SIEVE_OBJS)

version.h:
	echo "#define MAJOR_VER $(MAJOR_VER)" > version.h
	echo "#define MINOR_VER $(MINOR_VER)" >> version.h
	echo "#define PATCH_VER $(PATCH_VER)" >> version.h

ASM_H=asm-x86-64-gcc.h

arithmetic.o: arithmetic.c arithmetic.h $(ASM_H) config.h
bitmap.o: bitmap.c cksieve.h config.h bitmap.h memset_fast32.h $(ASM_H)
bsgs.o: bsgs.c cksieve.h config.h hashtable.h arithmetic.h $(ASM_H) bitmap.h 
choose.o: choose.c cksieve.h config.h
clock.o: clock.c cksieve.h config.h
events.o: events.c cksieve.h config.h
factors.o: factors.c cksieve.h config.h arithmetic.h $(ASM_H)
files.o: files.c cksieve.h config.h
legendre.o: legendre.c cksieve.h bitmap.h config.h
primes.o: primes.c cksieve.h arithmetic.h bitmap.h config.h memset_fast32.h $(ASM_H)
priority.o: priority.c cksieve.h config.h
cksieve.o: cksieve.c cksieve.h config.h version.h
hashtable.o: hashtable.c cksieve.h config.h
util.o: util.c cksieve.h config.h mm_malloc.h
terms.o: terms.c cksieve.h
mulmod-sse2.o: mulmod-sse2.S config.h
powmod-sse2.o: powmod-sse2.S config.h
powmod-x86-64.o: powmod-x86-64.S config.h
powmod-x87-64.o: powmod-x87-64.S config.h
mulmod-x86-64.o: mulmod-x86-64.S config.h
mulmod-x87-64.o: mulmod-x87-64.S config.h
misc-x86-64.o: misc-x86-64.S config.h

clean:
	rm -f *.o cksieve.log ck*.pfgw
