summaryrefslogtreecommitdiff
path: root/cras/src/dsp/tests/dsp_util_test.c
blob: 44f72575ebc63bdaf876c076ef3d59bbcf24a44c (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* Copyright 2016 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <math.h> /* for abs() */
#include <stdio.h> /* for printf() */
#include <string.h> /* for memset() */
#include <stdint.h> /* for uint64 definition */
#include <stdlib.h> /* for exit() definition */
#include <time.h> /* for clock_gettime */

#include "../drc_math.h"
#include "../dsp_util.h"

/* Constant for converting time to milliseconds. */
#define BILLION 1000000000LL
/* Number of iterations for performance testing. */
#define ITERATIONS 400000

#if defined(__aarch64__)
int16_t float_to_short(float a)
{
	int32_t ret;
	asm volatile("fcvtas %s[ret], %s[a]\n"
		     "sqxtn %h[ret], %s[ret]\n"
		     : [ret] "=w"(ret)
		     : [a] "w"(a)
		     :);
	return (int16_t)(ret);
}
#else
int16_t float_to_short(float a)
{
	a += (a >= 0) ? 0.5f : -0.5f;
	return (int16_t)(max(-32768, min(32767, a)));
}
#endif

void dsp_util_deinterleave_reference(int16_t *input, float *const *output,
				     int channels, int frames)
{
	float *output_ptr[channels];
	int i, j;

	for (i = 0; i < channels; i++)
		output_ptr[i] = output[i];

	for (i = 0; i < frames; i++)
		for (j = 0; j < channels; j++)
			*(output_ptr[j]++) = *input++ / 32768.0f;
}

void dsp_util_interleave_reference(float *const *input, int16_t *output,
				   int channels, int frames)
{
	float *input_ptr[channels];
	int i, j;

	for (i = 0; i < channels; i++)
		input_ptr[i] = input[i];

	for (i = 0; i < frames; i++)
		for (j = 0; j < channels; j++) {
			float f = *(input_ptr[j]++) * 32768.0f;
			*output++ = float_to_short(f);
		}
}

/* Use fixed size allocation to avoid performance fluctuation of allocation. */
#define MAXSAMPLES 4096
#define MINSAMPLES 256
/* PAD buffer to check for overflows. */
#define PAD 4096

void TestRounding(float in, int16_t expected, int samples)
{
	int i;
	int max_diff;
	int d;

	short *in_shorts = (short *)malloc(MAXSAMPLES * 2 * 2 + PAD);
	float *out_floats_left_c = (float *)malloc(MAXSAMPLES * 4 + PAD);
	float *out_floats_right_c = (float *)malloc(MAXSAMPLES * 4 + PAD);
	float *out_floats_left_opt = (float *)malloc(MAXSAMPLES * 4 + PAD);
	float *out_floats_right_opt = (float *)malloc(MAXSAMPLES * 4 + PAD);
	short *out_shorts_c = (short *)malloc(MAXSAMPLES * 2 * 2 + PAD);
	short *out_shorts_opt = (short *)malloc(MAXSAMPLES * 2 * 2 + PAD);

	memset(in_shorts, 0xfb, MAXSAMPLES * 2 * 2 + PAD);
	memset(out_floats_left_c, 0xfb, MAXSAMPLES * 4 + PAD);
	memset(out_floats_right_c, 0xfb, MAXSAMPLES * 4 + PAD);
	memset(out_floats_left_opt, 0xfb, MAXSAMPLES * 4 + PAD);
	memset(out_floats_right_opt, 0xfb, MAXSAMPLES * 4 + PAD);
	memset(out_shorts_c, 0xfb, MAXSAMPLES * 2 * 2 + PAD);
	memset(out_shorts_opt, 0xfb, MAXSAMPLES * 2 * 2 + PAD);

	float *out_floats_ptr_c[2];
	float *out_floats_ptr_opt[2];

	out_floats_ptr_c[0] = out_floats_left_c;
	out_floats_ptr_c[1] = out_floats_right_c;
	out_floats_ptr_opt[0] = out_floats_left_opt;
	out_floats_ptr_opt[1] = out_floats_right_opt;

	for (i = 0; i < MAXSAMPLES; ++i) {
		out_floats_left_c[i] = in;
		out_floats_right_c[i] = in;
	}

	/*  reference C interleave */
	dsp_util_interleave_reference(out_floats_ptr_c, out_shorts_c, 2,
				      samples);

	/* measure optimized interleave */
	for (i = 0; i < ITERATIONS; ++i) {
		dsp_util_interleave(out_floats_ptr_c, (uint8_t *)out_shorts_opt,
				    2, SND_PCM_FORMAT_S16_LE, samples);
	}

	max_diff = 0;
	for (i = 0; i < (MAXSAMPLES * 2 + PAD / 2); ++i) {
		d = abs(out_shorts_c[i] - out_shorts_opt[i]);
		if (d > max_diff) {
			max_diff = d;
		}
	}
	printf("test interleave compare %6d, %10f %13f %6d %6d %6d %s\n",
	       max_diff, in, in * 32768.0f, out_shorts_c[0], out_shorts_opt[0],
	       expected,
	       max_diff == 0 ? "PASS" :
			       (out_shorts_opt[0] == expected ?
					"EXPECTED DIFFERENCE" :
					"UNEXPECTED DIFFERENCE"));

	/* measure reference C deinterleave */
	dsp_util_deinterleave_reference(in_shorts, out_floats_ptr_c, 2,
					samples);

	/* measure optimized deinterleave */
	dsp_util_deinterleave((uint8_t *)in_shorts, out_floats_ptr_opt, 2,
			      SND_PCM_FORMAT_S16_LE, samples);

	d = memcmp(out_floats_ptr_c[0], out_floats_ptr_opt[0], samples * 4);
	if (d)
		printf("left compare %d, %f %f\n", d, out_floats_ptr_c[0][0],
		       out_floats_ptr_opt[0][0]);
	d = memcmp(out_floats_ptr_c[1], out_floats_ptr_opt[1], samples * 4);
	if (d)
		printf("right compare %d, %f %f\n", d, out_floats_ptr_c[1][0],
		       out_floats_ptr_opt[1][0]);

	free(in_shorts);
	free(out_floats_left_c);
	free(out_floats_right_c);
	free(out_floats_left_opt);
	free(out_floats_right_opt);
	free(out_shorts_c);
	free(out_shorts_opt);
}

int main(int argc, char **argv)
{
	float e = 0.000000001f;
	int samples = 16;

	dsp_enable_flush_denormal_to_zero();

	// Print headings for TestRounding output.
	printf("test interleave compare maxdif,     float,   float * 32k      "
	       "C   SIMD expect pass\n");

	// test clamping
	TestRounding(1.0f, 32767, samples);
	TestRounding(-1.0f, -32768, samples);
	TestRounding(1.1f, 32767, samples);
	TestRounding(-1.1f, -32768, samples);
	TestRounding(2000000000.f / 32768.f, 32767, samples);
	TestRounding(-2000000000.f / 32768.f, -32768, samples);

	/* Infinity produces zero on arm64. */
#if defined(__aarch64__)
#define EXPECTED_INF_RESULT 0
#define EXPECTED_NEGINF_RESULT 0
#elif defined(__i386__) || defined(__x86_64__)
#define EXPECTED_INF_RESULT -32768
#define EXPECTED_NEGINF_RESULT 0
#else
#define EXPECTED_INF_RESULT 32767
#define EXPECTED_NEGINF_RESULT -32768
#endif

	TestRounding(5000000000.f / 32768.f, EXPECTED_INF_RESULT, samples);
	TestRounding(-5000000000.f / 32768.f, EXPECTED_NEGINF_RESULT, samples);

	// test infinity
	union ieee754_float inf;
	inf.ieee.negative = 0;
	inf.ieee.exponent = 0xfe;
	inf.ieee.mantissa = 0x7fffff;
	TestRounding(inf.f, EXPECTED_INF_RESULT, samples); // expect fail
	inf.ieee.negative = 1;
	inf.ieee.exponent = 0xfe;
	inf.ieee.mantissa = 0x7fffff;
	TestRounding(inf.f, EXPECTED_NEGINF_RESULT, samples); // expect fail

	// test rounding
	TestRounding(0.25f, 8192, samples);
	TestRounding(-0.25f, -8192, samples);
	TestRounding(0.50f, 16384, samples);
	TestRounding(-0.50f, -16384, samples);
	TestRounding(1.0f / 32768.0f, 1, samples);
	TestRounding(-1.0f / 32768.0f, -1, samples);
	TestRounding(1.0f / 32768.0f + e, 1, samples);
	TestRounding(-1.0f / 32768.0f - e, -1, samples);
	TestRounding(1.0f / 32768.0f - e, 1, samples);
	TestRounding(-1.0f / 32768.0f + e, -1, samples);

	/* Rounding on 'tie' is different for Intel. */
#if defined(__i386__) || defined(__x86_64__)
	TestRounding(0.5f / 32768.0f, 0, samples); /* Expect round to even */
	TestRounding(-0.5f / 32768.0f, 0, samples);
#else
	TestRounding(0.5f / 32768.0f, 1, samples); /* Expect round away */
	TestRounding(-0.5f / 32768.0f, -1, samples);
#endif

	TestRounding(0.5f / 32768.0f + e, 1, samples);
	TestRounding(-0.5f / 32768.0f - e, 1, samples);
	TestRounding(0.5f / 32768.0f - e, 0, samples);
	TestRounding(-0.5f / 32768.0f + e, 0, samples);

	TestRounding(1.5f / 32768.0f, 2, samples);
	TestRounding(-1.5f / 32768.0f, -2, samples);
	TestRounding(1.5f / 32768.0f + e, 2, samples);
	TestRounding(-1.5f / 32768.0f - e, -2, samples);
	TestRounding(1.5f / 32768.0f - e, 1, samples);
	TestRounding(-1.5f / 32768.0f + e, -1, samples);

	/* Test denormals */
	union ieee754_float denorm;
	denorm.ieee.negative = 0;
	denorm.ieee.exponent = 0;
	denorm.ieee.mantissa = 1;
	TestRounding(denorm.f, 0, samples);
	denorm.ieee.negative = 1;
	denorm.ieee.exponent = 0;
	denorm.ieee.mantissa = 1;
	TestRounding(denorm.f, 0, samples);

	/* Test NaNs. Caveat Results vary by implementation. */
#if defined(__i386__) || defined(__x86_64__)
#define EXPECTED_NAN_RESULT -32768
#else
#define EXPECTED_NAN_RESULT 0
#endif
	union ieee754_float nan; /* Quiet NaN */
	nan.ieee.negative = 0;
	nan.ieee.exponent = 0xff;
	nan.ieee.mantissa = 0x400001;
	TestRounding(nan.f, EXPECTED_NAN_RESULT, samples);
	nan.ieee.negative = 0;
	nan.ieee.exponent = 0xff;
	nan.ieee.mantissa = 0x000001; /* Signalling NaN */
	TestRounding(nan.f, EXPECTED_NAN_RESULT, samples);

	/* Test Performance */
	uint64_t diff;
	struct timespec start, end;
	int i;
	int d;

	short *in_shorts = (short *)malloc(MAXSAMPLES * 2 * 2 + PAD);
	float *out_floats_left_c = (float *)malloc(MAXSAMPLES * 4 + PAD);
	float *out_floats_right_c = (float *)malloc(MAXSAMPLES * 4 + PAD);
	float *out_floats_left_opt = (float *)malloc(MAXSAMPLES * 4 + PAD);
	float *out_floats_right_opt = (float *)malloc(MAXSAMPLES * 4 + PAD);
	short *out_shorts_c = (short *)malloc(MAXSAMPLES * 2 * 2 + PAD);
	short *out_shorts_opt = (short *)malloc(MAXSAMPLES * 2 * 2 + PAD);

	memset(in_shorts, 0x11, MAXSAMPLES * 2 * 2 + PAD);
	memset(out_floats_left_c, 0x22, MAXSAMPLES * 4 + PAD);
	memset(out_floats_right_c, 0x33, MAXSAMPLES * 4 + PAD);
	memset(out_floats_left_opt, 0x44, MAXSAMPLES * 4 + PAD);
	memset(out_floats_right_opt, 0x55, MAXSAMPLES * 4 + PAD);
	memset(out_shorts_c, 0x66, MAXSAMPLES * 2 * 2 + PAD);
	memset(out_shorts_opt, 0x66, MAXSAMPLES * 2 * 2 + PAD);

	float *out_floats_ptr_c[2];
	float *out_floats_ptr_opt[2];

	out_floats_ptr_c[0] = out_floats_left_c;
	out_floats_ptr_c[1] = out_floats_right_c;
	out_floats_ptr_opt[0] = out_floats_left_opt;
	out_floats_ptr_opt[1] = out_floats_right_opt;

	/* Benchmark dsp_util_interleave */
	for (samples = MAXSAMPLES; samples >= MINSAMPLES; samples /= 2) {
		/* measure original C interleave */
		clock_gettime(CLOCK_MONOTONIC, &start); /* mark start time */
		for (i = 0; i < ITERATIONS; ++i) {
			dsp_util_interleave_reference(out_floats_ptr_c,
						      out_shorts_c, 2, samples);
		}
		clock_gettime(CLOCK_MONOTONIC, &end); /* mark the end time */
		diff = (BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec -
			start.tv_nsec) /
		       1000000;
		printf("interleave   ORIG size = %6d, elapsed time = %llu ms\n",
		       samples, (long long unsigned int)diff);

		/* measure optimized interleave */
		clock_gettime(CLOCK_MONOTONIC, &start); /* mark start time */
		for (i = 0; i < ITERATIONS; ++i) {
			dsp_util_interleave(out_floats_ptr_c,
					    (uint8_t *)out_shorts_opt, 2,
					    SND_PCM_FORMAT_S16_LE, samples);
		}
		clock_gettime(CLOCK_MONOTONIC, &end); /* mark the end time */
		diff = (BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec -
			start.tv_nsec) /
		       1000000;
		printf("interleave   SIMD size = %6d, elapsed time = %llu ms\n",
		       samples, (long long unsigned int)diff);

		/* Test C and SIMD output match */
		d = memcmp(out_shorts_c, out_shorts_opt,
			   MAXSAMPLES * 2 * 2 + PAD);
		if (d)
			printf("interleave compare %d, %d %d, %d %d\n", d,
			       out_shorts_c[0], out_shorts_c[1],
			       out_shorts_opt[0], out_shorts_opt[1]);
	}

	/* Benchmark dsp_util_deinterleave */
	for (samples = MAXSAMPLES; samples >= MINSAMPLES; samples /= 2) {
		/* Measure original C deinterleave */
		clock_gettime(CLOCK_MONOTONIC, &start); /* mark start time */
		for (i = 0; i < ITERATIONS; ++i) {
			dsp_util_deinterleave_reference(
				in_shorts, out_floats_ptr_c, 2, samples);
		}
		clock_gettime(CLOCK_MONOTONIC, &end); /* mark the end time */
		diff = (BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec -
			start.tv_nsec) /
		       1000000;
		printf("deinterleave ORIG size = %6d, "
		       "elapsed time = %llu ms\n",
		       samples, (long long unsigned int)diff);

		/* Measure optimized deinterleave */
		clock_gettime(CLOCK_MONOTONIC, &start); /* mark start time */
		for (i = 0; i < ITERATIONS; ++i) {
			dsp_util_deinterleave((uint8_t *)in_shorts,
					      out_floats_ptr_opt, 2,
					      SND_PCM_FORMAT_S16_LE, samples);
		}
		clock_gettime(CLOCK_MONOTONIC, &end); /* mark the end time */
		diff = (BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec -
			start.tv_nsec) /
		       1000000;
		printf("deinterleave SIMD size = %6d, elapsed time = %llu ms\n",
		       samples, (long long unsigned int)diff);

		/* Test C and SIMD output match */
		d = memcmp(out_floats_ptr_c[0], out_floats_ptr_opt[0],
			   samples * 4);
		if (d)
			printf("left compare %d, %f %f\n", d,
			       out_floats_ptr_c[0][0],
			       out_floats_ptr_opt[0][0]);
		d = memcmp(out_floats_ptr_c[1], out_floats_ptr_opt[1],
			   samples * 4);
		if (d)
			printf("right compare %d, %f %f\n", d,
			       out_floats_ptr_c[1][0],
			       out_floats_ptr_opt[1][0]);
	}

	free(in_shorts);
	free(out_floats_left_c);
	free(out_floats_right_c);
	free(out_floats_left_opt);
	free(out_floats_right_opt);
	free(out_shorts_c);
	free(out_shorts_opt);

	return 0;
}