Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright (C) 2023 Microsoft Corporation 3 : : */ 4 : : 5 : : #include <limits.h> 6 : : #include <string.h> 7 : : 8 : : #include <rte_bitops.h> 9 : : #include <rte_debug.h> 10 : : 11 : : #include "test.h" 12 : : 13 [ - + ]: 251 : RTE_LOG_REGISTER(bitcount_logtype_test, test.bitcount, INFO); 14 : : 15 : : static int 16 : 1 : test_clz32(void) 17 : : { 18 : : size_t leading; 19 : : uint32_t v = 0xffffffff; 20 : : 21 [ + + ]: 33 : for (leading = 0; v; leading++) { 22 [ - + ]: 32 : RTE_TEST_ASSERT(rte_clz32(v) == leading, 23 : : "Unexpected count."); 24 : 32 : v >>= 1; 25 : : } 26 : : 27 : : return 0; 28 : : } 29 : : 30 : : static int 31 : 1 : test_clz64(void) 32 : : { 33 : : size_t leading; 34 : : uint64_t v = 0xffffffffffffffff; 35 : : 36 [ + + ]: 65 : for (leading = 0; v; leading++) { 37 [ - + ]: 64 : RTE_TEST_ASSERT(rte_clz64(v) == leading, 38 : : "Unexpected count."); 39 : 64 : v >>= 1; 40 : : } 41 : : 42 : : return 0; 43 : : } 44 : : 45 : : static int 46 : 1 : test_ctz32(void) 47 : : { 48 : : size_t trailing; 49 : : uint32_t v = 1; 50 : : 51 [ + + ]: 33 : for (trailing = 0; v; trailing++) { 52 [ - + ]: 32 : RTE_TEST_ASSERT(rte_ctz32(v) == trailing, 53 : : "Unexpected count."); 54 : 32 : v <<= 1; 55 : : } 56 : : 57 : : return 0; 58 : : } 59 : : 60 : : static int 61 : 1 : test_ctz64(void) 62 : : { 63 : : size_t trailing; 64 : : uint64_t v = 1; 65 : : 66 [ + + ]: 65 : for (trailing = 0; v; trailing++) { 67 [ - + ]: 64 : RTE_TEST_ASSERT(rte_ctz64(v) == trailing, 68 : : "Unexpected count."); 69 : 64 : v <<= 1; 70 : : } 71 : : 72 : : return 0; 73 : : } 74 : : 75 : : static int 76 : 1 : test_popcount32(void) 77 : : { 78 : : size_t shift; 79 : : uint32_t v = 0; 80 : : const size_t bits = sizeof(v) * CHAR_BIT; 81 : : 82 [ + + ]: 33 : for (shift = 0; shift < bits; shift++) { 83 [ - + ]: 32 : RTE_TEST_ASSERT(rte_popcount32(v) == shift, 84 : : "Unexpected count."); 85 : 32 : v <<= 1; 86 : 32 : v |= 1; 87 : : } 88 : : 89 [ - + ]: 1 : RTE_TEST_ASSERT(rte_popcount32(v) == bits, 90 : : "Unexpected count."); 91 : : 92 : : return 0; 93 : : } 94 : : 95 : : static int 96 : 1 : test_popcount64(void) 97 : : { 98 : : size_t shift; 99 : : uint64_t v = 0; 100 : : const size_t bits = sizeof(v) * CHAR_BIT; 101 : : 102 [ + + ]: 65 : for (shift = 0; shift < bits; shift++) { 103 [ - + ]: 64 : RTE_TEST_ASSERT(rte_popcount64(v) == shift, 104 : : "Unexpected count."); 105 : 64 : v <<= 1; 106 : 64 : v |= 1; 107 : : } 108 : : 109 [ - + ]: 1 : RTE_TEST_ASSERT(rte_popcount64(v) == bits, 110 : : "Unexpected count."); 111 : : 112 : : return 0; 113 : : } 114 : : 115 : : static struct unit_test_suite bitcount_test_suite = { 116 : : .suite_name = "bitcount autotest", 117 : : .setup = NULL, 118 : : .teardown = NULL, 119 : : .unit_test_cases = { 120 : : TEST_CASE(test_clz32), 121 : : TEST_CASE(test_clz64), 122 : : TEST_CASE(test_ctz32), 123 : : TEST_CASE(test_ctz64), 124 : : TEST_CASE(test_popcount32), 125 : : TEST_CASE(test_popcount64), 126 : : TEST_CASES_END() 127 : : } 128 : : }; 129 : : 130 : : static int 131 : 1 : test_bitcount(void) 132 : : { 133 : 1 : return unit_test_suite_runner(&bitcount_test_suite); 134 : : } 135 : : 136 : 251 : REGISTER_FAST_TEST(bitcount_autotest, true, true, test_bitcount);