aboutsummaryrefslogtreecommitdiff
path: root/tools/tool.h
blob: 02ff0f2853ccab8fdef4113e88befa07297e5039 (plain)
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
#include <time.h>
#include <stdarg.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../src/nissy.h"
#include "nissy_extra.h"

static void log_stderr(const char *, ...);
static void log_stdout(const char *, ...);
static double timerun(void (*)(void));
static void writetable(const char *, int64_t, const char *);
static int64_t generatetable(const char *, char **);
static int64_t derivetable(const char *, const char *, const char *, char **);
static int getdata(const char *, char **, const char *);
static void gendata_run(const char *, uint64_t[static 21]);
static void derivedata_run(
    const char *, const char *, const char *, const char *);

static void
log_stderr(const char *str, ...)
{
	va_list args;

	va_start(args, str);
	vfprintf(stderr, str, args);
	va_end(args);
}

static void
write_stdout(const char *str, ...)
{
	va_list args;

	va_start(args, str);
	vfprintf(stdout, str, args);
	va_end(args);
}

static double
timerun(void (*run)(void))
{
	struct timespec start, end;
	double tdiff, tdsec, tdnano;

	fflush(stdout);

	if (run == NULL) {
		printf("nothing to run!\n");
		fflush(stdout);
		return -1.0;
	}

	clock_gettime(CLOCK_MONOTONIC, &start);
	run();
	clock_gettime(CLOCK_MONOTONIC, &end);

	tdsec = end.tv_sec - start.tv_sec;
	tdnano = end.tv_nsec - start.tv_nsec;
	tdiff = tdsec + 1e-9 * tdnano;

	printf("---------\n");
	printf("\nTotal time: %.4fs\n", tdiff);
	fflush(stdout);

	return tdiff;
}

static void
writetable(const char *buf, int64_t size, const char *filename)
{
	FILE *f;

	if ((f = fopen(filename, "wb")) == NULL) {
		printf("Could not write tables to file %s"
		    ", will be regenerated next time.\n", filename);
	} else {
		fwrite(buf, size, 1, f);
		fclose(f);
		printf("Table written to %s.\n", filename);
	}
}

static int64_t
generatetable(const char *solver, char **buf)
{
	int64_t size, gensize;

	size = nissy_datasize(solver);
	if (size == -1) {
		printf("Error getting table size.\n");
		return -1;
	}

	*buf = malloc(size);
	gensize = nissy_gendata(solver, *buf);

	if (gensize != size) {
		printf("Error generating table");
		if (gensize != -1)
			printf(" (got %" PRId64 " bytes)", gensize);
		printf("\n");
		return -2;
	}

	return gensize;
}

static int64_t
derivetable(
	const char *solver_large,
	const char *solver_small,
	const char *filename_large,
	char **buf
)
{
	uint8_t h, k;
	int64_t size, gensize;
	char *fulltable;

	if (getdata(solver_large, &fulltable, filename_large) != 0) {
		printf("Error reading full table.\n");
		gensize = -1;
		goto derivetable_error_nofree;
	}

	size = nissy_datasize(solver_small);
	if (size == -1) {
		printf("Error getting table size.\n");
		gensize = -2;
		goto derivetable_error;
	}

	if (parse_h48_solver(solver_small, &h, &k) != 0) {
		gensize = -3;
		goto derivetable_error;
	}

	*buf = malloc(size);
	gensize = gendata_h48_derive(h, fulltable, *buf);

	if (gensize != size) {
		printf("Error deriving table\n");
		gensize = -4;
		goto derivetable_error;
	}

derivetable_error:
	free(fulltable);

derivetable_error_nofree:
	return gensize;
}

static int
getdata(
	const char *solver,
	char **buf,
	const char *filename
) {
	int64_t size, sizeread;
	FILE *f;

	if ((f = fopen(filename, "rb")) == NULL) {
		printf("Table file not found, generating it.\n");
		size = generatetable(solver, buf);
		switch (size) {
		case -1:
			goto getdata_error_nofree;
		case -2:
			goto getdata_error;
		default:
			writetable(*buf, size, filename);
			break;
		}
	} else {
		printf("Reading tables from file %s\n", filename);
		size = nissy_datasize(solver);
		*buf = malloc(size);
		sizeread = fread(*buf, size, 1, f);
		fclose(f);
		if (sizeread != 1) {
			printf("Error reading table, stopping\n");
			goto getdata_error;
		}
	}

	return 0;

getdata_error:
	free(*buf);
getdata_error_nofree:
	return 1;
}

static void
gendata_run(
	const char *solver,
	uint64_t expected[static 21]
) {
	int64_t size;
	char *buf, filename[1024];

	sprintf(filename, "tables/%s", solver);
	size = generatetable(solver, &buf);
	switch (size) {
	case -1:
		return;
	case -2:
		goto gendata_run_finish;
	default:
		nissy_datainfo(buf, write_stdout);
		printf("\n");
		printf("Succesfully generated %" PRId64 " bytes. "
		       "See above for details on the tables.\n", size);

		/* TODO: check that the table is correct */
		writetable(buf, size, filename);
		break;
	}

gendata_run_finish:
	free(buf);
}

static void
derivedata_run(
	const char *solver_large,
	const char *solver_small,
	const char *filename_large,
	const char *filename_small
)
{
	int64_t size;
	char *buf;

	size = derivetable(solver_large, solver_small, filename_large, &buf);
	switch (size) {
	case -1:
		return;
	case -2:
		goto derivedata_run_finish;
	default:
		nissy_datainfo(buf, write_stdout);
		printf("\n");
		printf("Succesfully generated %" PRId64 " bytes. "
		       "See above for details on the tables.\n", size);

		writetable(buf, size, filename_small);
		break;
	}

derivedata_run_finish:
	free(buf);
}

Generated with cgit - Back to sebastiano.tronto.net