File: bit_operations_tests.c

package info (click to toggle)
spandsp 0.0.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 15,796 kB
  • sloc: ansic: 123,211; xml: 13,229; sh: 12,189; cpp: 1,521; makefile: 1,097
file content (293 lines) | stat: -rw-r--r-- 7,288 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * SpanDSP - a series of DSP components for telephony
 *
 * bit_operations_tests.c
 *
 * Written by Steve Underwood <steveu@coppice.org>
 *
 * Copyright (C) 2006 Steve Underwood
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*! \page bit_operations_tests_page Bit operations tests
\section bit_operations_tests_page_sec_1 What does it do?
These tests check the operation of efficient bit manipulation routines, by comparing
their operation with very dumb brute force versions of the same functionality.

\section bit_operations_tests_page_sec_2 How is it used?
*/

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>

#include "spandsp.h"

uint8_t from[1000000];
uint8_t to[1000000];

static __inline__ int top_bit_dumb(unsigned int data)
{
    int i;
    
    if (data == 0)
        return -1;
    for (i = 31;  i >= 0;  i--)
    {
        if ((data & (1 << i)))
            return i;
    }
    return -1;
}
/*- End of function --------------------------------------------------------*/

static __inline__ int bottom_bit_dumb(unsigned int data)
{
    int i;
    
    if (data == 0)
        return -1;
    for (i = 0;  i < 32;  i++)
    {
        if ((data & (1 << i)))
            return i;
    }
    return -1;
}
/*- End of function --------------------------------------------------------*/

static __inline__ uint8_t bit_reverse8_dumb(uint8_t data)
{
    int i;
    int result;
    
    result = 0;
    for (i = 0;  i < 8;  i++)
    {
        result = (result << 1) | (data & 1); 
        data >>= 1;
    }
    return result;
}
/*- End of function --------------------------------------------------------*/

static __inline__ uint32_t bit_reverse_4bytes_dumb(uint32_t data)
{
    int i;
    uint32_t result;
    
    result = 0;
    for (i = 0;  i < 8;  i++)
    {
        result = (result << 1) | (data & 0x01010101); 
        data >>= 1;
    }
    return result;
}
/*- End of function --------------------------------------------------------*/

static __inline__ uint16_t bit_reverse16_dumb(uint16_t data)
{
    int i;
    uint16_t result;
    
    result = 0;
    for (i = 0;  i < 16;  i++)
    {
        result = (result << 1) | (data & 1); 
        data >>= 1;
    }
    return result;
}
/*- End of function --------------------------------------------------------*/

static __inline__ uint32_t bit_reverse32_dumb(uint32_t data)
{
    int i;
    uint32_t result;
    
    result = 0;
    for (i = 0;  i < 32;  i++)
    {
        result = (result << 1) | (data & 1); 
        data >>= 1;
    }
    return result;
}
/*- End of function --------------------------------------------------------*/

static __inline__ int parity8_dumb(uint8_t x)
{
    uint8_t y;
    int i;

    for (y = 0, i = 0;  i < 8;  i++)
    {
        y ^= (x & 1);
        x >>= 1;
    }
    return y;
}
/*- End of function --------------------------------------------------------*/

static __inline__ int one_bits32_dumb(uint32_t x)
{
    int i;
    int bits;
    
    bits = 0;
    for (i = 0;  i < 32;  i++)
    {
        if (x & 1)
            bits++;
        x >>= 1;
    }
    return bits;
}
/*- End of function --------------------------------------------------------*/
    
int main(int argc, char *argv[])
{
    int i;
    uint32_t x;
    uint8_t ax;
    uint8_t bx;
    uint16_t ax16;
    uint16_t bx16;
    uint32_t ax32;
    uint32_t bx32;

    for (i = 0, x = 0;  i < 100000;  i++)
    {
        ax = top_bit_dumb(x);
        bx = top_bit(x);
        if (ax != bx)
        {
            printf("Test failed: top bit mismatch 0x%" PRIx32 " -> %u %u\n", x, ax, bx);
            exit(2);
        }
        ax = bottom_bit_dumb(x);
        bx = bottom_bit(x);
        if (ax != bx)
        {
            printf("Test failed: bottom bit mismatch 0x%" PRIx32 " -> %u %u\n", x, ax, bx);
            exit(2);
        }
        x = rand();
    }
    for (i = 0;  i < 256;  i++)
    {
        ax = bit_reverse8_dumb(i);
        bx = bit_reverse8(i);
        if (ax != bx)
        {
            printf("Test failed: bit reverse 8 - %02x %02x %02x\n", i, ax, bx);
            exit(2);
        }
    }
    for (i = 0;  i < 1000000;  i++)
        from[i] = rand();
    bit_reverse(to, from, 1000000);
    for (i = 0;  i < 1000000;  i++)
    {
        if (bit_reverse8_dumb(from[i]) != to[i])
        {
            printf("Test failed: bit reverse - at %d, %02x %02x %02x\n", i, from[i], bit_reverse8(from[i]), to[i]);
            exit(2);
        }
    }
    for (i = 0;  i < 256;  i++)
    {
        x = i | (((i + 1) & 0xFF) << 8) | (((i + 2) & 0xFF) << 16) | (((i + 3) & 0xFF) << 24);
        ax32 = bit_reverse_4bytes_dumb(x);
        bx32 = bit_reverse_4bytes(x);
        if (ax32 != bx32)
        {
            printf("Test failed: bit reverse 4 bytes - %" PRIx32 " %" PRIx32 " %" PRIx32 "\n", x, ax32, bx32);
            exit(2);
        }
    }
    for (i = 0;  i < 65536;  i++)
    {
        ax16 = bit_reverse16_dumb(i);
        bx16 = bit_reverse16(i);
        if (ax16 != bx16)
        {
            printf("Test failed: bit reverse 16 - %x %x %x\n", i, ax16, bx16);
            exit(2);
        }
    }
    for (i = 0;  i < 0x7FFFFF00;  i += 127)
    {
        ax32 = bit_reverse32_dumb(i);
        bx32 = bit_reverse32(i);
        if (ax32 != bx32)
        {
            printf("Test failed: bit reverse 32 - %d %" PRIx32 " %" PRIx32 "\n", i, ax32, bx32);
            exit(2);
        }
    }

    for (i = 0;  i < 256;  i++)
    {
        ax = parity8(i);
        bx = parity8_dumb(i);
        if (ax != bx)
        {
            printf("Test failed: parity 8 - %x %x %x\n", i, ax, bx);
            exit(2);
        }
    }

    for (i = -1;  i < 32;  i++)
    {
        ax32 = most_significant_one32(1 << i);
        if (ax32 != (1 << i))
        {
            printf("Test failed: most significant one 32 - %x %" PRIx32 " %x\n", i, ax32, (1 << i));
            exit(2);
        }
        ax32 = least_significant_one32(1 << i);
        if (ax32 != (1 << i))
        {
            printf("Test failed: least significant one 32 - %x %" PRIx32 " %x\n", i, ax32, (1 << i));
            exit(2);
        }
    }

    for (i = 0x80000000;  i < 0x800FFFFF;  i++)
    {
        ax = one_bits32_dumb(i);
        bx = one_bits32(i);
        if (ax != bx)
        {
            printf("Test failed: one bits - %d, %x %x\n", i, ax, bx);
            exit(2);
        }
    }

    printf("Tests passed.\n");
    return 0;
}
/*- End of function --------------------------------------------------------*/
/*- End of file ------------------------------------------------------------*/