more eval tunings.
[rattatechess.git] / hash.cpp
blob6935594155eeb81ca4a50a86ae6f1e90c4a0bb8c
1 /***************************************************************************
2 hash.cpp - Hash related implementations
3 -------------------
4 begin : Mon Sep 19 2005
5 copyright : (C) 2005 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include "engine.h"
19 #include <string.h>
21 #define HSIZE (1<<22)
22 #define HMASK (HSIZE-1)
23 #define REHASH 3
25 void
26 HashKey::print() const
28 printf("HashKey( 0x%llxLL, 0x%x )\n", (long long)check, index);
31 void
32 Engine::create_hash()
34 hash_table = new HashEntry[HSIZE];
37 void
38 Engine::reset_hash()
40 memset(hash_table, 0, sizeof(HashEntry)*HSIZE);
43 void
44 Engine::make_old_hash()
46 for(int i=0;i<HSIZE;i++)
47 hash_table[i].is_old = 1;
50 /* is this useful? */
51 void
52 Engine::prefetch_hash(const HashKey& hk)
54 __builtin_prefetch( &hash_table[ (hk.index) & HMASK] );
55 __builtin_prefetch( &hash_table[ (hk.index+REHASH-1) & HMASK] );
58 HashEntry*
59 Engine::probe_hash(const HashKey& hk)
61 for(int i=0;i<REHASH;i++)
63 HashEntry* h = &hash_table[(hk.index + i) & HMASK];
64 if(!h->check)
65 return NULL;
66 if(h->check == hk.check)
68 h->is_old = 0;
69 return h;
72 return NULL;
75 void
76 Engine::write_hash(int16_t lo, int16_t up, int depth, int best_cont)
78 HashEntry* h = 0;
80 /* look for an empty entry or the same entry to update */
81 for(int i=0;i<REHASH;i++)
83 HashEntry* tmp = &hash_table[(board.hash.index + i) & HMASK];
84 if(!tmp->check || tmp->check == board.hash.check)
86 h = tmp;
87 break;
91 if(!h) /* look for an entry searched at lower depth */
93 int d = INF;
94 int lowest = -1;
95 for(int i=0;i<REHASH;i++)
97 HashEntry* tmp = &hash_table[(board.hash.index + i) & HMASK];
98 if(tmp->depth < d)
100 lowest = i;
101 d = tmp->depth;
105 if(d < depth)
106 h = &hash_table[(board.hash.index + lowest) & HMASK];
109 if(!h) /* look for an old entry and take the one searched at lowest depth */
111 int d = INF;
112 int lowest = -1;
113 for(int i=0;i<REHASH;i++)
115 HashEntry* tmp = &hash_table[(board.hash.index + i) & HMASK];
116 if(tmp->depth < d && tmp->is_old == 1)
118 lowest = i;
119 d = tmp->depth;
123 if(d != INF)
124 h = &hash_table[(board.hash.index + lowest) & HMASK];
127 if(!h)
128 return;
129 #if 0
130 if(h->check == board.hash.check && h->depth>depth)
131 return;
133 if(h->check == board.hash.check && h->depth==depth)
135 /* same entry, improve bounds */
136 h->up = MIN(h->up, up);
137 h->lo = MAX(h->lo, lo);
139 else
141 /* replace bounds */
142 h->up = up;
143 h->lo = lo;
145 #endif
147 h->up = up;
148 h->lo = lo;
150 h->check = board.hash.check;
151 h->depth = depth;
152 h->best_mv = best_cont;
153 h->is_old = 0;