Babeltrace type: build warning cleanup
[babeltrace.git] / tests / test-bitfield.c
CommitLineData
6dc2ca62
MD
1/*
2 * test-bitfield.c
3 *
d79865b9 4 * BabelTrace - bitfield test program
6dc2ca62 5 *
de0ba614 6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6dc2ca62
MD
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#define _GNU_SOURCE
d79865b9 24#include <babeltrace/bitfield.h>
6dc2ca62
MD
25#include <stdio.h>
26
27unsigned int glob;
28
29/*
30 * This function is only declared to show the size of a bitfield write in
31 * objdump.
32 */
33void fct(void)
34{
d79865b9 35 bt_bitfield_write(&glob, 12, 15, 0x12345678);
6dc2ca62
MD
36}
37
38/* Test array size, in bytes */
39#define TEST_LEN 128
40#define NR_TESTS 10
41
42unsigned int srcrand;
43
44#if defined(__i386) || defined(__x86_64)
45
46static inline int fls(int x)
47{
48 int r;
49 asm("bsrl %1,%0\n\t"
50 "cmovzl %2,%0"
51 : "=&r" (r) : "rm" (x), "rm" (-1));
52 return r + 1;
53}
54
55#elif defined(__PPC__)
56
57static __inline__ int fls(unsigned int x)
58{
59 int lz;
60
61 asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
62 return 32 - lz;
63}
64
65#else
66
67static int fls(unsigned int x)
68{
69 int r = 32;
70
71 if (!x)
72 return 0;
73 if (!(x & 0xFFFF0000U)) {
74 x <<= 16;
75 r -= 16;
76 }
77 if (!(x & 0xFF000000U)) {
78 x <<= 8;
79 r -= 8;
80 }
81 if (!(x & 0xF0000000U)) {
82 x <<= 4;
83 r -= 4;
84 }
85 if (!(x & 0xC0000000U)) {
86 x <<= 2;
87 r -= 2;
88 }
89 if (!(x & 0x80000000U)) {
90 x <<= 1;
91 r -= 1;
92 }
93 return r;
94}
95
96#endif
97
98static void print_byte_array(const unsigned char *c, unsigned long len)
99{
100 unsigned long i;
101
102 for (i = 0; i < len; i++) {
103 printf("0x%X", c[i]);
104 if (i != len - 1)
105 printf(" ");
106 }
107 printf("\n");
108}
109
110static void init_byte_array(unsigned char *c,
111 unsigned long len,
112 unsigned char val)
113{
114 unsigned long i;
115
116 for (i = 0; i < len; i++)
117 c[i] = val;
118}
119
120int run_test_unsigned(void)
121{
122 unsigned int src, nrbits;
123 union {
124 unsigned char c[TEST_LEN];
125 unsigned short s[TEST_LEN/sizeof(unsigned short)];
126 unsigned int i[TEST_LEN/sizeof(unsigned int)];
127 unsigned long l[TEST_LEN/sizeof(unsigned long)];
128 unsigned long long ll[TEST_LEN/sizeof(unsigned long long)];
129 } target;
130 uint64_t readval;
131 unsigned int s, l;
132 int err = 0;
133
134 printf("Running unsigned test with 0x%X\n", srcrand);
135
136 src = srcrand;
137 nrbits = fls(src);
138
139 for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
140 for (l = nrbits; l < (CHAR_BIT * TEST_LEN) - s; l++) {
141 init_byte_array(target.c, TEST_LEN, 0xFF);
d79865b9
MD
142 bt_bitfield_write(target.c, s, l, src);
143 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
144 if (readval != src) {
145 printf("Error (bytewise) src %lX read %llX shift %d len %d\n",
146 src, readval, s, l);
147 print_byte_array(target.c, TEST_LEN);
148 err = 1;
149 }
150
151 init_byte_array(target.c, TEST_LEN, 0xFF);
d79865b9
MD
152 bt_bitfield_write(target.s, s, l, src);
153 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
154 if (readval != src) {
155 printf("Error (shortwise) src %lX read %llX shift %d len %d\n",
156 src, readval, s, l);
157 print_byte_array(target.c, TEST_LEN);
158 err = 1;
159 }
160
161 init_byte_array(target.c, TEST_LEN, 0xFF);
d79865b9
MD
162 bt_bitfield_write(target.i, s, l, src);
163 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
164 if (readval != src) {
165 printf("Error (intwise) src %lX read %llX shift %d len %d\n",
166 src, readval, s, l);
167 print_byte_array(target.c, TEST_LEN);
168 err = 1;
169 }
170
171 init_byte_array(target.c, TEST_LEN, 0xFF);
d79865b9
MD
172 bt_bitfield_write(target.l, s, l, src);
173 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
174 if (readval != src) {
175 printf("Error (longwise) src %lX read %llX shift %d len %d\n",
176 src, readval, s, l);
177 print_byte_array(target.c, TEST_LEN);
178 err = 1;
179 }
180
181 init_byte_array(target.c, TEST_LEN, 0xFF);
d79865b9
MD
182 bt_bitfield_write(target.ll, s, l, src);
183 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
184 if (readval != src) {
185 printf("Error (longlongwise) src %lX read %llX shift %d len %d\n",
186 src, readval, s, l);
187 print_byte_array(target.c, TEST_LEN);
188 err = 1;
189 }
190 }
191 }
192 if (!err)
193 printf("Success!\n");
194 else
195 printf("Failed!\n");
196 return err;
197}
198
199int run_test_signed(void)
200{
201 int src, nrbits;
202 union {
203 char c[TEST_LEN];
204 short s[TEST_LEN/sizeof(short)];
205 int i[TEST_LEN/sizeof(int)];
206 long l[TEST_LEN/sizeof(long)];
207 long long ll[TEST_LEN/sizeof(long long)];
208 } target;
209 int64_t readval;
210 unsigned int s, l;
211 int err = 0;
212
213 printf("Running signed test with 0x%X\n", srcrand);
214
215 src = srcrand;
216 if (src & 0x80000000U)
217 nrbits = fls(~src) + 1; /* Find least significant bit conveying sign */
218 else
219 nrbits = fls(src) + 1; /* Keep sign at 0 */
220
221 for (s = 0; s < 8 * TEST_LEN; s++) {
222 for (l = nrbits; l < (8 * TEST_LEN) - s; l++) {
223 init_byte_array(target.c, TEST_LEN, 0x0);
d79865b9
MD
224 bt_bitfield_write(target.c, s, l, src);
225 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
226 if (readval != src) {
227 printf("Error (bytewise) src %lX read %llX shift %d len %d\n",
228 src, readval, s, l);
229 print_byte_array(target.c, TEST_LEN);
230 err = 1;
231 }
232
233 init_byte_array(target.c, TEST_LEN, 0x0);
d79865b9
MD
234 bt_bitfield_write(target.s, s, l, src);
235 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
236 if (readval != src) {
237 printf("Error (shortwise) src %lX read %llX shift %d len %d\n",
238 src, readval, s, l);
239 print_byte_array(target.c, TEST_LEN);
240 err = 1;
241 }
242
243 init_byte_array(target.c, TEST_LEN, 0x0);
d79865b9
MD
244 bt_bitfield_write(target.i, s, l, src);
245 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
246 if (readval != src) {
247 printf("Error (intwise) src %lX read %llX shift %d len %d\n",
248 src, readval, s, l);
249 print_byte_array(target.c, TEST_LEN);
250 err = 1;
251 }
252
253 init_byte_array(target.c, TEST_LEN, 0x0);
d79865b9
MD
254 bt_bitfield_write(target.l, s, l, src);
255 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
256 if (readval != src) {
257 printf("Error (longwise) src %lX read %llX shift %d len %d\n",
258 src, readval, s, l);
259 print_byte_array(target.c, TEST_LEN);
260 err = 1;
261 }
262
263 init_byte_array(target.c, TEST_LEN, 0x0);
d79865b9
MD
264 bt_bitfield_write(target.ll, s, l, src);
265 bt_bitfield_read(target.c, s, l, &readval);
6dc2ca62
MD
266 if (readval != src) {
267 printf("Error (longlongwise) src %lX read %llX shift %d len %d\n",
268 src, readval, s, l);
269 print_byte_array(target.c, TEST_LEN);
270 err = 1;
271 }
272 }
273 }
274 if (!err)
275 printf("Success!\n");
276 else
277 printf("Failed!\n");
278 return err;
279}
280
281int run_test(void)
282{
283 int err = 0;
284 int i;
285
286 srand(time(NULL));
287
288 srcrand = 0;
289 err |= run_test_unsigned();
290 srcrand = 0;
291 err |= run_test_signed();
292 srcrand = 1;
293 err |= run_test_unsigned();
294 srcrand = ~0U;
295 err |= run_test_unsigned();
296 srcrand = -1;
297 err |= run_test_signed();
298 srcrand = (int)0x80000000U;
299 err |= run_test_signed();
300
301 for (i = 0; i < NR_TESTS; i++) {
302 srcrand = rand();
303 err |= run_test_unsigned();
304 err |= run_test_signed();
305 }
306 return err;
307}
308
309int main(int argc, char **argv)
310{
311 unsigned long src;
312 unsigned int shift, len;
313 int ret;
314 union {
315 unsigned char c[8];
316 unsigned short s[4];
317 unsigned int i[2];
318 unsigned long l[2];
319 unsigned long long ll[1];
320 } target;
321 uint64_t readval;
322
323 if (argc > 1)
324 src = atoi(argv[1]);
325 else
326 src = 0x12345678;
327 if (argc > 2)
328 shift = atoi(argv[2]);
329 else
330 shift = 12;
331 if (argc > 3)
332 len = atoi(argv[3]);
333 else
334 len = 40;
335
336 target.i[0] = 0xFFFFFFFF;
337 target.i[1] = 0xFFFFFFFF;
d79865b9 338 bt_bitfield_write(target.c, shift, len, src);
6dc2ca62
MD
339 printf("bytewise\n");
340 print_byte_array(target.c, 8);
341
342 target.i[0] = 0xFFFFFFFF;
343 target.i[1] = 0xFFFFFFFF;
d79865b9 344 bt_bitfield_write(target.s, shift, len, src);
6dc2ca62
MD
345 printf("shortwise\n");
346 print_byte_array(target.c, 8);
347
348 target.i[0] = 0xFFFFFFFF;
349 target.i[1] = 0xFFFFFFFF;
d79865b9 350 bt_bitfield_write(target.i, shift, len, src);
6dc2ca62
MD
351 printf("intwise\n");
352 print_byte_array(target.c, 8);
353
354 target.i[0] = 0xFFFFFFFF;
355 target.i[1] = 0xFFFFFFFF;
d79865b9 356 bt_bitfield_write(target.l, shift, len, src);
6dc2ca62
MD
357 printf("longwise\n");
358 print_byte_array(target.c, 8);
359
360 target.i[0] = 0xFFFFFFFF;
361 target.i[1] = 0xFFFFFFFF;
d79865b9 362 bt_bitfield_write(target.ll, shift, len, src);
6dc2ca62
MD
363 printf("lluwise\n");
364 print_byte_array(target.c, 8);
365
d79865b9 366 bt_bitfield_read(target.c, shift, len, &readval);
6dc2ca62
MD
367 printf("read: %llX\n", readval);
368
369 ret = run_test();
370
371 return ret;
372}
This page took 0.057984 seconds and 4 git commands to generate.