1 /*
2     Sjeng - a chess variants playing program
3     Copyright (C) 2000 Gian-Carlo Pascutto
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19     File: ecache.c
20     Purpose: handling of the evaluation cache
21 
22 */
23 
24 #include "sjeng.h"
25 #include "protos.h"
26 #include "extvars.h"
27 
28 typedef struct
29 {
30 unsigned long stored_hash;
31 unsigned long hold_hash;
32 unsigned int score;
33 } ECacheType;
34 
35 /*ECacheType ECache[ECACHESIZE];*/
36 ECacheType *ECache;
37 
38 unsigned long ECacheProbes;
39 unsigned long ECacheHits;
40 
storeECache(long int score)41 void storeECache(long int score)
42 {
43   int index;
44 
45   index = hash % ECacheSize;
46 
47   ECache[index].stored_hash = hash;
48   ECache[index].hold_hash = hold_hash;
49   ECache[index].score = score;
50 }
51 
checkECache(long int * score,int * in_cache)52 void checkECache(long int *score, int *in_cache)
53 {
54   int index;
55 
56   ECacheProbes++;
57 
58   index = hash % ECacheSize;
59 
60   if(ECache[index].stored_hash == hash &&
61 	  ECache[index].hold_hash == hold_hash)
62 
63     {
64       ECacheHits++;
65 
66       *in_cache = 1;
67       *score = ECache[index].score;
68     }
69 }
70 
reset_ecache(void)71 void reset_ecache(void)
72 {
73   memset(ECache, 0, sizeof(ECache));
74   return;
75 }
76 
alloc_ecache(void)77 void alloc_ecache(void)
78 {
79   ECache = (ECacheType*)malloc(sizeof(ECacheType)*ECacheSize);
80 
81   if (ECache == NULL)
82   {
83     printf("Out of memory allocating ECache.\n");
84     exit(EXIT_FAILURE);
85   }
86 
87   printf("Allocated %lu eval cache entries, totalling %lu bytes.\n",
88           ECacheSize, sizeof(ECacheType)*ECacheSize);
89   return;
90 }
91 
free_ecache(void)92 void free_ecache(void)
93 {
94   free(ECache);
95   return;
96 }
97