Compare commits

...

2 commits

Author SHA1 Message Date
4b4ded94b6 Added cross-compiling to windows 2025-08-16 12:33:59 +02:00
bc67a1f7f9 Removed unused include 2025-08-16 12:31:51 +02:00
3 changed files with 47 additions and 27 deletions

22
logic.c
View file

@ -17,13 +17,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <stdbit.h>
#include <stdio.h>
#include "logic.h" #include "logic.h"
#include "macros.h" #include "macros.h"
static column_t heightMask( const rowsint_t a ){ static column_t heightMask( const rowsint_t a ){
return ( 1 << (column_t)a ) - (column_t)1; return (column_t)( (column_t)1 << (column_t)a ) - (column_t)1;
} }
static column_t safeHeightMask( const rowsint_t a ){ static column_t safeHeightMask( const rowsint_t a ){
@ -35,16 +33,18 @@ static column_t bottomHeightMask( const rowsint_t a, const rowsint_t b ){
} }
static void updateTotal( wincount_t *count ){ static void updateTotal( wincount_t *count ){
count->total = count->total = (winint_t)(
count->horizontal count->horizontal
+ count->vertical + count->vertical
+ count->diagonalUp + count->diagonalUp
+ count->diagonalDown; + count->diagonalDown);
} }
// popcount for a column // popcount for a column
static winint_t popcnt_column( column_t column ){ static winint_t popcnt_column( const column_t column ){
winint_t i = (winint_t)__builtin_popcountg( (uint64_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;
return i; return i;
} }
@ -120,7 +120,7 @@ void calcWins_slow( board_t *boardptr ){
& safeHeightMask( boardptr->height[ column + 1 ] ) & safeHeightMask( boardptr->height[ column + 1 ] )
& safeHeightMask( boardptr->height[ column + 2 ] ) & safeHeightMask( boardptr->height[ column + 2 ] )
& safeHeightMask( boardptr->height[ column + 3 ] ) & safeHeightMask( boardptr->height[ column + 3 ] )
& ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) & safeHeightMask( boardptr->rows )
); );
boardptr->count1.horizontal += popcnt_column( boardptr->count1.horizontal += popcnt_column(
boardptr->column[ column ] boardptr->column[ column ]
@ -138,7 +138,7 @@ void calcWins_slow( board_t *boardptr ){
& safeHeightMask( boardptr->height[ column + 1 ] - 1 ) & safeHeightMask( boardptr->height[ column + 1 ] - 1 )
& safeHeightMask( boardptr->height[ column + 2 ] - 2 ) & safeHeightMask( boardptr->height[ column + 2 ] - 2 )
& safeHeightMask( boardptr->height[ column + 3 ] - 3 ) & safeHeightMask( boardptr->height[ column + 3 ] - 3 )
& ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) & safeHeightMask( boardptr->rows )
); );
boardptr->count1.diagonalUp += popcnt_column( boardptr->count1.diagonalUp += popcnt_column(
boardptr->column[ column ] boardptr->column[ column ]
@ -151,7 +151,7 @@ void calcWins_slow( board_t *boardptr ){
| boardptr->column[ column + 1 ] << 1 | boardptr->column[ column + 1 ] << 1
| boardptr->column[ column + 2 ] << 2 | boardptr->column[ column + 2 ] << 2
| boardptr->column[ column + 3 ] << 3 | boardptr->column[ column + 3 ] << 3
| 0x07 // A line diagonal down don't start at one of the bottom 3 | (column_t)( 0x07 ) // A line diagonal down don't start at one of the bottom 3
) )
& bottomHeightMask( & bottomHeightMask(
bottom( boardptr->height[ column ], bottom( boardptr->height[ column ],
@ -161,7 +161,7 @@ void calcWins_slow( board_t *boardptr ){
boardptr->height[ column + 3 ] + 3 boardptr->height[ column + 3 ] + 3
) )
) )
& ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) & safeHeightMask( boardptr->rows )
); );
boardptr->count1.diagonalDown += popcnt_column( boardptr->count1.diagonalDown += popcnt_column(
boardptr->column[ column ] boardptr->column[ column ]

View file

@ -24,9 +24,8 @@ FLAGS += -Wlogical-op
FLAGS += -Wdisabled-optimization FLAGS += -Wdisabled-optimization
FLAGS += -fanalyzer FLAGS += -fanalyzer
FLAGS += -flto FLAGS += -flto
FLAGS += -fuse-ld=gold
FLAGS += -s FLAGS += -s
flags += -Wl,--gc-sections FLAGS += -Wl,--gc-sections
# These flags are because of the compiler otherwise giving too much warning # These flags are because of the compiler otherwise giving too much warning
#FLAGS += -Wno-sign-conversion #FLAGS += -Wno-sign-conversion
#FLAGS += -Wformat-truncation=0 #FLAGS += -Wformat-truncation=0
@ -37,11 +36,12 @@ endif
# Compile flag for defining GITHASH to put at the end of the version string # Compile flag for defining GITHASH to put at the end of the version string
GITFLAG = -D'GITHASH=$(shell git rev-parse --short=1 HEAD)' FLAGS += -D'GITHASH=$(shell git rev-parse --short=1 HEAD)'
# Directories where .o files and excecutables will be placed # Directories where .o files and excecutables will be placed
OBJDIR = obj OBJDIR = obj
OUTDIR = out OUTDIR = out
TESTDIR = test
# The UI to compile for by default using make run # The UI to compile for by default using make run
#UI_TARGET = vt100 #UI_TARGET = vt100
@ -53,6 +53,9 @@ LICENSEURL = https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
# C compiler # C compiler
CC = cc CC = cc
LD = ld
CC_W64 = x86_64-w64-mingw32-gcc
LD_W64 = x86_64-w64-mingw32-ld
# Test options # Test options
TEST_COLUMNS = 1000 TEST_COLUMNS = 1000
@ -67,9 +70,10 @@ MSG_COMPILING = Compiling C:
MSG_CLEANING = Cleaning: MSG_CLEANING = Cleaning:
MSG_CLEANING_OBJ = Cleaning $(OBJDIR): MSG_CLEANING_OBJ = Cleaning $(OBJDIR):
MSG_CLEANING_OUT = Cleaning $(OUTDIR): MSG_CLEANING_OUT = Cleaning $(OUTDIR):
MSG_CLEANING_TEST = Cleaning $(TESTDIR):
# Compile for all UI targets # Compile for all UI targets
all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf stub.elf) all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf stub.elf stub.exe)
# Compile and run the default UI target # Compile and run the default UI target
run: $(OUTDIR)/connect4_$(UI_TARGET).elf run: $(OUTDIR)/connect4_$(UI_TARGET).elf
@ -80,19 +84,22 @@ run_%: $(OUTDIR)/connect4_%.elf
./$< ./$<
# Show result of test # Show result of test
test: test.diff test: $(TESTDIR)/test.diff
# Diff between the tests # Diff between the tests
test.diff: test.log test.log.slow $(TESTDIR)/test.diff: $(TESTDIR)/test.log $(TESTDIR)/test.log.slow
@mkdir -p $(@D)
diff -y $^ > $@ || tail $@ diff -y $^ > $@ || tail $@
diff -q $^ diff -q $^
# Log of testing fast logic # Log of testing fast logic
test.log: $(OUTDIR)/connect4_$(UI_TESTING).elf $(TESTDIR)/test.log: $(OUTDIR)/connect4_$(UI_TESTING).elf
@mkdir -p $(@D)
./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves ./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves
# Log of testing slow logic # Log of testing slow logic
test.log.slow: $(OUTDIR)/connect4_$(UI_TESTING).elf $(TESTDIR)/test.log.slow: $(OUTDIR)/connect4_$(UI_TESTING).elf
@mkdir -p $(@D)
./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves --slow-calcWins ./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves --slow-calcWins
# Compile specifically the ncurses version # Compile specifically the ncurses version
@ -108,25 +115,36 @@ $(OUTDIR)/connect4_%.elf: $(addprefix $(OBJDIR)/,ui_%.o logic.o connect4.o LICEN
@mkdir -p $(@D) @mkdir -p $(@D)
$(CC) $(FLAGS) -o $@ $^ $(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 # Make an object file for the license so it does not all need to be in the code
$(OBJDIR)/LICENSE.o: $(OBJDIR)/LICENSE $(OBJDIR)/LICENSE.o: $(OBJDIR)/LICENSE
@echo $(MSG_LINKING) $@ @echo $(MSG_LINKING) $@
@mkdir -p $(@D) @mkdir -p $(@D)
cd $(@D); ld -r -b binary -o $(@F) $(^F) cd $(@D); $(LD) -r -b binary -o $(@F) $(^F)
# Compile the object file of the main code. # Make an object file for the license so it does not all need to be in the code
# This one's special because it includes the git hash $(OBJDIR)/w64_LICENSE.o: $(OBJDIR)/LICENSE
$(OBJDIR)/connect4.o: connect4.c @echo $(MSG_LINKING) $@
@echo $(MSG_COMPILING) $^
@mkdir -p $(@D) @mkdir -p $(@D)
$(CC) -c $(FLAGS) $(GITFLAG) -o $@ $^ cd $(@D); $(LD_W64) -r -b binary -o $(@F) $(^F)
# Generic object file compile # Linux object file compile
$(OBJDIR)/%.o: %.c $(OBJDIR)/%.o: %.c
@echo $(MSG_COMPILING) $^ @echo $(MSG_COMPILING) $^
@mkdir -p $(@D) @mkdir -p $(@D)
$(CC) -c $(FLAGS) -o $@ $^ $(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. # Download the license file in non-markdown form.
# This is not cleaned as that would be too silly # This is not cleaned as that would be too silly
$(OBJDIR)/LICENSE: $(OBJDIR)/LICENSE:
@ -141,3 +159,6 @@ clean:
rm -f $(OBJDIR)/*.o rm -f $(OBJDIR)/*.o
@echo $(MSG_CLEANING_OUT) @echo $(MSG_CLEANING_OUT)
rm -f $(OUTDIR)/*.elf rm -f $(OUTDIR)/*.elf
rm -f $(OUTDIR)/*.exe
@echo $(MSG_CLEANING_TEST)
rm -f $(TESTDIR)/*

View file

@ -1,6 +1,5 @@
#include "ui.h" #include "ui.h"
#include "logic.h" #include "logic.h"
#include <stdlib.h>
void initBoard( void initBoard(
__attribute__((unused)) const board_t board __attribute__((unused)) const board_t board