File: lookup.c

package info (click to toggle)
crafty 15.20-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 6,952 kB
  • ctags: 2,538
  • sloc: ansic: 26,610; asm: 793; makefile: 101; csh: 15
file content (157 lines) | stat: -rw-r--r-- 5,959 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include "chess.h"
#include "data.h"

/* last modified 08/20/98 */
/*
********************************************************************************
*                                                                              *
*   LookUp() is used to retrieve entries from the transposition table so that  *
*   this sub-tree won't have to be searched again if we reach a position that  *
*   has been searched previously.  a transposition table position contains the *
*   following data packed into 128 bits with each item taking the number of    *
*   bits given in the table below:                                             *
*                                                                              *
*     bits     name  SL  description                                           *
*       3       age  61  search id to identify old trans/ref entried.          *
*       2      type  59  0->value is worthless; 1-> value represents a fail-   *
*                        low bound; 2-> value represents a fail-high bound;    *
*                        3-> value is an exact score.                          *
*       1    threat  58  threat extension flag, 1 -> extend this position.     *
*      16     value  21  unsigned integer value of this position + 65536.      *
*                        this might be a good score or search bound.           *
*      21      move   0  best move from the current position, according to the *
*                        search at the time this position was stored.          *
*                                                                              *
*      16     draft  48  the depth of the search below this position, which is *
*                        used to see if we can use this entry at the current   *
*                        position.  note that this is in units of 1/8th of a   *
*                        ply.                                                  *
*      48       key   0  leftmost 48 bits of the 64 bit hash key.  this is     *
*                        used to "verify" that this entry goes with the        *
*                        current board position.                               *
*                                                                              *
********************************************************************************
*/
int LookUp(TREE *tree, int ply, int depth, int wtm, int *alpha,
           int *beta, int *threat) {
  register BITBOARD temp_hash_key;
  register BITBOARD word1, word2;
  register HASH_ENTRY *htable;
  register int type, draft, avoid_null=WORTHLESS, val;
/*
 ----------------------------------------------------------
|                                                          |
|   first, compute the initial hash address and choose     |
|   which hash table (based on color) to probe.            |
|                                                          |
 ----------------------------------------------------------
*/
  tree->hash_move[ply]=0;
  temp_hash_key=HashKey>>16;
#if !defined(FAST)
  tree->transposition_probes++;
#endif
/*
 ----------------------------------------------------------
|                                                          |
|   now, check both "parts" of the hash table to see if    |
|   this position is in either one.                        |
|                                                          |
 ----------------------------------------------------------
*/
  htable=((wtm)?trans_ref_wa:trans_ref_ba)+(((int) HashKey) & hash_maska);
# if defined(SMP)
  Lock(lock_hasha);
#endif
  word1=htable->word1;
  word2=htable->word2;
# if defined(SMP)
  UnLock(lock_hasha);
#endif
  if (!Xor(And(word2,mask_80),temp_hash_key)) do {
#if !defined(FAST)
    tree->transposition_hits++;
#endif
    tree->hash_move[ply]=((int) word1) & 07777777;
    type=((int) Shiftr(word1,59)) & 03;
    draft=(int) Shiftr(word2,48);
    val=(((int) Shiftr(word1,21)) & 01777777)-65536;
    *threat=((int) Shiftr(word1,58)) & 01;
    if ((type & UPPER_BOUND) &&
        depth-NULL_MOVE_DEPTH-INCREMENT_PLY <= draft &&
        val < *beta) avoid_null=AVOID_NULL_MOVE;
    if (depth > draft) break;

    switch (type) {
      case EXACT_SCORE:
        if (abs(val) > MATE-100) {
          if (val > 0) val-=(ply-1);
          else val+=(ply-1);
        }
        *alpha=val;
        return(EXACT_SCORE);
      case UPPER_BOUND:
        if (val <= *alpha) {
          *alpha=val;
          return(UPPER_BOUND);
        }
        break;
      case LOWER_BOUND:
        if (val >= *beta) {
          *beta=val;
          return(LOWER_BOUND);
        }
        break;
    }
  } while(0);

  htable=((wtm)?trans_ref_wb:trans_ref_bb)+(((int) HashKey) & hash_maskb);
# if defined(SMP)
  Lock(lock_hashb);
#endif
  word1=htable->word1;
  word2=htable->word2;
# if defined(SMP)
  UnLock(lock_hashb);
#endif
  if (!Xor(And(word2,mask_80),temp_hash_key)) {
#if !defined(FAST)
    tree->transposition_hits++;
#endif
    if (tree->hash_move[ply]==0)
      tree->hash_move[ply]=((int) word1) & 07777777;
    type=((int) Shiftr(word1,59)) & 03;
    draft=Shiftr(word2,48);
    val=(((int) Shiftr(word1,21)) & 01777777)-65536;
    *threat=((int) Shiftr(word1,58)) & 01;
    if ((type & UPPER_BOUND) &&
        depth-NULL_MOVE_DEPTH-INCREMENT_PLY <= draft &&
        val < *beta) avoid_null=AVOID_NULL_MOVE;
    if (depth > draft) return(avoid_null);

    switch (type) {
      case EXACT_SCORE:
        if (abs(val) > MATE-100) {
          if (val > 0) val-=(ply-1);
          else val+=(ply-1);
        }
        *alpha=val;
        return(EXACT_SCORE);
      case UPPER_BOUND:
        if (val <= *alpha) {
          *alpha=val;
          return(UPPER_BOUND);
        }
        return(avoid_null);
      case LOWER_BOUND:
        if (val >= *beta) {
          *beta=val;
          return(LOWER_BOUND);
        }
        return(avoid_null);
    }
  }
  return(avoid_null);
}