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>
33 * This function is only declared to show the size of a bitfield write in
38 bt_bitfield_write(&glob
, unsigned int, 12, 15, 0x12345678);
41 /* Test array size, in bytes */
44 #define SIGNED_TEST_DESC_FMT_STR "Writing and reading back 0x%X, signed"
45 #define UNSIGNED_TEST_DESC_FMT_STR "Writing and reading back 0x%X, unsigned"
46 #define DIAG_FMT_STR "Failed reading value written \"%s\"-wise, with start=%i" \
47 " and length=%i. Read %llX"
51 #if defined(__i386) || defined(__x86_64)
53 static inline int fls(int x
)
58 : "=&r" (r
) : "rm" (x
), "rm" (-1));
62 #elif defined(__PPC__)
64 static __inline__
int fls(unsigned int x
)
68 asm ("cntlzw %0,%1" : "=r" (lz
) : "r" (x
));
74 static int fls(unsigned int x
)
80 if (!(x
& 0xFFFF0000U
)) {
84 if (!(x
& 0xFF000000U
)) {
88 if (!(x
& 0xF0000000U
)) {
92 if (!(x
& 0xC0000000U
)) {
96 if (!(x
& 0x80000000U
)) {
105 #define print_byte_array(c, len) \
109 for (i = 0; i < (len); i++) { \
110 printf("0x%X", (c)[i]); \
111 if (i != (len) - 1) \
117 #define init_byte_array(c, len, val) \
121 for (i = 0; i < (len); i++) \
125 #define check_result(ref, val, buffer, typename, start, len, \
128 if ((val) != (ref)) { \
129 fail(desc_fmt_str, ref); \
130 diag(DIAG_FMT_STR, #typename, start, len, val); \
132 print_byte_array(buffer, TEST_LEN); \
137 void run_test_unsigned(void)
139 unsigned int src
, nrbits
;
141 unsigned char c
[TEST_LEN
];
142 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
143 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
144 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
145 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
147 unsigned long long readval
;
153 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
154 for (l
= nrbits
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
155 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
156 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src
);
157 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
158 if (check_result(src
, readval
, target
.c
, unsigned char,
159 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
163 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
164 bt_bitfield_write(target
.s
, unsigned short, s
, l
, src
);
165 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
166 if (check_result(src
, readval
, target
.c
, unsigned short,
167 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
171 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
172 bt_bitfield_write(target
.i
, unsigned int, s
, l
, src
);
173 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
174 if (check_result(src
, readval
, target
.c
, unsigned int,
175 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
179 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
180 bt_bitfield_write(target
.l
, unsigned long, s
, l
, src
);
181 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
182 if (check_result(src
, readval
, target
.c
, unsigned long,
183 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
187 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
188 bt_bitfield_write(target
.ll
, unsigned long long, s
, l
, src
);
189 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
190 if (check_result(src
, readval
, target
.c
, unsigned long long,
191 s
, l
, UNSIGNED_TEST_DESC_FMT_STR
)) {
197 pass(UNSIGNED_TEST_DESC_FMT_STR
, src
);
200 void run_test_signed(void)
204 signed char c
[TEST_LEN
];
205 short s
[TEST_LEN
/sizeof(short)];
206 int i
[TEST_LEN
/sizeof(int)];
207 long l
[TEST_LEN
/sizeof(long)];
208 long long ll
[TEST_LEN
/sizeof(long long)];
214 if (src
& 0x80000000U
)
215 nrbits
= fls(~src
) + 1; /* Find least significant bit conveying sign */
217 nrbits
= fls(src
) + 1; /* Keep sign at 0 */
219 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
220 for (l
= nrbits
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
221 init_byte_array(target
.c
, TEST_LEN
, 0x0);
222 bt_bitfield_write(target
.c
, signed char, s
, l
, src
);
223 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
224 if (check_result(src
, readval
, target
.c
, signed char,
225 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
229 init_byte_array(target
.c
, TEST_LEN
, 0x0);
230 bt_bitfield_write(target
.s
, short, s
, l
, src
);
231 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
232 if (check_result(src
, readval
, target
.c
, short,
233 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
237 init_byte_array(target
.c
, TEST_LEN
, 0x0);
238 bt_bitfield_write(target
.i
, int, s
, l
, src
);
239 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
240 if (check_result(src
, readval
, target
.c
, int,
241 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
245 init_byte_array(target
.c
, TEST_LEN
, 0x0);
246 bt_bitfield_write(target
.l
, long, s
, l
, src
);
247 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
248 if (check_result(src
, readval
, target
.c
, long,
249 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
253 init_byte_array(target
.c
, TEST_LEN
, 0x0);
254 bt_bitfield_write(target
.ll
, long long, s
, l
, src
);
255 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
256 if (check_result(src
, readval
, target
.c
, long long,
257 s
, l
, SIGNED_TEST_DESC_FMT_STR
)) {
263 pass(SIGNED_TEST_DESC_FMT_STR
, src
);
269 plan_tests(NR_TESTS
* 2 + 6);
287 srcrand
= (int)0x80000000U
;
290 for (i
= 0; i
< NR_TESTS
; i
++) {
298 int print_encodings(unsigned long src
, unsigned int shift
, unsigned int len
)
305 unsigned long long ll
[1];
307 unsigned long long readval
;
309 init_byte_array(target
.c
, 8, 0xFF);
310 bt_bitfield_write(target
.c
, unsigned char, shift
, len
, src
);
311 printf("bytewise\n");
312 print_byte_array(target
.c
, 8);
314 init_byte_array(target
.c
, 8, 0xFF);
315 bt_bitfield_write(target
.s
, unsigned short, shift
, len
, src
);
316 printf("shortwise\n");
317 print_byte_array(target
.c
, 8);
319 init_byte_array(target
.c
, 8, 0xFF);
320 bt_bitfield_write(target
.i
, unsigned int, shift
, len
, src
);
322 print_byte_array(target
.c
, 8);
324 init_byte_array(target
.c
, 8, 0xFF);
325 bt_bitfield_write(target
.l
, unsigned long, shift
, len
, src
);
326 printf("longwise\n");
327 print_byte_array(target
.c
, 8);
329 init_byte_array(target
.c
, 8, 0xFF);
330 bt_bitfield_write(target
.ll
, unsigned long long, shift
, len
, src
);
332 print_byte_array(target
.c
, 8);
334 bt_bitfield_read(target
.c
, unsigned char, shift
, len
, &readval
);
335 printf("read: %llX\n", readval
);
336 print_byte_array(target
.c
, 8);
341 int main(int argc
, char **argv
)
344 /* Print encodings */
346 unsigned int shift
, len
;
350 shift
= atoi(argv
[2]);
357 return print_encodings(src
, shift
, len
);
360 /* Run tap-formated tests */
362 return exit_status();