This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransposition_table.go
97 lines (82 loc) · 2.11 KB
/
transposition_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package transposition
import (
"github.com/mhib/combusken/chess"
. "github.com/mhib/combusken/utils"
)
const NoneDepth = -6
var GlobalTransTable TranspositionTable
func ValueFromTrans(value int16, height int) int16 {
if value >= Mate-500 {
return value - int16(height)
}
if value <= -Mate+500 {
return value + int16(height)
}
return value
}
func ValueToTrans(value int, height int) int16 {
if value >= Mate-500 {
return int16(value + height)
}
if value <= -Mate+500 {
return int16(value - height)
}
return int16(value)
}
type flagPv uint8
func newFlagPv(flag int, pvNode bool) flagPv {
return flagPv(uint8(flag) | (uint8(BoolToInt(pvNode)) << 2))
}
func (f flagPv) getFlag() uint8 {
return uint8(f) & 3
}
func (f flagPv) isPvNode() bool {
return uint8(f)>>2 != 0
}
type transEntry struct {
key uint32
bestMove chess.Move
value int16
eval int16
flagPv flagPv
depth uint8
}
type TranspositionTable struct {
Entries []transEntry
Mask uint64
}
func (t *TranspositionTable) Clear() {
for i := range t.Entries {
t.Entries[i] = transEntry{}
}
}
func (t *TranspositionTable) Get(key uint64) (ok bool, value int16, eval int16, depth int16, move chess.Move, flag uint8, pvNode bool) {
var element = &t.Entries[key&t.Mask]
if element.key != uint32(key>>32) {
return
}
ok = true
value = element.value
eval = element.eval
depth = int16(element.depth) + NoneDepth
move = element.bestMove
flag = element.flagPv.getFlag()
pvNode = element.flagPv.isPvNode()
return
}
func (t *TranspositionTable) Set(key uint64, value int16, eval int16, depth int, bestMove chess.Move, flag int, pvNode bool) {
var element = &t.Entries[key&t.Mask]
adjustedKey := uint32(key >> 32)
if flag != TransExact && element.key == adjustedKey && (int(element.depth)+NoneDepth)/2 >= depth && element.flagPv.getFlag() != TransNone {
return
}
element.key = adjustedKey
element.value = value
element.eval = eval
element.flagPv = newFlagPv(flag, pvNode)
element.depth = uint8(depth - NoneDepth)
element.bestMove = bestMove
}
func (t *TranspositionTable) Prefetch(key uint64) {
prefetch(&t.Entries[key&t.Mask])
}