6 static int ehash_probe( uint64_t current_key, unsigned int hand_b,
8 static void ehash_store( uint64_t key, unsigned int hand_b, int score );
9 static int make_list( const tree_t * restrict ptree, int * restrict pscore,
10 int list0[52], int list1[52] );
13 eval_material( const tree_t * restrict ptree )
17 itemp = PopuCount( BB_BPAWN ) + (int)I2HandPawn( HAND_B );
18 itemp -= PopuCount( BB_WPAWN ) + (int)I2HandPawn( HAND_W );
19 material = itemp * p_value[15+pawn];
21 itemp = PopuCount( BB_BLANCE ) + (int)I2HandLance( HAND_B );
22 itemp -= PopuCount( BB_WLANCE ) + (int)I2HandLance( HAND_W );
23 material += itemp * p_value[15+lance];
25 itemp = PopuCount( BB_BKNIGHT ) + (int)I2HandKnight( HAND_B );
26 itemp -= PopuCount( BB_WKNIGHT ) + (int)I2HandKnight( HAND_W );
27 material += itemp * p_value[15+knight];
29 itemp = PopuCount( BB_BSILVER ) + (int)I2HandSilver( HAND_B );
30 itemp -= PopuCount( BB_WSILVER ) + (int)I2HandSilver( HAND_W );
31 material += itemp * p_value[15+silver];
33 itemp = PopuCount( BB_BGOLD ) + (int)I2HandGold( HAND_B );
34 itemp -= PopuCount( BB_WGOLD ) + (int)I2HandGold( HAND_W );
35 material += itemp * p_value[15+gold];
37 itemp = PopuCount( BB_BBISHOP ) + (int)I2HandBishop( HAND_B );
38 itemp -= PopuCount( BB_WBISHOP ) + (int)I2HandBishop( HAND_W );
39 material += itemp * p_value[15+bishop];
41 itemp = PopuCount( BB_BROOK ) + (int)I2HandRook( HAND_B );
42 itemp -= PopuCount( BB_WROOK ) + (int)I2HandRook( HAND_W );
43 material += itemp * p_value[15+rook];
45 itemp = PopuCount( BB_BPRO_PAWN );
46 itemp -= PopuCount( BB_WPRO_PAWN );
47 material += itemp * p_value[15+pro_pawn];
49 itemp = PopuCount( BB_BPRO_LANCE );
50 itemp -= PopuCount( BB_WPRO_LANCE );
51 material += itemp * p_value[15+pro_lance];
53 itemp = PopuCount( BB_BPRO_KNIGHT );
54 itemp -= PopuCount( BB_WPRO_KNIGHT );
55 material += itemp * p_value[15+pro_knight];
57 itemp = PopuCount( BB_BPRO_SILVER );
58 itemp -= PopuCount( BB_WPRO_SILVER );
59 material += itemp * p_value[15+pro_silver];
61 itemp = PopuCount( BB_BHORSE );
62 itemp -= PopuCount( BB_WHORSE );
63 material += itemp * p_value[15+horse];
65 itemp = PopuCount( BB_BDRAGON );
66 itemp -= PopuCount( BB_WDRAGON );
67 material += itemp * p_value[15+dragon];
74 evaluate( tree_t * restrict ptree, int ply, int turn )
76 int list0[52], list1[52];
77 int nlist, score, sq_bk, sq_wk, k0, k1, l0, l1, i, j, sum;
79 ptree->neval_called++;
81 if ( ptree->stand_pat[ply] != score_bound )
83 return (int)ptree->stand_pat[ply];
86 if ( ehash_probe( HASH_KEY, HAND_B, &score ) )
88 score = turn ? -score : score;
89 ptree->stand_pat[ply] = (short)score;
96 nlist = make_list( ptree, &score, list0, list1 );
98 sq_wk = Inv( SQ_WKING );
101 for ( i = 0; i < nlist; i++ )
105 for ( j = 0; j <= i; j++ )
109 assert( k0 >= l0 && k1 >= l1 );
110 sum += PcPcOnSq( sq_bk, k0, l0 );
111 sum -= PcPcOnSq( sq_wk, k1, l1 );
121 if ( sckt_mnj != SCKT_NULL ) { score += mnj_tbl[ HASH_KEY & MNJ_MASK ]; }
124 #if ! defined(MINIMUM)
125 if ( abs(score) > score_max_eval )
127 out_warning( "A score at evaluate() is out of bounce." );
131 ehash_store( HASH_KEY, HAND_B, score );
133 score = turn ? -score : score;
134 ptree->stand_pat[ply] = (short)score;
141 void ehash_clear( void )
143 memset( ehash_tbl, 0, sizeof(ehash_tbl) );
147 static int ehash_probe( uint64_t current_key, unsigned int hand_b,
150 uint64_t hash_word, hash_key;
152 hash_word = ehash_tbl[ (unsigned int)current_key & EHASH_MASK ];
154 #if ! defined(__x86_64__)
155 hash_word ^= hash_word << 32;
158 current_key ^= (uint64_t)hand_b << 16;
159 current_key &= ~(uint64_t)0xffffU;
161 hash_key = hash_word;
162 hash_key &= ~(uint64_t)0xffffU;
164 if ( hash_key != current_key ) { return 0; }
166 *pscore = (int)( (unsigned int)hash_word & 0xffffU ) - 32768;
172 static void ehash_store( uint64_t key, unsigned int hand_b, int score )
177 hash_word ^= (uint64_t)hand_b << 16;
178 hash_word &= ~(uint64_t)0xffffU;
179 hash_word |= (uint64_t)( score + 32768 );
181 #if ! defined(__x86_64__)
182 hash_word ^= hash_word << 32;
185 ehash_tbl[ (unsigned int)key & EHASH_MASK ] = hash_word;
190 make_list( const tree_t * restrict ptree, int * restrict pscore,
191 int list0[52], int list1[52] )
195 int nlist, sq, n2, i, score, sq_bk0, sq_wk0, sq_bk1, sq_wk1;
201 sq_bk1 = Inv(SQ_WKING);
202 sq_wk1 = Inv(SQ_BKING);
204 list0[ 0] = f_hand_pawn + I2HandPawn(HAND_B);
205 list0[ 1] = e_hand_pawn + I2HandPawn(HAND_W);
206 list0[ 2] = f_hand_lance + I2HandLance(HAND_B);
207 list0[ 3] = e_hand_lance + I2HandLance(HAND_W);
208 list0[ 4] = f_hand_knight + I2HandKnight(HAND_B);
209 list0[ 5] = e_hand_knight + I2HandKnight(HAND_W);
210 list0[ 6] = f_hand_silver + I2HandSilver(HAND_B);
211 list0[ 7] = e_hand_silver + I2HandSilver(HAND_W);
212 list0[ 8] = f_hand_gold + I2HandGold(HAND_B);
213 list0[ 9] = e_hand_gold + I2HandGold(HAND_W);
214 list0[10] = f_hand_bishop + I2HandBishop(HAND_B);
215 list0[11] = e_hand_bishop + I2HandBishop(HAND_W);
216 list0[12] = f_hand_rook + I2HandRook(HAND_B);
217 list0[13] = e_hand_rook + I2HandRook(HAND_W);
219 list1[ 0] = f_hand_pawn + I2HandPawn(HAND_W);
220 list1[ 1] = e_hand_pawn + I2HandPawn(HAND_B);
221 list1[ 2] = f_hand_lance + I2HandLance(HAND_W);
222 list1[ 3] = e_hand_lance + I2HandLance(HAND_B);
223 list1[ 4] = f_hand_knight + I2HandKnight(HAND_W);
224 list1[ 5] = e_hand_knight + I2HandKnight(HAND_B);
225 list1[ 6] = f_hand_silver + I2HandSilver(HAND_W);
226 list1[ 7] = e_hand_silver + I2HandSilver(HAND_B);
227 list1[ 8] = f_hand_gold + I2HandGold(HAND_W);
228 list1[ 9] = e_hand_gold + I2HandGold(HAND_B);
229 list1[10] = f_hand_bishop + I2HandBishop(HAND_W);
230 list1[11] = e_hand_bishop + I2HandBishop(HAND_B);
231 list1[12] = f_hand_rook + I2HandRook(HAND_W);
232 list1[13] = e_hand_rook + I2HandRook(HAND_B);
234 score += kkp[sq_bk0][sq_wk0][ kkp_hand_pawn + I2HandPawn(HAND_B) ];
235 score += kkp[sq_bk0][sq_wk0][ kkp_hand_lance + I2HandLance(HAND_B) ];
236 score += kkp[sq_bk0][sq_wk0][ kkp_hand_knight + I2HandKnight(HAND_B) ];
237 score += kkp[sq_bk0][sq_wk0][ kkp_hand_silver + I2HandSilver(HAND_B) ];
238 score += kkp[sq_bk0][sq_wk0][ kkp_hand_gold + I2HandGold(HAND_B) ];
239 score += kkp[sq_bk0][sq_wk0][ kkp_hand_bishop + I2HandBishop(HAND_B) ];
240 score += kkp[sq_bk0][sq_wk0][ kkp_hand_rook + I2HandRook(HAND_B) ];
242 score -= kkp[sq_bk1][sq_wk1][ kkp_hand_pawn + I2HandPawn(HAND_W) ];
243 score -= kkp[sq_bk1][sq_wk1][ kkp_hand_lance + I2HandLance(HAND_W) ];
244 score -= kkp[sq_bk1][sq_wk1][ kkp_hand_knight + I2HandKnight(HAND_W) ];
245 score -= kkp[sq_bk1][sq_wk1][ kkp_hand_silver + I2HandSilver(HAND_W) ];
246 score -= kkp[sq_bk1][sq_wk1][ kkp_hand_gold + I2HandGold(HAND_W) ];
247 score -= kkp[sq_bk1][sq_wk1][ kkp_hand_bishop + I2HandBishop(HAND_W) ];
248 score -= kkp[sq_bk1][sq_wk1][ kkp_hand_rook + I2HandRook(HAND_W) ];
252 while ( BBToU(bb) ) {
256 list0[nlist] = f_pawn + sq;
257 list2[n2] = e_pawn + Inv(sq);
258 score += kkp[sq_bk0][sq_wk0][ kkp_pawn + sq ];
264 while ( BBToU(bb) ) {
268 list0[nlist] = e_pawn + sq;
269 list2[n2] = f_pawn + Inv(sq);
270 score -= kkp[sq_bk1][sq_wk1][ kkp_pawn + Inv(sq) ];
274 for ( i = 0; i < n2; i++ ) { list1[nlist-i-1] = list2[i]; }
278 while ( BBToU(bb) ) {
282 list0[nlist] = f_lance + sq;
283 list2[n2] = e_lance + Inv(sq);
284 score += kkp[sq_bk0][sq_wk0][ kkp_lance + sq ];
290 while ( BBToU(bb) ) {
294 list0[nlist] = e_lance + sq;
295 list2[n2] = f_lance + Inv(sq);
296 score -= kkp[sq_bk1][sq_wk1][ kkp_lance + Inv(sq) ];
300 for ( i = 0; i < n2; i++ ) { list1[nlist-i-1] = list2[i]; }
305 while ( BBToU(bb) ) {
309 list0[nlist] = f_knight + sq;
310 list2[n2] = e_knight + Inv(sq);
311 score += kkp[sq_bk0][sq_wk0][ kkp_knight + sq ];
317 while ( BBToU(bb) ) {
321 list0[nlist] = e_knight + sq;
322 list2[n2] = f_knight + Inv(sq);
323 score -= kkp[sq_bk1][sq_wk1][ kkp_knight + Inv(sq) ];
327 for ( i = 0; i < n2; i++ ) { list1[nlist-i-1] = list2[i]; }
332 while ( BBToU(bb) ) {
336 list0[nlist] = f_silver + sq;
337 list2[n2] = e_silver + Inv(sq);
338 score += kkp[sq_bk0][sq_wk0][ kkp_silver + sq ];
344 while ( BBToU(bb) ) {
348 list0[nlist] = e_silver + sq;
349 list2[n2] = f_silver + Inv(sq);
350 score -= kkp[sq_bk1][sq_wk1][ kkp_silver + Inv(sq) ];
354 for ( i = 0; i < n2; i++ ) { list1[nlist-i-1] = list2[i]; }
359 while ( BBToU(bb) ) {
363 list0[nlist] = f_gold + sq;
364 list2[n2] = e_gold + Inv(sq);
365 score += kkp[sq_bk0][sq_wk0][ kkp_gold + sq ];
371 while ( BBToU(bb) ) {
375 list0[nlist] = e_gold + sq;
376 list2[n2] = f_gold + Inv(sq);
377 score -= kkp[sq_bk1][sq_wk1][ kkp_gold + Inv(sq) ];
381 for ( i = 0; i < n2; i++ ) { list1[nlist-i-1] = list2[i]; }
386 while ( BBToU(bb) ) {
390 list0[nlist] = f_bishop + sq;
391 list2[n2] = e_bishop + Inv(sq);
392 score += kkp[sq_bk0][sq_wk0][ kkp_bishop + sq ];
398 while ( BBToU(bb) ) {
402 list0[nlist] = e_bishop + sq;
403 list2[n2] = f_bishop + Inv(sq);
404 score -= kkp[sq_bk1][sq_wk1][ kkp_bishop + Inv(sq) ];
408 for ( i = 0; i < n2; i++ ) { list1[nlist-i-1] = list2[i]; }
413 while ( BBToU(bb) ) {
417 list0[nlist] = f_horse + sq;
418 list2[n2] = e_horse + Inv(sq);
419 score += kkp[sq_bk0][sq_wk0][ kkp_horse + sq ];
425 while ( BBToU(bb) ) {
429 list0[nlist] = e_horse + sq;
430 list2[n2] = f_horse + Inv(sq);
431 score -= kkp[sq_bk1][sq_wk1][ kkp_horse + Inv(sq) ];
435 for ( i = 0; i < n2; i++ ) { list1[nlist-i-1] = list2[i]; }
440 while ( BBToU(bb) ) {
444 list0[nlist] = f_rook + sq;
445 list2[n2] = e_rook + Inv(sq);
446 score += kkp[sq_bk0][sq_wk0][ kkp_rook + sq ];
452 while ( BBToU(bb) ) {
456 list0[nlist] = e_rook + sq;
457 list2[n2] = f_rook + Inv(sq);
458 score -= kkp[sq_bk1][sq_wk1][ kkp_rook + Inv(sq) ];
462 for ( i = 0; i < n2; i++ ) { list1[nlist-i-1] = list2[i]; }
467 while ( BBToU(bb) ) {
471 list0[nlist] = f_dragon + sq;
472 list2[n2] = e_dragon + Inv(sq);
473 score += kkp[sq_bk0][sq_wk0][ kkp_dragon + sq ];
479 while ( BBToU(bb) ) {
483 list0[nlist] = e_dragon + sq;
484 list2[n2] = f_dragon + Inv(sq);
485 score -= kkp[sq_bk1][sq_wk1][ kkp_dragon + Inv(sq) ];
489 for ( i = 0; i < n2; i++ ) { list1[nlist-i-1] = list2[i]; }
491 assert( nlist <= 52 );