More cleanups and some checking.
[rattatechess.git] / hash.cpp
blob5e5f065739ad622a18dbd357aaedf9e912b2cbaa
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 char* HashKey::to_string(char* buf) const
33 snprintf(buf, 64, "%08x%08x%08x",
34 check_hi, check_lo, index);
35 return buf;
38 void
39 Engine::create_hash()
41 hash_table = new HashEntry[HSIZE];
44 void
45 Engine::reset_hash()
47 memset(hash_table, 0, sizeof(HashEntry)*HSIZE);
50 void
51 Engine::make_old_hash()
53 for(int i=0;i<HSIZE;i++)
54 hash_table[i].is_old = 1;
57 /* is this useful? */
58 void
59 Engine::prefetch_hash(const HashKey& hk)
61 __builtin_prefetch( &hash_table[ (hk.index) & HMASK] );
62 __builtin_prefetch( &hash_table[ (hk.index+REHASH-1) & HMASK] );
65 HashEntry*
66 Engine::probe_hash(const HashKey& hk)
68 for(int i=0;i<REHASH;i++)
70 HashEntry* h = &hash_table[(hk.index + i) & HMASK];
71 if(!h->check)
72 return NULL;
73 if(h->check == hk.check)
75 h->is_old = 0;
76 return h;
79 return NULL;
82 void
83 Engine::write_hash(const HashKey& hk, int16_t lo, int16_t up, int depth, int best_cont, bool no_good_moves)
85 HashEntry* h = 0;
87 /* look for an empty entry or the same entry to update */
88 for(int i=0;i<REHASH;i++)
90 HashEntry* tmp = &hash_table[(hk.index + i) & HMASK];
91 if(!tmp->check || tmp->check == hk.check)
93 h = tmp;
94 break;
98 if(!h) /* look for an entry searched at lower depth */
100 int d = INF;
101 int lowest = -1;
102 for(int i=0;i<REHASH;i++)
104 HashEntry* tmp = &hash_table[(hk.index + i) & HMASK];
105 if(tmp->depth < d)
107 lowest = i;
108 d = tmp->depth;
112 if(d < depth)
113 h = &hash_table[(hk.index + lowest) & HMASK];
116 if(!h) /* look for an old entry and take the one searched at lowest depth */
118 int d = INF;
119 int lowest = -1;
120 for(int i=0;i<REHASH;i++)
122 HashEntry* tmp = &hash_table[(hk.index + i) & HMASK];
123 if(tmp->depth < d && tmp->is_old == 1)
125 lowest = i;
126 d = tmp->depth;
130 if(d != INF)
131 h = &hash_table[(hk.index + lowest) & HMASK];
134 if(!h)
135 return;
136 #if 0
137 if(h->check == hk.check && h->depth>depth)
138 return;
140 if(h->check == hk.check && h->depth==depth)
142 /* same entry, improve bounds */
143 h->up = MIN(h->up, up);
144 h->lo = MAX(h->lo, lo);
146 else
148 /* replace bounds */
149 h->up = up;
150 h->lo = lo;
152 #endif
154 h->up = up;
155 h->lo = lo;
157 h->check = hk.check;
158 h->depth = depth;
159 h->best_mv = best_cont;
160 h->no_good_moves = no_good_moves;
161 h->is_old = 0;