Update codebase to remove clang warnings (and a couple of legit errors
[typhoon.git] / src / evalhash.c
1 /*++
2
3 Copyright (c) Scott Gasch
4
5 Module Name:
6
7     evalhash.c
8
9 Abstract:
10
11     Evaluation score hashing code.
12
13 Author:
14
15     Scott Gasch ([email protected]) 11 Jul 2006
16
17 Revision History:
18
19 --*/
20
21 #include "chess.h"
22 #ifdef EVAL_HASH
23
24 extern ULONG g_uIterateDepth;
25
26 SCORE 
27 GetRoughEvalScore(IN OUT SEARCHER_THREAD_CONTEXT *ctx, 
28                   IN SCORE iAlpha,
29                   IN SCORE iBeta,
30                   IN FLAG fUseHash)
31 /*++
32
33 Routine description:
34
35     This is meant to be the "one place" that code looks when it needs to
36     get an idea of the estimated evaluation of a position (in mid tree,
37     not at the leaves... at the leaves just call Eval directly).
38     
39     The thought is that:
40     
41     1. The top of the tree (near the root) represents very few nodes
42     which have big subtrees.  Use the real Eval here.
43     
44     2. Otherwise possibly probe the eval hash -- if there's a score in
45     it then we can return quickly.
46     
47     3. Otherwise do a (very) rough material estimate.
48
49 Parameters:
50
51     IN OUT SEARCHER_THREAD_CONTEXT *ctx,
52     IN SCORE iAlpha,
53     IN SCORE iBeta,
54     IN FLAG fUseHash
55
56 Return value:
57
58     SCORE
59
60 --*/
61 {
62     POSITION *pos = &(ctx->sPosition);
63     UINT64 u64Key;
64     ULONG u;
65
66     if (ctx->uPly <= 4)
67     {
68         return Eval(ctx, iAlpha, iBeta);
69     }
70     else if ((fUseHash) || (ctx->uPly <= (g_uIterateDepth / 2)))
71     {
72         u64Key = (pos->u64PawnSig ^ pos->u64NonPawnSig);
73         u = (ULONG)u64Key & (EVAL_HASH_TABLE_SIZE - 1);
74         if (ctx->rgEvalHash[u].u64Key == u64Key)
75         {
76             ctx->uPositional = ctx->rgEvalHash[u].uPositional;
77             return(ctx->rgEvalHash[u].iEval);
78         }
79     }
80     return(pos->iMaterialBalance[pos->uToMove] + ctx->uPositional);
81 }
82
83
84 SCORE 
85 ProbeEvalHash(IN OUT SEARCHER_THREAD_CONTEXT *ctx)
86 /*++
87
88 Routine description:
89
90     Probe the eval hash; return a real score if there's a hit
91     otherwise return INVALID_SCORE.
92
93 Parameters:
94
95     IN OUT SEARCHER_THREAD_CONTEXT *ctx : note that in the case
96         of a hit, *ctx is modified also.
97
98 Return value:
99
100     SCORE
101
102 --*/
103 {
104     POSITION *pos = &(ctx->sPosition);
105     UINT64 u64Key = (pos->u64PawnSig ^ pos->u64NonPawnSig);
106     ULONG u = (ULONG)u64Key & (EVAL_HASH_TABLE_SIZE - 1);
107
108     if (ctx->rgEvalHash[u].u64Key == u64Key)
109     {
110         ctx->uPositional = ctx->rgEvalHash[u].uPositional;
111         pos->cTrapped[WHITE] = ctx->rgEvalHash[u].cTrapped[WHITE];
112         pos->cTrapped[BLACK] = ctx->rgEvalHash[u].cTrapped[BLACK];
113         return(ctx->rgEvalHash[u].iEval);
114     }
115     return(INVALID_SCORE);
116 }
117
118 void 
119 StoreEvalHash(IN OUT SEARCHER_THREAD_CONTEXT *ctx, 
120               IN SCORE iScore)
121 /*++
122
123 Routine description:
124
125     Store a score in the eval hash table (which is pointed to
126     indirectly via ctx).
127
128 Parameters:
129
130     SEARCHER_THREAD_CONTEXT *ctx,
131     SCORE iScore
132
133 Return value:
134
135     void
136
137 --*/
138 {
139     POSITION *pos = &(ctx->sPosition);
140     UINT64 u64Key = (pos->u64PawnSig ^ pos->u64NonPawnSig);
141     ULONG u = (ULONG)u64Key & (EVAL_HASH_TABLE_SIZE - 1);
142     ctx->rgEvalHash[u].u64Key = u64Key;
143     ctx->rgEvalHash[u].iEval = iScore;
144     ctx->rgEvalHash[u].uPositional = ctx->uPositional;
145     ctx->rgEvalHash[u].cTrapped[WHITE] = pos->cTrapped[WHITE];
146     ctx->rgEvalHash[u].cTrapped[BLACK] = pos->cTrapped[BLACK];
147 }
148 #endif // EVAL_HASH