Skip to content

Commit 86d69ff

Browse files
include SEE score in ordering of captures
1 parent 8d458f7 commit 86d69ff

File tree

5 files changed

+117
-85
lines changed

5 files changed

+117
-85
lines changed

history.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,35 @@
66
package main
77

88
import (
9+
"fmt"
910
"sync/atomic"
1011
)
1112

1213
type HistoryTable [2][8][64]uint32
1314

1415
// Store atomically adds count to the history table h.
1516
func (h *HistoryTable) Store(m Move, c uint8, count int) {
16-
atomic.AddUint32(&h[c][m.Piece()][m.To()], uint32((count>>4)|1))
17+
atomic.AddUint32(&h[c][m.Piece()][m.To()], uint32((count>>3)|1))
1718
}
1819

1920
// Probe atomically reads the history table h.
2021
func (h *HistoryTable) Probe(pc Piece, c uint8, to int) uint32 {
21-
v := atomic.LoadUint32(&h[c][pc][to])
22-
if v > 0 {
23-
return ((v >> 3) & uint32(1<<21)) | 1
22+
if v := atomic.LoadUint32(&h[c][pc][to]); v > 0 {
23+
return ((v >> 3) & (uint32(1<<22) - 1)) | 1
2424
}
2525
return 0
2626
}
27+
28+
func (h *HistoryTable) PrintMax() {
29+
var val uint32
30+
for i := 0; i < 2; i++ {
31+
for j := 0; j < 8; j++ {
32+
for k := 0; k < 64; k++ {
33+
if h[i][j][k] > val {
34+
val = h[i][j][k]
35+
}
36+
}
37+
}
38+
}
39+
fmt.Printf("%d\n", val)
40+
}

move_gen.go

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ func getCaptures(brd *Board, htable *HistoryTable, winning, losing *MoveList) {
175175
to = furthestForward(c, leftAttacks)
176176
from = to + pawnFromOffsets[c][OFF_LEFT]
177177
m = NewCapture(from, to, PAWN, brd.squares[to])
178-
winning.Push(SortItem{mvvLva(brd.squares[to], PAWN), m})
178+
winning.Push(SortItem{SortCapture(brd.squares[to], PAWN, 0), m})
179179
}
180180
for ; rightAttacks > 0; rightAttacks.Clear(to) {
181181
to = furthestForward(c, rightAttacks)
182182
from = to + pawnFromOffsets[c][OFF_RIGHT]
183183
m = NewCapture(from, to, PAWN, brd.squares[to])
184-
winning.Push(SortItem{mvvLva(brd.squares[to], PAWN), m})
184+
winning.Push(SortItem{SortCapture(brd.squares[to], PAWN, 0), m})
185185
}
186186
// en-passant captures
187187
if brd.enpTarget != SQ_INVALID {
@@ -194,19 +194,21 @@ func getCaptures(brd *Board, htable *HistoryTable, winning, losing *MoveList) {
194194
to = int(enpTarget) - 8
195195
}
196196
m = NewCapture(from, to, PAWN, PAWN)
197-
winning.Push(SortItem{mvvLva(PAWN, PAWN), m})
197+
winning.Push(SortItem{SortCapture(PAWN, PAWN, 0), m})
198198
}
199199
}
200200
// Knights
201+
var see int
201202
for f := brd.pieces[c][KNIGHT]; f > 0; f.Clear(from) {
202203
from = furthestForward(c, f)
203204
for t := (knightMasks[from] & enemy); t > 0; t.Clear(to) { // generate to squares
204205
to = furthestForward(c, t)
205206
m = NewCapture(from, to, KNIGHT, brd.squares[to])
206-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
207-
winning.Push(SortItem{mvvLva(brd.squares[to], KNIGHT), m})
207+
see = getSee(brd, from, to, brd.squares[to])
208+
if see >= 0 {
209+
winning.Push(SortItem{SortCapture(brd.squares[to], KNIGHT, see), m})
208210
} else {
209-
losing.Push(SortItem{mvvLva(brd.squares[to], KNIGHT), m})
211+
losing.Push(SortItem{SortCapture(brd.squares[to], KNIGHT, see), m})
210212
}
211213
}
212214
}
@@ -216,10 +218,11 @@ func getCaptures(brd *Board, htable *HistoryTable, winning, losing *MoveList) {
216218
for t := (bishopAttacks(occ, from) & enemy); t > 0; t.Clear(to) { // generate to squares
217219
to = furthestForward(c, t)
218220
m = NewCapture(from, to, BISHOP, brd.squares[to])
219-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
220-
winning.Push(SortItem{mvvLva(brd.squares[to], BISHOP), m})
221+
see = getSee(brd, from, to, brd.squares[to])
222+
if see >= 0 {
223+
winning.Push(SortItem{SortCapture(brd.squares[to], BISHOP, see), m})
221224
} else {
222-
losing.Push(SortItem{mvvLva(brd.squares[to], BISHOP), m})
225+
losing.Push(SortItem{SortCapture(brd.squares[to], BISHOP, see), m})
223226
}
224227
}
225228
}
@@ -229,10 +232,11 @@ func getCaptures(brd *Board, htable *HistoryTable, winning, losing *MoveList) {
229232
for t := (rookAttacks(occ, from) & enemy); t > 0; t.Clear(to) { // generate to squares
230233
to = furthestForward(c, t)
231234
m = NewCapture(from, to, ROOK, brd.squares[to])
232-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
233-
winning.Push(SortItem{mvvLva(brd.squares[to], ROOK), m})
235+
see = getSee(brd, from, to, brd.squares[to])
236+
if see >= 0 {
237+
winning.Push(SortItem{SortCapture(brd.squares[to], ROOK, see), m})
234238
} else {
235-
losing.Push(SortItem{mvvLva(brd.squares[to], ROOK), m})
239+
losing.Push(SortItem{SortCapture(brd.squares[to], ROOK, see), m})
236240
}
237241
}
238242
}
@@ -242,10 +246,11 @@ func getCaptures(brd *Board, htable *HistoryTable, winning, losing *MoveList) {
242246
for t := (queenAttacks(occ, from) & enemy); t > 0; t.Clear(to) { // generate to squares
243247
to = furthestForward(c, t)
244248
m = NewCapture(from, to, QUEEN, brd.squares[to])
245-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
246-
winning.Push(SortItem{mvvLva(brd.squares[to], QUEEN), m})
249+
see = getSee(brd, from, to, brd.squares[to])
250+
if see >= 0 {
251+
winning.Push(SortItem{SortCapture(brd.squares[to], QUEEN, see), m})
247252
} else {
248-
losing.Push(SortItem{mvvLva(brd.squares[to], QUEEN), m})
253+
losing.Push(SortItem{SortCapture(brd.squares[to], QUEEN, see), m})
249254
}
250255
}
251256
}
@@ -255,8 +260,9 @@ func getCaptures(brd *Board, htable *HistoryTable, winning, losing *MoveList) {
255260
for t := (kingMasks[from] & enemy); t > 0; t.Clear(to) { // generate to squares
256261
to = furthestForward(c, t)
257262
m = NewCapture(from, to, KING, brd.squares[to])
258-
if getSee(brd, from, to, brd.squares[to]) >= 0 { // Cannot move into check
259-
winning.Push(SortItem{mvvLva(brd.squares[to], KING), m})
263+
see = getSee(brd, from, to, brd.squares[to])
264+
if see >= 0 { // Cannot move into check
265+
winning.Push(SortItem{SortCapture(brd.squares[to], KING, see), m})
260266
}
261267
}
262268
}
@@ -322,13 +328,13 @@ func getWinningCaptures(brd *Board, htable *HistoryTable, winning *MoveList) {
322328
to = furthestForward(c, leftAttacks)
323329
from = to + pawnFromOffsets[c][OFF_LEFT]
324330
m = NewCapture(from, to, PAWN, brd.squares[to])
325-
winning.Push(SortItem{mvvLva(brd.squares[to], PAWN), m})
331+
winning.Push(SortItem{SortCapture(brd.squares[to], PAWN, 0), m})
326332
}
327333
for ; rightAttacks > 0; rightAttacks.Clear(to) {
328334
to = furthestForward(c, rightAttacks)
329335
from = to + pawnFromOffsets[c][OFF_RIGHT]
330336
m = NewCapture(from, to, PAWN, brd.squares[to])
331-
winning.Push(SortItem{mvvLva(brd.squares[to], PAWN), m})
337+
winning.Push(SortItem{SortCapture(brd.squares[to], PAWN, 0), m})
332338
}
333339
// en-passant captures
334340
if brd.enpTarget != SQ_INVALID {
@@ -341,17 +347,19 @@ func getWinningCaptures(brd *Board, htable *HistoryTable, winning *MoveList) {
341347
to = int(enpTarget) - 8
342348
}
343349
m = NewCapture(from, to, PAWN, PAWN)
344-
winning.Push(SortItem{mvvLva(PAWN, PAWN), m})
350+
winning.Push(SortItem{SortCapture(PAWN, PAWN, 0), m})
345351
}
346352
}
353+
var see int
347354
// Knights
348355
for f := brd.pieces[c][KNIGHT]; f > 0; f.Clear(from) {
349356
from = furthestForward(c, f)
350357
for t := (knightMasks[from] & enemy); t > 0; t.Clear(to) { // generate to squares
351358
to = furthestForward(c, t)
352359
m = NewCapture(from, to, KNIGHT, brd.squares[to])
353-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
354-
winning.Push(SortItem{mvvLva(brd.squares[to], KNIGHT), m})
360+
see = getSee(brd, from, to, brd.squares[to])
361+
if see >= 0 {
362+
winning.Push(SortItem{SortCapture(brd.squares[to], KNIGHT, see), m})
355363
}
356364
}
357365
}
@@ -361,8 +369,9 @@ func getWinningCaptures(brd *Board, htable *HistoryTable, winning *MoveList) {
361369
for t := (bishopAttacks(occ, from) & enemy); t > 0; t.Clear(to) { // generate to squares
362370
to = furthestForward(c, t)
363371
m = NewCapture(from, to, BISHOP, brd.squares[to])
364-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
365-
winning.Push(SortItem{mvvLva(brd.squares[to], BISHOP), m})
372+
see = getSee(brd, from, to, brd.squares[to])
373+
if see >= 0 {
374+
winning.Push(SortItem{SortCapture(brd.squares[to], BISHOP, see), m})
366375
}
367376
}
368377
}
@@ -372,8 +381,9 @@ func getWinningCaptures(brd *Board, htable *HistoryTable, winning *MoveList) {
372381
for t := (rookAttacks(occ, from) & enemy); t > 0; t.Clear(to) { // generate to squares
373382
to = furthestForward(c, t)
374383
m = NewCapture(from, to, ROOK, brd.squares[to])
375-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
376-
winning.Push(SortItem{mvvLva(brd.squares[to], ROOK), m})
384+
see = getSee(brd, from, to, brd.squares[to])
385+
if see >= 0 {
386+
winning.Push(SortItem{SortCapture(brd.squares[to], ROOK, see), m})
377387
}
378388
}
379389
}
@@ -383,8 +393,9 @@ func getWinningCaptures(brd *Board, htable *HistoryTable, winning *MoveList) {
383393
for t := (queenAttacks(occ, from) & enemy); t > 0; t.Clear(to) { // generate to squares
384394
to = furthestForward(c, t)
385395
m = NewCapture(from, to, QUEEN, brd.squares[to])
386-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
387-
winning.Push(SortItem{mvvLva(brd.squares[to], QUEEN), m})
396+
see = getSee(brd, from, to, brd.squares[to])
397+
if see >= 0 {
398+
winning.Push(SortItem{SortCapture(brd.squares[to], QUEEN, see), m})
388399
}
389400
}
390401
}
@@ -394,8 +405,9 @@ func getWinningCaptures(brd *Board, htable *HistoryTable, winning *MoveList) {
394405
for t := (kingMasks[from] & enemy); t > 0; t.Clear(to) { // generate to squares
395406
to = furthestForward(c, t)
396407
m = NewCapture(from, to, KING, brd.squares[to])
397-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
398-
winning.Push(SortItem{mvvLva(brd.squares[to], KING), m})
408+
see = getSee(brd, from, to, brd.squares[to])
409+
if see >= 0 {
410+
winning.Push(SortItem{SortCapture(brd.squares[to], KING, see), m})
399411
}
400412
}
401413
}
@@ -497,15 +509,15 @@ func getEvasions(brd *Board, htable *HistoryTable, winning, losing, remainingMov
497509
from = to + pawnFromOffsets[c][OFF_LEFT]
498510
if pinnedCanMove(brd, from, to, c, e) {
499511
m = NewCapture(from, to, PAWN, brd.squares[to])
500-
winning.Push(SortItem{mvvLva(brd.squares[to], PAWN), m})
512+
winning.Push(SortItem{SortCapture(brd.squares[to], PAWN, 0), m})
501513
}
502514
}
503515
for ; rightAttacks > 0; rightAttacks.Clear(to) {
504516
to = furthestForward(c, rightAttacks)
505517
from = to + pawnFromOffsets[c][OFF_RIGHT]
506518
if pinnedCanMove(brd, from, to, c, e) {
507519
m = NewCapture(from, to, PAWN, brd.squares[to])
508-
winning.Push(SortItem{mvvLva(brd.squares[to], PAWN), m})
520+
winning.Push(SortItem{SortCapture(brd.squares[to], PAWN, 0), m})
509521
}
510522
}
511523
// en-passant captures
@@ -523,9 +535,8 @@ func getEvasions(brd *Board, htable *HistoryTable, winning, losing, remainingMov
523535
// king in check.
524536
if (sqMaskOn[to]&defenseMap) > 0 && pinnedCanMove(brd, from, to, c, e) &&
525537
isPinned(brd, int(enpTarget), c, e)&sqMaskOn[to] > 0 {
526-
527538
m = NewCapture(from, to, PAWN, PAWN)
528-
winning.Push(SortItem{mvvLva(PAWN, PAWN), m})
539+
winning.Push(SortItem{SortCapture(PAWN, PAWN, 0), m})
529540
}
530541
}
531542
}
@@ -547,6 +558,7 @@ func getEvasions(brd *Board, htable *HistoryTable, winning, losing, remainingMov
547558
remainingMoves.Push(SortItem{htable.Probe(PAWN, c, to), m})
548559
}
549560
}
561+
var see int
550562
// Knights
551563
for f := brd.pieces[c][KNIGHT]; f > 0; f.Clear(from) {
552564
from = furthestForward(c, f) // Locate each knight for the side to move.
@@ -557,10 +569,11 @@ func getEvasions(brd *Board, htable *HistoryTable, winning, losing, remainingMov
557569
to = furthestForward(c, t)
558570
if sqMaskOn[to]&enemy > 0 {
559571
m = NewCapture(from, to, KNIGHT, brd.squares[to])
560-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
561-
winning.Push(SortItem{mvvLva(brd.squares[to], KNIGHT), m})
572+
see = getSee(brd, from, to, brd.squares[to])
573+
if see >= 0 {
574+
winning.Push(SortItem{SortCapture(brd.squares[to], KNIGHT, see), m})
562575
} else {
563-
losing.Push(SortItem{mvvLva(brd.squares[to], KNIGHT), m})
576+
losing.Push(SortItem{SortCapture(brd.squares[to], KNIGHT, see), m})
564577
}
565578
} else {
566579
m = NewRegularMove(from, to, KNIGHT)
@@ -577,10 +590,11 @@ func getEvasions(brd *Board, htable *HistoryTable, winning, losing, remainingMov
577590
if pinnedCanMove(brd, from, to, c, e) {
578591
if sqMaskOn[to]&enemy > 0 {
579592
m = NewCapture(from, to, BISHOP, brd.squares[to])
580-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
581-
winning.Push(SortItem{mvvLva(brd.squares[to], BISHOP), m})
593+
see = getSee(brd, from, to, brd.squares[to])
594+
if see >= 0 {
595+
winning.Push(SortItem{SortCapture(brd.squares[to], BISHOP, see), m})
582596
} else {
583-
losing.Push(SortItem{mvvLva(brd.squares[to], BISHOP), m})
597+
losing.Push(SortItem{SortCapture(brd.squares[to], BISHOP, see), m})
584598
}
585599
} else {
586600
m = NewRegularMove(from, to, BISHOP)
@@ -597,10 +611,11 @@ func getEvasions(brd *Board, htable *HistoryTable, winning, losing, remainingMov
597611
if pinnedCanMove(brd, from, to, c, e) {
598612
if sqMaskOn[to]&enemy > 0 {
599613
m = NewCapture(from, to, ROOK, brd.squares[to])
600-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
601-
winning.Push(SortItem{mvvLva(brd.squares[to], ROOK), m})
614+
see = getSee(brd, from, to, brd.squares[to])
615+
if see >= 0 {
616+
winning.Push(SortItem{SortCapture(brd.squares[to], ROOK, see), m})
602617
} else {
603-
losing.Push(SortItem{mvvLva(brd.squares[to], ROOK), m})
618+
losing.Push(SortItem{SortCapture(brd.squares[to], ROOK, see), m})
604619
}
605620
} else {
606621
m = NewRegularMove(from, to, ROOK)
@@ -617,10 +632,11 @@ func getEvasions(brd *Board, htable *HistoryTable, winning, losing, remainingMov
617632
if pinnedCanMove(brd, from, to, c, e) {
618633
if sqMaskOn[to]&enemy > 0 {
619634
m = NewCapture(from, to, QUEEN, brd.squares[to])
620-
if getSee(brd, from, to, brd.squares[to]) >= 0 {
621-
winning.Push(SortItem{mvvLva(brd.squares[to], QUEEN), m})
635+
see = getSee(brd, from, to, brd.squares[to])
636+
if see >= 0 {
637+
winning.Push(SortItem{SortCapture(brd.squares[to], QUEEN, see), m})
622638
} else {
623-
losing.Push(SortItem{mvvLva(brd.squares[to], QUEEN), m})
639+
losing.Push(SortItem{SortCapture(brd.squares[to], QUEEN, see), m})
624640
}
625641
} else {
626642
m = NewRegularMove(from, to, QUEEN)
@@ -637,7 +653,7 @@ func getEvasions(brd *Board, htable *HistoryTable, winning, losing, remainingMov
637653
if !isAttackedBy(brd, occ, to, e, c) && threatDir1 != directions[kingSq][to] &&
638654
threatDir2 != directions[kingSq][to] {
639655
m = NewCapture(kingSq, to, KING, brd.squares[to])
640-
winning.Push(SortItem{mvvLva(brd.squares[to], KING), m})
656+
winning.Push(SortItem{SortCapture(brd.squares[to], KING, 0), m})
641657
}
642658
}
643659
// King moves
@@ -802,40 +818,40 @@ func getChecks(brd *Board, htable *HistoryTable, remainingMoves *MoveList) {
802818
}
803819

804820
// // uncomment for movegen testing.
805-
806-
// func get_promotion_advances(brd *Board, winning, losing *MoveList, from, to int) {
821+
//
822+
// func getPromotionAdvances(brd *Board, winning, losing *MoveList, from, to int) {
807823
// var m Move
808-
// var sort uint64
824+
// var sort uint32
809825
// for pc := Piece(QUEEN); pc >= KNIGHT; pc-- {
810826
// m = NewMove(from, to, PAWN, EMPTY, pc)
811-
// sort = sort_promotion_advances(brd, from, to, pc)
827+
// sort = SortPromotionAdvances(brd, from, to, pc)
812828
// if sort >= SORT_WINNING_PROMOTION {
813-
// winning.Push(SortItem{sort})
829+
// winning.Push(SortItem{sort, m})
814830
// } else {
815-
// losing.Push(SortItem{sort})
831+
// losing.Push(SortItem{sort, m})
816832
// }
817833
// }
818834
// }
819835
//
820-
// func get_promotion_captures(brd *Board, winning *MoveList, from, to int, captured_piece Piece) {
836+
// func getPromotionCaptures(brd *Board, winning *MoveList, from, to int, captured_piece Piece) {
821837
// var m Move
822838
// for pc := Piece(QUEEN); pc >= KNIGHT; pc-- {
823839
// m = NewMove(from, to, PAWN, captured_piece, pc)
824-
// winning.Push(SortItem{sort_promotion_captures(brd, from, to, captured_piece, pc)})
840+
// winning.Push(SortItem{SortPromotionCaptures(brd, from, to, captured_piece, pc), m})
825841
// }
826842
// }
827843

828844
func getPromotionAdvances(brd *Board, winning, losing *MoveList, from, to int) {
829845
var m Move
830846
m = NewMove(from, to, PAWN, EMPTY, QUEEN)
831-
sort := sortPromotionAdvances(brd, from, to, QUEEN)
847+
sort := SortPromotionAdvances(brd, from, to, QUEEN)
832848
if sort >= SORT_WINNING_PROMOTION {
833849
winning.Push(SortItem{sort, m})
834850
} else {
835851
losing.Push(SortItem{sort, m})
836852
}
837853
m = NewMove(from, to, PAWN, EMPTY, KNIGHT)
838-
sort = sortPromotionAdvances(brd, from, to, KNIGHT)
854+
sort = SortPromotionAdvances(brd, from, to, KNIGHT)
839855
if sort >= SORT_WINNING_PROMOTION {
840856
winning.Push(SortItem{sort, m})
841857
} else {
@@ -844,8 +860,8 @@ func getPromotionAdvances(brd *Board, winning, losing *MoveList, from, to int) {
844860
}
845861

846862
func getPromotionCaptures(brd *Board, winning *MoveList, from, to int, capturedPiece Piece) {
847-
winning.Push(SortItem{sortPromotionCaptures(brd, from, to, capturedPiece, QUEEN),
863+
winning.Push(SortItem{SortPromotionCaptures(brd, from, to, capturedPiece, QUEEN),
848864
NewMove(from, to, PAWN, capturedPiece, QUEEN)})
849-
winning.Push(SortItem{sortPromotionCaptures(brd, from, to, capturedPiece, KNIGHT),
865+
winning.Push(SortItem{SortPromotionCaptures(brd, from, to, capturedPiece, KNIGHT),
850866
NewMove(from, to, PAWN, capturedPiece, KNIGHT)})
851867
}

0 commit comments

Comments
 (0)