File: probe.c

package info (click to toggle)
sjeng 11.2-8
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 952 kB
  • ctags: 710
  • sloc: ansic: 13,313; sh: 324; makefile: 83
file content (298 lines) | stat: -rw-r--r-- 6,395 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
    Sjeng - a chess variants playing program
    Copyright (C) 2000-2001 Gian-Carlo Pascutto

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, Gian-Carlo Pascutto gives permission
    to link this program with the Nalimov endgame database access
    code. See the Copying/Distribution section in the README file
    for more details.

    File: probe.c                                        
    Purpose: access Nalimov endgame databases

*/
#include "sjeng.h"
#include "protos.h"
#include "extvars.h"

#undef USE_EGTB

#define  XX  127
#define  KINGCAP 50000

typedef unsigned int INDEX;
typedef int square;

#if defined (_MSC_VER)
#define  TB_FASTCALL  __fastcall
#else
#define  TB_FASTCALL
#endif

typedef  int  color;
#define  x_colorWhite  0
#define  x_colorBlack  1
#define  x_colorNeutral  2
#define COLOR_DECLARED

typedef  int  piece;
#define  x_pieceNone    0
#define  x_piecePawn    1
#define  x_pieceKnight  2
#define  x_pieceBishop  3
#define  x_pieceRook    4
#define  x_pieceQueen   5
#define  x_pieceKing    6
#define PIECES_DECLARED

typedef signed char tb_t;

#define pageL       65536
#define tbbe_ssL    ((pageL-4)/2)
#define bev_broken  (tbbe_ssL+1)  /* illegal or busted */
#define bev_mi1     tbbe_ssL      /* mate in 1 move */
#define bev_mimin   1               /* mate in max moves */
#define bev_draw    0               /* draw */
#define bev_limax   (-1)            /* mated in max moves */
#define bev_li0     (-tbbe_ssL)   /* mated in 0 moves */

typedef INDEX (TB_FASTCALL * PfnCalcIndex)
    (square*, square*, square, int fInverse);

extern int IDescFindFromCounters (int*);
extern int FRegisteredFun (int, color);
extern PfnCalcIndex PfnIndCalcFun (int, color);
extern int TB_FASTCALL L_TbtProbeTable (int, color, INDEX);

extern int IInitializeTb (char*);
extern int FTbSetCacheSize(void *pv, unsigned long cbSize);

#define PfnIndCalc PfnIndCalcFun
#define FRegistered FRegisteredFun

int EGTBProbes;
int EGTBHits;
int EGTBPieces;
int EGTBCacheSize;
char EGTBDir[STR_BUFF];

void init_egtb(void)
{
#ifdef USE_EGTB
  void *buffer;

  buffer = malloc(EGTBCacheSize);

  if (buffer == NULL && (EGTBCacheSize != 0)) 
  {
	printf("Could not allocate EGTB buffer.\n");
	exit(EXIT_FAILURE);
  };

  EGTBPieces = IInitializeTb (EGTBDir);

  printf("%d piece endgame tablebases found\n", EGTBPieces);
  printf("Allocated %dKb for indices and tables.\n",((cbEGTBCompBytes+1023)/1024));
  
  if(FTbSetCacheSize (buffer, EGTBCacheSize) == FALSE
      && (EGTBCacheSize != 0))
  {
  	printf("Could not enable EGTB buffer.\n");
	exit(EXIT_FAILURE);
  };
  
  return;
#else
  return;
#endif
}

const static int EGTranslate(int sqidx)
{
  int r;

  r = (((rank(sqidx)-1)*8)+(file(sqidx)-1));
  
  return r;
}

int probe_egtb(void)
{
#ifdef USE_EGTB
  int *psqW, *psqB;
  int rgiCounters[10] = {0,0,0,0,0,0,0,0,0,0};
  int side;
  int fInvert;
  int sqEnP;
  int wi = 1, W[8] = {6,0,0,0,0,0};
  int bi = 1, B[8] = {6,0,0,0,0,0};
  int tbScore;
  INDEX ind;
  int j, a, i;
  int iTb;

  EGTBProbes++;
  
  W[4] = EGTranslate(wking_loc);
  B[4] = EGTranslate(bking_loc);

  for (j = 1, a = 1;(a <= piece_count); j++)
  {
  	i = pieces[j];
	if (!i)
		continue;
	else
		a++;
	switch(board[i])
	{
		case wpawn:
			rgiCounters[0]++;
			W[wi] = 1;
			W[wi+4] = EGTranslate(i);
			wi++;
			break;
		case wknight:
			rgiCounters[1]++;
			W[wi] = 2;
			W[wi+4] = EGTranslate(i);
			wi++;
			break;
		case wbishop:
			rgiCounters[2]++;
			W[wi] = 3;
			W[wi+4] = EGTranslate(i);
			wi++;
			break;
		case wrook:
			rgiCounters[3]++;
			W[wi] = 4;
			W[wi+4] = EGTranslate(i);
			wi++;
			break;
		case wqueen:
			rgiCounters[4]++;
			W[wi] = 5;
			W[wi+4] = EGTranslate(i);
			wi++;
			break;
		case bpawn:
			rgiCounters[5]++;
			B[bi] = 1;
			B[bi+4] = EGTranslate(i);
			bi++;
			break;
		case bknight:
			rgiCounters[6]++;
			B[bi] = 2;
			B[bi+4] = EGTranslate(i);
			bi++;
			break;
		case bbishop:
			rgiCounters[7]++;
			B[bi] = 3;
			B[bi+4] = EGTranslate(i);
			bi++;
			break;
		case brook:
			rgiCounters[8]++;
			B[bi] = 4;
			B[bi+4] = EGTranslate(i);
			bi++;
			break;
		case bqueen:
			rgiCounters[9]++;
			B[bi] = 5;
			B[bi+4] = EGTranslate(i);
			bi++;
			break;
	}
  }

  /* more than 4 pieces for one side: not a class we can index */
  if (wi >= 4 || bi >= 4)
  {
	  return KINGCAP;
  }

  iTb = IDescFindFromCounters (rgiCounters);
  if (0 == iTb)
  {
  	return KINGCAP;
  }
  else if (iTb > 0)
  {
  	/* white = 0*/
  	side = !white_to_move;
	fInvert = 0;
	psqW = W;
	psqB = B;
  }
  else
  {
  	side = white_to_move;
	fInvert = 1;
	psqW = B;
	psqB = W;
	iTb = -iTb;
  }
  if (!FRegistered(iTb, side))
  	return KINGCAP;
  
  if (ep_square == 0)
  {
  	sqEnP = XX;
  }
  else
  {
    	if (white_to_move)
	{
	  if (board[ep_square - 11] == wpawn || board[ep_square - 13] == wpawn)
  		sqEnP = EGTranslate(ep_square);
	  else
	    	sqEnP = XX;
	}
	else
	{
	  if (board[ep_square + 11] == bpawn || board[ep_square + 13] == bpawn)
  		sqEnP = EGTranslate(ep_square);
	  else
	    	sqEnP = XX;
	  
	}
  }

  ind = PfnIndCalc(iTb, side) (psqW, psqB, sqEnP, fInvert);

  tbScore = L_TbtProbeTable( iTb, side, ind);

  if (tbScore == bev_broken) return KINGCAP;

  EGTBHits++;
  
  if (tbScore > 0)
  {
  	return ((tbScore-bev_mi1)*2+INF-ply-1);
  }
  else if (tbScore < 0)
  {
  	return ((tbScore+bev_mi1)*2-INF+ply);
  }
  return 0;
#else
  return KINGCAP;
#endif
}