diff --git a/logic.c b/logic.c index 452f267..92754a8 100644 --- a/logic.c +++ b/logic.c @@ -17,11 +17,13 @@ * along with this program. If not, see . */ #include +#include +#include #include "logic.h" #include "macros.h" static column_t heightMask( const rowsint_t a ){ - return (column_t)( (column_t)1 << (column_t)a ) - (column_t)1; + return ( 1 << (column_t)a ) - (column_t)1; } static column_t safeHeightMask( const rowsint_t a ){ @@ -33,18 +35,16 @@ static column_t bottomHeightMask( const rowsint_t a, const rowsint_t b ){ } static void updateTotal( wincount_t *count ){ - count->total = (winint_t)( + count->total = count->horizontal + count->vertical + count->diagonalUp - + count->diagonalDown); + + count->diagonalDown; } // popcount for a column -static winint_t popcnt_column( const column_t column ){ - // winint_t i = (winint_t)__builtin_popcountg( (uint64_t)column ); - winint_t i = 0; - for( column_t c = column; c != 0; i++ ) c &= c - 1; +static winint_t popcnt_column( column_t column ){ + winint_t i = (winint_t)__builtin_popcountg( (uint64_t)column ); return i; } @@ -120,7 +120,7 @@ void calcWins_slow( board_t *boardptr ){ & safeHeightMask( boardptr->height[ column + 1 ] ) & safeHeightMask( boardptr->height[ column + 2 ] ) & safeHeightMask( boardptr->height[ column + 3 ] ) - & safeHeightMask( boardptr->rows ) + & ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) ); boardptr->count1.horizontal += popcnt_column( boardptr->column[ column ] @@ -138,7 +138,7 @@ void calcWins_slow( board_t *boardptr ){ & safeHeightMask( boardptr->height[ column + 1 ] - 1 ) & safeHeightMask( boardptr->height[ column + 2 ] - 2 ) & safeHeightMask( boardptr->height[ column + 3 ] - 3 ) - & safeHeightMask( boardptr->rows ) + & ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) ); boardptr->count1.diagonalUp += popcnt_column( boardptr->column[ column ] @@ -151,7 +151,7 @@ void calcWins_slow( board_t *boardptr ){ | boardptr->column[ column + 1 ] << 1 | boardptr->column[ column + 2 ] << 2 | boardptr->column[ column + 3 ] << 3 - | (column_t)( 0x07 ) // A line diagonal down don't start at one of the bottom 3 + | 0x07 // A line diagonal down don't start at one of the bottom 3 ) & bottomHeightMask( bottom( boardptr->height[ column ], @@ -161,7 +161,7 @@ void calcWins_slow( board_t *boardptr ){ boardptr->height[ column + 3 ] + 3 ) ) - & safeHeightMask( boardptr->rows ) + & ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) ); boardptr->count1.diagonalDown += popcnt_column( boardptr->column[ column ] diff --git a/makefile b/makefile index 0baf14a..604f492 100644 --- a/makefile +++ b/makefile @@ -24,8 +24,9 @@ FLAGS += -Wlogical-op FLAGS += -Wdisabled-optimization FLAGS += -fanalyzer FLAGS += -flto +FLAGS += -fuse-ld=gold FLAGS += -s -FLAGS += -Wl,--gc-sections +flags += -Wl,--gc-sections # These flags are because of the compiler otherwise giving too much warning #FLAGS += -Wno-sign-conversion #FLAGS += -Wformat-truncation=0 @@ -36,12 +37,11 @@ endif # Compile flag for defining GITHASH to put at the end of the version string -FLAGS += -D'GITHASH=$(shell git rev-parse --short=1 HEAD)' +GITFLAG = -D'GITHASH=$(shell git rev-parse --short=1 HEAD)' # Directories where .o files and excecutables will be placed OBJDIR = obj OUTDIR = out -TESTDIR = test # The UI to compile for by default using make run #UI_TARGET = vt100 @@ -53,9 +53,6 @@ LICENSEURL = https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt # C compiler CC = cc -LD = ld -CC_W64 = x86_64-w64-mingw32-gcc -LD_W64 = x86_64-w64-mingw32-ld # Test options TEST_COLUMNS = 1000 @@ -70,10 +67,9 @@ MSG_COMPILING = Compiling C: MSG_CLEANING = Cleaning: MSG_CLEANING_OBJ = Cleaning $(OBJDIR): MSG_CLEANING_OUT = Cleaning $(OUTDIR): -MSG_CLEANING_TEST = Cleaning $(TESTDIR): # Compile for all UI targets -all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf stub.elf stub.exe) +all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf stub.elf) # Compile and run the default UI target run: $(OUTDIR)/connect4_$(UI_TARGET).elf @@ -84,22 +80,19 @@ run_%: $(OUTDIR)/connect4_%.elf ./$< # Show result of test -test: $(TESTDIR)/test.diff +test: test.diff # Diff between the tests -$(TESTDIR)/test.diff: $(TESTDIR)/test.log $(TESTDIR)/test.log.slow - @mkdir -p $(@D) +test.diff: test.log test.log.slow diff -y $^ > $@ || tail $@ diff -q $^ # Log of testing fast logic -$(TESTDIR)/test.log: $(OUTDIR)/connect4_$(UI_TESTING).elf - @mkdir -p $(@D) +test.log: $(OUTDIR)/connect4_$(UI_TESTING).elf ./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves # Log of testing slow logic -$(TESTDIR)/test.log.slow: $(OUTDIR)/connect4_$(UI_TESTING).elf - @mkdir -p $(@D) +test.log.slow: $(OUTDIR)/connect4_$(UI_TESTING).elf ./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves --slow-calcWins # Compile specifically the ncurses version @@ -115,36 +108,25 @@ $(OUTDIR)/connect4_%.elf: $(addprefix $(OBJDIR)/,ui_%.o logic.o connect4.o LICEN @mkdir -p $(@D) $(CC) $(FLAGS) -o $@ $^ -# Compile a windows excecutable -$(OUTDIR)/connect4_%.exe: $(addprefix $(OBJDIR)/w64_,ui_%.o logic.o connect4.o LICENSE.o) - @echo $(MSG_LINKING) $@ - @mkdir -p $(@D) - $(CC_W64) $(FLAGS) -o $@ $^ - # Make an object file for the license so it does not all need to be in the code $(OBJDIR)/LICENSE.o: $(OBJDIR)/LICENSE @echo $(MSG_LINKING) $@ @mkdir -p $(@D) - cd $(@D); $(LD) -r -b binary -o $(@F) $(^F) + cd $(@D); ld -r -b binary -o $(@F) $(^F) -# Make an object file for the license so it does not all need to be in the code -$(OBJDIR)/w64_LICENSE.o: $(OBJDIR)/LICENSE - @echo $(MSG_LINKING) $@ +# Compile the object file of the main code. +# This one's special because it includes the git hash +$(OBJDIR)/connect4.o: connect4.c + @echo $(MSG_COMPILING) $^ @mkdir -p $(@D) - cd $(@D); $(LD_W64) -r -b binary -o $(@F) $(^F) + $(CC) -c $(FLAGS) $(GITFLAG) -o $@ $^ -# Linux object file compile +# Generic object file compile $(OBJDIR)/%.o: %.c @echo $(MSG_COMPILING) $^ @mkdir -p $(@D) $(CC) -c $(FLAGS) -o $@ $^ -# Windows object file compile -$(OBJDIR)/w64_%.o: %.c - @echo $(MSG_COMPILING) $^ - @mkdir -p $(@D) - $(CC_W64) -c $(FLAGS) -o $@ $^ - # Download the license file in non-markdown form. # This is not cleaned as that would be too silly $(OBJDIR)/LICENSE: @@ -159,6 +141,3 @@ clean: rm -f $(OBJDIR)/*.o @echo $(MSG_CLEANING_OUT) rm -f $(OUTDIR)/*.elf - rm -f $(OUTDIR)/*.exe - @echo $(MSG_CLEANING_TEST) - rm -f $(TESTDIR)/* diff --git a/ui_stub.c b/ui_stub.c index d5f7ca7..e8d397f 100644 --- a/ui_stub.c +++ b/ui_stub.c @@ -1,5 +1,6 @@ #include "ui.h" #include "logic.h" +#include void initBoard( __attribute__((unused)) const board_t board