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

Revision 849, 7.6 kB (checked in by ppalmers, 15 years ago)

temp commit of some SSE/memory optimizations

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 3 of the License, or
12  * (at your option) any later version.
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     Util::SystemTimeSource time;
45     ffado_microsecs_t start;
46     ffado_microsecs_t elapsed;
47    
48     setDebugLevel(DEBUG_LEVEL_NORMAL);
49    
50     buffer_1 = new quadlet_t[nb_quadlets];
51     buffer_ref = new quadlet_t[nb_quadlets];
52    
53     printMessage( "Generating test data...\n");
54     for (i=0; i<nb_quadlets; i++) {
55         byte_t tmp = i & 0xFF;
56         buffer_1[i]   = tmp << 24;
57         tmp = (i + 1) & 0xFF;
58         buffer_1[i]   |= tmp << 16;
59         tmp = (i + 2) & 0xFF;
60         buffer_1[i]   |= tmp << 8;
61         tmp = (i + 3) & 0xFF;
62         buffer_1[i]   |= tmp;
63     }
64    
65     // do reference conversion
66    
67     for (i=0; i<nb_quadlets; i++) {
68         buffer_ref[i] = htonl(buffer_1[i]);
69     }
70    
71     printMessage( "Performing byte-swap...\n");
72    
73     int test=0;
74     for (test=0; test<nb_tests; test++) {
75         for (i=0; i<nb_quadlets; i++) {
76             byte_t tmp = i & 0xFF;
77             buffer_1[i]   = tmp << 24;
78             tmp = (i + 1) & 0xFF;
79             buffer_1[i]   |= tmp << 16;
80             tmp = (i + 2) & 0xFF;
81             buffer_1[i]   |= tmp << 8;
82             tmp = (i + 3) & 0xFF;
83             buffer_1[i]   |= tmp;
84         }
85
86         start = time.getCurrentTimeAsUsecs();
87         byteSwapToBus(buffer_1, nb_quadlets);
88         elapsed = time.getCurrentTimeAsUsecs() - start;
89         printMessage( " took %lluusec...\n", elapsed);
90        
91     }
92
93     // check
94     printMessage( "Checking results...\n");
95     bool all_ok=true;
96     for (i=0; i<nb_quadlets; i++) {
97         if (buffer_1[i] != buffer_ref[i]) {
98             printMessage( " bad result: %08X should be %08X\n",
99                         buffer_1[i], buffer_ref[i]);
100             all_ok=false;
101         } else {
102             //debugOutput(DEBUG_LEVEL_VERBOSE, "good result: %08X should be %08X\n",
103             //            buffer_1[i], buffer_ref[i]);
104         }
105     }
106
107     delete[] buffer_1;
108     delete[] buffer_ref;
109     return all_ok;
110 }
111
112 bool
113 testInt24Label(int nb_quadlets, int nb_tests) {
114     quadlet_t *buffer_1;
115     quadlet_t *buffer_ref;
116     int i=0;
117    
118     Util::SystemTimeSource time;
119     ffado_microsecs_t start;
120     ffado_microsecs_t elapsed;
121    
122     setDebugLevel(DEBUG_LEVEL_MESSAGE);
123    
124     buffer_1 = new quadlet_t[nb_quadlets];
125     buffer_ref = new quadlet_t[nb_quadlets];
126    
127     printMessage( "Generating test data...\n");
128     for (i=0; i<nb_quadlets; i++) {
129         byte_t tmp = i & 0xFF;
130         buffer_1[i]   = tmp << 16;
131         tmp = (i + 1) & 0xFF;
132         buffer_1[i]   |= tmp << 8;
133         tmp = (i + 2) & 0xFF;
134         buffer_1[i]   |= tmp;
135     }
136    
137     // do reference conversion
138     for (i=0; i<nb_quadlets; i++) {
139         buffer_ref[i] = buffer_1[i] | 0x40000000;
140     }
141    
142     printMessage( "Performing AMDTP labeling...\n");
143    
144     int test=0;
145     for (test=0; test<nb_tests; test++) {
146         for (i=0; i<nb_quadlets; i++) {
147             byte_t tmp = i & 0xFF;
148             buffer_1[i]   = tmp << 16;
149             tmp = (i + 1) & 0xFF;
150             buffer_1[i]   |= tmp << 8;
151             tmp = (i + 2) & 0xFF;
152             buffer_1[i]   |= tmp;
153         }
154
155         start = time.getCurrentTimeAsUsecs();
156         convertFromInt24AndLabelAsMBLA(buffer_1, nb_quadlets);
157         elapsed = time.getCurrentTimeAsUsecs() - start;
158         printMessage( " took %lluusec...\n", elapsed);
159     }
160
161     // check
162     printMessage( "Checking results...\n");
163     bool all_ok=true;
164     for (i=0; i<nb_quadlets; i++) {
165         if (buffer_1[i] != buffer_ref[i]) {
166             printMessage( " bad result: %08X should be %08X\n",
167                         buffer_1[i], buffer_ref[i]);
168             all_ok=false;
169         } else {
170             //debugOutput(DEBUG_LEVEL_VERBOSE, "good result: %08X should be %08X\n",
171             //            buffer_1[i], buffer_ref[i]);
172         }
173     }
174
175     delete[] buffer_1;
176     delete[] buffer_ref;
177     return all_ok;
178 }
179
180 bool
181 testFloatLabel(int nb_quadlets, int nb_tests) {
182     quadlet_t *buffer_1;
183     quadlet_t *buffer_ref;
184     quadlet_t *buffer_in;
185     float *buffer_float;
186     int i=0;
187    
188     Util::SystemTimeSource time;
189     ffado_microsecs_t start;
190     ffado_microsecs_t elapsed;
191    
192     setDebugLevel(DEBUG_LEVEL_MESSAGE);
193    
194     buffer_1 = new quadlet_t[nb_quadlets];
195     buffer_in = new quadlet_t[nb_quadlets];
196     buffer_ref = new quadlet_t[nb_quadlets];
197     buffer_float = new float[nb_quadlets];
198    
199     printMessage( "Generating test data...\n");
200     for (i=0; i<nb_quadlets; i++) {
201         byte_t tmp = i & 0xFF;
202         buffer_in[i]   = tmp << 16;
203         tmp = (i + 1) & 0xFF;
204         buffer_in[i]   |= tmp << 8;
205         tmp = (i + 2) & 0xFF;
206         buffer_in[i]   |= tmp;
207        
208         // convert to float and normalize
209         buffer_float[i] = (float)(buffer_in[i]);
210         buffer_float[i] /= (float)(0x007FFFFF); // range: 0..2
211         buffer_float[i] -= 1.0; // range: 1..-1
212        
213         // copy to input buffer
214         float *t = &(buffer_float[i]);
215         quadlet_t *v = (quadlet_t *)t;
216         buffer_1[i] = *v;
217     }
218    
219     // do reference conversion
220     for (i=0; i<nb_quadlets; i++) {
221         float v = (buffer_float[i]) * AMDTP_FLOAT_MULTIPLIER;
222         unsigned int tmp = ((int) v);
223         tmp = ( tmp >> 8 ) | 0x40000000;
224         buffer_ref[i] = tmp;
225     }
226    
227     printMessage( "Performing AMDTP labeling...\n");
228    
229     int test=0;
230     for (test=0; test<nb_tests; test++) {
231     for (i=0; i<nb_quadlets; i++) {
232         // copy float to input buffer
233         float *t = &(buffer_float[i]);
234         quadlet_t *v = (quadlet_t *)t;
235         buffer_1[i] = *v;
236     }
237
238         start = time.getCurrentTimeAsUsecs();
239         convertFromFloatAndLabelAsMBLA(buffer_1, nb_quadlets);
240         elapsed = time.getCurrentTimeAsUsecs() - start;
241         printMessage( " took %lluusec...\n", elapsed);
242     }
243
244     // check
245     printMessage( "Checking results...\n");
246     bool all_ok=true;
247     for (i=0; i<nb_quadlets; i++) {
248         if (buffer_1[i] != buffer_ref[i]) {
249             printMessage( " bad result: %08X should be %08X\n",
250                         buffer_1[i], buffer_ref[i]);
251             all_ok=false;
252         } else {
253             //debugOutput(DEBUG_LEVEL_VERBOSE, "good result: %08X should be %08X\n",
254             //            buffer_1[i], buffer_ref[i]);
255         }
256     }
257
258     delete[] buffer_1;
259     delete[] buffer_ref;
260     delete[] buffer_in;
261     delete[] buffer_float;
262     return all_ok;
263 }
264
265 int
266 main(int argc, char **argv) {
267
268     testByteSwap(NB_QUADLETS, NB_TESTS);
269     testInt24Label(NB_QUADLETS, NB_TESTS);
270     testFloatLabel(NB_QUADLETS, NB_TESTS);
271    
272     return 0;
273 }
Note: See TracBrowser for help on using the browser.