Warnings: format string issue.
[gnushogi.git] / gnushogi / sizetest.c
1 /*
2  * FILE: sizetest.c
3  *
4  *     Display memory usage of GNU Shogi data structures.
5  *
6  * ----------------------------------------------------------------------
7  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
9  * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
10  *
11  * GNU SHOGI is based on GNU CHESS
12  *
13  * Copyright (c) 1988, 1989, 1990 John Stanback
14  * Copyright (c) 1992 Free Software Foundation
15  *
16  * This file is part of GNU SHOGI.
17  *
18  * GNU Shogi is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU General Public License as published by the
20  * Free Software Foundation; either version 3 of the License,
21  * or (at your option) any later version.
22  *
23  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
24  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with GNU Shogi; see the file COPYING. If not, see
30  * <http://www.gnu.org/licenses/>.
31  * ----------------------------------------------------------------------
32  *
33  */
34
35 #include "gnushogi.h"
36
37 #include <signal.h>
38
39 struct leaf  *Tree, *root;
40
41 short FROMsquare, TOsquare;
42
43 small_short ChkFlag[MAXDEPTH], CptrFlag[MAXDEPTH], TesujiFlag[MAXDEPTH];
44 short Pscore[MAXDEPTH], Tscore[MAXDEPTH];
45 small_short Pindex[NO_SQUARES];
46
47 short mtl[2], hung[2];
48 small_short PieceCnt[2];
49
50 struct GameRec  *GameList;
51
52 char ColorStr[2][10];
53
54 long znodes;
55
56
57 /*
58  * In a networked enviroment gnushogi might be compiled on different hosts
59  * with different random number generators; that is not acceptable if they
60  * are going to share the same transposition table.
61  */
62
63 unsigned long next = 1;
64
65 unsigned int
66 urand(void)
67 {
68     next *= 1103515245;
69     next += 12345;
70     return ((unsigned int) (next >> 16) & 0xFFFF);
71 }
72
73
74 void
75 gsrand(unsigned int seed)
76 {
77     next = seed;
78 }
79
80
81 #if ttblsz
82 struct hashentry *ttable[2];
83 unsigned int ttblsize;
84 #endif
85
86 #ifdef BINBOOK
87 extern char *binbookfile;
88 #endif
89
90 extern char *bookfile;
91
92 char savefile[128] = "";
93 char listfile[128] = "";
94
95 #if defined HISTORY
96 unsigned short *history;
97 #endif
98
99 short rpthash[2][256];
100 short TrPnt[MAXDEPTH];
101 small_short PieceList[2][NO_SQUARES];
102 small_short PawnCnt[2][NO_COLS];
103 small_short Captured[2][NO_PIECES];
104 small_short Mvboard[NO_SQUARES];
105
106 #if !defined SAVE_SVALUE
107 short svalue[NO_SQUARES];
108 #endif
109
110 struct flags flag;
111
112 short opponent, computer, WAwindow, WBwindow, BAwindow, BBwindow, dither,
113     INCscore;
114 long ResponseTime, ExtraTime, MaxResponseTime, et, et0, time0, ft;
115 long GenCnt, NodeCnt, ETnodes, EvalNodes, HashCnt,
116     HashAdd, FHashCnt, FHashAdd,
117     HashCol, THashCol, filesz, hashmask, hashbase;
118 long replus, reminus;
119 short HashDepth = HASHDEPTH, HashMoveLimit = HASHMOVELIMIT;
120 short player, xwndw;
121 /*unsigned*/ short rehash; /* -1 is used as a flag --tpm */
122 short Sdepth, Game50, MaxSearchDepth;
123 short GameCnt = 0;
124 short contempt;
125 int Book;
126 struct TimeControlRec TimeControl;
127 int TCadd = 0;
128 short TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
129 short XCmoves[3], XCminutes[3], XCseconds[3], XC, XCmore;
130 const short otherside[3] = { white, black, neutral };
131 unsigned short hint;
132 short TOflag;       /* force search re-init if we backup search */
133
134 unsigned short killr0[MAXDEPTH], killr1[MAXDEPTH];
135 unsigned short killr2[MAXDEPTH], killr3[MAXDEPTH];
136 unsigned short PV, SwagHt, Swag0, Swag1, Swag2, Swag3, Swag4, sidebit;
137
138 small_short HasPiece[2][NO_PIECES];
139 const short kingP[3] = { 4, 76, 0 };
140
141 const long control[NO_PIECES] =
142 { 0, ctlP, ctlL, ctlN, ctlS, ctlG, ctlB, ctlR,
143   ctlPp, ctlLp, ctlNp, ctlSp, ctlBp, ctlRp, ctlK };
144
145 short stage, stage2;
146
147 FILE *hashfile;
148
149 unsigned int starttime;
150 short ahead = true, hash = true;
151
152
153 int timeopp[MINGAMEIN], timecomp[MINGAMEIN];
154 int compptr, oppptr;
155
156 void
157 TimeCalc()
158 {
159     /* adjust number of moves remaining in gamein games */
160     int increment = 0;
161     int topsum = 0;
162     int tcompsum = 0;
163     int me, him;
164     int i;
165
166     /* don't do anything til you have enough numbers */
167     if (GameCnt < (MINGAMEIN * 2))
168         return;
169
170     /* calculate average time in sec for last MINGAMEIN moves */
171     for (i = 0; i < MINGAMEIN; i++)
172     {
173         tcompsum += timecomp[i];
174         topsum += timeopp[i];
175     }
176
177     topsum /= (100 * MINGAMEIN);
178     tcompsum /= (100 * MINGAMEIN);
179     /* if I have less time than opponent add another move */
180     me = TimeControl.clock[computer] / 100;
181     him = TimeControl.clock[opponent] / 100;
182
183     if (me < him)
184         increment += 2;
185
186     if (((him - me) > 60) || ((me < him) && (me < 120)))
187         increment++;
188
189     /* if I am losing more time with each move add another */
190     /* if (!((me - him) > 60) && tcompsum > topsum) increment++; */
191
192     if (tcompsum > topsum)
193     {
194         increment += 2;
195     }
196     else if ((TimeControl.moves[computer] < MINMOVES) && !increment)
197     {
198         /* but don't let moves go below MINMOVES */
199         increment++;
200     }
201     else if ((me > him) && (tcompsum < topsum))
202     {
203         /* if I am doing really well use more time per move */
204         increment = -1;
205     }
206
207     TimeControl.moves[computer] += increment;
208 }
209
210
211
212 int
213 main(int argc, char **argv)
214 {
215     long l;
216     int  n;
217
218 #if ttblsz
219     l = (long)sizeof(struct hashentry);
220     n = (int)((l * (ttblsz + rehash) * 2) / 1000);
221     printf("ttable:\t\t%4d\tkByte\t[hashentry:%ld "
222            "* (ttblsz:%d + rehash:%d) * 2]\n",
223            n, l, ttblsz, rehash);
224 #endif
225
226 #if defined CACHE
227     l = (long)sizeof(struct etable);
228     n = (int)((l * (size_t)ETABLE) / 1000);
229 #else
230     l = n = 0;
231 #endif
232
233     printf("etab:\t\t%4d\tkByte\t[etable:%ld ETABLE:%d]\n",
234            n, l, ETABLE);
235
236     l = (long)sizeof(struct leaf);
237     n = (int)(l * TREE / 1000);
238     printf("Tree:\t\t%4d\tkByte\t[leaf:%ld * TREE:%d]\n",
239            n, l, TREE);
240
241 #if defined HISTORY
242     n = (int)(sizeof_history / 1000);
243 #else
244     n = 0;
245 #endif
246
247     printf("history:\t%4d\tkByte\t[unsigned short:%lu "
248            "* HISTORY_SIZE:%ld]\n",
249            n, sizeof(unsigned short), (long)HISTORY_SIZE);
250
251 #ifndef SAVE_NEXTPOS
252     l = (long)sizeof(next_array);
253     n = (int)((l * NO_PTYPE_PIECES) / 1000);
254
255     printf("nextpos:\t%4d\tkByte\t[next_array:%ld "
256            "* NO_PTYPE_PIECES:%d]\n",
257            n, l, NO_PTYPE_PIECES);
258
259     l = (long)sizeof(next_array);
260     n = (int)((l * NO_PTYPE_PIECES) / 1000);
261     printf("nextdir:\t%4d\tkByte\t[next_array:%ld "
262            "* NO_PTYPE_PIECES:%d]\n",
263            n, l, NO_PTYPE_PIECES);
264 #endif
265
266 #ifndef SAVE_DISTDATA
267     n = (int)(sizeof(distdata_array) / 1000);
268     printf("distdata:\t%4d\tkByte\n", n);
269 #endif
270
271 #ifndef SAVE_PTYPE_DISTDATA
272     l = (long)sizeof(distdata_array);
273     n = (int)((l * NO_PTYPE_PIECES) / 1000);
274     printf("ptype_distdata:\t%4d\tkByte\t[distdata_array:%ld "
275            "* NO_PTYPE_PIECES:%d]\n",
276            n, l, NO_PTYPE_PIECES);
277 #endif
278
279     l = (long)sizeof(hashcode_array);
280     n = (int)(l / 1000);
281     printf("hashcode:\t%4d\tkByte\t[hashval:%ld]\n",
282            n, (long)sizeof(struct hashval));
283
284     l = (long)sizeof(drop_hashcode_array);
285     n = (int)(l / 1000);
286     printf("drop_hashcode:\t%4d\tkByte\t[hashval:%ld]\n",
287            n, (long)sizeof(struct hashval));
288
289     l = (long)sizeof(value_array);
290     n = (int)(l / 1000);
291     printf("value:\t\t%4d\tkByte\n", n);
292
293     l = (long)sizeof(fscore_array);
294     n = (int)(l / 1000);
295     printf("fscore:\t\t%4d\tkByte\n", n);
296
297     return 0;
298 }
299