summaryrefslogtreecommitdiff
path: root/example-clients/simdtests.cpp
blob: 40b30e90db03262d47aec73ae4817cdca43e0e55 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 *  simdtests.c -- test accuracy and performance of simd optimizations
 *
 *  Copyright (C) 2017 Andreas Mueller.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* We must include all headers memops.c includes to avoid trouble with
 * out namespace game below.
 */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <memory.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#ifdef __linux__
#include <endian.h>
#endif
#include "memops.h"

#if defined (__SSE2__) && !defined (__sun__)
#include <emmintrin.h>
#ifdef __SSE4_1__
#include <smmintrin.h>
#endif
#endif

#if defined (__ARM_NEON__) || defined (__ARM_NEON)
#include <arm_neon.h>
#endif

// our additional headers
#include <time.h>

/* Dirty: include mempos.c twice the second time with SIMD disabled
 * so we can compare aceelerated non accelerated
 */
namespace accelerated {
#include "../common/memops.c"
}

namespace origerated {
#ifdef __SSE2__
#undef __SSE2__
#endif

#ifdef __ARM_NEON__
#undef __ARM_NEON__
#endif

#ifdef __ARM_NEON
#undef __ARM_NEON
#endif

#include "../common/memops.c"
}

// define conversion function types
typedef void (*t_jack_to_integer)(
	char *dst,
	jack_default_audio_sample_t *src,
	unsigned long nsamples,
	unsigned long dst_skip,
	dither_state_t *state);

typedef void (*t_integer_to_jack)(
	jack_default_audio_sample_t *dst,
	char *src,
	unsigned long nsamples,
	unsigned long src_skip);

// define/setup test case data
typedef struct test_case_data {
	uint32_t frame_size;
	uint32_t sample_size;
	bool reverse;
	t_jack_to_integer jack_to_integer_accel;
	t_jack_to_integer jack_to_integer_orig;
	t_integer_to_jack integer_to_jack_accel;
	t_integer_to_jack integer_to_jack_orig;
	dither_state_t *ditherstate;
	const char *name;
} test_case_data_t;

test_case_data_t test_cases[] = {
	{
		4,
		3,
		true,
		accelerated::sample_move_d32u24_sSs,
		origerated::sample_move_d32u24_sSs,
		accelerated::sample_move_dS_s32u24s,
		origerated::sample_move_dS_s32u24s,
		NULL,
		"32u24s" },
	{
		4,
		3,
		false,
		accelerated::sample_move_d32u24_sS,
		origerated::sample_move_d32u24_sS,
		accelerated::sample_move_dS_s32u24,
		origerated::sample_move_dS_s32u24,
		NULL,
		"32u24" },
	{
		4,
		3,
		true,
		accelerated::sample_move_d32l24_sSs,
		origerated::sample_move_d32l24_sSs,
		accelerated::sample_move_dS_s32l24s,
		origerated::sample_move_dS_s32l24s,
		NULL,
		"32l24s" },
	{
		4,
		3,
		false,
		accelerated::sample_move_d32l24_sS,
		origerated::sample_move_d32l24_sS,
		accelerated::sample_move_dS_s32l24,
		origerated::sample_move_dS_s32l24,
		NULL,
		"32l24" },
	{
		3,
		3,
		true,
		accelerated::sample_move_d24_sSs,
		origerated::sample_move_d24_sSs,
		accelerated::sample_move_dS_s24s,
		origerated::sample_move_dS_s24s,
		NULL,
		"24s" },
	{
		3,
		3,
		false,
		accelerated::sample_move_d24_sS,
		origerated::sample_move_d24_sS,
		accelerated::sample_move_dS_s24,
		origerated::sample_move_dS_s24,
		NULL,
		"24" },
	{
		2,
		2,
		true,
		accelerated::sample_move_d16_sSs,
		origerated::sample_move_d16_sSs,
		accelerated::sample_move_dS_s16s,
		origerated::sample_move_dS_s16s,
		NULL,
		"16s" },
	{
		2,
		2,
		false,
		accelerated::sample_move_d16_sS,
		origerated::sample_move_d16_sS,
		accelerated::sample_move_dS_s16,
		origerated::sample_move_dS_s16,
		NULL,
		"16" },
};

// we need to repeat for better accuracy at time measurement
const uint32_t retry_per_case = 1000;

// setup test buffers
#define TESTBUFF_SIZE 1024
jack_default_audio_sample_t jackbuffer_source[TESTBUFF_SIZE];
// integer buffers: max 4 bytes per value / * 2 for stereo
char integerbuffer_accel[TESTBUFF_SIZE*4*2];
char integerbuffer_orig[TESTBUFF_SIZE*4*2];
// float buffers
jack_default_audio_sample_t jackfloatbuffer_accel[TESTBUFF_SIZE];
jack_default_audio_sample_t jackfloatbuffer_orig[TESTBUFF_SIZE];

// comparing unsigned makes life easier
uint32_t extract_integer(
	char* buff,
	uint32_t offset,
	uint32_t frame_size,
	uint32_t sample_size,
	bool big_endian)
{
	uint32_t retval = 0;
	unsigned char* curr;
	uint32_t mult = 1;
	if(big_endian) {
		curr = (unsigned char*)buff + offset + sample_size-1;
		for(uint32_t i=0; i<sample_size; i++) {
			retval += *(curr--) * mult;
			mult*=256;
		}
	}
	else {
		curr = (unsigned char*)buff + offset + frame_size-sample_size;
		for(uint32_t i=0; i<sample_size; i++) {
			retval += *(curr++) * mult;
			mult*=256;
		}
	}
	return retval;
}

int main(int argc, char *argv[])
{
//	parse_arguments(argc, argv);
	uint32_t maxerr_displayed = 10;

	// fill jackbuffer
	for(int i=0; i<TESTBUFF_SIZE; i++) {
		// ramp
		jack_default_audio_sample_t value =
			((jack_default_audio_sample_t)((i % TESTBUFF_SIZE) - TESTBUFF_SIZE/2)) / (TESTBUFF_SIZE/2);
		// force clipping
		value *= 1.02;
		jackbuffer_source[i] = value;
	}

	for(uint32_t testcase=0; testcase<sizeof(test_cases)/sizeof(test_case_data_t); testcase++) {
		// test mono/stereo
		for(uint32_t channels=1; channels<=2; channels++) {
			//////////////////////////////////////////////////////////////////////////////
			// jackfloat -> integer

			// clean target buffers
			memset(integerbuffer_accel, 0, sizeof(integerbuffer_accel));
			memset(integerbuffer_orig, 0, sizeof(integerbuffer_orig));
			// accel
			clock_t time_to_integer_accel = clock();
			for(uint32_t repetition=0; repetition<retry_per_case; repetition++)
			{
				test_cases[testcase].jack_to_integer_accel(
					integerbuffer_accel,
					jackbuffer_source,
					TESTBUFF_SIZE,
					test_cases[testcase].frame_size*channels,
					test_cases[testcase].ditherstate);
			}
			float timediff_to_integer_accel = ((float)(clock() - time_to_integer_accel)) / CLOCKS_PER_SEC;
			// orig
			clock_t time_to_integer_orig = clock();
			for(uint32_t repetition=0; repetition<retry_per_case; repetition++)
			{
				test_cases[testcase].jack_to_integer_orig(
					integerbuffer_orig,
					jackbuffer_source,
					TESTBUFF_SIZE,
					test_cases[testcase].frame_size*channels,
					test_cases[testcase].ditherstate);
			}
			float timediff_to_integer_orig = ((float)(clock() - time_to_integer_orig)) / CLOCKS_PER_SEC;
			// output performance results
			printf(
				"JackFloat->Integer @%7.7s/%u: Orig %7.6f sec / Accel %7.6f sec -> Win: %5.2f %%\n",
				test_cases[testcase].name,
				channels,
				timediff_to_integer_orig,
				timediff_to_integer_accel,
				(timediff_to_integer_orig/timediff_to_integer_accel-1)*100.0);
			uint32_t int_deviation_max = 0;
			uint32_t int_error_count = 0;
			// output error (avoid spam -> limit error lines per test case)
			for(uint32_t sample=0; sample<TESTBUFF_SIZE; sample++) {
				uint32_t sample_offset = sample*test_cases[testcase].frame_size*channels;
				// compare both results
				uint32_t intval_accel=extract_integer(
					integerbuffer_accel,
					sample_offset,
					test_cases[testcase].frame_size,
					test_cases[testcase].sample_size,
#if __BYTE_ORDER == __BIG_ENDIAN
					!test_cases[testcase].reverse);
#else
					test_cases[testcase].reverse);
#endif
				uint32_t intval_orig=extract_integer(
					integerbuffer_orig,
					sample_offset,
					test_cases[testcase].frame_size,
					test_cases[testcase].sample_size,
#if __BYTE_ORDER == __BIG_ENDIAN
					!test_cases[testcase].reverse);
#else
					test_cases[testcase].reverse);
#endif
				// allow a deviation of 1
				if(intval_accel>intval_orig+1 || intval_orig>intval_accel+1) {
					if(int_error_count<maxerr_displayed) {
						printf("Value error sample %u:", sample);
						printf(" Orig 0x");
						char formatstr[10];
						sprintf(formatstr, "%%0%uX", test_cases[testcase].sample_size*2);
						printf(formatstr, intval_orig);
						printf(" Accel 0x");
						printf(formatstr, intval_accel);
						printf("\n");
					}
					int_error_count++;
					uint32_t int_deviation;
					if(intval_accel > intval_orig)
						int_deviation = intval_accel-intval_orig;
					else
						int_deviation = intval_orig-intval_accel;
					if(int_deviation > int_deviation_max)
						int_deviation_max = int_deviation;
				}
			}
			printf(
				"JackFloat->Integer @%7.7s/%u: Errors: %u Max deviation %u\n",
				test_cases[testcase].name,
				channels,
				int_error_count,
				int_deviation_max);

			//////////////////////////////////////////////////////////////////////////////
			// integer -> jackfloat

			// clean target buffers
			memset(jackfloatbuffer_accel, 0, sizeof(jackfloatbuffer_accel));
			memset(jackfloatbuffer_orig, 0, sizeof(jackfloatbuffer_orig));
			// accel
			clock_t time_to_float_accel = clock();
			for(uint32_t repetition=0; repetition<retry_per_case; repetition++)
			{
				test_cases[testcase].integer_to_jack_accel(
					jackfloatbuffer_accel,
					integerbuffer_orig,
					TESTBUFF_SIZE,
					test_cases[testcase].frame_size*channels);
			}
			float timediff_to_float_accel = ((float)(clock() - time_to_float_accel)) / CLOCKS_PER_SEC;
			// orig
			clock_t time_to_float_orig = clock();
			for(uint32_t repetition=0; repetition<retry_per_case; repetition++)
			{
				test_cases[testcase].integer_to_jack_orig(
					jackfloatbuffer_orig,
					integerbuffer_orig,
					TESTBUFF_SIZE,
					test_cases[testcase].frame_size*channels);
			}
			float timediff_to_float_orig = ((float)(clock() - time_to_float_orig)) / CLOCKS_PER_SEC;
			// output performance results
			printf(
				"Integer->JackFloat @%7.7s/%u: Orig %7.6f sec / Accel %7.6f sec -> Win: %5.2f %%\n",
				test_cases[testcase].name,
				channels,
				timediff_to_float_orig,
				timediff_to_float_accel,
				(timediff_to_float_orig/timediff_to_float_accel-1)*100.0);
			jack_default_audio_sample_t float_deviation_max = 0.0;
			uint32_t float_error_count = 0;
			// output error (avoid spam -> limit error lines per test case)
			for(uint32_t sample=0; sample<TESTBUFF_SIZE; sample++) {
				// For easier estimation/readability we scale floats back to integer
				jack_default_audio_sample_t sample_scaling;
				switch(test_cases[testcase].sample_size) {
					case 2:
						sample_scaling = SAMPLE_16BIT_SCALING;
						break;
					default:
						sample_scaling = SAMPLE_24BIT_SCALING;
						break;
				}
				jack_default_audio_sample_t floatval_accel = jackfloatbuffer_accel[sample] * sample_scaling;
				jack_default_audio_sample_t floatval_orig = jackfloatbuffer_orig[sample] * sample_scaling;
				// compare both results
				jack_default_audio_sample_t float_deviation;
				if(floatval_accel > floatval_orig)
					float_deviation = floatval_accel-floatval_orig;
				else
					float_deviation = floatval_orig-floatval_accel;
				if(float_deviation > float_deviation_max)
					float_deviation_max = float_deviation;
				// deviation > half bit => error
				if(float_deviation > 0.5) {
					if(float_error_count<maxerr_displayed) {
						printf("Value error sample %u:", sample);
						printf(" Orig %8.1f Accel %8.1f\n", floatval_orig, floatval_accel);
					}
					float_error_count++;
				}
			}
			printf(
				"Integer->JackFloat @%7.7s/%u: Errors: %u Max deviation %f\n",
				test_cases[testcase].name,
				channels,
				float_error_count,
				float_deviation_max);

			printf("\n");
		}
	}
	return 0;
}