Skip to content

Commit f9e9064

Browse files
reduce memory needed for move lists, history table
1 parent b2aa903 commit f9e9064

File tree

6 files changed

+37
-45
lines changed

6 files changed

+37
-45
lines changed

bitboard_setup.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ var oppositeDir = [16]int{SE, SW, NW, NE, SOUTH, WEST, NORTH, EAST, DIR_INVALID}
4141

4242
// var middle_rows BB
4343

44-
var maskOfLength [65]uint64
45-
4644
var rowMasks, columnMasks [8]BB
4745

4846
var pawnIsolatedMasks, pawnSideMasks, pawnDoubledMasks, knightMasks, bishopMasks, rookMasks,
@@ -67,7 +65,6 @@ func setupSquareMasks() {
6765
for i := 0; i < 64; i++ {
6866
sqMaskOn[i] = BB(1 << uint(i))
6967
sqMaskOff[i] = (^sqMaskOn[i])
70-
maskOfLength[i] = uint64(sqMaskOn[i] - 1)
7168
}
7269
}
7370

history.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import (
99
"sync/atomic"
1010
)
1111

12-
type HistoryTable [2][8][64]uint64
12+
type HistoryTable [2][8][64]uint32
1313

1414
// Store atomically adds count to the history table h.
1515
func (h *HistoryTable) Store(m Move, c uint8, count int) {
16-
atomic.AddUint64(&h[c][m.Piece()][m.To()], uint64((count>>2)|1))
16+
atomic.AddUint32(&h[c][m.Piece()][m.To()], uint32((count>>4)|1))
1717
}
1818

1919
// Probe atomically reads the history table h.
20-
func (h *HistoryTable) Probe(pc Piece, c uint8, to int) uint64 {
21-
v := atomic.LoadUint64(&h[c][pc][to])
20+
func (h *HistoryTable) Probe(pc Piece, c uint8, to int) uint32 {
21+
v := atomic.LoadUint32(&h[c][pc][to])
2222
if v > 0 {
23-
return ((((v >> 3) & maskOfLength[21]) | 1) << 1)
23+
return ((v >> 3) & uint32(1<<21)) | 1
2424
}
2525
return 0
2626
}

move_gen.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,9 +827,8 @@ func getChecks(brd *Board, htable *HistoryTable, remainingMoves *MoveList) {
827827

828828
func getPromotionAdvances(brd *Board, winning, losing *MoveList, from, to int) {
829829
var m Move
830-
var sort uint64
831830
m = NewMove(from, to, PAWN, EMPTY, QUEEN)
832-
sort = sortPromotionAdvances(brd, from, to, QUEEN)
831+
sort := sortPromotionAdvances(brd, from, to, QUEEN)
833832
if sort >= SORT_WINNING_PROMOTION {
834833
winning.Push(SortItem{sort, m})
835834
} else {

search.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const (
3030

3131
const (
3232
MAX_DEPTH = 32 // default maximum search depth
33-
COMMS_MIN = 7 // minimum depth at which to send info to GUI.
33+
COMMS_MIN = 1 // minimum depth at which to send info to GUI.
3434
)
3535

3636
const (
@@ -159,17 +159,16 @@ func (s *Search) iterativeDeepening(brd *Board) int {
159159
inCheck := brd.InCheck()
160160

161161
for d := 1; d <= s.maxDepth; d++ {
162-
163-
stk[0].inCheck = inCheck
164-
guess, total = s.ybw(brd, stk, s.alpha, s.beta, d, 0, Y_PV, SP_NONE, false)
165-
sum += total
166-
167162
select {
168163
case <-s.cancel:
169164
return sum
170165
default:
171166
}
172167

168+
stk[0].inCheck = inCheck
169+
guess, total = s.ybw(brd, stk, s.alpha, s.beta, d, 0, Y_PV, SP_NONE, false)
170+
sum += total
171+
173172
if stk[0].pv.m.IsMove() {
174173
s.Lock()
175174
s.bestMove, s.bestScore[c] = stk[0].pv.m, guess

sort.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import "sort"
1111
// At root, moves should be sorted based on subtree value rather than standard sorting.
1212

1313
// bit pos. (LSB order)
14-
// 28 Winning promotions (1 bits)
15-
// 1 <<padding>> (1 bit)
14+
// 30 Winning promotions (1 bits)
15+
// 29 Losing promotions (1 bits)
16+
// 28 <<padding>> (1 bit)
1617
// 22 MVV/LVA (6 bits) - Used to choose between captures of equal material gain/loss
1718
// 1 History heuristic : (21 bits)
1819
// 0 Castles (1 bit)
1920

2021
const (
21-
SORT_WINNING_PROMOTION = (1 << 31)
22+
SORT_WINNING_PROMOTION = (1 << 30)
2223
SORT_LOSING_PROMOTION = (1 << 29)
2324
)
2425

@@ -31,48 +32,34 @@ const (
3132
// if square undefended, gain is promote_values[promoted_piece].
3233
// If defended, gain is SEE score where captured_piece == EMPTY
3334

34-
// func sort_promotion(brd *Board, m Move) uint64 {
35-
// if is_attacked_by(brd, brd.AllOccupied()&sq_mask_off[m.From()],
36-
// m.To(), brd.Enemy(), brd.c) { // defended
37-
// if get_see(brd, m.From(), m.To(), m.CapturedPiece()) >= 0 {
38-
// return SORT_WINNING_PROMOTION | mvv_lva(m.CapturedPiece(), PAWN)
39-
// } else {
40-
// return mvv_lva(m.CapturedPiece(), PAWN)
41-
// }
42-
// } else {
43-
// // val = m.PromotedTo().PromoteValue() + m.CapturedPiece().Value() // undefended
44-
// return SORT_WINNING_PROMOTION | mvv_lva(m.CapturedPiece(), PAWN)
45-
// }
46-
// }
47-
48-
func sortPromotionAdvances(brd *Board, from, to int, promotedTo Piece) uint64 {
35+
func sortPromotionAdvances(brd *Board, from, to int, promotedTo Piece) uint32 {
4936
if isAttackedBy(brd, brd.AllOccupied()&sqMaskOff[from],
5037
to, brd.Enemy(), brd.c) { // defended
5138
see := getSee(brd, from, to, EMPTY)
5239
if see >= 0 {
53-
return SORT_WINNING_PROMOTION | uint64(see)
40+
return SORT_WINNING_PROMOTION | uint32(see)
5441
} else {
55-
return uint64(SORT_LOSING_PROMOTION + see)
42+
return uint32(SORT_LOSING_PROMOTION + see)
5643
}
5744
} else { // undefended
58-
return SORT_WINNING_PROMOTION | uint64(promotedTo.PromoteValue())
45+
return SORT_WINNING_PROMOTION | uint32(promotedTo.PromoteValue())
5946
}
6047
}
6148

62-
func sortPromotionCaptures(brd *Board, from, to int, capturedPiece, promotedTo Piece) uint64 {
49+
func sortPromotionCaptures(brd *Board, from, to int, capturedPiece, promotedTo Piece) uint32 {
6350
if isAttackedBy(brd, brd.AllOccupied()&sqMaskOff[from], to, brd.Enemy(), brd.c) { // defended
64-
return uint64(SORT_WINNING_PROMOTION + getSee(brd, from, to, capturedPiece))
51+
return uint32(SORT_WINNING_PROMOTION + getSee(brd, from, to, capturedPiece))
6552
} else { // undefended
66-
return SORT_WINNING_PROMOTION | uint64(promotedTo.PromoteValue()+capturedPiece.Value())
53+
return SORT_WINNING_PROMOTION | uint32(promotedTo.PromoteValue()+capturedPiece.Value())
6754
}
6855
}
6956

70-
func mvvLva(victim, attacker Piece) uint64 { // returns value between 0 and 64
71-
return uint64(((victim+1)<<3)-attacker) << 22
57+
func mvvLva(victim, attacker Piece) uint32 { // returns value between 0 and 64
58+
return uint32(((victim+1)<<3)-attacker) << 22
7259
}
7360

7461
type SortItem struct {
75-
order uint64
62+
order uint32
7663
move Move
7764
}
7865

uci.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"fmt"
1717
"log"
1818
"os"
19+
"path/filepath"
1920
"runtime"
2021
"strconv"
2122
"strings"
@@ -77,10 +78,19 @@ func (uci *UCIAdapter) Read(reader *bufio.Reader) {
7778
var input string
7879
var uciFields []string
7980

80-
f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
81+
f, err := os.OpenFile("./log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
8182
if err != nil {
82-
fmt.Printf("error opening file: %v\n", err)
83+
fmt.Printf("info string error opening file: %v\n", err)
84+
} else {
85+
fmt.Printf("info string log file created\n")
86+
87+
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
88+
if err != nil {
89+
// log.Fatal(err)
90+
}
91+
fmt.Println(dir)
8392
}
93+
8494
defer f.Close()
8595
log.SetOutput(f)
8696

0 commit comments

Comments
 (0)