4 * BabelTrace - bitfield test program
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <babeltrace/bitfield.h>
31 * This function is only declared to show the size of a bitfield write in
36 bt_bitfield_write(&glob
, unsigned int, 12, 15, 0x12345678);
39 /* Test array size, in bytes */
45 #if defined(__i386) || defined(__x86_64)
47 static inline int fls(int x
)
52 : "=&r" (r
) : "rm" (x
), "rm" (-1));
56 #elif defined(__PPC__)
58 static __inline__
int fls(unsigned int x
)
62 asm ("cntlzw %0,%1" : "=r" (lz
) : "r" (x
));
68 static int fls(unsigned int x
)
74 if (!(x
& 0xFFFF0000U
)) {
78 if (!(x
& 0xFF000000U
)) {
82 if (!(x
& 0xF0000000U
)) {
86 if (!(x
& 0xC0000000U
)) {
90 if (!(x
& 0x80000000U
)) {
99 #define print_byte_array(c, len) \
103 for (i = 0; i < (len); i++) { \
104 printf("0x%X", (c)[i]); \
105 if (i != (len) - 1) \
111 #define init_byte_array(c, len, val) \
115 for (i = 0; i < (len); i++) \
119 int run_test_unsigned(void)
121 unsigned int src
, nrbits
;
123 unsigned char c
[TEST_LEN
];
124 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
125 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
126 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
127 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
129 unsigned long long readval
;
133 printf("Running unsigned test with 0x%X\n", srcrand
);
138 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
139 for (l
= nrbits
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
140 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
141 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src
);
142 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
143 if (readval
!= src
) {
144 printf("Error (bytewise) src %X read %llX shift %d len %d\n",
146 print_byte_array(target
.c
, TEST_LEN
);
150 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
151 bt_bitfield_write(target
.s
, unsigned short, s
, l
, src
);
152 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
153 if (readval
!= src
) {
154 printf("Error (shortwise) src %X read %llX shift %d len %d\n",
156 print_byte_array(target
.c
, TEST_LEN
);
160 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
161 bt_bitfield_write(target
.i
, unsigned int, s
, l
, src
);
162 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
163 if (readval
!= src
) {
164 printf("Error (intwise) src %X read %llX shift %d len %d\n",
166 print_byte_array(target
.c
, TEST_LEN
);
170 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
171 bt_bitfield_write(target
.l
, unsigned long, s
, l
, src
);
172 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
173 if (readval
!= src
) {
174 printf("Error (longwise) src %X read %llX shift %d len %d\n",
176 print_byte_array(target
.c
, TEST_LEN
);
180 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
181 bt_bitfield_write(target
.ll
, unsigned long long, s
, l
, src
);
182 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
183 if (readval
!= src
) {
184 printf("Error (longlongwise) src %X read %llX shift %d len %d\n",
186 print_byte_array(target
.c
, TEST_LEN
);
192 printf("Success!\n");
198 int run_test_signed(void)
202 signed char c
[TEST_LEN
];
203 short s
[TEST_LEN
/sizeof(short)];
204 int i
[TEST_LEN
/sizeof(int)];
205 long l
[TEST_LEN
/sizeof(long)];
206 long long ll
[TEST_LEN
/sizeof(long long)];
212 printf("Running signed test with 0x%X\n", srcrand
);
215 if (src
& 0x80000000U
)
216 nrbits
= fls(~src
) + 1; /* Find least significant bit conveying sign */
218 nrbits
= fls(src
) + 1; /* Keep sign at 0 */
220 for (s
= 0; s
< 8 * TEST_LEN
; s
++) {
221 for (l
= nrbits
; l
< (8 * TEST_LEN
) - s
; l
++) {
222 init_byte_array(target
.c
, TEST_LEN
, 0x0);
223 bt_bitfield_write(target
.c
, signed char, s
, l
, src
);
224 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
225 if (readval
!= src
) {
226 printf("Error (bytewise) src %X read %llX shift %d len %d\n",
228 print_byte_array(target
.c
, TEST_LEN
);
232 init_byte_array(target
.c
, TEST_LEN
, 0x0);
233 bt_bitfield_write(target
.s
, short, s
, l
, src
);
234 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
235 if (readval
!= src
) {
236 printf("Error (shortwise) src %X read %llX shift %d len %d\n",
238 print_byte_array(target
.c
, TEST_LEN
);
242 init_byte_array(target
.c
, TEST_LEN
, 0x0);
243 bt_bitfield_write(target
.i
, int, s
, l
, src
);
244 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
245 if (readval
!= src
) {
246 printf("Error (intwise) src %X read %llX shift %d len %d\n",
248 print_byte_array(target
.c
, TEST_LEN
);
252 init_byte_array(target
.c
, TEST_LEN
, 0x0);
253 bt_bitfield_write(target
.l
, long, s
, l
, src
);
254 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
255 if (readval
!= src
) {
256 printf("Error (longwise) src %X read %llX shift %d len %d\n",
258 print_byte_array(target
.c
, TEST_LEN
);
262 init_byte_array(target
.c
, TEST_LEN
, 0x0);
263 bt_bitfield_write(target
.ll
, long long, s
, l
, src
);
264 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
265 if (readval
!= src
) {
266 printf("Error (longlongwise) src %X read %llX shift %d len %d\n",
268 print_byte_array(target
.c
, TEST_LEN
);
274 printf("Success!\n");
288 err
|= run_test_unsigned();
290 err
|= run_test_signed();
292 err
|= run_test_unsigned();
294 err
|= run_test_unsigned();
296 err
|= run_test_signed();
297 srcrand
= (int)0x80000000U
;
298 err
|= run_test_signed();
300 for (i
= 0; i
< NR_TESTS
; i
++) {
302 err
|= run_test_unsigned();
303 err
|= run_test_signed();
308 int main(int argc
, char **argv
)
311 unsigned int shift
, len
;
318 unsigned long long ll
[1];
320 unsigned long long readval
;
327 shift
= atoi(argv
[2]);
335 target
.i
[0] = 0xFFFFFFFF;
336 target
.i
[1] = 0xFFFFFFFF;
337 bt_bitfield_write(target
.c
, unsigned char, shift
, len
, src
);
338 printf("bytewise\n");
339 print_byte_array(target
.c
, 8);
341 target
.i
[0] = 0xFFFFFFFF;
342 target
.i
[1] = 0xFFFFFFFF;
343 bt_bitfield_write(target
.s
, unsigned short, shift
, len
, src
);
344 printf("shortwise\n");
345 print_byte_array(target
.c
, 8);
347 target
.i
[0] = 0xFFFFFFFF;
348 target
.i
[1] = 0xFFFFFFFF;
349 bt_bitfield_write(target
.i
, unsigned int, shift
, len
, src
);
351 print_byte_array(target
.c
, 8);
353 target
.i
[0] = 0xFFFFFFFF;
354 target
.i
[1] = 0xFFFFFFFF;
355 bt_bitfield_write(target
.l
, unsigned long, shift
, len
, src
);
356 printf("longwise\n");
357 print_byte_array(target
.c
, 8);
359 target
.i
[0] = 0xFFFFFFFF;
360 target
.i
[1] = 0xFFFFFFFF;
361 bt_bitfield_write(target
.ll
, unsigned long long, shift
, len
, src
);
363 print_byte_array(target
.c
, 8);
365 bt_bitfield_read(target
.c
, unsigned char, shift
, len
, &readval
);
366 printf("read: %llX\n", readval
);
This page took 0.036824 seconds and 4 git commands to generate.