Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/gomoku gomoku: turn spotstr.s_frame into a frame index



details:   https://anonhg.NetBSD.org/src/rev/a8fbb229cab3
branches:  trunk
changeset: 366498:a8fbb229cab3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 29 10:37:21 2022 +0000

description:
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.

diffstat:

 games/gomoku/bdinit.c   |  18 ++++++++++--------
 games/gomoku/gomoku.h   |  10 ++++++----
 games/gomoku/main.c     |   8 ++++----
 games/gomoku/makemove.c |  16 ++++++++--------
 games/gomoku/pickmove.c |   6 +++---
 5 files changed, 31 insertions(+), 27 deletions(-)

diffs (248 lines):

diff -r 8ae4b5c38b6b -r a8fbb229cab3 games/gomoku/bdinit.c
--- a/games/gomoku/bdinit.c     Sun May 29 10:06:43 2022 +0000
+++ b/games/gomoku/bdinit.c     Sun May 29 10:37:21 2022 +0000
@@ -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 @@
 
 /* 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 @@
        }
 
        /* 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 @@
                        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);
diff -r 8ae4b5c38b6b -r a8fbb229cab3 games/gomoku/gomoku.h
--- a/games/gomoku/gomoku.h     Sun May 29 10:06:43 2022 +0000
+++ b/games/gomoku/gomoku.h     Sun May 29 10:37:21 2022 +0000
@@ -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 @@
        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 @@
        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 @@
        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 */
diff -r 8ae4b5c38b6b -r a8fbb229cab3 games/gomoku/main.c
--- a/games/gomoku/main.c       Sun May 29 10:06:43 2022 +0000
+++ b/games/gomoku/main.c       Sun May 29 10:37:21 2022 +0000
@@ -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 @@
                                                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 @@
                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;
diff -r 8ae4b5c38b6b -r a8fbb229cab3 games/gomoku/makemove.c
--- a/games/gomoku/makemove.c   Sun May 29 10:06:43 2022 +0000
+++ b/games/gomoku/makemove.c   Sun May 29 10:37:21 2022 +0000
@@ -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 @@
                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 @@
 
 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 @@
                }
        }
 
-       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 @@
  * 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 @@
                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 @@
                 * 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) {
diff -r 8ae4b5c38b6b -r a8fbb229cab3 games/gomoku/pickmove.c
--- a/games/gomoku/pickmove.c   Sun May 29 10:06:43 2022 +0000
+++ b/games/gomoku/pickmove.c   Sun May 29 10:37:21 2022 +0000
@@ -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 @@
                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;



Home | Main Index | Thread Index | Old Index