comparison spandsp-0.0.3/spandsp-0.0.3/tests/bit_operations_tests.c @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
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.8 2006/11/24 12:34:55 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 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <inttypes.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <fcntl.h>
44 #include <string.h>
45 #if defined(HAVE_TGMATH_H)
46 #include <tgmath.h>
47 #endif
48 #if defined(HAVE_MATH_H)
49 #include <math.h>
50 #endif
51 #include <assert.h>
52 #include <audiofile.h>
53 #include <tiffio.h>
54
55 #include "spandsp.h"
56
57 static __inline__ int top_bit_dumb(unsigned int data)
58 {
59 int i;
60
61 if (data == 0)
62 return -1;
63 for (i = 31; i >= 0; i--)
64 {
65 if ((data & (1 << i)))
66 return i;
67 }
68 return -1;
69 }
70 /*- End of function --------------------------------------------------------*/
71
72 static __inline__ int bottom_bit_dumb(unsigned int data)
73 {
74 int i;
75
76 if (data == 0)
77 return -1;
78 for (i = 0; i < 32; i++)
79 {
80 if ((data & (1 << i)))
81 return i;
82 }
83 return -1;
84 }
85 /*- End of function --------------------------------------------------------*/
86
87 static __inline__ uint8_t bit_reverse8_dumb(uint8_t data)
88 {
89 int i;
90 int result;
91
92 result = 0;
93 for (i = 0; i < 8; i++)
94 {
95 result = (result << 1) | (data & 1);
96 data >>= 1;
97 }
98 return result;
99 }
100 /*- End of function --------------------------------------------------------*/
101
102 static __inline__ uint32_t bit_reverse_4bytes_dumb(uint32_t data)
103 {
104 int i;
105 uint32_t result;
106
107 result = 0;
108 for (i = 0; i < 8; i++)
109 {
110 result = (result << 1) | (data & 0x01010101);
111 data >>= 1;
112 }
113 return result;
114 }
115 /*- End of function --------------------------------------------------------*/
116
117 static __inline__ uint16_t bit_reverse16_dumb(uint16_t data)
118 {
119 int i;
120 uint16_t result;
121
122 result = 0;
123 for (i = 0; i < 16; i++)
124 {
125 result = (result << 1) | (data & 1);
126 data >>= 1;
127 }
128 return result;
129 }
130 /*- End of function --------------------------------------------------------*/
131
132 static __inline__ uint32_t bit_reverse32_dumb(uint32_t data)
133 {
134 int i;
135 uint32_t result;
136
137 result = 0;
138 for (i = 0; i < 32; i++)
139 {
140 result = (result << 1) | (data & 1);
141 data >>= 1;
142 }
143 return result;
144 }
145 /*- End of function --------------------------------------------------------*/
146
147 static __inline__ int parity8_dumb(uint8_t x)
148 {
149 uint8_t y;
150 int i;
151
152 for (y = 0, i = 0; i < 8; i++)
153 {
154 y ^= (x & 1);
155 x >>= 1;
156 }
157 return y;
158 }
159 /*- End of function --------------------------------------------------------*/
160
161 static __inline__ int one_bits32_dumb(uint32_t x)
162 {
163 int i;
164 int bits;
165
166 bits = 0;
167 for (i = 0; i < 32; i++)
168 {
169 if (x & 1)
170 bits++;
171 x >>= 1;
172 }
173 return bits;
174 }
175 /*- End of function --------------------------------------------------------*/
176
177 int main(int argc, char *argv[])
178 {
179 int i;
180 uint32_t x;
181 uint8_t ax;
182 uint8_t bx;
183 uint16_t ax16;
184 uint16_t bx16;
185 uint32_t ax32;
186 uint32_t bx32;
187
188 for (i = 0, x = 0; i < 100000; i++)
189 {
190 ax = top_bit_dumb(x);
191 bx = top_bit(x);
192 if (ax != bx)
193 {
194 printf("Test failed: top bit mismatch 0x%" PRIx32 " -> %u %u\n", x, ax, bx);
195 exit(2);
196 }
197 ax = bottom_bit_dumb(x);
198 bx = bottom_bit(x);
199 if (ax != bx)
200 {
201 printf("Test failed: bottom bit mismatch 0x%" PRIx32 " -> %u %u\n", x, ax, bx);
202 exit(2);
203 }
204 x = rand();
205 }
206 for (i = 0; i < 256; i++)
207 {
208 ax = bit_reverse8_dumb(i);
209 bx = bit_reverse8(i);
210 if (ax != bx)
211 {
212 printf("Test failed: bit reverse 8 - %02x %02x %02x\n", i, ax, bx);
213 exit(2);
214 }
215 }
216 for (i = 0; i < 256; i++)
217 {
218 x = i | (i << 8) | (i << 16) | (i << 24);
219 ax32 = bit_reverse_4bytes_dumb(x);
220 bx32 = bit_reverse_4bytes(x);
221 if (ax32 != bx32)
222 {
223 printf("Test failed: bit reverse 4 bytes - %" PRIx32 " %" PRIx32 " %" PRIx32 "\n", x, ax32, bx32);
224 exit(2);
225 }
226 }
227 for (i = 0; i < 65536; i++)
228 {
229 ax16 = bit_reverse16_dumb(i);
230 bx16 = bit_reverse16(i);
231 if (ax16 != bx16)
232 {
233 printf("Test failed: bit reverse 16 - %x %x %x\n", i, ax16, bx16);
234 exit(2);
235 }
236 }
237 for (i = 0; i < 0x7FFFFF00; i += 127)
238 {
239 ax32 = bit_reverse32_dumb(i);
240 bx32 = bit_reverse32(i);
241 if (ax32 != bx32)
242 {
243 printf("Test failed: bit reverse 32 - %d %" PRIx32 " %" PRIx32 "\n", i, ax32, bx32);
244 exit(2);
245 }
246 }
247
248 for (i = 0; i < 256; i++)
249 {
250 ax = parity8(i);
251 bx = parity8_dumb(i);
252 if (ax != bx)
253 {
254 printf("Test failed: parity 8 - %x %x %x\n", i, ax, bx);
255 exit(2);
256 }
257 }
258
259 for (i = -1; i < 32; i++)
260 {
261 ax32 = most_significant_one32(1 << i);
262 if (ax32 != (1 << i))
263 {
264 printf("Test failed: most significant one 32 - %x %" PRIx32 " %x\n", i, ax32, (1 << i));
265 exit(2);
266 }
267 ax32 = least_significant_one32(1 << i);
268 if (ax32 != (1 << i))
269 {
270 printf("Test failed: least significant one 32 - %x %" PRIx32 " %x\n", i, ax32, (1 << i));
271 exit(2);
272 }
273 }
274
275 for (i = 0x80000000; i < 0x800FFFFF; i++)
276 {
277 ax = one_bits32_dumb(i);
278 bx = one_bits32(i);
279 if (ax != bx)
280 {
281 printf("Test failed: one bits - %d, %x %x\n", i, ax, bx);
282 exit(2);
283 }
284 }
285
286 printf("Tests passed.\n");
287 return 0;
288 }
289 /*- End of function --------------------------------------------------------*/
290 /*- End of file ------------------------------------------------------------*/

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