Skip to content

Commit d747284

Browse files
use power of 2 array sizes in Board struct
1 parent 8b0fd3e commit d747284

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

bitboard_setup.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@ var rookOffsets = [4]int{8, 1, -8, -1}
3333
var kingOffsets = [8]int{-9, -7, 7, 9, -8, -1, 1, 8}
3434
var pawnAttackOffsets = [4]int{9, 7, -9, -7}
3535

36-
// var pawnAdvanceOffsets = [4]int{8, 16, -8, -16}
37-
3836
var directions [64][64]int
3937

4038
var oppositeDir = [16]int{SE, SW, NW, NE, SOUTH, WEST, NORTH, EAST, DIR_INVALID}
4139

42-
// var middle_rows BB
43-
4440
var rowMasks, columnMasks [8]BB
4541

4642
var castleMasks [16]BB

board.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ var printMutex sync.Mutex
2121
// and passed to the new goroutine. Keep this struct as small as possible.
2222
// TODO: change material to uint16
2323
type Board struct {
24-
pieces [2][6]BB // 768 bits
25-
squares [64]Piece // 512 bits
26-
occupied [2]BB // 128 bits
27-
material [2]int32 // 64 bits
28-
hashKey uint64 // 64 bits
29-
worker *Worker // 64 bits
30-
pawnHashKey uint32 // 32 bits
31-
c uint8 // 8 bits
32-
castle uint8 // 8 bits
33-
enpTarget uint8 // 8 bits
34-
halfmoveClock uint8 // 8 bits
35-
endgameCounter uint8 // 8 bits
36-
24+
pieces [2][8]BB // 1024 bits
25+
squares [64]Piece // 512 bits
26+
occupied [2]BB // 128 bits
27+
hashKey uint64 // 64 bits
28+
worker *Worker // 64 bits
29+
material [2]int16 // 32 bits
30+
pawnHashKey uint32 // 32 bits
31+
c uint8 // 8 bits
32+
castle uint8 // 8 bits
33+
enpTarget uint8 // 8 bits
34+
halfmoveClock uint8 // 8 bits
35+
endgameCounter uint8 // 8 bits
36+
// ...24 bits padding
3737
}
3838

3939
type BoardMemento struct { // memento object used to store board state to unmake later.
@@ -59,7 +59,6 @@ func (brd *Board) InCheck() bool { // determines if side to move is in check
5959
}
6060

6161
func (brd *Board) KingSq(c uint8) int {
62-
// assert(brd.pieces[c][KING] > 0, "King missing from board")
6362
return furthestForward(c, brd.pieces[c][KING])
6463
}
6564

make.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func removePiece(brd *Board, removedPiece Piece, sq int, e uint8) {
240240
func unmakeRemovePiece(brd *Board, removedPiece Piece, sq int, e uint8) {
241241
brd.pieces[e][removedPiece].Clear(sq)
242242
brd.occupied[e].Clear(sq)
243-
brd.material[e] -= int32(removedPiece.Value() + mainPst[e][removedPiece][sq])
243+
brd.material[e] -= int16(removedPiece.Value() + mainPst[e][removedPiece][sq])
244244
brd.endgameCounter -= endgameCountValues[removedPiece]
245245
}
246246

@@ -253,7 +253,7 @@ func unmakeAddPiece(brd *Board, addedPiece Piece, sq int, c uint8) {
253253
brd.pieces[c][addedPiece].Add(sq)
254254
brd.squares[sq] = addedPiece
255255
brd.occupied[c].Add(sq)
256-
brd.material[c] += int32(addedPiece.Value() + mainPst[c][addedPiece][sq])
256+
brd.material[c] += int16(addedPiece.Value() + mainPst[c][addedPiece][sq])
257257
brd.endgameCounter += endgameCountValues[addedPiece]
258258
}
259259

@@ -269,7 +269,7 @@ func unmakeRelocatePiece(brd *Board, piece Piece, from, to int, c uint8) {
269269
brd.occupied[c] ^= fromTo
270270
brd.squares[from] = EMPTY
271271
brd.squares[to] = piece
272-
brd.material[c] += int32(mainPst[c][piece][to] - mainPst[c][piece][from])
272+
brd.material[c] += int16(mainPst[c][piece][to] - mainPst[c][piece][from])
273273
}
274274

275275
func relocateKing(brd *Board, piece, capturedPiece Piece, from, to int, c uint8) {

utilities.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ func CompareBoards(brd, other *Board) bool {
124124
func isBoardConsistent(brd *Board) bool {
125125
var squares [64]Piece
126126
var occupied [2]BB
127-
var material [2]int32
127+
var material [2]int16
128128

129129
var sq int
130-
for sq := 0; sq < 64; sq++ {
130+
for sq = 0; sq < 64; sq++ {
131131
squares[sq] = EMPTY
132132
}
133133
consistent := true
@@ -142,7 +142,7 @@ func isBoardConsistent(brd *Board) bool {
142142

143143
for bb := brd.pieces[c][pc]; bb > 0; bb.Clear(sq) {
144144
sq = furthestForward(c, bb)
145-
material[c] += int32(pc.Value() + mainPst[c][pc][sq])
145+
material[c] += int16(pc.Value() + mainPst[c][pc][sq])
146146
if squares[sq] != EMPTY {
147147
fmt.Printf("brd.pieces[%d][%d] overlaps with another pieces bitboard at %s.\n", c, pc, SquareString(sq))
148148
consistent = false

zobrist.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ package main
1111
// piece/square combination, and merging in keys representing the side to move, castling rights,
1212
// and any en-passant target square.
1313
var pawnZobristTable [2][64]uint32
14-
var zobristTable [2][8][64]uint64 // keep array dimensions powers of 2 for faster array access.
15-
var enpTable [65]uint64 // integer keys representing the en-passant target square, if any.
14+
var zobristTable [2][8][64]uint64
15+
16+
// integer keys representing the en-passant target square, if any.
17+
var enpTable [128]uint64
1618
var castleTable [16]uint64
1719
var sideKey64 uint64 // keys representing a change in side-to-move.
1820

0 commit comments

Comments
 (0)