summaryrefslogtreecommitdiff
path: root/src/evalhash.c
blob: fc29b885d0c14f291eb797631bfd498bf67fcb44 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*++

Copyright (c) Scott Gasch

Module Name:

    evalhash.c

Abstract:

    Evaluation score hashing code.

Author:

    Scott Gasch ([email protected]) 11 Jul 2006

Revision History:

--*/

#include "chess.h"

extern ULONG g_uIterateDepth;

SCORE
GetRoughEvalScore(IN OUT SEARCHER_THREAD_CONTEXT *ctx,
                  IN SCORE iAlpha,
                  IN SCORE iBeta,
                  IN FLAG fUseHash)
/*++

Routine description:

    This is meant to be the "one place" that code looks when it needs to
    get an idea of the estimated evaluation of a position (in mid tree,
    not at the leaves... at the leaves just call Eval directly).
    
    The thought is that:
    
    1. The top of the tree (near the root) represents very few nodes
    which have big subtrees.  Use the real Eval here.
    
    2. Otherwise possibly probe the eval hash -- if there's a score in
    it then we can return quickly.
    
    3. Otherwise do a (very) rough material estimate.

Parameters:

    IN OUT SEARCHER_THREAD_CONTEXT *ctx,
    IN SCORE iAlpha,
    IN SCORE iBeta,
    IN FLAG fUseHash

Return value:

    SCORE

--*/
{
    POSITION *pos = &(ctx->sPosition);
    UINT64 u64Key;
    ULONG u;

    if (ctx->uPly <= 4)
    {
        return Eval(ctx, iAlpha, iBeta, NULL);
    }
#ifdef EVAL_HASH
    else if ((fUseHash) || (ctx->uPly <= (g_uIterateDepth / 2)))
    {
        u64Key = (pos->u64PawnSig ^ pos->u64NonPawnSig);
        u = (ULONG)u64Key & (EVAL_HASH_TABLE_SIZE - 1);
        if (ctx->rgEvalHash[u].u64Key == u64Key)
        {
            switch(ctx->rgEvalHash[u].bvFlags)
            {
                case EVAL_HASH_EXACT:
                    ctx->uPositional = ctx->rgEvalHash[u].uPositional;
                    return(ctx->rgEvalHash[u].iEval);
                case EVAL_HASH_UPPER:
                    if (ctx->rgEvalHash[u].iBound <= iAlpha)
                    {
                        return(ctx->rgEvalHash[u].iEval);
                    }
                    break;
                case EVAL_HASH_LOWER:
                    if (ctx->rgEvalHash[u].iBound >= iBeta)
                    {
                        return(ctx->rgEvalHash[u].iEval);
                    }
                    break;
            }
        }
    }
#else
    (void)u64Key;
    (void)u;
    (void)fUseHash;
#endif
    return(pos->iMaterialBalance[pos->uToMove] + ctx->uPositional);
}

#ifdef EVAL_HASH


SCORE
ProbeEvalHash(IN OUT SEARCHER_THREAD_CONTEXT *ctx, IN SCORE iAlpha, IN SCORE iBeta)
/*++

Routine description:

    Probe the eval hash; return a real score if there's a hit
    otherwise return INVALID_SCORE.  An EXACT entry is always usable.
    An UPPER/LOWER entry (recorded from a lazy-eval early exit) is
    only usable if the bound it proved still resolves the caller's
    current alpha/beta window; otherwise it's treated as a miss and
    the caller must actually compute something.

Parameters:

    IN OUT SEARCHER_THREAD_CONTEXT *ctx : note that in the case
        of a hit, *ctx is modified also.
    IN SCORE iAlpha, IN SCORE iBeta : caller's current window

Return value:

    SCORE

--*/
{
    POSITION *pos = &(ctx->sPosition);
    UINT64 u64Key = (pos->u64PawnSig ^ pos->u64NonPawnSig);
    ULONG u = (ULONG)u64Key & (EVAL_HASH_TABLE_SIZE - 1);

    if (ctx->rgEvalHash[u].u64Key == u64Key)
    {
        switch(ctx->rgEvalHash[u].bvFlags)
        {
            case EVAL_HASH_EXACT:
                ctx->uPositional = ctx->rgEvalHash[u].uPositional;
                pos->cTrapped[WHITE] = ctx->rgEvalHash[u].cTrapped[WHITE];
                pos->cTrapped[BLACK] = ctx->rgEvalHash[u].cTrapped[BLACK];
                return(ctx->rgEvalHash[u].iEval);
            case EVAL_HASH_UPPER:
                if (ctx->rgEvalHash[u].iBound <= iAlpha)
                {
                    return(ctx->rgEvalHash[u].iEval);
                }
                break;
            case EVAL_HASH_LOWER:
                if (ctx->rgEvalHash[u].iBound >= iBeta)
                {
                    return(ctx->rgEvalHash[u].iEval);
                }
                break;
        }
    }
    return(INVALID_SCORE);
}

void
StoreEvalHash(IN OUT SEARCHER_THREAD_CONTEXT *ctx,
              IN SCORE iScore,
              IN SCORE iBound,
              IN UCHAR bvFlags)
/*++

Routine description:

    Store a score in the eval hash table (which is pointed to
    indirectly via ctx).  bvFlags is EVAL_HASH_EXACT for a fully
    computed eval (iBound ignored) or EVAL_HASH_UPPER/EVAL_HASH_LOWER
    for a bound proven by a lazy-eval early exit (iScore is the
    uncorrected partial score that would be returned, iBound is the
    proven bound on the true eval).

Parameters:

    SEARCHER_THREAD_CONTEXT *ctx,
    SCORE iScore,
    SCORE iBound,
    UCHAR bvFlags

Return value:

    void

--*/
{
    POSITION *pos = &(ctx->sPosition);
    UINT64 u64Key = (pos->u64PawnSig ^ pos->u64NonPawnSig);
    ULONG u = (ULONG)u64Key & (EVAL_HASH_TABLE_SIZE - 1);
    ctx->rgEvalHash[u].u64Key = u64Key;
    ctx->rgEvalHash[u].iEval = iScore;
    ctx->rgEvalHash[u].iBound = iBound;
    ctx->rgEvalHash[u].bvFlags = bvFlags;
    if (bvFlags == EVAL_HASH_EXACT)
    {
        ctx->rgEvalHash[u].uPositional = ctx->uPositional;
        ctx->rgEvalHash[u].cTrapped[WHITE] = pos->cTrapped[WHITE];
        ctx->rgEvalHash[u].cTrapped[BLACK] = pos->cTrapped[BLACK];
    }
}
#endif // EVAL_HASH