Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/tests/bit_operations_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 * bit_operations_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: bit_operations_tests.c,v 1.14 2008/05/13 13:17:25 steveu Exp $ | |
26 */ | |
27 | |
28 /*! \page bit_operations_tests_page Bit operations tests | |
29 \section bit_operations_tests_page_sec_1 What does it do? | |
30 These tests check the operation of efficient bit manipulation routines, by comparing | |
31 their operation with very dumb brute force versions of the same functionality. | |
32 | |
33 \section bit_operations_tests_page_sec_2 How is it used? | |
34 */ | |
35 | |
36 #if defined(HAVE_CONFIG_H) | |
37 #include "config.h" | |
38 #endif | |
39 | |
40 #include <stdlib.h> | |
41 #include <stdio.h> | |
42 #include <fcntl.h> | |
43 #include <string.h> | |
44 #include <assert.h> | |
45 | |
46 #include "spandsp.h" | |
47 | |
48 uint8_t from[1000000]; | |
49 uint8_t to[1000000]; | |
50 | |
51 static __inline__ int top_bit_dumb(unsigned int data) | |
52 { | |
53 int i; | |
54 | |
55 if (data == 0) | |
56 return -1; | |
57 for (i = 31; i >= 0; i--) | |
58 { | |
59 if ((data & (1 << i))) | |
60 return i; | |
61 } | |
62 return -1; | |
63 } | |
64 /*- End of function --------------------------------------------------------*/ | |
65 | |
66 static __inline__ int bottom_bit_dumb(unsigned int data) | |
67 { | |
68 int i; | |
69 | |
70 if (data == 0) | |
71 return -1; | |
72 for (i = 0; i < 32; i++) | |
73 { | |
74 if ((data & (1 << i))) | |
75 return i; | |
76 } | |
77 return -1; | |
78 } | |
79 /*- End of function --------------------------------------------------------*/ | |
80 | |
81 static __inline__ uint8_t bit_reverse8_dumb(uint8_t data) | |
82 { | |
83 int i; | |
84 int result; | |
85 | |
86 result = 0; | |
87 for (i = 0; i < 8; i++) | |
88 { | |
89 result = (result << 1) | (data & 1); | |
90 data >>= 1; | |
91 } | |
92 return result; | |
93 } | |
94 /*- End of function --------------------------------------------------------*/ | |
95 | |
96 static __inline__ uint32_t bit_reverse_4bytes_dumb(uint32_t data) | |
97 { | |
98 int i; | |
99 uint32_t result; | |
100 | |
101 result = 0; | |
102 for (i = 0; i < 8; i++) | |
103 { | |
104 result = (result << 1) | (data & 0x01010101); | |
105 data >>= 1; | |
106 } | |
107 return result; | |
108 } | |
109 /*- End of function --------------------------------------------------------*/ | |
110 | |
111 static __inline__ uint16_t bit_reverse16_dumb(uint16_t data) | |
112 { | |
113 int i; | |
114 uint16_t result; | |
115 | |
116 result = 0; | |
117 for (i = 0; i < 16; i++) | |
118 { | |
119 result = (result << 1) | (data & 1); | |
120 data >>= 1; | |
121 } | |
122 return result; | |
123 } | |
124 /*- End of function --------------------------------------------------------*/ | |
125 | |
126 static __inline__ uint32_t bit_reverse32_dumb(uint32_t data) | |
127 { | |
128 int i; | |
129 uint32_t result; | |
130 | |
131 result = 0; | |
132 for (i = 0; i < 32; i++) | |
133 { | |
134 result = (result << 1) | (data & 1); | |
135 data >>= 1; | |
136 } | |
137 return result; | |
138 } | |
139 /*- End of function --------------------------------------------------------*/ | |
140 | |
141 static __inline__ int parity8_dumb(uint8_t x) | |
142 { | |
143 uint8_t y; | |
144 int i; | |
145 | |
146 for (y = 0, i = 0; i < 8; i++) | |
147 { | |
148 y ^= (x & 1); | |
149 x >>= 1; | |
150 } | |
151 return y; | |
152 } | |
153 /*- End of function --------------------------------------------------------*/ | |
154 | |
155 static __inline__ int one_bits32_dumb(uint32_t x) | |
156 { | |
157 int i; | |
158 int bits; | |
159 | |
160 bits = 0; | |
161 for (i = 0; i < 32; i++) | |
162 { | |
163 if (x & 1) | |
164 bits++; | |
165 x >>= 1; | |
166 } | |
167 return bits; | |
168 } | |
169 /*- End of function --------------------------------------------------------*/ | |
170 | |
171 int main(int argc, char *argv[]) | |
172 { | |
173 int i; | |
174 uint32_t x; | |
175 uint8_t ax; | |
176 uint8_t bx; | |
177 uint16_t ax16; | |
178 uint16_t bx16; | |
179 uint32_t ax32; | |
180 uint32_t bx32; | |
181 | |
182 for (i = 0, x = 0; i < 100000; i++) | |
183 { | |
184 ax = top_bit_dumb(x); | |
185 bx = top_bit(x); | |
186 if (ax != bx) | |
187 { | |
188 printf("Test failed: top bit mismatch 0x%" PRIx32 " -> %u %u\n", x, ax, bx); | |
189 exit(2); | |
190 } | |
191 ax = bottom_bit_dumb(x); | |
192 bx = bottom_bit(x); | |
193 if (ax != bx) | |
194 { | |
195 printf("Test failed: bottom bit mismatch 0x%" PRIx32 " -> %u %u\n", x, ax, bx); | |
196 exit(2); | |
197 } | |
198 x = rand(); | |
199 } | |
200 for (i = 0; i < 256; i++) | |
201 { | |
202 ax = bit_reverse8_dumb(i); | |
203 bx = bit_reverse8(i); | |
204 if (ax != bx) | |
205 { | |
206 printf("Test failed: bit reverse 8 - %02x %02x %02x\n", i, ax, bx); | |
207 exit(2); | |
208 } | |
209 } | |
210 for (i = 0; i < 1000000; i++) | |
211 from[i] = rand(); | |
212 bit_reverse(to, from, 1000000); | |
213 for (i = 0; i < 1000000; i++) | |
214 { | |
215 if (bit_reverse8_dumb(from[i]) != to[i]) | |
216 { | |
217 printf("Test failed: bit reverse - at %d, %02x %02x %02x\n", i, from[i], bit_reverse8(from[i]), to[i]); | |
218 exit(2); | |
219 } | |
220 } | |
221 for (i = 0; i < 256; i++) | |
222 { | |
223 x = i | (((i + 1) & 0xFF) << 8) | (((i + 2) & 0xFF) << 16) | (((i + 3) & 0xFF) << 24); | |
224 ax32 = bit_reverse_4bytes_dumb(x); | |
225 bx32 = bit_reverse_4bytes(x); | |
226 if (ax32 != bx32) | |
227 { | |
228 printf("Test failed: bit reverse 4 bytes - %" PRIx32 " %" PRIx32 " %" PRIx32 "\n", x, ax32, bx32); | |
229 exit(2); | |
230 } | |
231 } | |
232 for (i = 0; i < 65536; i++) | |
233 { | |
234 ax16 = bit_reverse16_dumb(i); | |
235 bx16 = bit_reverse16(i); | |
236 if (ax16 != bx16) | |
237 { | |
238 printf("Test failed: bit reverse 16 - %x %x %x\n", i, ax16, bx16); | |
239 exit(2); | |
240 } | |
241 } | |
242 for (i = 0; i < 0x7FFFFF00; i += 127) | |
243 { | |
244 ax32 = bit_reverse32_dumb(i); | |
245 bx32 = bit_reverse32(i); | |
246 if (ax32 != bx32) | |
247 { | |
248 printf("Test failed: bit reverse 32 - %d %" PRIx32 " %" PRIx32 "\n", i, ax32, bx32); | |
249 exit(2); | |
250 } | |
251 } | |
252 | |
253 for (i = 0; i < 256; i++) | |
254 { | |
255 ax = parity8(i); | |
256 bx = parity8_dumb(i); | |
257 if (ax != bx) | |
258 { | |
259 printf("Test failed: parity 8 - %x %x %x\n", i, ax, bx); | |
260 exit(2); | |
261 } | |
262 } | |
263 | |
264 for (i = -1; i < 32; i++) | |
265 { | |
266 ax32 = most_significant_one32(1 << i); | |
267 if (ax32 != (1 << i)) | |
268 { | |
269 printf("Test failed: most significant one 32 - %x %" PRIx32 " %x\n", i, ax32, (1 << i)); | |
270 exit(2); | |
271 } | |
272 ax32 = least_significant_one32(1 << i); | |
273 if (ax32 != (1 << i)) | |
274 { | |
275 printf("Test failed: least significant one 32 - %x %" PRIx32 " %x\n", i, ax32, (1 << i)); | |
276 exit(2); | |
277 } | |
278 } | |
279 | |
280 for (i = 0x80000000; i < 0x800FFFFF; i++) | |
281 { | |
282 ax = one_bits32_dumb(i); | |
283 bx = one_bits32(i); | |
284 if (ax != bx) | |
285 { | |
286 printf("Test failed: one bits - %d, %x %x\n", i, ax, bx); | |
287 exit(2); | |
288 } | |
289 } | |
290 | |
291 printf("Tests passed.\n"); | |
292 return 0; | |
293 } | |
294 /*- End of function --------------------------------------------------------*/ | |
295 /*- End of file ------------------------------------------------------------*/ |