📄 ttable.c
字号:
HFileSize = ftell (hashfile) / sizeof (struct fileentry);
}
}
void
CloseHashFile()
{
/* remember to write accumulated statistics if we keep such */
if (hashfile)
fclose (hashfile);
hashfile = NULL;
}
void
TestHashFile()
{
int iopendit = false;
if (!hashfile)
{
OpenHashFile();
iopendit = (hashfile != NULL);
}
if (hashfile)
{
int i;
long j;
long nr[MAXDEPTH+1];
struct fileentry n;
printf (CP[18]);
for (i = 0; i < MAXDEPTH; i++)
nr[i] = 0;
fseek (hashfile, 0L, SEEK_SET);
for (j = 0; j < HFileSize; j++)
{
fread (&n, sizeof (struct fileentry), 1, hashfile);
if (n.depth > MAXDEPTH){printf("ERROR\n");exit(1);}
if (n.depth)
{
nr[n.depth]++;
nr[0]++;
}
}
printf (CP[36], nr[0], HFileSize);
for (i = 1; i <= MAXDEPTH; i++)
if (nr[i])
printf (" %d:%ld", i,nr[i]);
printf ("\n");
if (iopendit)
CloseHashFile();
}
}
int
Fbdcmp(UCHAR *a,UCHAR *b)
{
register int i;
for(i = 0;i<32;i++)
if(a[i] != b[i])
return false;
return true;
}
int
ProbeFTable (SHORT side,
SHORT depth,
SHORT ply,
SHORT *alpha,
SHORT *beta,
SHORT *score)
/*
* Look for the current board position in the persistent transposition table.
*/
{
register SHORT i;
register unsigned long hashix;
struct fileentry new, t;
#ifdef DEBUG
if(flag.noft)return false;
#endif
hashix = ((side == white) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) % HFileSize;
for (i = 0; i < 32; i++)
new.bd[i] = CB (i);
new.flags = 0;
if (Mvboard[kingP[side]] == 0)
{
if (Mvboard[qrook[side]] == 0)
new.flags |= queencastle;
if (Mvboard[krook[side]] == 0)
new.flags |= kingcastle;
}
for (i = 0; i < frehash; i++)
{
fseek (hashfile,
sizeof (struct fileentry) * ((hashix + 2 * i) % (HFileSize)),
SEEK_SET);
fread (&t, sizeof (struct fileentry), 1, hashfile);
if (!t.depth) break;
if(!Fbdcmp(t.bd, new.bd)) continue;
if (((SHORT) t.depth >= depth)
&& (new.flags == (UTSHORT)(t.flags & (kingcastle | queencastle))))
{
#ifdef HASHSTATS
FHashCnt++;
#endif
PV = (t.f << 8) | t.t;
*score = (t.sh << 8) | t.sl;
/* adjust *score so moves to mate is from root */
if (*score > 9000)
*score -= ply;
else if (*score < -9000)
*score += ply;
if (t.flags & truescore)
{
*beta = -20000;
}
else if (t.flags & lowerbound)
{
if (*score > *alpha)
*alpha = *score - 1;
}
else if (t.flags & upperbound)
{
if (*score < *beta)
*beta = *score + 1;
}
return (true);
}
}
return (false);
}
void
PutInFTable (SHORT side,
SHORT score,
SHORT depth,
SHORT ply,
SHORT alpha,
SHORT beta,
UTSHORT f,
UTSHORT t)
/*
* Store the current board position in the persistent transposition table.
*/
{
register UTSHORT i;
register unsigned long hashix;
struct fileentry new, tmp;
hashix = ((side == white) ? (hashkey & 0xFFFFFFFE) : (hashkey | 1)) % HFileSize;
for (i = 0; i < 32; i++) new.bd[i] = CB (i);
new.f = (UCHAR) f;
new.t = (UCHAR) t;
if (score < alpha)
new.flags = upperbound;
else
new.flags = ((score > beta) ? lowerbound : truescore);
if (Mvboard[kingP[side]] == 0)
{
if (Mvboard[qrook[side]] == 0)
new.flags |= queencastle;
if (Mvboard[krook[side]] == 0)
new.flags |= kingcastle;
}
new.depth = (UCHAR) depth;
/* adjust *score so moves to mate is from root */
if (score > 9000)
score += ply;
else if (score < -9000)
score -= ply;
new.sh = (UCHAR) (score >> 8);
new.sl = (UCHAR) (score & 0xFF);
for (i = 0; i < frehash; i++)
{
fseek (hashfile,
sizeof (struct fileentry) * ((hashix + 2 * i) % (HFileSize)),
SEEK_SET);
if(fread (&tmp, sizeof (struct fileentry), 1, hashfile) == 0){perror("hashfile");exit(1);}
if (tmp.depth && !Fbdcmp(tmp.bd,new.bd))continue;
if ((SHORT) tmp.depth < new.depth)
{
fseek (hashfile,
sizeof (struct fileentry) * ((hashix + 2 * i) % (HFileSize)),
SEEK_SET);
fwrite (&new, sizeof (struct fileentry), 1, hashfile);
#ifdef HASHSTATS
FHashAdd++;
#endif
}
break;
}
}
#endif /* HASHFILE */
/********************** Repitition cache ***********************/
SHORT rpthash[2][256];
void
ZeroRPT (void)
{
memset ((CHAR *) rpthash, 0, sizeof (rpthash));
}
/********************** Hash code stuff ***********************/
/*
* In a networked enviroment gnuchess might be compiled on different hosts
* with different random number generators, that is not acceptable if they
* are going to share the same transposition table.
*/
unsigned long int next = 1;
#if defined NEWURAND
/*
This code copied from:
G. Wiesenekker. ZZZZZZ a chess program.
Copyright (C) 1993 G. Wiesenekker
E-mail: wiesenecker@sara.nl
A 32 bit random number generator. An implementation in C of the algorithm given by
Knuth, the art of computer programming, vol. 2, pp. 26-27. We use e=32, so
we have to evaluate y(n) = y(n - 24) + y(n - 55) mod 2^32, which is implicitly
done by unsigned arithmetic.
*/
unsigned int urand(void)
{
/*
random numbers from Mathematica 2.0.
SeedRandom = 1;
Table[Random[Integer, {0, 2^32 - 1}]
*/
static unsigned long x[55] =
{
1410651636UL,
3012776752UL,
3497475623UL,
2892145026UL,
1571949714UL,
3253082284UL,
3489895018UL,
387949491UL,
2597396737UL,
1981903553UL,
3160251843UL,
129444464UL,
1851443344UL,
4156445905UL,
224604922UL,
1455067070UL,
3953493484UL,
1460937157UL,
2528362617UL,
317430674UL,
3229354360UL,
117491133UL,
832845075UL,
1961600170UL,
1321557429UL,
747750121UL,
545747446UL,
810476036UL,
503334515UL,
4088144633UL,
2824216555UL,
3738252341UL,
3493754131UL,
3672533954UL,
29494241UL,
1180928407UL,
4213624418UL,
33062851UL,
3221315737UL,
1145213552UL,
2957984897UL,
4078668503UL,
2262661702UL,
65478801UL,
2527208841UL,
1960622036UL,
315685891UL,
1196037864UL,
804614524UL,
1421733266UL,
2017105031UL,
3882325900UL,
810735053UL,
384606609UL,
2393861397UL
};
static int init = TRUE;
static unsigned long y[55];
static int j, k;
unsigned long ul;
if (init)
{
int i;
init = FALSE;
for (i = 0; i < 55; i++)
y[i] = x[i];
j = 24 - 1;
k = 55 - 1;
}
ul = (y[k] += y[j]);
if (--j < 0) j = 55 - 1;
if (--k < 0) k = 55 - 1;
return((unsigned int)ul);
}
#else
unsigned int
urand (void)
{
next *= 1103515245;
next += 12345;
return ((unsigned int) (next >> 16) & 0xFFFF);
}
#endif
void
gsrand (unsigned int seed)
{
next = seed;
}
unsigned long hashkey, hashbd;
struct hashval hashcode[2][7][64];
#ifdef LONG64
#define XRANDTSIZE 8000
#else
#define XRANDTSIZE 4000
#endif
#if !defined NOXRAND
unsigned int
xrand (int a, unsigned int b[])
{
unsigned int i, r, r2, loop;
unsigned int j, c;
if (!a)
{
for (i = 0; i < XRANDTSIZE; i++)
b[i] = 0;
return 0;
}
loop = true;
while (loop)
{
r = r2 = urand ();
c = 0;
while(r2)
{
c += r2 & 1;
r2 >>= 1;
}
if (c < 8)
continue;
loop = false;
for (i = 0; i < a; i++)
if (r == b[i])
{
loop = true;
break;
}
if (!loop)
{
b[a] = r;
return r;
}
}
return 0;
}
#else
#define xrand(a,b) urand()
#endif
void
InitHashCode(unsigned int seed)
{
SHORT l, c, p;
unsigned int t[XRANDTSIZE];
int cnt = 0;
xrand(cnt++,t);
gsrand (seed); /* idealy we would preserve the old seed but... */
for (c = white; c <= black; c++)
for (p = pawn; p <= king; p++)
for (l = 0; l < 64; l++)
{
hashcode[c][p][l].key = (((unsigned long) xrand (cnt++,t)));
hashcode[c][p][l].key += (((unsigned long) xrand (cnt++,t)) << 16);
hashcode[c][p][l].bd = (((unsigned long) xrand (cnt++,t)));
hashcode[c][p][l].bd += (((unsigned long) xrand (cnt++,t)) << 16);
#ifdef LONG64
hashcode[c][p][l].key += (((unsigned long) xrand (cnt++,t)) << 32);
hashcode[c][p][l].key += (((unsigned long) xrand (cnt++,t)) << 48);
hashcode[c][p][l].bd += (((unsigned long) xrand (cnt++,t)) << 32);
hashcode[c][p][l].bd += (((unsigned long) xrand (cnt++,t)) << 48);
#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -