OSDN Git Service

dbcc2665ceb35bdb33f2be49faf872b63fcb86c1
[libbfin32/algorithm_vector.git] / algorithm_vector / fx_vector_test.c
1 /*
2  * fx_vector_test.c
3  *
4  *  Created on: 2014/01/19
5  *      Author: takemasa
6  */
7
8 #include "fx_vector_test.h"
9 #include <stdio.h>
10
11
12 #define SATP(x)  (((x)>2147483647LL)?(2147483647LL):x)
13 #define SAT(x) (((x)>=-2147483648LL)?(SATP(x)):-2147483648LL)
14
15 #define mult_fr1x32x32(x,y) SAT(((long long )(x)*(long long)(y)+0x40000000)>>31)
16 #define neg_mult_fr1x32x32(x,y) SAT((-(long long )(x)*(long long)(y)+0x40000000)>>31)
17
18
19 void clearBuffer ( fract32 buf[], int count)
20 {
21     int i;
22     for ( i= 0; i<count; i++ )
23         buf[i] = 0;
24 }
25
26 /*
27  * Basic test to see vector + vector addition.
28  */
29 #define NUMSAMPLE_01 4
30
31 fract32 buf_a_01[NUMSAMPLE_01]=
32     {
33         0x08000000,     //
34         0x7FFFFFFF,     //
35         0x01000000,
36         0x20000000
37     };
38
39 fract32 buf_b_01[NUMSAMPLE_01] =
40     {
41         0x01000000,         //
42         0x00000001,         //
43         0x02000000,         //
44         0x00000000
45     };
46
47 fract32 desired_01[NUMSAMPLE_01] =
48     {
49         0x09000000,         // Simple addition
50         0x7FFFFFFF,         // Saturated
51         0x03000000,         // dummy
52         0x00000000,         // 0 for count test
53     };
54
55 void test_01_fr32_vector_add()
56 {
57     fract32 output[NUMSAMPLE_01];
58     int i;
59
60
61         // clear output buffer
62     clearBuffer( output, NUMSAMPLE_01);
63         // test addition. Sample is less than NUMSAMPLE_01 to test the count parameter
64     fr32_vector_add( buf_a_01, buf_b_01, output, NUMSAMPLE_01-1);
65
66     for ( i=0; i<NUMSAMPLE_01; i++)
67     {
68         if ( output[i] != desired_01[i] )
69         {
70             printf( "test_01 NG :output[%2d] = 0x%08X but should be 0x%08X\n", i, output[i], desired_01[i] );
71             return;
72         }
73     }
74     printf ("test_01 OK\n");
75 }
76
77 #undef TAPS_01
78 #undef NUMSAMPLE_01
79
80 /*
81  * Basic test to see scalar + vector addition.
82  */
83 #define NUMSAMPLE_02 4
84
85 fract32 buf_a_02[NUMSAMPLE_02]=
86     {
87         0x08000000,     //
88         0x7FFFFFFF,     //
89         0x02000000,
90         0x20000000
91     };
92
93
94 fract32 desired_02[NUMSAMPLE_02] =
95     {
96         0x09000000,         // Simple addition
97         0x7FFFFFFF,         // Saturated
98         0x03000000,         // dummy
99         0x00000000,         // 0 for count test
100     };
101
102 void test_02_fr32_vector_add_svv()
103 {
104     fract32 output[NUMSAMPLE_02];
105     int i;
106
107
108         // clear output buffer
109     clearBuffer( output, NUMSAMPLE_02);
110         // test addition. Sample is less than NUMSAMPLE_02 to test the count parameter
111     fr32_vector_add_svv( 0x01000000, buf_a_02, output, NUMSAMPLE_02-1);
112
113     for ( i=0; i<NUMSAMPLE_02; i++)
114     {
115         if ( output[i] != desired_02[i] )
116         {
117             printf( "test_02 NG :output[%2d] = 0x%08X but should be 0x%08X\n", i, output[i], desired_02[i] );
118             return;
119         }
120     }
121     printf ("test_02 OK\n");
122 }
123
124 #undef TAPS_02
125 #undef NUMSAMPLE_02
126
127 /*
128  * Basic test to see vector + vector addition.
129  */
130 #define NUMSAMPLE_03 4
131
132 fract32 buf_a_03[NUMSAMPLE_03]=
133     {
134         0x09000000,     //
135         0x80000000,     //
136         0x09000000,
137         0x20000000
138     };
139
140 fract32 buf_b_03[NUMSAMPLE_03] =
141     {
142         0x01000000,         //
143         0x00000001,         //
144         0x08000000,         //
145         0x00000000
146     };
147
148 fract32 desired_03[NUMSAMPLE_03] =
149     {
150         0x08000000,         // Simple addition
151         0x80000000,         // Saturated
152         0x01000000,         // dummy
153         0x00000000,         // 0 for count test
154     };
155
156 void test_03_fr32_vector_sub()
157 {
158     fract32 output[NUMSAMPLE_03];
159     int i;
160
161
162         // clear output buffer
163     clearBuffer( output, NUMSAMPLE_03);
164         // test subtraction. Sample is less than NUMSAMPLE_03 to test the count parameter
165     fr32_vector_sub( buf_a_03, buf_b_03, output, NUMSAMPLE_03-1);
166
167     for ( i=0; i<NUMSAMPLE_03; i++)
168     {
169         if ( output[i] != desired_03[i] )
170         {
171             printf( "test_03 NG :output[%2d] = 0x%08X but should be 0x%08X\n", i, output[i], desired_03[i] );
172             return;
173         }
174     }
175     printf ("test_03 OK\n");
176 }
177
178 #undef TAPS_03
179 #undef NUMSAMPLE_03
180
181 /*
182  * Basic test to see scalar + vector addition.
183  */
184 #define NUMSAMPLE_04 4
185
186 fract32 buf_a_04[NUMSAMPLE_04]=
187     {
188         0x01000000,     //
189         0x80000000,     //
190         0x08000000,
191         0x20000000
192     };
193
194
195 fract32 desired_04[NUMSAMPLE_04] =
196     {
197         0x08000000,         // Simple addition
198         0x7fffffff,         // Saturated
199         0x01000000,         // dummy
200         0x00000000,         // 0 for count test
201     };
202
203 void test_04_fr32_vector_sub_svv()
204 {
205     fract32 output[NUMSAMPLE_04];
206     int i;
207
208
209         // clear output buffer
210     clearBuffer( output, NUMSAMPLE_04);
211         // test subtraction. Sample is less than NUMSAMPLE_04 to test the count parameter
212     fr32_vector_sub_svv( 0x09000000, buf_a_04, output, NUMSAMPLE_04-1);
213
214     for ( i=0; i<NUMSAMPLE_04; i++)
215     {
216         if ( output[i] != desired_04[i] )
217         {
218             printf( "test_04 NG :output[%2d] = 0x%08X but should be 0x%08X\n", i, output[i], desired_04[i] );
219             return;
220         }
221     }
222     printf ("test_04 OK\n");
223 }
224
225 #undef TAPS_04
226 #undef NUMSAMPLE_04
227
228
229 /*
230  * Basic test to see scalar + vector addition.
231  */
232 #define NUMSAMPLE_05 15
233
234 fract32 buf_a_05[NUMSAMPLE_05]=
235     {
236         0x40008000,     // summation test for 4 partial products
237         0x80000000,     // -1*H,
238         0x80000000,     // -1*L
239         0x10000000,     // H*-1
240         0x00001000,     // L*-1
241         0x00008000,     // L*L test 1
242         0x00008000,     // L*L test 2
243         0x00008000,     // L*L test 3
244         0x00000002,     // H*L test 1
245         0x00000002,     // H*L test 2
246         0x00000002,     // H*L test 3
247         0x40000000,
248         0x20000000,
249         0x10000000,
250         0x11110000,     // dummy
251     };
252
253
254 fract32 buf_b_05[NUMSAMPLE_05]=
255     {
256         0x20008000,     //
257         0x10000000,     //
258         0x00001000,
259         0x80000000,
260         0x80000000,
261         0x00008000,
262         0x00004000,
263         0x00002000,
264         0x40000000,
265         0x20000000,
266         0x10000000,
267         0x00000002,     // H*L test 1
268         0x00000002,     // H*L test 2
269         0x00000002,     // H*L test 3
270         0x10000000,
271     };
272
273
274 fract32 desired_05[NUMSAMPLE_05] =
275     {
276         0x10006001,         // Simple addition
277         0xF0000000,         //
278         0xFFFFF000,         //
279         0xF0000000,         //
280         0xFFFFF000,         //
281         0x00000001,         // LSB
282         0x00000000,         // round up
283         0x00000000,         // truncate to 0
284         0x00000001,         // LSB
285         0x00000001,         // round up
286         0x00000000,         // truncate to 0
287         0x00000001,         // LSB
288         0x00000001,         // round up
289         0x00000000,         // truncate to 0
290         0x00000000,         // must zero
291     };
292
293 void test_05_fr32_vector_mul()
294 {
295     fract32 output[NUMSAMPLE_05];
296     int i;
297
298
299         // clear output buffer
300     clearBuffer( output, NUMSAMPLE_05);
301         // test subtraction. Sample is less than NUMSAMPLE_05 to test the count parameter
302     fr32_vector_mul( buf_a_05, buf_b_05, output, NUMSAMPLE_05-1);
303
304     for ( i=0; i<NUMSAMPLE_05; i++)
305     {
306         if ( output[i] != desired_05[i] )
307         {
308             printf( "test_05 NG :output[%2d] = 0x%08X but should be 0x%08X\n", i, output[i], desired_05[i] );
309             return;
310         }
311     }
312     printf ("test_05 OK\n");
313 }
314
315 #undef TAPS_05
316 #undef NUMSAMPLE_05
317
318
319 /*
320  * Basic test to see scalar + vector addition.
321  */
322 #define NUMSAMPLE_06 15
323
324 fract32 buf_a_06[NUMSAMPLE_06]=
325     {
326         0x40008000,     // summation test for 4 partial products
327         0x80000000,     // -1*H,
328         0x80000000,     // -1*L
329         0x10000000,     // H*-1
330         0x00001000,     // L*-1
331         0x00008000,     // L*L test 1
332         0x00008000,     // L*L test 2
333         0x00008000,     // L*L test 3
334         0x00000002,     // H*L test 1
335         0x00000002,     // H*L test 2
336         0x00000002,     // H*L test 3
337         0x40000000,
338         0x20000000,
339         0x10000000,
340         0x11110000,     // dummy
341     };
342
343
344 fract32 buf_b_06[NUMSAMPLE_06]=
345     {
346         0x20008000,     //
347         0x10000000,     //
348         0x00001000,
349         0x80000000,
350         0x80000000,
351         0x00008000,
352         0x00004000,
353         0x00002000,
354         0x40000000,
355         0x20000000,
356         0x10000000,
357         0x00000002,     // H*L test 1
358         0x00000002,     // H*L test 2
359         0x00000002,     // H*L test 3
360         0x10000000,
361     };
362
363
364 fract32 desired_06[NUMSAMPLE_06] =
365     {
366         0x10006001,         // Simple addition
367         0xF0000000,         //
368         0xFFFFF000,         //
369         0xF0000000,         //
370         0xFFFFF000,         //
371         0x00000001,         // LSB
372         0x00000000,         // round up
373         0x00000000,         // truncate to 0
374         0x00000001,         // LSB
375         0x00000001,         // round up
376         0x00000000,         // truncate to 0
377         0x00000001,         // LSB
378         0x00000001,         // round up
379         0x00000000,         // truncate to 0
380         0x00000000,         // must zero
381     };
382
383 void test_06_fr32_vector_mul_svv()
384 {
385     fract32 output[NUMSAMPLE_06];
386     int i;
387
388
389         // clear output buffer
390     clearBuffer( output, NUMSAMPLE_06);
391         // test subtraction. Sample is less than NUMSAMPLE_06 to test the count parameter
392     for( i=0; i<NUMSAMPLE_06-1; i++)
393         fr32_vector_mul_svv( buf_a_06[i], &buf_b_06[i], &output[i], 1);
394
395     for ( i=0; i<NUMSAMPLE_06; i++)
396     {
397         if ( output[i] != desired_06[i] )
398         {
399             printf( "test_06 NG :output[%2d] = 0x%08X but should be 0x%08X\n", i, output[i], desired_06[i] );
400             return;
401         }
402     }
403     printf ("test_06 OK\n");
404 }
405
406 #undef TAPS_06
407 #undef NUMSAMPLE_06
408
409
410
411 /*
412  * Basic test to see scalar + vector addition.
413  */
414 #define NUMSAMPLE_07 61
415
416 fract32 buf_ar_07[NUMSAMPLE_07]=
417     {
418         0x00000000,     // offset check,
419
420         0x40008000,     // summation test for 4 partial products
421         0x80000000,     // -1*H,
422         0x80000000,     // -1*L
423         0x10000000,     // H*-1
424         0x00001000,     // L*-1
425         0x00008000,     // L*L test 1
426         0x00008000,     // L*L test 2
427         0x00008000,     // L*L test 3
428         0x00000002,     // H*L test 1
429         0x00000002,     // H*L test 2
430         0x00000002,     // H*L test 3
431         0x40000000,
432         0x20000000,
433         0x10000000,
434         0x00000000,     // dummy
435
436         0x40008000,     // summation test for 4 partial products
437         0x80000000,     // -1*H,
438         0x80000000,     // -1*L
439         0x10000000,     // H*-1
440         0x00001000,     // L*-1
441         0x00008000,     // L*L test 1
442         0x00008000,     // L*L test 2
443         0x00008000,     // L*L test 3
444         0x00000002,     // H*L test 1
445         0x00000002,     // H*L test 2
446         0x00000002,     // H*L test 3
447         0x40000000,
448         0x20000000,
449         0x10000000,
450         0x00000000,     // dummy
451
452         0x00000000,
453         0x00000000,
454         0x00000000,
455         0x00000000,
456         0x00000000,
457         0x00000000,
458         0x00000000,
459         0x00000000,
460         0x00000000,
461         0x00000000,
462         0x00000000,
463         0x00000000,
464         0x00000000,
465         0x00000000,
466         0x00000000,
467
468         0x00000000,
469         0x00000000,
470         0x00000000,
471         0x00000000,
472         0x00000000,
473         0x00000000,
474         0x00000000,
475         0x00000000,
476         0x00000000,
477         0x00000000,
478         0x00000000,
479         0x00000000,
480         0x00000000,
481         0x00000000,
482         0x00000000,
483
484
485     };
486
487
488 fract32 buf_ai_07[NUMSAMPLE_07]=
489     {
490         0x00000000,
491
492         0x00000000,
493         0x00000000,
494         0x00000000,
495         0x00000000,
496         0x00000000,
497         0x00000000,
498         0x00000000,
499         0x00000000,
500         0x00000000,
501         0x00000000,
502         0x00000000,
503         0x00000000,
504         0x00000000,
505         0x00000000,
506         0x00000000,
507
508         0x00000000,
509         0x00000000,
510         0x00000000,
511         0x00000000,
512         0x00000000,
513         0x00000000,
514         0x00000000,
515         0x00000000,
516         0x00000000,
517         0x00000000,
518         0x00000000,
519         0x00000000,
520         0x00000000,
521         0x00000000,
522         0x00000000,
523
524         0x40008000,     // summation test for 4 partial products
525         0x80000000,     // -1*H,
526         0x80000000,     // -1*L
527         0x10000000,     // H*-1
528         0x00001000,     // L*-1
529         0x00008000,     // L*L test 1
530         0x00008000,     // L*L test 2
531         0x00008000,     // L*L test 3
532         0x00000002,     // H*L test 1
533         0x00000002,     // H*L test 2
534         0x00000002,     // H*L test 3
535         0x40000000,
536         0x20000000,
537         0x10000000,
538         0x00000000,     // dummy
539
540         0x40008000,     // summation test for 4 partial products
541         0x80000000,     // -1*H,
542         0x80000000,     // -1*L
543         0x10000000,     // H*-1
544         0x00001000,     // L*-1
545         0x00008000,     // L*L test 1
546         0x00008000,     // L*L test 2
547         0x00008000,     // L*L test 3
548         0x00000002,     // H*L test 1
549         0x00000002,     // H*L test 2
550         0x00000002,     // H*L test 3
551         0x40000000,
552         0x20000000,
553         0x10000000,
554         0x00000000,     // dummy
555
556     };
557
558
559 fract32 buf_br_07[NUMSAMPLE_07]=
560     {
561         0x00000000,
562
563         0x20008000,     //
564         0x10000000,     //
565         0x00001000,
566         0x80000000,
567         0x80000000,
568         0x00008000,
569         0x00004000,
570         0x00002000,
571         0x40000000,
572         0x20000000,
573         0x10000000,
574         0x00000002,     // H*L test 1
575         0x00000002,     // H*L test 2
576         0x00000002,     // H*L test 3
577         0x00000000,
578
579         0x00000000,
580         0x00000000,
581         0x00000000,
582         0x00000000,
583         0x00000000,
584         0x00000000,
585         0x00000000,
586         0x00000000,
587         0x00000000,
588         0x00000000,
589         0x00000000,
590         0x00000000,
591         0x00000000,
592         0x00000000,
593         0x00000000,
594
595         0x20008000,     //
596         0x10000000,     //
597         0x00001000,
598         0x80000000,
599         0x80000000,
600         0x00008000,
601         0x00004000,
602         0x00002000,
603         0x40000000,
604         0x20000000,
605         0x10000000,
606         0x00000002,     // H*L test 1
607         0x00000002,     // H*L test 2
608         0x00000002,     // H*L test 3
609         0x00000000,
610
611         0x00000000,
612         0x00000000,
613         0x00000000,
614         0x00000000,
615         0x00000000,
616         0x00000000,
617         0x00000000,
618         0x00000000,
619         0x00000000,
620         0x00000000,
621         0x00000000,
622         0x00000000,
623         0x00000000,
624         0x00000000,
625         0x00000000,
626
627
628     };
629
630 fract32 buf_bi_07[NUMSAMPLE_07]=
631     {
632         0x00000000,
633
634         0x00000000,
635         0x00000000,
636         0x00000000,
637         0x00000000,
638         0x00000000,
639         0x00000000,
640         0x00000000,
641         0x00000000,
642         0x00000000,
643         0x00000000,
644         0x00000000,
645         0x00000000,
646         0x00000000,
647         0x00000000,
648         0x00000000,
649
650         0x20008000,     //
651         0x10000000,     //
652         0x00001000,
653         0x80000000,
654         0x80000000,
655         0x00008000,
656         0x00004000,
657         0x00002000,
658         0x40000000,
659         0x20000000,
660         0x10000000,
661         0x00000002,     // H*L test 1
662         0x00000002,     // H*L test 2
663         0x00000002,     // H*L test 3
664         0x00000000,
665
666         0x00000000,
667         0x00000000,
668         0x00000000,
669         0x00000000,
670         0x00000000,
671         0x00000000,
672         0x00000000,
673         0x00000000,
674         0x00000000,
675         0x00000000,
676         0x00000000,
677         0x00000000,
678         0x00000000,
679         0x00000000,
680         0x00000000,
681
682         0x20008000,     //
683         0x10000000,     //
684         0x00001000,
685         0x80000000,
686         0x80000000,
687         0x00008000,
688         0x00004000,
689         0x00002000,
690         0x40000000,
691         0x20000000,
692         0x10000000,
693         0x00000002,     // H*L test 1
694         0x00000002,     // H*L test 2
695         0x00000002,     // H*L test 3
696         0x00000000,
697
698
699     };
700
701 fract32 desired_r_07[NUMSAMPLE_07] ;
702
703 fract32 desired_i_07[NUMSAMPLE_07] ;
704
705 void test_07_fr32_vector_complex_mul()
706 {
707     fract32 output_r[NUMSAMPLE_07], output_i[NUMSAMPLE_07];
708     int i;
709
710
711         // clear output buffer
712     clearBuffer( output_r, NUMSAMPLE_07);
713     clearBuffer( output_i, NUMSAMPLE_07);
714
715     for ( i=0; i<NUMSAMPLE_07; i++)
716     {
717         desired_r_07[i] = mult_fr1x32x32( buf_ar_07[i], buf_br_07[i])
718                         - mult_fr1x32x32( buf_ai_07[i], buf_bi_07[i]);
719         desired_i_07[i] = mult_fr1x32x32( buf_ar_07[i], buf_bi_07[i])
720                         + mult_fr1x32x32( buf_ai_07[i], buf_br_07[i]);
721     }
722
723     for ( i=46; i<NUMSAMPLE_07; i++)
724     {
725         desired_r_07[i] = mult_fr1x32x32( buf_ar_07[i], buf_br_07[i])
726                         + neg_mult_fr1x32x32(buf_ai_07[i], buf_bi_07[i]);
727     }
728
729         // test subtraction. Sample is less than NUMSAMPLE_07 to test the count parameter
730     fr32_vector_complex_mul( buf_ar_07, buf_ai_07, buf_br_07, buf_bi_07,
731                                 output_r, output_i, NUMSAMPLE_07);
732
733     for ( i=0; i<NUMSAMPLE_07; i++)
734     {
735         if ( output_r[i] != desired_r_07[i] )
736         {
737             printf( "test_07 NG :output_r[%2d] = 0x%08X but should be 0x%08X\n", i, output_r[i], desired_r_07[i] );
738             return;
739         }
740         if ( output_i[i] != desired_i_07[i] )
741         {
742             printf( "test_07 NG :output_i[%2d] = 0x%08X but should be 0x%08X\n", i, output_i[i], desired_i_07[i] );
743             return;
744         }
745     }
746     printf ("test_07 OK\n");
747 }
748
749 #undef TAPS_07
750 #undef NUMSAMPLE_07
751
752
753 /*
754  * Basic test to see scalar + vector addition.
755  */
756 #define NUMSAMPLE_08 5
757
758 fract32 buf_a_08[NUMSAMPLE_08]=
759     {
760         0x00000001,     //
761         0xFFFFFFFF,     //
762         0x80000001,
763         0x80000000,
764         0x20000000
765     };
766
767
768 fract32 desired_08[NUMSAMPLE_08] =
769     {
770         0xFFFFFFFF,         // -1
771         0x00000001,         // 1
772         0x7FFFFFFF,
773         0x7FFFFFFF,         // saturated
774         0x00000000,         // 0 for count test
775     };
776
777 void test_08_fr32_vector_neg()
778 {
779     fract32 output[NUMSAMPLE_08];
780     int i;
781
782
783         // clear output buffer
784     clearBuffer( output, NUMSAMPLE_08);
785         // test subtraction. Sample is less than NUMSAMPLE_08 to test the count parameter
786     fr32_vector_neg( buf_a_08, output, NUMSAMPLE_08-1);
787
788     for ( i=0; i<NUMSAMPLE_08; i++)
789     {
790         if ( output[i] != desired_08[i] )
791         {
792             printf( "test_08 NG :output[%2d] = 0x%08X but should be 0x%08X\n", i, output[i], desired_08[i] );
793             return;
794         }
795     }
796     printf ("test_08 OK\n");
797 }
798
799 #undef TAPS_08
800 #undef NUMSAMPLE_08
801