aboutsummaryrefslogtreecommitdiffstats
path: root/tests/Transceiver52M/convolve_test.c
blob: 168c8474c7bc6153109f5bd79953603c0bebb64e (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "convolve.h"


// ---------------------------------------------------------------------------
// Misc utils
// ---------------------------------------------------------------------------

/* Generate some random values for testing */
static unsigned long rand_state = 0;

static void
rand_reset(void)
{
	rand_state = 0;
}

static unsigned long
rand_int(void)
{
	rand_state = (1103515245UL * rand_state + 12345UL) & 0x7fffffffUL;
	return rand_state;
}

static float
rand_float(void)
{
	union {
		uint32_t u;
		float f;
	} r;
	uint32_t u = rand_int();
	int e = 112 + ((u ^ (u>>8)) & 15);

	r.u  =  u & 0x007fffffUL;	// Mantissa
	r.u |= (u & 0x00800000UL) << 8;	// Sign
	r.u |= (e & 0xffUL) << 23;	// Exponent

	return r.f;
}

static void
gen_floats(float *vect, int len)
{
	int i;
	for (i = 0; i < len; i++)
		vect[i] = rand_float();
}

/* Show float vector data cut and paste friendly */
static void
dump_floats(float *vect, int len, char *name)
{
	int i;

	printf("static const float %s[] = {\n\t", name);
	for(i = 0; i < len; i++) {
		char *end;
		if (i == len-1)
			end = "\n";
		else if ((i&3) == 3)
			end = ",\n\t";
		else
			end = ", ";
		printf("%14.7ef%s", vect[i], end);
	}
	printf("};\n");
}

/* Compare float with tolerance of delta (absolute) and epsilon (relative) */
static int
compare_floats(const float *v0, const float *v1, int len, float delta, float epsilon)
{
	int i;

	for (i=0; i<len; i++)
	{
		float a = v0[i];
		float b = v1[i];

		if (fabsf(a - b) < delta)
			continue;

		if (fabsf(1.0f - (a/b)) < epsilon)
			continue;

		return 1;
	}

	return 0;
}


// ---------------------------------------------------------------------------
// Golden reference results
// ---------------------------------------------------------------------------

#include "convolve_test_golden.h"

enum test_type {
	CONV_REAL_BASE		= 0,
	CONV_REAL_OPT		= 1,
	CONV_COMPLEX_BASE	= 2,
	CONV_COMPLEX_OPT	= 3
};

struct test_data {
	enum test_type type;
	int h_len;
	const  float *y_ref;
};

static const char *type_name[] = {
	"real_base", "real_opt", "complex_base", "complex_opt",
};

static const struct test_data tests[] = {
	{ CONV_REAL_BASE,  4, y_ref_real_base_4 },
	{ CONV_REAL_BASE,  8, y_ref_real_base_8 },
	{ CONV_REAL_BASE, 12, y_ref_real_base_12 },
	{ CONV_REAL_BASE, 16, y_ref_real_base_16 },
	{ CONV_REAL_BASE, 20, y_ref_real_base_20 },
	{ CONV_REAL_BASE, 24, y_ref_real_base_24 },
	{ CONV_COMPLEX_BASE,  4, y_ref_complex_base_4 },
	{ CONV_COMPLEX_BASE,  8, y_ref_complex_base_8 },
	{ CONV_COMPLEX_BASE, 12, y_ref_complex_base_12 },
	{ CONV_COMPLEX_BASE, 16, y_ref_complex_base_16 },
	{ CONV_COMPLEX_BASE, 20, y_ref_complex_base_20 },
	{ CONV_COMPLEX_BASE, 24, y_ref_complex_base_24 },
	{ 0, 0, NULL },
};


// ---------------------------------------------------------------------------
// Main testing logic
// ---------------------------------------------------------------------------

struct test_vec
{
	float *x;
	float *h;
	float *y;

	int x_len;	/* These are in # of _floats_ ! */
	int h_len;	/* These are in # of _floats_ ! */
	int y_len;	/* These are in # of _floats_ ! */
};

/* Reset test vectors */
static void
test_vec_reset(struct test_vec *tv, int seed)
{
	rand_reset();

	memset(tv->x, 0, tv->x_len * sizeof(float));
	memset(tv->h, 0, tv->h_len * sizeof(float));
	memset(tv->y, 0, tv->y_len * sizeof(float));

	gen_floats(tv->x, tv->x_len);
	gen_floats(tv->h, tv->h_len);
}

/* Allocate test vectors */
static struct test_vec *
test_vec_alloc(int x_len, int h_len)
{
	struct test_vec *tv;

	tv = calloc(1, sizeof(struct test_vec));
	if (!tv)
		return NULL;

	tv->x_len = x_len;
	tv->h_len = h_len;
	tv->y_len = x_len;	/* Results can never be longer than x */

	tv->x = convolve_h_alloc(x_len);
	tv->h = convolve_h_alloc(h_len);
	tv->y = convolve_h_alloc(tv->y_len);

	test_vec_reset(tv, 0);

	return tv;
}

/* Release test vectors */
static void
test_vec_release(struct test_vec *tv)
{
	if (!tv)
		return;

	free(tv->x);
	free(tv->h);
	free(tv->y);

	free(tv);
}

/* Run convolution */
static int
run_convolve(struct test_vec *tv, int h_len, enum test_type type)
{
	int x_len;
	int start, len;

	test_vec_reset(tv, 0);

	/* Compute params that fit within our test vectors */
	x_len = tv->x_len / 2; /* float vs complex */
	start = h_len - 1;
	len   = x_len - start;

	/* Run implementation */
	switch (type) {
	case CONV_REAL_BASE:
		base_convolve_real(
			tv->x, x_len,
			tv->h, h_len,
			tv->y, tv->y_len,
			start, len
		);
		break;

	case CONV_REAL_OPT:
		convolve_real(
			tv->x, x_len,
			tv->h, h_len,
			tv->y, tv->y_len,
			start, len
		);
		break;

	case CONV_COMPLEX_BASE:
		base_convolve_complex(
			tv->x, x_len,
			tv->h, h_len,
			tv->y, tv->y_len,
			start, len
		);
		break;

	case CONV_COMPLEX_OPT:
		convolve_complex(
			tv->x, x_len,
			tv->h, h_len,
			tv->y, tv->y_len,
			start, len
		);
		break;
	}

	return len * 2;
}


int main(int argc, char *argv[])
{
	struct test_vec *tv;
	int gen_ref_mode = 0;
	char name[80];
	int i, j, len;

	convolve_init();

	/* Mode */
	gen_ref_mode = (argc == 2) && !strcmp("genref", argv[1]);

	/* Alloc test vectors */
		/* All *2 is to account for the facts all vectors are actually
		 * complex and need two floats */
	tv = test_vec_alloc(100*2, 25*2);

	/* Dump all input data to make sure we work off the same input data */
	if (!gen_ref_mode) {
		printf("==== TEST INPUT DATA ====\n");
		dump_floats(tv->x, tv->x_len, "x");
		dump_floats(tv->h, tv->h_len, "h");
		printf("\n");
		printf("\n");
	}

	/* Run through all the tests */
	if (!gen_ref_mode)
		printf("==== TEST  ====\n");

	for (i=0; tests[i].h_len; i++)
	{
		for (j=0; j<(gen_ref_mode ? 1 : 2); j++)
		{
			len = run_convolve(tv, tests[i].h_len, tests[i].type + j);

			snprintf(name, sizeof(name)-1, "y_ref_%s_%d", type_name[tests[i].type + j], tests[i].h_len);

			if (gen_ref_mode)
			{
				/* If in generate mode, output data */
				dump_floats(tv->y, len, name);
			} else {
				/* If in test mode, compare with data */
				printf("%s: %s\n",
					name, 
					compare_floats(tests[i].y_ref, tv->y, len, 1e-5f, 1e-5f) ? "FAIL" : "PASS"
				);
			}
		}
	}

	if (!gen_ref_mode) {
		printf("\n");
		printf("\n");
	}

	/* All done ! */
	test_vec_release(tv);

	return 0;
}