aboutsummaryrefslogtreecommitdiff
path: root/tools/tool.h
blob: c6fafbc0a97217606df559e74a0d9a84459ad122 (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
#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"

static void log_stderr(const char *, void *);
static double timerun(void (*)(void));
static void writetable(const unsigned char *, int64_t, const char *);
static long long int generatetable(const char *, unsigned char **,
    char [static NISSY_SIZE_DATAID]);
static int getdata(const char *, unsigned char **, const char *);
static void gendata_run(const char *, uint64_t[static 21]);

static void
log_stderr(const char *str, void *unused)
{
	fprintf(stderr, "%s", str);
}

#ifdef _WIN32

#include <windows.h>

static double
timerun(void (*run)(void))
{
	LARGE_INTEGER freq, start, end;
	double tdiff;
	
	fflush(stdout);
	
	if (run == NULL) {
		printf("nothing to run!\n");
		fflush(stdout);
		return -1.0;
	}

	QueryPerformanceFrequency(&freq);
	QueryPerformanceCounter(&start);
	run();
	QueryPerformanceCounter(&end);
	
	tdiff = (double)(end.QuadPart - start.QuadPart) / freq.QuadPart;

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

	return tdiff;
}

#else

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;
}

#endif

static void
writetable(const unsigned 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 long long int
generatetable(
	const char *solver,
	unsigned char **buf,
	char dataid[static NISSY_SIZE_DATAID]
)
{
	long long int size, gensize;

	size = nissy_solverinfo(solver, dataid);
	if (size < 0) {
		printf("Error getting table size.\n");
		return -1;
	}

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

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

	return gensize;
}

static int
getdata(
	const char *solver,
	unsigned char **buf,
	const char *filename
) {
	long long int size, sizeread;
	FILE *f;
	char dataid[NISSY_SIZE_DATAID];

	if ((f = fopen(filename, "rb")) == NULL) {
		printf("Table file not found, generating it.\n");
		size = generatetable(solver, buf, dataid);
		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_solverinfo(solver, dataid);
		*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]
) {
	long long int size;
	char filename[1024], dataid[NISSY_SIZE_DATAID];
	unsigned char *buf;

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

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

gendata_run_finish:
	free(buf);
}

Generated with cgit - Back to sebastiano.tronto.net