1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <sys/stat.h>
4 
5 typedef enum {OPENING=0, MIDDLE, ENDING, MATING} GameStage;
6 #define __COEFFS_PER_STAGE__ (__TOTAL_COEFFS__/(MATING+1))
7 
8 typedef int etype;
9 #include "../eval.h"
10 #include "../names.h"
11 
12 #define READ_COEFFS 0
13 #define ZERO 1
14 
15 
16 char *stage_name[] = {"OPENING", "MIDDLE", "ENDING", "MATING"};
17 etype *coefficients;
18 etype total_coefficients[__TOTAL_COEFFS__];
19 
20 
p_coeff_vector(struct coefficient_name * cn,FILE * large,FILE * small)21 static void p_coeff_vector(struct coefficient_name *cn, FILE *large, FILE *small)
22 {
23 	int x;
24 	fprintf(large,"/* %s */\n", cn->name);
25 	if (small)
26 		fprintf(small,"/* %s */\n", cn->name);
27 	for (x=0; x<(cn+1)->index - cn->index; x++) {
28 		fprintf(large,"%7d,", coefficients[cn->index + x]);
29 		if (small)
30 			fprintf(small,"%7d,", coefficients[cn->index + x]/100);
31 	}
32 	fprintf(large,"\n");
33 	if (small)
34 		fprintf(small,"\n");
35 }
36 
p_coeff_array(struct coefficient_name * cn,FILE * large,FILE * small)37 static void p_coeff_array(struct coefficient_name *cn, FILE *large, FILE *small)
38 {
39 	int x;
40 	fprintf(large,"/* %s */\n", cn->name);
41 	if (small)
42 		fprintf(small,"/* %s */\n", cn->name);
43 	for (x=0; x<(cn+1)->index - cn->index; x++) {
44 		fprintf(large,"%7d,", coefficients[cn->index + x]);
45 		if (small)
46 			fprintf(small,"%7d,", coefficients[cn->index + x]/100);
47 		if ((x+1)%10 == 0) {
48 			fprintf(large, "\n");
49 			if (small)
50 				fprintf(small, "\n");
51 		}
52 	}
53 	fprintf(large,"\n");
54 	if (small)
55 		fprintf(small,"\n");
56 }
57 
58 
p_coeff_board(struct coefficient_name * cn,FILE * large,FILE * small)59 static void p_coeff_board(struct coefficient_name *cn, FILE *large, FILE *small)
60 {
61 	int x, y;
62 	fprintf(large,"/* %s */\n", cn->name);
63 	if (small)
64 		fprintf(small,"/* %s */\n", cn->name);
65 	for (y=0; y<8; y++) {
66 		for (x=0; x<8; x++) {
67 			fprintf(large,"%7d,", coefficients[cn->index + x + y*8]);
68 			if (small)
69 				fprintf(small,"%7d,", coefficients[cn->index + x + y*8]/100);
70 		}
71 		fprintf(large,"\n");
72 		if (small)
73 			fprintf(small,"\n");
74 	}
75 }
76 
p_coeff_half_board(struct coefficient_name * cn,FILE * large,FILE * small)77 static void p_coeff_half_board(struct coefficient_name *cn, FILE *large, FILE *small)
78 {
79 	int x, y;
80 	fprintf(large,"/* %s */\n", cn->name);
81 	if (small)
82 		fprintf(small,"/* %s */\n", cn->name);
83 	for (y=0; y<8; y++) {
84 		for (x=0; x<4; x++) {
85 			fprintf(large,"%7d,", coefficients[cn->index + x + y*4]);
86 			if (small)
87 				fprintf(small,"%7d,", coefficients[cn->index + x + y*4]/100);
88 		}
89 		fprintf(large,"\n");
90 		if (small)
91 			fprintf(small,"\n");
92 	}
93 }
94 
main()95 main()
96 {
97         struct stat st;
98         struct coefficient_name *cn;
99         FILE *large, *small;
100 	int i, fd;
101 	char *coeffs_file = "coeffs.dat";
102 
103 	large = (FILE *)fopen("large_coeffs.h", "w");
104 	small = (FILE *)fopen("small_coeffs.h", "w");
105 
106 	if (large == NULL) {
107 		exit(1);
108         }
109 
110 	if (READ_COEFFS) {
111 		fd = open("coeffs.dat", O_RDONLY);
112 		if (fd == -1) {
113 			printf("Failed to open %s\n", coeffs_file);
114 			exit(1);
115 		}
116 
117 		if (fstat(fd, &st) != 0 &&
118 		    st.st_size != __TOTAL_COEFFS__*sizeof(total_coefficients[0])) {
119 			printf("%s is corrupt\n", coeffs_file);
120 			exit(1);
121 		}
122 
123 		if (read(fd, (char *)total_coefficients,st.st_size) != st.st_size) {
124 			printf("failed to read coefficients\n");
125 			close(fd);
126 			exit(1);
127 		}
128 
129 		printf("Loaded coefficients from %s\n", coeffs_file);
130 		close(fd);
131 	}
132 
133 	if (ZERO)
134 		memset(total_coefficients, 0,
135 		       __TOTAL_COEFFS__*sizeof(total_coefficients[0]));
136 
137         fprintf(large, "etype orig_coefficients[] = {\n");
138 	if (small)
139 		fprintf(small, "etype orig_coefficients[] = {\n");
140 	for (i=OPENING; i<=MATING; i++) {
141 		fprintf(large, "\n/* %%%s%% */\n", stage_name[i]);
142 		if (small)
143 			fprintf(small, "\n/* %%%s%% */\n", stage_name[i]);
144 		cn = &coefficient_names[0];
145 		coefficients = 	total_coefficients + i*__COEFFS_PER_STAGE__;
146 		while (cn->name) {
147 			int n = cn[1].index - cn[0].index;
148 			if (n == 1) {
149 				fprintf(large, "/* %s */ %d,\n", cn[0].name,
150 					coefficients[cn[0].index]);
151 				if (small)
152 					fprintf(small, "/* %s */ %d,\n", cn[0].name,
153 						coefficients[cn[0].index]/100);
154 			} else if (n == 64) {
155 				p_coeff_board(cn,large,small);
156 			} else if (n == 32) {
157 				p_coeff_half_board(cn,large,small);
158 			} else if (n % 10 == 0) {
159 				p_coeff_array(cn,large,small);
160 			} else {
161 				p_coeff_vector(cn,large,small);
162 			}
163 			cn++;
164 		}
165 	}
166 
167         fprintf(large, "};\n");
168 	if (small)
169 		fprintf(small, "};\n");
170         fclose(large);
171 	if (small)
172 		fclose(small);
173 
174         return;
175 }
176 
177