comparison spandsp-0.0.6pre17/tests/vector_float_tests.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * vector_float_tests.c
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2006 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: vector_float_tests.c,v 1.13 2009/04/26 07:00:39 steveu Exp $
26 */
27
28 #if defined(HAVE_CONFIG_H)
29 #include "config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <string.h>
36
37 #include "spandsp.h"
38
39 static void vec_copyf_dumb(float z[], const float x[], int n)
40 {
41 int i;
42
43 for (i = 0; i < n; i++)
44 z[i] = x[i];
45 }
46 /*- End of function --------------------------------------------------------*/
47
48 static int test_vec_copyf(void)
49 {
50 int i;
51 float x[100];
52 float za[100];
53 float zb[100];
54
55 printf("Testing vec_copyf()\n");
56 for (i = 0; i < 99; i++)
57 {
58 x[i] = i;
59 za[i] = -1.0f;
60 zb[i] = -1.0f;
61 }
62 vec_copyf_dumb(za + 3, x + 1, 0);
63 vec_copyf(zb + 3, x + 1, 0);
64 for (i = 0; i < 99; i++)
65 {
66 if (za[i] != zb[i])
67 {
68 printf("vec_copyf() - %d %f %f\n", i, za[i], zb[i]);
69 printf("Tests failed\n");
70 exit(2);
71 }
72 }
73 vec_copyf_dumb(za + 3, x + 1, 1);
74 vec_copyf(zb + 3, x + 1, 1);
75 for (i = 0; i < 99; i++)
76 {
77 if (za[i] != zb[i])
78 {
79 printf("vec_copyf() - %d %f %f\n", i, za[i], zb[i]);
80 printf("Tests failed\n");
81 exit(2);
82 }
83 }
84 vec_copyf_dumb(za + 3, x + 1, 29);
85 vec_copyf(zb + 3, x + 1, 29);
86 for (i = 0; i < 99; i++)
87 {
88 if (za[i] != zb[i])
89 {
90 printf("vec_copyf() - %d %f %f\n", i, za[i], zb[i]);
91 printf("Tests failed\n");
92 exit(2);
93 }
94 }
95 return 0;
96 }
97 /*- End of function --------------------------------------------------------*/
98
99 static void vec_negatef_dumb(float z[], const float x[], int n)
100 {
101 int i;
102
103 for (i = 0; i < n; i++)
104 z[i] = -x[i];
105 }
106 /*- End of function --------------------------------------------------------*/
107
108 static int test_vec_negatef(void)
109 {
110 int i;
111 float x[100];
112 float za[100];
113 float zb[100];
114
115 printf("Testing vec_negatef()\n");
116 for (i = 0; i < 99; i++)
117 {
118 x[i] = i;
119 za[i] = -1.0f;
120 zb[i] = -1.0f;
121 }
122 vec_negatef_dumb(za + 3, x + 1, 0);
123 vec_negatef(zb + 3, x + 1, 0);
124 for (i = 0; i < 99; i++)
125 {
126 if (za[i] != zb[i])
127 {
128 printf("vec_negatef() - %d %f %f\n", i, za[i], zb[i]);
129 printf("Tests failed\n");
130 exit(2);
131 }
132 }
133 vec_negatef_dumb(za + 3, x + 1, 1);
134 vec_negatef(zb + 3, x + 1, 1);
135 for (i = 0; i < 99; i++)
136 {
137 if (za[i] != zb[i])
138 {
139 printf("vec_megatef() - %d %f %f\n", i, za[i], zb[i]);
140 printf("Tests failed\n");
141 exit(2);
142 }
143 }
144 vec_negatef_dumb(za + 3, x + 1, 29);
145 vec_negatef(zb + 3, x + 1, 29);
146 for (i = 0; i < 99; i++)
147 {
148 if (za[i] != zb[i])
149 {
150 printf("vec_negatef() - %d %f %f\n", i, za[i], zb[i]);
151 printf("Tests failed\n");
152 exit(2);
153 }
154 }
155 return 0;
156 }
157 /*- End of function --------------------------------------------------------*/
158
159 static void vec_zerof_dumb(float z[], int n)
160 {
161 int i;
162
163 for (i = 0; i < n; i++)
164 z[i] = 0.0f;
165 }
166 /*- End of function --------------------------------------------------------*/
167
168 static int test_vec_zerof(void)
169 {
170 int i;
171 float za[100];
172 float zb[100];
173
174 printf("Testing vec_zerof()\n");
175 for (i = 0; i < 99; i++)
176 {
177 za[i] = -1.0f;
178 zb[i] = -1.0f;
179 }
180 vec_zerof_dumb(za + 3, 0);
181 vec_zerof(zb + 3, 0);
182 for (i = 0; i < 99; i++)
183 {
184 if (za[i] != zb[i])
185 {
186 printf("vec_zerof() - %d %f %f\n", i, za[i], zb[i]);
187 printf("Tests failed\n");
188 exit(2);
189 }
190 }
191 vec_zerof_dumb(za + 3, 1);
192 vec_zerof(zb + 3, 1);
193 for (i = 0; i < 99; i++)
194 {
195 if (za[i] != zb[i])
196 {
197 printf("vec_zerof() - %d %f %f\n", i, za[i], zb[i]);
198 printf("Tests failed\n");
199 exit(2);
200 }
201 }
202 vec_zerof_dumb(za + 3, 29);
203 vec_zerof(zb + 3, 29);
204 for (i = 0; i < 99; i++)
205 {
206 if (za[i] != zb[i])
207 {
208 printf("vec_zerof() - %d %f %f\n", i, za[i], zb[i]);
209 printf("Tests failed\n");
210 exit(2);
211 }
212 }
213 return 0;
214 }
215 /*- End of function --------------------------------------------------------*/
216
217 static void vec_setf_dumb(float z[], float x, int n)
218 {
219 int i;
220
221 for (i = 0; i < n; i++)
222 z[i] = x;
223 }
224 /*- End of function --------------------------------------------------------*/
225
226 static int test_vec_setf(void)
227 {
228 int i;
229 float za[100];
230 float zb[100];
231
232 printf("Testing vec_setf()\n");
233 for (i = 0; i < 99; i++)
234 {
235 za[i] = -1.0f;
236 zb[i] = -1.0f;
237 }
238 vec_setf_dumb(za + 3, 42.0f, 0);
239 vec_setf(zb + 3, 42.0f, 0);
240 for (i = 0; i < 99; i++)
241 {
242 if (za[i] != zb[i])
243 {
244 printf("vec_setf() - %d %f %f\n", i, za[i], zb[i]);
245 printf("Tests failed\n");
246 exit(2);
247 }
248 }
249 vec_setf_dumb(za + 3, 42.0f, 1);
250 vec_setf(zb + 3, 42.0f, 1);
251 for (i = 0; i < 99; i++)
252 {
253 if (za[i] != zb[i])
254 {
255 printf("vec_setf() - %d %f %f\n", i, za[i], zb[i]);
256 printf("Tests failed\n");
257 exit(2);
258 }
259 }
260 vec_setf_dumb(za + 3, 42.0f, 29);
261 vec_setf(zb + 3, 42.0f, 29);
262 for (i = 0; i < 99; i++)
263 {
264 if (za[i] != zb[i])
265 {
266 printf("vec_setf() - %d %f %f\n", i, za[i], zb[i]);
267 printf("Tests failed\n");
268 exit(2);
269 }
270 }
271 return 0;
272 }
273 /*- End of function --------------------------------------------------------*/
274
275 static double vec_dot_prod_dumb(const double x[], const double y[], int n)
276 {
277 int i;
278 double z;
279
280 z = 0.0;
281 for (i = 0; i < n; i++)
282 z += x[i]*y[i];
283 return z;
284 }
285 /*- End of function --------------------------------------------------------*/
286
287 static int test_vec_dot_prod(void)
288 {
289 int i;
290 double x[100];
291 double y[100];
292 double zsa;
293 double zsb;
294 double ratio;
295
296 printf("Testing vec_dot_prod()\n");
297 for (i = 0; i < 99; i++)
298 {
299 x[i] = rand();
300 y[i] = rand();
301 }
302 for (i = 1; i < 99; i++)
303 {
304 zsa = vec_dot_prod(x, y, i);
305 zsb = vec_dot_prod_dumb(x, y, i);
306 ratio = zsa/zsb;
307 if (ratio < 0.9999 || ratio > 1.0001)
308 {
309 printf("vec_dot_prod() - %f %f\n", zsa, zsb);
310 printf("Tests failed\n");
311 exit(2);
312 }
313 }
314 return 0;
315 }
316 /*- End of function --------------------------------------------------------*/
317
318 static float vec_dot_prodf_dumb(const float x[], const float y[], int n)
319 {
320 int i;
321 float z;
322
323 z = 0.0;
324 for (i = 0; i < n; i++)
325 z += x[i]*y[i];
326 return z;
327 }
328 /*- End of function --------------------------------------------------------*/
329
330 static int test_vec_dot_prodf(void)
331 {
332 int i;
333 float x[100];
334 float y[100];
335 float zsa;
336 float zsb;
337 float ratio;
338
339 printf("Testing vec_dot_prodf()\n");
340 for (i = 0; i < 99; i++)
341 {
342 x[i] = rand();
343 y[i] = rand();
344 }
345 for (i = 1; i < 99; i++)
346 {
347 zsa = vec_dot_prodf(x, y, i);
348 zsb = vec_dot_prodf_dumb(x, y, i);
349 ratio = zsa/zsb;
350 if (ratio < 0.9999f || ratio > 1.0001f)
351 {
352 printf("vec_dot_prodf() - %e %e\n", zsa, zsb);
353 printf("Tests failed\n");
354 exit(2);
355 }
356 }
357 return 0;
358 }
359 /*- End of function --------------------------------------------------------*/
360
361 static void vec_addf_dumb(float z[], const float x[], const float y[], int n)
362 {
363 int i;
364
365 for (i = 0; i < n; i++)
366 z[i] = x[i] + y[i];
367 }
368 /*- End of function --------------------------------------------------------*/
369
370 static int test_vec_addf(void)
371 {
372 int i;
373 int j;
374 float x[100];
375 float y[100];
376 float zsa[100];
377 float zsb[100];
378 float ratio;
379
380 printf("Testing vec_addf()\n");
381 for (i = 0; i < 99; i++)
382 {
383 x[i] = rand();
384 y[i] = rand();
385 }
386 for (i = 1; i < 90; i++)
387 {
388 /* Force address misalignment, to check this works OK */
389 vec_addf(zsa + 1, x + 1, y + 1, i);
390 vec_addf_dumb(zsb + 1, x + 1, y + 1, i);
391 for (j = 1; j <= i; j++)
392 {
393 ratio = zsa[j]/zsb[j];
394 if (ratio < 0.9999f || ratio > 1.0001f)
395 {
396 printf("vec_mulf() - %d %e %e\n", j, zsa[j], zsb[j]);
397 printf("Tests failed\n");
398 exit(2);
399 }
400 }
401 }
402 return 0;
403 }
404 /*- End of function --------------------------------------------------------*/
405
406 static void vec_subf_dumb(float z[], const float x[], const float y[], int n)
407 {
408 int i;
409
410 for (i = 0; i < n; i++)
411 z[i] = x[i] - y[i];
412 }
413 /*- End of function --------------------------------------------------------*/
414
415 static int test_vec_subf(void)
416 {
417 int i;
418 int j;
419 float x[100];
420 float y[100];
421 float zsa[100];
422 float zsb[100];
423 float ratio;
424
425 printf("Testing vec_subf()\n");
426 for (i = 0; i < 99; i++)
427 {
428 x[i] = rand();
429 y[i] = rand();
430 }
431 for (i = 1; i < 90; i++)
432 {
433 /* Force address misalignment, to check this works OK */
434 vec_subf(zsa + 1, x + 1, y + 1, i);
435 vec_subf_dumb(zsb + 1, x + 1, y + 1, i);
436 for (j = 1; j <= i; j++)
437 {
438 ratio = zsa[j]/zsb[j];
439 if (ratio < 0.9999f || ratio > 1.0001f)
440 {
441 printf("vec_mulf() - %d %e %e\n", j, zsa[j], zsb[j]);
442 printf("Tests failed\n");
443 exit(2);
444 }
445 }
446 }
447 return 0;
448 }
449 /*- End of function --------------------------------------------------------*/
450
451 static void vec_mulf_dumb(float z[], const float x[], const float y[], int n)
452 {
453 int i;
454
455 for (i = 0; i < n; i++)
456 z[i] = x[i]*y[i];
457 }
458 /*- End of function --------------------------------------------------------*/
459
460 static int test_vec_mulf(void)
461 {
462 int i;
463 int j;
464 float x[100];
465 float y[100];
466 float zsa[100];
467 float zsb[100];
468 float ratio;
469
470 printf("Testing vec_mulf()\n");
471 for (i = 0; i < 99; i++)
472 {
473 x[i] = rand();
474 y[i] = rand();
475 }
476 for (i = 1; i < 90; i++)
477 {
478 /* Force address misalignment, to check this works OK */
479 vec_mulf(zsa + 1, x + 1, y + 1, i);
480 vec_mulf_dumb(zsb + 1, x + 1, y + 1, i);
481 for (j = 1; j <= i; j++)
482 {
483 ratio = zsa[j]/zsb[j];
484 if (ratio < 0.9999f || ratio > 1.0001f)
485 {
486 printf("vec_mulf() - %d %e %e\n", j, zsa[j], zsb[j]);
487 printf("Tests failed\n");
488 exit(2);
489 }
490 }
491 }
492 return 0;
493 }
494 /*- End of function --------------------------------------------------------*/
495
496 #define LMS_LEAK_RATE 0.9999f
497
498 static void vec_lmsf_dumb(const float x[], float y[], int n, float error)
499 {
500 int i;
501
502 for (i = 0; i < n; i++)
503 {
504 /* Leak a little to tame uncontrolled wandering */
505 y[i] = y[i]*LMS_LEAK_RATE + x[i]*error;
506 }
507 }
508 /*- End of function --------------------------------------------------------*/
509
510 static int test_vec_lmsf(void)
511 {
512 int i;
513 int j;
514 float x[100];
515 float ya[100];
516 float yb[100];
517 float ratio;
518
519 printf("Testing vec_lmsf()\n");
520 for (i = 0; i < 99; i++)
521 {
522 x[i] = rand();
523 ya[i] =
524 yb[i] = rand();
525 }
526 for (i = 1; i < 99; i++)
527 {
528 vec_lmsf(x, ya, i, 0.1f);
529 vec_lmsf_dumb(x, yb, i, 0.1f);
530 for (j = 0; j < i; j++)
531 {
532 ratio = ya[j]/yb[j];
533 if (ratio < 0.9999f || ratio > 1.0001f)
534 {
535 printf("vec_lmsf() - %d %e %e\n", j, ya[j], yb[j]);
536 printf("Tests failed\n");
537 exit(2);
538 }
539 }
540 }
541 return 0;
542 }
543 /*- End of function --------------------------------------------------------*/
544
545 static void vec_scaledxy_addf_dumb(float z[], const float x[], float x_scale, const float y[], float y_scale, int n)
546 {
547 int i;
548
549 for (i = 0; i < n; i++)
550 z[i] = x[i]*x_scale + y[i]*y_scale;
551 }
552 /*- End of function --------------------------------------------------------*/
553
554 static int test_vec_scaledxy_addf(void)
555 {
556 int i;
557 int j;
558 float x[100];
559 float y[100];
560 float za[100];
561 float zb[100];
562 float ratio;
563
564 printf("Testing vec_scaledxy_addf()\n");
565 for (i = 0; i < 99; i++)
566 {
567 x[i] = rand();
568 y[i] = rand();
569 }
570 for (i = 1; i < 99; i++)
571 {
572 vec_scaledxy_addf(za, x, 2.5f, y, 1.5f, i);
573 vec_scaledxy_addf_dumb(zb, x, 2.5f, y, 1.5f, i);
574 for (j = 0; j < i; j++)
575 {
576 ratio = za[j]/zb[j];
577 if (ratio < 0.9999f || ratio > 1.0001f)
578 {
579 printf("vec_scaledxy_addf() - %d %e %e\n", j, za[j], zb[j]);
580 printf("Tests failed\n");
581 exit(2);
582 }
583 }
584 }
585 return 0;
586 }
587 /*- End of function --------------------------------------------------------*/
588
589 static void vec_scaledy_addf_dumb(float z[], const float x[], const float y[], float y_scale, int n)
590 {
591 int i;
592
593 for (i = 0; i < n; i++)
594 z[i] = x[i] + y[i]*y_scale;
595 }
596 /*- End of function --------------------------------------------------------*/
597
598 static int test_vec_scaledy_addf(void)
599 {
600 int i;
601 int j;
602 float x[100];
603 float y[100];
604 float za[100];
605 float zb[100];
606 float ratio;
607
608 printf("Testing vec_scaledy_addf()\n");
609 for (i = 0; i < 99; i++)
610 {
611 x[i] = rand();
612 y[i] = rand();
613 }
614 for (i = 1; i < 99; i++)
615 {
616 vec_scaledy_addf(za, x, y, 1.5f, i);
617 vec_scaledy_addf_dumb(zb, x, y, 1.5f, i);
618 for (j = 0; j < i; j++)
619 {
620 ratio = za[j]/zb[j];
621 if (ratio < 0.9999f || ratio > 1.0001f)
622 {
623 printf("vec_scaledy_addf() - %d %e %e\n", j, za[j], zb[j]);
624 printf("Tests failed\n");
625 exit(2);
626 }
627 }
628 }
629 return 0;
630 }
631 /*- End of function --------------------------------------------------------*/
632
633 int main(int argc, char *argv[])
634 {
635 test_vec_copyf();
636 test_vec_negatef();
637 test_vec_zerof();
638 test_vec_setf();
639 test_vec_addf();
640 test_vec_subf();
641 test_vec_mulf();
642 test_vec_scaledxy_addf();
643 test_vec_scaledy_addf();
644 test_vec_dot_prod();
645 test_vec_dot_prodf();
646 test_vec_lmsf();
647
648 printf("Tests passed.\n");
649 return 0;
650 }
651 /*- End of function --------------------------------------------------------*/
652 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.