4 * BabelTrace - bitfield test program
6 * Copyright 2010-2019 - 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.
22 #include <babeltrace/bitfield-internal.h>
32 * This function is only declared to show the size of a bitfield write in
37 bt_bitfield_write(&glob
, unsigned int, 12, 15, 0x12345678);
40 /* Test array size, in bytes */
43 #define SIGNED_INT_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%X, signed int dest, varying read unit size"
44 #define SIGNED_INT_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%X, signed int source, varying write unit size"
45 #define SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, signed long long dest, varying read unit size"
46 #define SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, signed long long source, varying write unit size"
47 #define UNSIGNED_INT_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%X, unsigned int dest, varying read unit size"
48 #define UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%X, unsigned int source, varying write unit size"
49 #define UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, unsigned long long dest, varying read unit size"
50 #define UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR "Writing and reading back 0x%llX, unsigned long long source, varying write unit size"
51 #define DIAG_FMT_STR "Failed reading value written \"%s\"-wise, with start=%i" \
52 " and length=%i. Read %llX"
55 unsigned int fls_u64(uint64_t x
)
62 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
66 if (!(x
& 0xFFFF000000000000ULL
)) {
70 if (!(x
& 0xFF00000000000000ULL
)) {
74 if (!(x
& 0xF000000000000000ULL
)) {
78 if (!(x
& 0xC000000000000000ULL
)) {
82 if (!(x
& 0x8000000000000000ULL
)) {
90 unsigned int fls_u32(uint32_t x
)
96 if (!(x
& 0xFFFF0000U
)) {
100 if (!(x
& 0xFF000000U
)) {
104 if (!(x
& 0xF0000000U
)) {
108 if (!(x
& 0xC0000000U
)) {
112 if (!(x
& 0x80000000U
)) {
119 #define print_byte_array(c, len) \
123 for (i = 0; i < (len); i++) { \
124 printf("0x%X", (c)[i]); \
125 if (i != (len) - 1) \
131 #define init_byte_array(c, len, val) \
135 for (i = 0; i < (len); i++) \
139 #define check_result(ref, val, buffer, typename, start, len, \
142 if ((val) != (ref)) { \
143 fail(desc_fmt_str, ref); \
144 diag(DIAG_FMT_STR, #typename, start, len, val); \
146 print_byte_array(buffer, TEST_LEN); \
151 void run_test_unsigned_write(unsigned int src_ui
, unsigned long long src_ull
)
153 unsigned int nrbits_ui
, nrbits_ull
;
155 unsigned char c
[TEST_LEN
];
156 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
157 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
158 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
159 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
161 unsigned long long readval
;
164 nrbits_ui
= fls_u32(src_ui
);
166 /* Write from unsigned integer src input. */
167 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
168 for (l
= nrbits_ui
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
169 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
170 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
171 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
172 if (check_result(src_ui
, readval
, target
.c
, unsigned char,
173 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
177 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
178 bt_bitfield_write(target
.s
, unsigned short, s
, l
, src_ui
);
179 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
180 if (check_result(src_ui
, readval
, target
.c
, unsigned short,
181 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
185 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
186 bt_bitfield_write(target
.i
, unsigned int, s
, l
, src_ui
);
187 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
188 if (check_result(src_ui
, readval
, target
.c
, unsigned int,
189 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
193 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
194 bt_bitfield_write(target
.l
, unsigned long, s
, l
, src_ui
);
195 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
196 if (check_result(src_ui
, readval
, target
.c
, unsigned long,
197 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
201 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
202 bt_bitfield_write(target
.ll
, unsigned long long, s
, l
, src_ui
);
203 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
204 if (check_result(src_ui
, readval
, target
.c
, unsigned long long,
205 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
210 pass(UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
, src_ui
);
212 nrbits_ull
= fls_u64(src_ull
);
214 /* Write from unsigned long long src input. */
215 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
216 for (l
= nrbits_ull
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
217 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
218 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
219 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
220 if (check_result(src_ull
, readval
, target
.c
, unsigned char,
221 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
225 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
226 bt_bitfield_write(target
.s
, unsigned short, s
, l
, src_ull
);
227 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
228 if (check_result(src_ull
, readval
, target
.c
, unsigned short,
229 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
233 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
234 bt_bitfield_write(target
.i
, unsigned int, s
, l
, src_ull
);
235 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
236 if (check_result(src_ull
, readval
, target
.c
, unsigned int,
237 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
241 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
242 bt_bitfield_write(target
.l
, unsigned long, s
, l
, src_ull
);
243 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
244 if (check_result(src_ull
, readval
, target
.c
, unsigned long,
245 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
249 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
250 bt_bitfield_write(target
.ll
, unsigned long long, s
, l
, src_ull
);
251 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
252 if (check_result(src_ull
, readval
, target
.c
, unsigned long long,
253 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
258 pass(UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
, src_ull
);
261 void run_test_unsigned_read(unsigned int src_ui
, unsigned long long src_ull
)
263 unsigned int nrbits_ui
, nrbits_ull
, readval_ui
;
265 unsigned char c
[TEST_LEN
];
266 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
267 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
268 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
269 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
271 unsigned long long readval_ull
;
274 nrbits_ui
= fls_u32(src_ui
);
276 /* Read to unsigned integer readval output. */
277 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
278 for (l
= nrbits_ui
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
279 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
280 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
281 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval_ui
);
282 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned char,
283 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
287 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
288 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
289 bt_bitfield_read(target
.s
, unsigned short, s
, l
, &readval_ui
);
290 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned short,
291 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
295 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
296 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
297 bt_bitfield_read(target
.i
, unsigned int, s
, l
, &readval_ui
);
298 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned int,
299 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
303 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
304 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
305 bt_bitfield_read(target
.l
, unsigned long, s
, l
, &readval_ui
);
306 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned long,
307 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
311 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
312 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
313 bt_bitfield_read(target
.ll
, unsigned long long, s
, l
, &readval_ui
);
314 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned long long,
315 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
320 pass(UNSIGNED_INT_READ_TEST_DESC_FMT_STR
, src_ui
);
322 nrbits_ull
= fls_u64(src_ull
);
324 /* Read to unsigned long long readval output. */
325 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
326 for (l
= nrbits_ull
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
327 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
328 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
329 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval_ull
);
330 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned char,
331 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
335 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
336 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
337 bt_bitfield_read(target
.s
, unsigned short, s
, l
, &readval_ull
);
338 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned short,
339 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
343 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
344 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
345 bt_bitfield_read(target
.i
, unsigned int, s
, l
, &readval_ull
);
346 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned int,
347 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
351 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
352 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
353 bt_bitfield_read(target
.l
, unsigned long, s
, l
, &readval_ull
);
354 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned long,
355 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
359 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
360 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
361 bt_bitfield_read(target
.ll
, unsigned long long, s
, l
, &readval_ull
);
362 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned long long,
363 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
368 pass(UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
, src_ull
);
371 void run_test_unsigned(unsigned int src_ui
, unsigned long long src_ull
)
373 run_test_unsigned_write(src_ui
, src_ull
);
374 run_test_unsigned_read(src_ui
, src_ull
);
377 void run_test_signed_write(int src_i
, long long src_ll
)
379 int nrbits_i
, nrbits_ll
;
381 signed char c
[TEST_LEN
];
382 short s
[TEST_LEN
/sizeof(short)];
383 int i
[TEST_LEN
/sizeof(int)];
384 long l
[TEST_LEN
/sizeof(long)];
385 long long ll
[TEST_LEN
/sizeof(long long)];
391 nrbits_i
= 0; /* The number of bits needed to represent 0 is 0. */
392 else if (src_i
& 0x80000000U
)
393 nrbits_i
= fls_u32(~src_i
) + 1; /* Find least significant bit conveying sign */
395 nrbits_i
= fls_u32(src_i
) + 1; /* Keep sign at 0 */
397 /* Write from signed integer src input. */
398 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
399 for (l
= nrbits_i
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
400 init_byte_array(target
.c
, TEST_LEN
, 0x0);
401 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
402 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
403 if (check_result(src_i
, readval
, target
.c
, signed char,
404 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
408 init_byte_array(target
.c
, TEST_LEN
, 0x0);
409 bt_bitfield_write(target
.s
, short, s
, l
, src_i
);
410 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
411 if (check_result(src_i
, readval
, target
.c
, short,
412 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
416 init_byte_array(target
.c
, TEST_LEN
, 0x0);
417 bt_bitfield_write(target
.i
, int, s
, l
, src_i
);
418 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
419 if (check_result(src_i
, readval
, target
.c
, int,
420 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
424 init_byte_array(target
.c
, TEST_LEN
, 0x0);
425 bt_bitfield_write(target
.l
, long, s
, l
, src_i
);
426 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
427 if (check_result(src_i
, readval
, target
.c
, long,
428 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
432 init_byte_array(target
.c
, TEST_LEN
, 0x0);
433 bt_bitfield_write(target
.ll
, long long, s
, l
, src_i
);
434 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
435 if (check_result(src_i
, readval
, target
.c
, long long,
436 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
441 pass(SIGNED_INT_WRITE_TEST_DESC_FMT_STR
, src_i
);
444 nrbits_ll
= 0; /* The number of bits needed to represent 0 is 0. */
445 else if (src_ll
& 0x8000000000000000ULL
)
446 nrbits_ll
= fls_u64(~src_ll
) + 1; /* Find least significant bit conveying sign */
448 nrbits_ll
= fls_u64(src_ll
) + 1; /* Keep sign at 0 */
450 /* Write from signed long long src input. */
451 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
452 for (l
= nrbits_ll
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
453 init_byte_array(target
.c
, TEST_LEN
, 0x0);
454 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
455 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
456 if (check_result(src_ll
, readval
, target
.c
, signed char,
457 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
461 init_byte_array(target
.c
, TEST_LEN
, 0x0);
462 bt_bitfield_write(target
.s
, short, s
, l
, src_ll
);
463 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
464 if (check_result(src_ll
, readval
, target
.c
, short,
465 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
469 init_byte_array(target
.c
, TEST_LEN
, 0x0);
470 bt_bitfield_write(target
.i
, int, s
, l
, src_ll
);
471 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
472 if (check_result(src_ll
, readval
, target
.c
, int,
473 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
477 init_byte_array(target
.c
, TEST_LEN
, 0x0);
478 bt_bitfield_write(target
.l
, long, s
, l
, src_ll
);
479 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
480 if (check_result(src_ll
, readval
, target
.c
, long,
481 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
485 init_byte_array(target
.c
, TEST_LEN
, 0x0);
486 bt_bitfield_write(target
.ll
, long long, s
, l
, src_ll
);
487 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
488 if (check_result(src_ll
, readval
, target
.c
, long long,
489 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
494 pass(SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
, src_ll
);
497 void run_test_signed_read(int src_i
, long long src_ll
)
499 int nrbits_i
, nrbits_ll
, readval_i
;
501 unsigned char c
[TEST_LEN
];
502 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
503 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
504 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
505 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
507 long long readval_ll
;
511 nrbits_i
= 0; /* The number of bits needed to represent 0 is 0. */
512 else if (src_i
& 0x80000000U
)
513 nrbits_i
= fls_u32(~src_i
) + 1; /* Find least significant bit conveying sign */
515 nrbits_i
= fls_u32(src_i
) + 1; /* Keep sign at 0 */
517 /* Read to signed integer readval output. */
518 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
519 for (l
= nrbits_i
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
520 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
521 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
522 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval_i
);
523 if (check_result(src_i
, readval_i
, target
.c
, signed char,
524 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
528 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
529 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
530 bt_bitfield_read(target
.s
, short, s
, l
, &readval_i
);
531 if (check_result(src_i
, readval_i
, target
.c
, short,
532 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
536 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
537 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
538 bt_bitfield_read(target
.i
, int, s
, l
, &readval_i
);
539 if (check_result(src_i
, readval_i
, target
.c
, int,
540 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
544 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
545 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
546 bt_bitfield_read(target
.l
, long, s
, l
, &readval_i
);
547 if (check_result(src_i
, readval_i
, target
.c
, long,
548 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
552 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
553 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
554 bt_bitfield_read(target
.ll
, long long, s
, l
, &readval_i
);
555 if (check_result(src_i
, readval_i
, target
.c
, long long,
556 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
561 pass(SIGNED_INT_READ_TEST_DESC_FMT_STR
, src_i
);
564 nrbits_ll
= 0; /* The number of bits needed to represent 0 is 0. */
565 if (src_ll
& 0x8000000000000000ULL
)
566 nrbits_ll
= fls_u64(~src_ll
) + 1; /* Find least significant bit conveying sign */
568 nrbits_ll
= fls_u64(src_ll
) + 1; /* Keep sign at 0 */
570 /* Read to signed long long readval output. */
571 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
572 for (l
= nrbits_ll
; l
< (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
573 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
574 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
575 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval_ll
);
576 if (check_result(src_ll
, readval_ll
, target
.c
, signed char,
577 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
581 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
582 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
583 bt_bitfield_read(target
.s
, short, s
, l
, &readval_ll
);
584 if (check_result(src_ll
, readval_ll
, target
.c
, short,
585 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
589 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
590 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
591 bt_bitfield_read(target
.i
, int, s
, l
, &readval_ll
);
592 if (check_result(src_ll
, readval_ll
, target
.c
, int,
593 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
597 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
598 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
599 bt_bitfield_read(target
.l
, long, s
, l
, &readval_ll
);
600 if (check_result(src_ll
, readval_ll
, target
.c
, long,
601 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
605 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
606 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
607 bt_bitfield_read(target
.ll
, long long, s
, l
, &readval_ll
);
608 if (check_result(src_ll
, readval_ll
, target
.c
, long long,
609 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
614 pass(SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
, src_ll
);
617 void run_test_signed(int src_i
, long long src_ll
)
619 run_test_signed_write(src_i
, src_ll
);
620 run_test_signed_read(src_i
, src_ll
);
627 plan_tests(NR_TESTS
* 8 + 24);
631 run_test_unsigned(0, 0);
632 run_test_signed(0, 0);
633 run_test_unsigned(1, 1);
634 run_test_unsigned(~0U, ~0ULL);
635 run_test_signed(-1U, -1ULL);
636 run_test_signed(0x80000000U
, 0x8000000000000000ULL
);
638 for (i
= 0; i
< NR_TESTS
; i
++) {
639 unsigned int src_ui
= rand();
640 unsigned long long src_ull
= ((unsigned long long) (unsigned int) rand() << 32) |
641 (unsigned long long) (unsigned int) rand();
643 run_test_unsigned(src_ui
, src_ull
);
644 run_test_signed((int) src_ui
, (long long) src_ull
);
649 int print_encodings(unsigned long src
, unsigned int shift
, unsigned int len
)
656 unsigned long long ll
[1];
658 unsigned long long readval
;
660 init_byte_array(target
.c
, 8, 0xFF);
661 bt_bitfield_write(target
.c
, unsigned char, shift
, len
, src
);
662 printf("bytewise\n");
663 print_byte_array(target
.c
, 8);
665 init_byte_array(target
.c
, 8, 0xFF);
666 bt_bitfield_write(target
.s
, unsigned short, shift
, len
, src
);
667 printf("shortwise\n");
668 print_byte_array(target
.c
, 8);
670 init_byte_array(target
.c
, 8, 0xFF);
671 bt_bitfield_write(target
.i
, unsigned int, shift
, len
, src
);
673 print_byte_array(target
.c
, 8);
675 init_byte_array(target
.c
, 8, 0xFF);
676 bt_bitfield_write(target
.l
, unsigned long, shift
, len
, src
);
677 printf("longwise\n");
678 print_byte_array(target
.c
, 8);
680 init_byte_array(target
.c
, 8, 0xFF);
681 bt_bitfield_write(target
.ll
, unsigned long long, shift
, len
, src
);
683 print_byte_array(target
.c
, 8);
685 bt_bitfield_read(target
.c
, unsigned char, shift
, len
, &readval
);
686 printf("read: %llX\n", readval
);
687 print_byte_array(target
.c
, 8);
692 int main(int argc
, char **argv
)
695 /* Print encodings */
697 unsigned int shift
, len
;
701 shift
= atoi(argv
[2]);
708 return print_encodings(src
, shift
, len
);
711 /* Run tap-formated tests */
713 return exit_status();