From 214112ad1730a5b1f70696f69a1d6da33e3db9c6 Mon Sep 17 00:00:00 2001 From: AnnaSnoeijs Date: Thu, 14 Aug 2025 20:05:18 +0200 Subject: [PATCH 1/3] Fixed calcWins_slow for 31 high boards --- logic.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/logic.c b/logic.c index 093fd60..e1459a8 100644 --- a/logic.c +++ b/logic.c @@ -18,6 +18,7 @@ */ #include #include +#include #include "logic.h" #include "macros.h" @@ -43,7 +44,8 @@ static void updateTotal( wincount_t *count ){ // popcount for a column static winint_t popcnt_column( column_t column ){ - winint_t i = (winint_t)stdc_count_ones( column ); + winint_t i = (winint_t)__builtin_popcountg( (uint64_t)column ); + //if( i > 32 ) printf( "%d %lb", i, column ); // TODO: REMOVE return i; } @@ -119,6 +121,7 @@ void calcWins_slow( board_t *boardptr ){ & safeHeightMask( boardptr->height[ column + 1 ] ) & safeHeightMask( boardptr->height[ column + 2 ] ) & safeHeightMask( boardptr->height[ column + 3 ] ) + & ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) ); boardptr->count1.horizontal += popcnt_column( boardptr->column[ column ] @@ -136,6 +139,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 ) + & ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) ); boardptr->count1.diagonalUp += popcnt_column( boardptr->column[ column ] @@ -158,6 +162,7 @@ void calcWins_slow( board_t *boardptr ){ boardptr->height[ column + 3 ] + 3 ) ) + & ( ( (column_t)1 << (column_t)boardptr->rows ) - (column_t)1 ) ); boardptr->count1.diagonalDown += popcnt_column( boardptr->column[ column ] From 72a56a15d7df2a6faa5ced6a4707564b2c14a48f Mon Sep 17 00:00:00 2001 From: AnnaSnoeijs Date: Thu, 14 Aug 2025 20:21:06 +0200 Subject: [PATCH 2/3] Made maximum rows more limited to avoid bugs --- connect4.c | 4 ++-- types.h | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/connect4.c b/connect4.c index b054145..91710c8 100644 --- a/connect4.c +++ b/connect4.c @@ -114,7 +114,7 @@ int main( int argc, char *argv[] ){ "\n" " -o file --output file Ouput moves taken in game to file\n" "\n" -" --slow-calcwins Use the 32bit reference implementation of calcWins()\n" +" --slow-calcwins Use the reference implementation of calcWins()\n" " --random-moves Play random moves instead of asking for input\n" ); return 0; @@ -150,7 +150,7 @@ int main( int argc, char *argv[] ){ fprintf( stderr, "ERR: AMOUT OF ROWS MUST BE AT LEAST 1\n" ); return -1; } -#define ROWOVERFLOW (rowsint_t)( sizeof(column_t) * CHAR_BIT ) +#define ROWOVERFLOW (rowsint_t)( sizeof(column_t) * CHAR_BIT - 1 ) if( playboard.rows >= ROWOVERFLOW ){ fprintf( stderr, "ERR: AMOUT OF ROWS MUST BE LESS THAN %d\n", ROWOVERFLOW diff --git a/types.h b/types.h index 028565d..9590065 100644 --- a/types.h +++ b/types.h @@ -5,13 +5,13 @@ #include #include "config.h" -// Use fastest available ints unless it's 64 bits because that uses more memory +// Use fastest available ints unless it's 64 bits because that is doing silly #if INT_FAST16_MAX == INT_FAST64_MAX -typedef int rowsint_t; -typedef unsigned columnsint_t; +typedef int rowsint_t; +typedef unsigned columnsint_t; #define QUITCOLUMN UINT_MAX -typedef unsigned winint_t; -typedef uintmax_t column_t; +typedef unsigned winint_t; +typedef unsigned column_t; #else typedef int_fast8_t rowsint_t; typedef uint_fast8_t columnsint_t; From 3e450b5909cfc2a0b4943353df4041dd2a86945a Mon Sep 17 00:00:00 2001 From: AnnaSnoeijs Date: Thu, 14 Aug 2025 20:23:04 +0200 Subject: [PATCH 3/3] Added a test option to the makefile --- makefile | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/makefile b/makefile index 28eb98f..ca2de49 100644 --- a/makefile +++ b/makefile @@ -25,7 +25,7 @@ FLAGS += -Wdisabled-optimization FLAGS += -fanalyzer # These flags are because of the compiler otherwise giving too much warning FLAGS += -Wno-sign-conversion -#FLAGS += -Wformat-truncation=0 +FLAGS += -Wformat-truncation=0 # Compile flag for defining GITHASH to put at the end of the version string GITFLAG = -D'GITHASH=$(shell git rev-parse --short=1 HEAD)' @@ -37,6 +37,7 @@ OUTDIR = out # The UI to compile for by default using make run #UI_TARGET = vt100 UI_TARGET = ncurses +UI_TESTING = stub # The URL of the plaintext version of the lisence, for the --license option LICENSEURL = https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt @@ -44,6 +45,10 @@ LICENSEURL = https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt # C compiler CC = cc +# Test options +TEST_COLUMNS = 1000 +TEST_ROWS = 30 + .NOTINTERMEDIATE: # Messages to print during different steps in making @@ -65,6 +70,22 @@ run: $(OUTDIR)/connect4_$(UI_TARGET).elf run_%: $(OUTDIR)/connect4_%.elf ./$< +# Show result of test +test: test.diff + +# Diff between the tests +test.diff: test.log test.log.slow + diff -y $^ > $@ || tail $@ + diff -q $^ + +# Log of testing fast logic +test.log: $(OUTDIR)/connect4_$(UI_TESTING).elf + ./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves + +# Log of testing slow logic +test.log.slow: $(OUTDIR)/connect4_$(UI_TESTING).elf + ./$< -o $@ -c $(TEST_COLUMNS) -r $(TEST_ROWS) --random-moves --slow-calcWins + # Compile specifically the ncurses version # This one's special because it needs -lncursesw $(OUTDIR)/connect4_ncurses.elf: $(addprefix $(OBJDIR)/,ui_ncurses.o logic.o connect4.o LICENSE.o)