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 "compat/bitfield.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 /* The number of bits needed to represent 0 is 0. */
165 nrbits_ui
= fls_u32(src_ui
);
167 /* Write from unsigned integer src input. */
168 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
169 for (l
= nrbits_ui
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
170 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
171 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
172 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
173 if (check_result(src_ui
, readval
, target
.c
, unsigned char,
174 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
178 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
179 bt_bitfield_write(target
.s
, unsigned short, s
, l
, src_ui
);
180 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
181 if (check_result(src_ui
, readval
, target
.c
, unsigned short,
182 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
186 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
187 bt_bitfield_write(target
.i
, unsigned int, s
, l
, src_ui
);
188 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
189 if (check_result(src_ui
, readval
, target
.c
, unsigned int,
190 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
194 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
195 bt_bitfield_write(target
.l
, unsigned long, s
, l
, src_ui
);
196 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
197 if (check_result(src_ui
, readval
, target
.c
, unsigned long,
198 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
202 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
203 bt_bitfield_write(target
.ll
, unsigned long long, s
, l
, src_ui
);
204 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
205 if (check_result(src_ui
, readval
, target
.c
, unsigned long long,
206 s
, l
, UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
211 pass(UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR
, src_ui
);
213 /* The number of bits needed to represent 0 is 0. */
214 nrbits_ull
= fls_u64(src_ull
);
216 /* Write from unsigned long long src input. */
217 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
218 for (l
= nrbits_ull
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
219 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
220 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
221 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
222 if (check_result(src_ull
, readval
, target
.c
, unsigned char,
223 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
227 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
228 bt_bitfield_write(target
.s
, unsigned short, s
, l
, src_ull
);
229 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
230 if (check_result(src_ull
, readval
, target
.c
, unsigned short,
231 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
235 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
236 bt_bitfield_write(target
.i
, unsigned int, s
, l
, src_ull
);
237 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
238 if (check_result(src_ull
, readval
, target
.c
, unsigned int,
239 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
243 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
244 bt_bitfield_write(target
.l
, unsigned long, s
, l
, src_ull
);
245 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
246 if (check_result(src_ull
, readval
, target
.c
, unsigned long,
247 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
251 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
252 bt_bitfield_write(target
.ll
, unsigned long long, s
, l
, src_ull
);
253 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval
);
254 if (check_result(src_ull
, readval
, target
.c
, unsigned long long,
255 s
, l
, UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
260 pass(UNSIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
, src_ull
);
263 void run_test_unsigned_read(unsigned int src_ui
, unsigned long long src_ull
)
265 unsigned int nrbits_ui
, nrbits_ull
, readval_ui
;
267 unsigned char c
[TEST_LEN
];
268 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
269 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
270 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
271 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
273 unsigned long long readval_ull
;
276 /* The number of bits needed to represent 0 is 0. */
277 nrbits_ui
= fls_u32(src_ui
);
279 /* Read to unsigned integer readval output. */
280 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
281 for (l
= nrbits_ui
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
282 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
283 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
284 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval_ui
);
285 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned char,
286 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
290 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
291 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
292 bt_bitfield_read(target
.s
, unsigned short, s
, l
, &readval_ui
);
293 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned short,
294 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
298 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
299 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
300 bt_bitfield_read(target
.i
, unsigned int, s
, l
, &readval_ui
);
301 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned int,
302 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
306 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
307 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
308 bt_bitfield_read(target
.l
, unsigned long, s
, l
, &readval_ui
);
309 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned long,
310 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
314 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
315 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ui
);
316 bt_bitfield_read(target
.ll
, unsigned long long, s
, l
, &readval_ui
);
317 if (check_result(src_ui
, readval_ui
, target
.c
, unsigned long long,
318 s
, l
, UNSIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
323 pass(UNSIGNED_INT_READ_TEST_DESC_FMT_STR
, src_ui
);
325 /* The number of bits needed to represent 0 is 0. */
326 nrbits_ull
= fls_u64(src_ull
);
328 /* Read to unsigned long long readval output. */
329 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
330 for (l
= nrbits_ull
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
331 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
332 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
333 bt_bitfield_read(target
.c
, unsigned char, s
, l
, &readval_ull
);
334 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned char,
335 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
339 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
340 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
341 bt_bitfield_read(target
.s
, unsigned short, s
, l
, &readval_ull
);
342 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned short,
343 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
347 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
348 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
349 bt_bitfield_read(target
.i
, unsigned int, s
, l
, &readval_ull
);
350 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned int,
351 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
355 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
356 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
357 bt_bitfield_read(target
.l
, unsigned long, s
, l
, &readval_ull
);
358 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned long,
359 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
363 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
364 bt_bitfield_write(target
.c
, unsigned char, s
, l
, src_ull
);
365 bt_bitfield_read(target
.ll
, unsigned long long, s
, l
, &readval_ull
);
366 if (check_result(src_ull
, readval_ull
, target
.c
, unsigned long long,
367 s
, l
, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
372 pass(UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
, src_ull
);
375 void run_test_unsigned(unsigned int src_ui
, unsigned long long src_ull
)
377 run_test_unsigned_write(src_ui
, src_ull
);
378 run_test_unsigned_read(src_ui
, src_ull
);
381 void run_test_signed_write(int src_i
, long long src_ll
)
383 unsigned int nrbits_i
, nrbits_ll
;
385 signed char c
[TEST_LEN
];
386 short s
[TEST_LEN
/sizeof(short)];
387 int i
[TEST_LEN
/sizeof(int)];
388 long l
[TEST_LEN
/sizeof(long)];
389 long long ll
[TEST_LEN
/sizeof(long long)];
395 nrbits_i
= 0; /* The number of bits needed to represent 0 is 0. */
396 else if (src_i
& 0x80000000U
)
397 nrbits_i
= fls_u32(~src_i
) + 1; /* Find least significant bit conveying sign */
399 nrbits_i
= fls_u32(src_i
) + 1; /* Keep sign at 0 */
401 /* Write from signed integer src input. */
402 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
403 for (l
= nrbits_i
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
404 init_byte_array(target
.c
, TEST_LEN
, 0x0);
405 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
406 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
407 if (check_result(src_i
, readval
, target
.c
, signed char,
408 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
412 init_byte_array(target
.c
, TEST_LEN
, 0x0);
413 bt_bitfield_write(target
.s
, short, s
, l
, src_i
);
414 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
415 if (check_result(src_i
, readval
, target
.c
, short,
416 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
420 init_byte_array(target
.c
, TEST_LEN
, 0x0);
421 bt_bitfield_write(target
.i
, int, s
, l
, src_i
);
422 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
423 if (check_result(src_i
, readval
, target
.c
, int,
424 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
428 init_byte_array(target
.c
, TEST_LEN
, 0x0);
429 bt_bitfield_write(target
.l
, long, s
, l
, src_i
);
430 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
431 if (check_result(src_i
, readval
, target
.c
, long,
432 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
436 init_byte_array(target
.c
, TEST_LEN
, 0x0);
437 bt_bitfield_write(target
.ll
, long long, s
, l
, src_i
);
438 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
439 if (check_result(src_i
, readval
, target
.c
, long long,
440 s
, l
, SIGNED_INT_WRITE_TEST_DESC_FMT_STR
)) {
445 pass(SIGNED_INT_WRITE_TEST_DESC_FMT_STR
, src_i
);
448 nrbits_ll
= 0; /* The number of bits needed to represent 0 is 0. */
449 else if (src_ll
& 0x8000000000000000ULL
)
450 nrbits_ll
= fls_u64(~src_ll
) + 1; /* Find least significant bit conveying sign */
452 nrbits_ll
= fls_u64(src_ll
) + 1; /* Keep sign at 0 */
454 /* Write from signed long long src input. */
455 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
456 for (l
= nrbits_ll
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
457 init_byte_array(target
.c
, TEST_LEN
, 0x0);
458 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
459 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
460 if (check_result(src_ll
, readval
, target
.c
, signed char,
461 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
465 init_byte_array(target
.c
, TEST_LEN
, 0x0);
466 bt_bitfield_write(target
.s
, short, s
, l
, src_ll
);
467 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
468 if (check_result(src_ll
, readval
, target
.c
, short,
469 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
473 init_byte_array(target
.c
, TEST_LEN
, 0x0);
474 bt_bitfield_write(target
.i
, int, s
, l
, src_ll
);
475 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
476 if (check_result(src_ll
, readval
, target
.c
, int,
477 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
481 init_byte_array(target
.c
, TEST_LEN
, 0x0);
482 bt_bitfield_write(target
.l
, long, s
, l
, src_ll
);
483 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
484 if (check_result(src_ll
, readval
, target
.c
, long,
485 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
489 init_byte_array(target
.c
, TEST_LEN
, 0x0);
490 bt_bitfield_write(target
.ll
, long long, s
, l
, src_ll
);
491 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval
);
492 if (check_result(src_ll
, readval
, target
.c
, long long,
493 s
, l
, SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
)) {
498 pass(SIGNED_LONG_LONG_WRITE_TEST_DESC_FMT_STR
, src_ll
);
501 void run_test_signed_read(int src_i
, long long src_ll
)
503 unsigned int nrbits_i
, nrbits_ll
;
506 unsigned char c
[TEST_LEN
];
507 unsigned short s
[TEST_LEN
/sizeof(unsigned short)];
508 unsigned int i
[TEST_LEN
/sizeof(unsigned int)];
509 unsigned long l
[TEST_LEN
/sizeof(unsigned long)];
510 unsigned long long ll
[TEST_LEN
/sizeof(unsigned long long)];
512 long long readval_ll
;
516 nrbits_i
= 0; /* The number of bits needed to represent 0 is 0. */
517 else if (src_i
& 0x80000000U
)
518 nrbits_i
= fls_u32(~src_i
) + 1; /* Find least significant bit conveying sign */
520 nrbits_i
= fls_u32(src_i
) + 1; /* Keep sign at 0 */
522 /* Read to signed integer readval output. */
523 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
524 for (l
= nrbits_i
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
525 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
526 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
527 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval_i
);
528 if (check_result(src_i
, readval_i
, target
.c
, signed char,
529 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
533 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
534 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
535 bt_bitfield_read(target
.s
, short, s
, l
, &readval_i
);
536 if (check_result(src_i
, readval_i
, target
.c
, short,
537 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
541 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
542 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
543 bt_bitfield_read(target
.i
, int, s
, l
, &readval_i
);
544 if (check_result(src_i
, readval_i
, target
.c
, int,
545 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
549 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
550 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
551 bt_bitfield_read(target
.l
, long, s
, l
, &readval_i
);
552 if (check_result(src_i
, readval_i
, target
.c
, long,
553 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
557 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
558 bt_bitfield_write(target
.c
, signed char, s
, l
, src_i
);
559 bt_bitfield_read(target
.ll
, long long, s
, l
, &readval_i
);
560 if (check_result(src_i
, readval_i
, target
.c
, long long,
561 s
, l
, SIGNED_INT_READ_TEST_DESC_FMT_STR
)) {
566 pass(SIGNED_INT_READ_TEST_DESC_FMT_STR
, src_i
);
569 nrbits_ll
= 0; /* The number of bits needed to represent 0 is 0. */
570 if (src_ll
& 0x8000000000000000ULL
)
571 nrbits_ll
= fls_u64(~src_ll
) + 1; /* Find least significant bit conveying sign */
573 nrbits_ll
= fls_u64(src_ll
) + 1; /* Keep sign at 0 */
575 /* Read to signed long long readval output. */
576 for (s
= 0; s
< CHAR_BIT
* TEST_LEN
; s
++) {
577 for (l
= nrbits_ll
; l
<= (CHAR_BIT
* TEST_LEN
) - s
; l
++) {
578 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
579 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
580 bt_bitfield_read(target
.c
, signed char, s
, l
, &readval_ll
);
581 if (check_result(src_ll
, readval_ll
, target
.c
, signed char,
582 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
586 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
587 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
588 bt_bitfield_read(target
.s
, short, s
, l
, &readval_ll
);
589 if (check_result(src_ll
, readval_ll
, target
.c
, short,
590 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
594 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
595 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
596 bt_bitfield_read(target
.i
, int, s
, l
, &readval_ll
);
597 if (check_result(src_ll
, readval_ll
, target
.c
, int,
598 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
602 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
603 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
604 bt_bitfield_read(target
.l
, long, s
, l
, &readval_ll
);
605 if (check_result(src_ll
, readval_ll
, target
.c
, long,
606 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
610 init_byte_array(target
.c
, TEST_LEN
, 0xFF);
611 bt_bitfield_write(target
.c
, signed char, s
, l
, src_ll
);
612 bt_bitfield_read(target
.ll
, long long, s
, l
, &readval_ll
);
613 if (check_result(src_ll
, readval_ll
, target
.c
, long long,
614 s
, l
, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
)) {
619 pass(SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR
, src_ll
);
622 void run_test_signed(int src_i
, long long src_ll
)
624 run_test_signed_write(src_i
, src_ll
);
625 run_test_signed_read(src_i
, src_ll
);
632 plan_tests(NR_TESTS
* 8 + 24);
636 run_test_unsigned(0, 0);
637 run_test_signed(0, 0);
638 run_test_unsigned(1, 1);
639 run_test_unsigned(~0U, ~0ULL);
640 run_test_signed(-1U, -1ULL);
641 run_test_signed(0x80000000U
, 0x8000000000000000ULL
);
643 for (i
= 0; i
< NR_TESTS
; i
++) {
644 unsigned int src_ui
= rand();
645 unsigned long long src_ull
= ((unsigned long long) (unsigned int) rand() << 32) |
646 (unsigned long long) (unsigned int) rand();
648 run_test_unsigned(src_ui
, src_ull
);
649 run_test_signed((int) src_ui
, (long long) src_ull
);
654 int print_encodings(unsigned long src
, unsigned int shift
, unsigned int len
)
661 unsigned long long ll
[1];
663 unsigned long long readval
;
665 init_byte_array(target
.c
, 8, 0xFF);
666 bt_bitfield_write(target
.c
, unsigned char, shift
, len
, src
);
667 printf("bytewise\n");
668 print_byte_array(target
.c
, 8);
670 init_byte_array(target
.c
, 8, 0xFF);
671 bt_bitfield_write(target
.s
, unsigned short, shift
, len
, src
);
672 printf("shortwise\n");
673 print_byte_array(target
.c
, 8);
675 init_byte_array(target
.c
, 8, 0xFF);
676 bt_bitfield_write(target
.i
, unsigned int, shift
, len
, src
);
678 print_byte_array(target
.c
, 8);
680 init_byte_array(target
.c
, 8, 0xFF);
681 bt_bitfield_write(target
.l
, unsigned long, shift
, len
, src
);
682 printf("longwise\n");
683 print_byte_array(target
.c
, 8);
685 init_byte_array(target
.c
, 8, 0xFF);
686 bt_bitfield_write(target
.ll
, unsigned long long, shift
, len
, src
);
688 print_byte_array(target
.c
, 8);
690 bt_bitfield_read(target
.c
, unsigned char, shift
, len
, &readval
);
691 printf("read: %llX\n", readval
);
692 print_byte_array(target
.c
, 8);
697 int main(int argc
, char **argv
)
700 /* Print encodings */
702 unsigned int shift
, len
;
706 shift
= atoi(argv
[2]);
713 return print_encodings(src
, shift
, len
);
716 /* Run tap-formated tests */
718 return exit_status();