root/trunk/libffado/tests/test-bufferops.cpp

Revision 1136, 7.6 kB (checked in by ppalmers, 13 years ago)

keep bus direction in endian swapping functions

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "debugmodule/debugmodule.h"
25
26 DECLARE_GLOBAL_DEBUG_MODULE;
27
28 #include "libutil/ByteSwap.h"
29 #include "libstreaming/amdtp/AmdtpBufferOps.h"
30
31 #include "libutil/SystemTimeSource.h"
32 #include <inttypes.h>
33
34 // 32M of test data
35 #define NB_QUADLETS (1024 * 1024 * 32)
36 #define NB_TESTS 10
37
38 bool
39 testByteSwap(int nb_quadlets, int nb_tests) {
40     quadlet_t *buffer_1;
41     quadlet_t *buffer_ref;
42     int i=0;
43    
44     ffado_microsecs_t start;
45     ffado_microsecs_t elapsed;
46    
47     setDebugLevel(DEBUG_LEVEL_NORMAL);
48    
49     buffer_1 = new quadlet_t[nb_quadlets];
50     buffer_ref = new quadlet_t[nb_quadlets];
51    
52     printMessage( "Generating test data...\n");
53     for (i=0; i<nb_quadlets; i++) {
54         byte_t tmp = i & 0xFF;
55         buffer_1[i]   = tmp << 24;
56         tmp = (i + 1) & 0xFF;
57         buffer_1[i]   |= tmp << 16;
58         tmp = (i + 2) & 0xFF;
59         buffer_1[i]   |= tmp << 8;
60         tmp = (i + 3) & 0xFF;
61         buffer_1[i]   |= tmp;
62     }
63    
64     // do reference conversion
65    
66     for (i=0; i<nb_quadlets; i++) {
67         buffer_ref[i] = CondSwapToBus32(buffer_1[i]);
68     }
69    
70     printMessage( "Performing byte-swap...\n");
71    
72     int test=0;
73     for (test=0; test<nb_tests; test++) {
74         for (i=0; i<nb_quadlets; i++) {
75             byte_t tmp = i & 0xFF;
76             buffer_1[i]   = tmp << 24;
77             tmp = (i + 1) & 0xFF;
78             buffer_1[i]   |= tmp << 16;
79             tmp = (i + 2) & 0xFF;
80             buffer_1[i]   |= tmp << 8;
81             tmp = (i + 3) & 0xFF;
82             buffer_1[i]   |= tmp;
83         }
84
85         start = Util::SystemTimeSource::getCurrentTimeAsUsecs();
86         byteSwapToBus(buffer_1, nb_quadlets);
87         elapsed = Util::SystemTimeSource::getCurrentTimeAsUsecs() - start;
88         printMessage( " took %lluusec...\n", elapsed);
89        
90     }
91
92     // check
93     printMessage( "Checking results...\n");
94     bool all_ok=true;
95     for (i=0; i<nb_quadlets; i++) {
96         if (buffer_1[i] != buffer_ref[i]) {
97             printMessage( " bad result: %08X should be %08X\n",
98                         buffer_1[i], buffer_ref[i]);
99             all_ok=false;
100         } else {
101             //debugOutput(DEBUG_LEVEL_VERBOSE, "good result: %08X should be %08X\n",
102             //            buffer_1[i], buffer_ref[i]);
103         }
104     }
105
106     delete[] buffer_1;
107     delete[] buffer_ref;
108     return all_ok;
109 }
110
111 bool
112 testInt24Label(int nb_quadlets, int nb_tests) {
113     quadlet_t *buffer_1;
114     quadlet_t *buffer_ref;
115     int i=0;
116    
117     ffado_microsecs_t start;
118     ffado_microsecs_t elapsed;
119    
120     setDebugLevel(DEBUG_LEVEL_MESSAGE);
121    
122     buffer_1 = new quadlet_t[nb_quadlets];
123     buffer_ref = new quadlet_t[nb_quadlets];
124    
125     printMessage( "Generating test data...\n");
126     for (i=0; i<nb_quadlets; i++) {
127         byte_t tmp = i & 0xFF;
128         buffer_1[i]   = tmp << 16;
129         tmp = (i + 1) & 0xFF;
130         buffer_1[i]   |= tmp << 8;
131         tmp = (i + 2) & 0xFF;
132         buffer_1[i]   |= tmp;
133     }
134    
135     // do reference conversion
136     for (i=0; i<nb_quadlets; i++) {
137         buffer_ref[i] = buffer_1[i] | 0x40000000;
138     }
139    
140     printMessage( "Performing AMDTP labeling...\n");
141    
142     int test=0;
143     for (test=0; test<nb_tests; test++) {
144         for (i=0; i<nb_quadlets; i++) {
145             byte_t tmp = i & 0xFF;
146             buffer_1[i]   = tmp << 16;
147             tmp = (i + 1) & 0xFF;
148             buffer_1[i]   |= tmp << 8;
149             tmp = (i + 2) & 0xFF;
150             buffer_1[i]   |= tmp;
151         }
152
153         start = Util::SystemTimeSource::getCurrentTimeAsUsecs();
154         convertFromInt24AndLabelAsMBLA(buffer_1, nb_quadlets);
155         elapsed = Util::SystemTimeSource::getCurrentTimeAsUsecs() - start;
156         printMessage( " took %lluusec...\n", elapsed);
157     }
158
159     // check
160     printMessage( "Checking results...\n");
161     bool all_ok=true;
162     for (i=0; i<nb_quadlets; i++) {
163         if (buffer_1[i] != buffer_ref[i]) {
164             printMessage( " bad result: %08X should be %08X\n",
165                         buffer_1[i], buffer_ref[i]);
166             all_ok=false;
167         } else {
168             //debugOutput(DEBUG_LEVEL_VERBOSE, "good result: %08X should be %08X\n",
169             //            buffer_1[i], buffer_ref[i]);
170         }
171     }
172
173     delete[] buffer_1;
174     delete[] buffer_ref;
175     return all_ok;
176 }
177
178 bool
179 testFloatLabel(int nb_quadlets, int nb_tests) {
180     quadlet_t *buffer_1;
181     quadlet_t *buffer_ref;
182     quadlet_t *buffer_in;
183     float *buffer_float;
184     int i=0;
185    
186     ffado_microsecs_t start;
187     ffado_microsecs_t elapsed;
188    
189     setDebugLevel(DEBUG_LEVEL_MESSAGE);
190    
191     buffer_1 = new quadlet_t[nb_quadlets];
192     buffer_in = new quadlet_t[nb_quadlets];
193     buffer_ref = new quadlet_t[nb_quadlets];
194     buffer_float = new float[nb_quadlets];
195    
196     printMessage( "Generating test data...\n");
197     for (i=0; i<nb_quadlets; i++) {
198         byte_t tmp = i & 0xFF;
199         buffer_in[i]   = tmp << 16;
200         tmp = (i + 1) & 0xFF;
201         buffer_in[i]   |= tmp << 8;
202         tmp = (i + 2) & 0xFF;
203         buffer_in[i]   |= tmp;
204        
205         // convert to float and normalize
206         buffer_float[i] = (float)(buffer_in[i]);
207         buffer_float[i] /= (float)(0x007FFFFF); // range: 0..2
208         buffer_float[i] -= 1.0; // range: 1..-1
209        
210         // copy to input buffer
211         float *t = &(buffer_float[i]);
212         quadlet_t *v = (quadlet_t *)t;
213         buffer_1[i] = *v;
214     }
215    
216     // do reference conversion
217     for (i=0; i<nb_quadlets; i++) {
218         float v = (buffer_float[i]) * AMDTP_FLOAT_MULTIPLIER;
219         unsigned int tmp = ((int) v);
220         tmp = ( tmp >> 8 ) | 0x40000000;
221         buffer_ref[i] = tmp;
222     }
223    
224     printMessage( "Performing AMDTP labeling...\n");
225    
226     int test=0;
227     for (test=0; test<nb_tests; test++) {
228     for (i=0; i<nb_quadlets; i++) {
229         // copy float to input buffer
230         float *t = &(buffer_float[i]);
231         quadlet_t *v = (quadlet_t *)t;
232         buffer_1[i] = *v;
233     }
234
235         start = Util::SystemTimeSource::getCurrentTimeAsUsecs();
236         convertFromFloatAndLabelAsMBLA(buffer_1, nb_quadlets);
237         elapsed = Util::SystemTimeSource::getCurrentTimeAsUsecs() - start;
238         printMessage( " took %lluusec...\n", elapsed);
239     }
240
241     // check
242     printMessage( "Checking results...\n");
243     bool all_ok=true;
244     for (i=0; i<nb_quadlets; i++) {
245         if (buffer_1[i] != buffer_ref[i]) {
246             printMessage( " bad result: %08X should be %08X\n",
247                         buffer_1[i], buffer_ref[i]);
248             all_ok=false;
249         } else {
250             //debugOutput(DEBUG_LEVEL_VERBOSE, "good result: %08X should be %08X\n",
251             //            buffer_1[i], buffer_ref[i]);
252         }
253     }
254
255     delete[] buffer_1;
256     delete[] buffer_ref;
257     delete[] buffer_in;
258     delete[] buffer_float;
259     return all_ok;
260 }
261
262 int
263 main(int argc, char **argv) {
264
265     testByteSwap(NB_QUADLETS, NB_TESTS);
266     testInt24Label(NB_QUADLETS, NB_TESTS);
267     testFloatLabel(NB_QUADLETS, NB_TESTS);
268    
269     return 0;
270 }
Note: See TracBrowser for help on using the browser.