qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

tests: New test-bitcnt

Add some unit tests for bit count functions (currently only ctpop). As
the routines are based on the Hackers Delight optimisations I based
the test patterns on their tests.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Richard Henderson <rth@twiddle.net>

authored by

Alex Bennée and committed by
Richard Henderson
c3f8962f 7bdcecb7

+143
+1
tests/.gitignore
··· 13 13 test-aio 14 14 test-base64 15 15 test-bitops 16 + test-bitcnt 16 17 test-blockjob 17 18 test-blockjob-txn 18 19 test-bufferiszero
+2
tests/Makefile.include
··· 81 81 check-unit-y += tests/test-qht-par$(EXESUF) 82 82 gcov-files-test-qht-par-y = util/qht.c 83 83 check-unit-y += tests/test-bitops$(EXESUF) 84 + check-unit-y += tests/test-bitcnt$(EXESUF) 84 85 check-unit-$(CONFIG_HAS_GLIB_SUBPROCESS_TESTS) += tests/test-qdev-global-props$(EXESUF) 85 86 check-unit-y += tests/check-qom-interface$(EXESUF) 86 87 gcov-files-check-qom-interface-y = qom/object.c ··· 571 572 572 573 tests/test-mul64$(EXESUF): tests/test-mul64.o $(test-util-obj-y) 573 574 tests/test-bitops$(EXESUF): tests/test-bitops.o $(test-util-obj-y) 575 + tests/test-bitcnt$(EXESUF): tests/test-bitcnt.o $(test-util-obj-y) 574 576 tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-y) 575 577 tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-obj-y) 576 578 tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypto-obj-y)
+140
tests/test-bitcnt.c
··· 1 + /* 2 + * Test bit count routines 3 + * 4 + * This work is licensed under the terms of the GNU LGPL, version 2 or later. 5 + * See the COPYING.LIB file in the top-level directory. 6 + * 7 + */ 8 + 9 + #include "qemu/osdep.h" 10 + #include "qemu/host-utils.h" 11 + 12 + struct bitcnt_test_data { 13 + /* value to count */ 14 + union { 15 + uint8_t w8; 16 + uint16_t w16; 17 + uint32_t w32; 18 + uint64_t w64; 19 + } value; 20 + /* expected result */ 21 + int popct; 22 + }; 23 + 24 + struct bitcnt_test_data eight_bit_data[] = { 25 + { { .w8 = 0x00 }, .popct=0 }, 26 + { { .w8 = 0x01 }, .popct=1 }, 27 + { { .w8 = 0x03 }, .popct=2 }, 28 + { { .w8 = 0x04 }, .popct=1 }, 29 + { { .w8 = 0x0f }, .popct=4 }, 30 + { { .w8 = 0x3f }, .popct=6 }, 31 + { { .w8 = 0x40 }, .popct=1 }, 32 + { { .w8 = 0xf0 }, .popct=4 }, 33 + { { .w8 = 0x7f }, .popct=7 }, 34 + { { .w8 = 0x80 }, .popct=1 }, 35 + { { .w8 = 0xf1 }, .popct=5 }, 36 + { { .w8 = 0xfe }, .popct=7 }, 37 + { { .w8 = 0xff }, .popct=8 }, 38 + }; 39 + 40 + static void test_ctpop8(void) 41 + { 42 + int i; 43 + 44 + for (i = 0; i < ARRAY_SIZE(eight_bit_data); i++) { 45 + struct bitcnt_test_data *d = &eight_bit_data[i]; 46 + g_assert(ctpop8(d->value.w8)==d->popct); 47 + } 48 + } 49 + 50 + struct bitcnt_test_data sixteen_bit_data[] = { 51 + { { .w16 = 0x0000 }, .popct=0 }, 52 + { { .w16 = 0x0001 }, .popct=1 }, 53 + { { .w16 = 0x0003 }, .popct=2 }, 54 + { { .w16 = 0x000f }, .popct=4 }, 55 + { { .w16 = 0x003f }, .popct=6 }, 56 + { { .w16 = 0x00f0 }, .popct=4 }, 57 + { { .w16 = 0x0f0f }, .popct=8 }, 58 + { { .w16 = 0x1f1f }, .popct=10 }, 59 + { { .w16 = 0x4000 }, .popct=1 }, 60 + { { .w16 = 0x4001 }, .popct=2 }, 61 + { { .w16 = 0x7000 }, .popct=3 }, 62 + { { .w16 = 0x7fff }, .popct=15 }, 63 + }; 64 + 65 + static void test_ctpop16(void) 66 + { 67 + int i; 68 + 69 + for (i = 0; i < ARRAY_SIZE(sixteen_bit_data); i++) { 70 + struct bitcnt_test_data *d = &sixteen_bit_data[i]; 71 + g_assert(ctpop16(d->value.w16)==d->popct); 72 + } 73 + } 74 + 75 + struct bitcnt_test_data thirtytwo_bit_data[] = { 76 + { { .w32 = 0x00000000 }, .popct=0 }, 77 + { { .w32 = 0x00000001 }, .popct=1 }, 78 + { { .w32 = 0x0000000f }, .popct=4 }, 79 + { { .w32 = 0x00000f0f }, .popct=8 }, 80 + { { .w32 = 0x00001f1f }, .popct=10 }, 81 + { { .w32 = 0x00004001 }, .popct=2 }, 82 + { { .w32 = 0x00007000 }, .popct=3 }, 83 + { { .w32 = 0x00007fff }, .popct=15 }, 84 + { { .w32 = 0x55555555 }, .popct=16 }, 85 + { { .w32 = 0xaaaaaaaa }, .popct=16 }, 86 + { { .w32 = 0xff000000 }, .popct=8 }, 87 + { { .w32 = 0xc0c0c0c0 }, .popct=8 }, 88 + { { .w32 = 0x0ffffff0 }, .popct=24 }, 89 + { { .w32 = 0x80000000 }, .popct=1 }, 90 + { { .w32 = 0xffffffff }, .popct=32 }, 91 + }; 92 + 93 + static void test_ctpop32(void) 94 + { 95 + int i; 96 + 97 + for (i = 0; i < ARRAY_SIZE(thirtytwo_bit_data); i++) { 98 + struct bitcnt_test_data *d = &thirtytwo_bit_data[i]; 99 + g_assert(ctpop32(d->value.w32)==d->popct); 100 + } 101 + } 102 + 103 + struct bitcnt_test_data sixtyfour_bit_data[] = { 104 + { { .w64 = 0x0000000000000000ULL }, .popct=0 }, 105 + { { .w64 = 0x0000000000000001ULL }, .popct=1 }, 106 + { { .w64 = 0x000000000000000fULL }, .popct=4 }, 107 + { { .w64 = 0x0000000000000f0fULL }, .popct=8 }, 108 + { { .w64 = 0x0000000000001f1fULL }, .popct=10 }, 109 + { { .w64 = 0x0000000000004001ULL }, .popct=2 }, 110 + { { .w64 = 0x0000000000007000ULL }, .popct=3 }, 111 + { { .w64 = 0x0000000000007fffULL }, .popct=15 }, 112 + { { .w64 = 0x0000005500555555ULL }, .popct=16 }, 113 + { { .w64 = 0x00aa0000aaaa00aaULL }, .popct=16 }, 114 + { { .w64 = 0x000f000000f00000ULL }, .popct=8 }, 115 + { { .w64 = 0x0c0c0000c0c0c0c0ULL }, .popct=12 }, 116 + { { .w64 = 0xf00f00f0f0f0f000ULL }, .popct=24 }, 117 + { { .w64 = 0x8000000000000000ULL }, .popct=1 }, 118 + { { .w64 = 0xf0f0f0f0f0f0f0f0ULL }, .popct=32 }, 119 + { { .w64 = 0xffffffffffffffffULL }, .popct=64 }, 120 + }; 121 + 122 + static void test_ctpop64(void) 123 + { 124 + int i; 125 + 126 + for (i = 0; i < ARRAY_SIZE(sixtyfour_bit_data); i++) { 127 + struct bitcnt_test_data *d = &sixtyfour_bit_data[i]; 128 + g_assert(ctpop64(d->value.w64)==d->popct); 129 + } 130 + } 131 + 132 + int main(int argc, char **argv) 133 + { 134 + g_test_init(&argc, &argv, NULL); 135 + g_test_add_func("/bitcnt/ctpop8", test_ctpop8); 136 + g_test_add_func("/bitcnt/ctpop16", test_ctpop16); 137 + g_test_add_func("/bitcnt/ctpop32", test_ctpop32); 138 + g_test_add_func("/bitcnt/ctpop64", test_ctpop64); 139 + return g_test_run(); 140 + }