Module Name:    src
Committed By:   rillig
Date:           Sun May 29 10:37:21 UTC 2022

Modified Files:
        src/games/gomoku: bdinit.c gomoku.h main.c makemove.c pickmove.c

Log Message:
gomoku: turn spotstr.s_frame into a frame index

Most calculations are done on the frame index, not the pointer. This
avoids dealing with ptrdiff_t conversion to int.

Changing the type of s_frame changes the size of struct spotstr, it is
now 56 bytes on LP64 and 48 bytes on ILP32, neither of which is a power
of two. Remove the dummy padding since compilers no longer generate
division instructions for divisions by small integer constants, so that
optimization is no longer necessary.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/games/gomoku/bdinit.c
cvs rdiff -u -r1.48 -r1.49 src/games/gomoku/gomoku.h
cvs rdiff -u -r1.68 -r1.69 src/games/gomoku/main.c
cvs rdiff -u -r1.35 -r1.36 src/games/gomoku/makemove.c
cvs rdiff -u -r1.52 -r1.53 src/games/gomoku/pickmove.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/gomoku/bdinit.c
diff -u src/games/gomoku/bdinit.c:1.29 src/games/gomoku/bdinit.c:1.30
--- src/games/gomoku/bdinit.c:1.29	Sun May 29 00:38:26 2022
+++ src/games/gomoku/bdinit.c	Sun May 29 10:37:21 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: bdinit.c,v 1.29 2022/05/29 00:38:26 rillig Exp $	*/
+/*	$NetBSD: bdinit.c,v 1.30 2022/05/29 10:37:21 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*	from: @(#)bdinit.c	8.2 (Berkeley) 5/3/95	*/
-__RCSID("$NetBSD: bdinit.c,v 1.29 2022/05/29 00:38:26 rillig Exp $");
+__RCSID("$NetBSD: bdinit.c,v 1.30 2022/05/29 10:37:21 rillig Exp $");
 
 #include <string.h>
 #include "gomoku.h"
@@ -106,19 +106,21 @@ init_spot_flags_and_fval(struct spotstr 
 
 /* Allocate one of the pre-allocated frames for each non-blocked frame. */
 static void
-init_spot_frame(struct spotstr *sp, struct combostr **cbpp)
+init_spot_frame(struct spotstr *sp, frame_index *fip)
 {
 
 	for (int r = 4; --r >= 0; ) {
 		if ((sp->s_flags & (BFLAG << r)) != 0)
 			continue;
 
-		struct combostr *cbp = (*cbpp)++;
+		frame_index fi = (*fip)++;
+		sp->s_frame[r] = fi;
+
+		struct combostr *cbp = &frames[fi];
 		cbp->c_combo.s = sp->s_fval[BLACK][r].s;
 		cbp->c_vertex = (u_short)(sp - board);
 		cbp->c_nframes = 1;
 		cbp->c_dir = r;
-		sp->s_frame[r] = cbp;
 	}
 }
 
@@ -136,14 +138,14 @@ init_board(void)
 	}
 
 	/* fill the playing area of the board with EMPTY spots */
-	struct combostr *cbp = frames;
+	frame_index fi = 0;
 	memset(frames, 0, sizeof(frames));
 	for (int row = 1; row <= BSZ; row++, sp++) {
 		for (int col = 1; col <= BSZ; col++, sp++) {
 			sp->s_occ = EMPTY;
 			sp->s_wval = 0;
 			init_spot_flags_and_fval(sp, col, row);
-			init_spot_frame(sp, &cbp);
+			init_spot_frame(sp, &fi);
 		}
 		sp->s_occ = BORDER;	/* combined left and right border */
 		sp->s_flags = BFLAGALL;
@@ -247,7 +249,7 @@ init_overlap_frame(int fia, int ra, int 
 			if ((spb0->s_flags & BFLAG << rb) != 0)
 				continue;
 
-			int fib = (int)(spb0->s_frame[rb] - frames);
+			frame_index fib = spb0->s_frame[rb];
 			intersect[fia * FAREA + fib] = s;
 			u_char *op = &overlap[fia * FAREA + fib];
 			*op = adjust_overlap(*op, ra, sia, rb, sib, mask);

Index: src/games/gomoku/gomoku.h
diff -u src/games/gomoku/gomoku.h:1.48 src/games/gomoku/gomoku.h:1.49
--- src/games/gomoku/gomoku.h:1.48	Sun May 29 00:38:26 2022
+++ src/games/gomoku/gomoku.h	Sun May 29 10:37:21 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: gomoku.h,v 1.48 2022/05/29 00:38:26 rillig Exp $	*/
+/*	$NetBSD: gomoku.h,v 1.49 2022/05/29 10:37:21 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -154,7 +154,7 @@ struct combostr {
 	union comboval	c_linkv[2];	/* C: combo value for link[0, 1] */
 	union comboval	c_combo;	/* F: initial combo value (read-only),
 					 * C: combo value for this level */
-	u_short		c_vertex;	/* F: frame head,
+	spot_index	c_vertex;	/* F: frame head,
 					 * C: intersection */
 	u_char		c_nframes;	/* F: 1,
 					 * C: number of frames in the combo */
@@ -187,6 +187,9 @@ struct	elist {
 	union comboval	e_fval;		/* frame combo value */
 };
 
+/* The index of a frame in the global 'frames'. */
+typedef unsigned short frame_index;
+
 /*
  * One spot structure for each location on the board.
  * A frame consists of the combination for the current spot plus the five spots
@@ -196,14 +199,13 @@ struct	spotstr {
 	short		s_occ;		/* color of occupant */
 	short		s_wval;		/* weighted value */
 	int		s_flags;	/* flags for graph walks */
-	struct combostr	*s_frame[4];	/* level 1 combo for frame[dir] */
+	frame_index	s_frame[4];	/* level 1 combo for [dir] */
 	union comboval	s_fval[2][4];	/* combo value for [color][frame] */
 	union comboval	s_combo[2];	/* minimum combo value for BLK & WHT */
 	u_char		s_level[2];	/* number of frames in the min combo */
 	u_char		s_nforce[2];	/* number of <1,x> combos */
 	struct elist	*s_empty;	/* level n combo completion spots */
 	struct elist	*s_nempty;	/* level n+1 combo completion spots */
-	int		dummy[2];	/* XXX */
 };
 
 /* flag values for s_flags */

Index: src/games/gomoku/main.c
diff -u src/games/gomoku/main.c:1.68 src/games/gomoku/main.c:1.69
--- src/games/gomoku/main.c:1.68	Sun May 29 00:38:26 2022
+++ src/games/gomoku/main.c	Sun May 29 10:37:21 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.68 2022/05/29 00:38:26 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.69 2022/05/29 10:37:21 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -36,7 +36,7 @@
 __COPYRIGHT("@(#) Copyright (c) 1994\
  The Regents of the University of California.  All rights reserved.");
 /*	@(#)main.c	8.4 (Berkeley) 5/4/95	*/
-__RCSID("$NetBSD: main.c,v 1.68 2022/05/29 00:38:26 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.69 2022/05/29 10:37:21 rillig Exp $");
 
 #include <sys/stat.h>
 #include <curses.h>
@@ -495,7 +495,7 @@ top:
 						break;
 				str[-1] = '\0';
 				sp = &board[s1 = ctos(input + 1)];
-				n = (int)(sp->s_frame[d1] - frames) * FAREA;
+				n = sp->s_frame[d1] * FAREA;
 				*str++ = '\0';
 				break;
 			}
@@ -505,7 +505,7 @@ top:
 		for (d2 = 0; d2 < 4; d2++)
 			if (str[-1] == pdir[d2])
 				break;
-		n += (int)(sp->s_frame[d2] - frames);
+		n += sp->s_frame[d2];
 		debuglog("overlap %s%c,%s%c = %x", stoc(s1), pdir[d1],
 		    stoc(s2), pdir[d2], overlap[n]);
 		goto top;

Index: src/games/gomoku/makemove.c
diff -u src/games/gomoku/makemove.c:1.35 src/games/gomoku/makemove.c:1.36
--- src/games/gomoku/makemove.c:1.35	Sun May 29 01:34:49 2022
+++ src/games/gomoku/makemove.c	Sun May 29 10:37:21 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: makemove.c,v 1.35 2022/05/29 01:34:49 rillig Exp $	*/
+/*	$NetBSD: makemove.c,v 1.36 2022/05/29 10:37:21 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*	@(#)makemove.c	8.2 (Berkeley) 5/3/95	*/
-__RCSID("$NetBSD: makemove.c,v 1.35 2022/05/29 01:34:49 rillig Exp $");
+__RCSID("$NetBSD: makemove.c,v 1.36 2022/05/29 10:37:21 rillig Exp $");
 
 #include "gomoku.h"
 
@@ -125,7 +125,7 @@ makemove(int us, spot_index mv)
 		if ((fsp->s_flags & BFLAG << r) != 0)
 		    continue;
 
-		struct combostr *cbp = fsp->s_frame[r];
+		struct combostr *cbp = &frames[fsp->s_frame[r]];
 		sortframes_remove(cbp);
 
 		int val = old_weight_value(fsp, r);
@@ -233,7 +233,7 @@ makemove(int us, spot_index mv)
 
 static void
 update_overlap_same_direction(spot_index s1, spot_index s2,
-			      int a, int d, int i_minus_f, int r)
+			      frame_index a, int d, int i_minus_f, int r)
 {
 	/*
 	 * count the number of empty spots to see if there is
@@ -249,7 +249,7 @@ update_overlap_same_direction(spot_index
 		}
 	}
 
-	int b = (int)(board[s2].s_frame[r] - frames);
+	frame_index b = board[s2].s_frame[r];
 	if (n == 0) {
 		if (board[s].s_occ == EMPTY) {
 			overlap[a * FAREA + b] &= 0xA;
@@ -281,7 +281,7 @@ update_overlap_same_direction(spot_index
  * frames as non-overlapping with frame 'a'.
  */
 static void
-update_overlap_different_direction(spot_index os, int a, int rb)
+update_overlap_different_direction(spot_index os, frame_index a, int rb)
 {
 
 	int db = dd[rb];
@@ -292,7 +292,7 @@ update_overlap_different_direction(spot_
 		if ((sp->s_flags & BFLAG << rb) != 0)
 			continue;
 
-		int b = (int)(sp->s_frame[rb] - frames);
+		frame_index b = sp->s_frame[rb];
 		overlap[a * FAREA + b] = 0;
 		overlap[b * FAREA + a] = 0;
 	}
@@ -323,7 +323,7 @@ update_overlap(spot_index os)
 		 * do the rows 0 <= r1 <= r. The r1 == r case is special
 		 * since the two frames can overlap at more than one point.
 		 */
-		int a = (int)(board[s1].s_frame[r] - frames);
+		frame_index a = board[s1].s_frame[r];
 
 		spot_index s2 = s1 - d;
 		for (int i = f + 1; i < 6; i++, s2 -= d) {

Index: src/games/gomoku/pickmove.c
diff -u src/games/gomoku/pickmove.c:1.52 src/games/gomoku/pickmove.c:1.53
--- src/games/gomoku/pickmove.c:1.52	Sun May 29 10:06:43 2022
+++ src/games/gomoku/pickmove.c	Sun May 29 10:37:21 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: pickmove.c,v 1.52 2022/05/29 10:06:43 rillig Exp $	*/
+/*	$NetBSD: pickmove.c,v 1.53 2022/05/29 10:37:21 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*	@(#)pickmove.c	8.2 (Berkeley) 5/3/95	*/
-__RCSID("$NetBSD: pickmove.c,v 1.52 2022/05/29 10:06:43 rillig Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.53 2022/05/29 10:37:21 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -477,7 +477,7 @@ makecombo2(struct combostr *ocbp, struct
 		if (ncbp == NULL)
 		    panic("Out of memory!");
 		scbpp = (void *)(ncbp + 1);
-		fcbp = fsp->s_frame[r];
+		fcbp = &frames[fsp->s_frame[r]];
 		if (ocbp < fcbp) {
 		    scbpp[0] = ocbp;
 		    scbpp[1] = fcbp;

Reply via email to