Skip to content

Commit a8b5926

Browse files
committed
include: util: Add generic function to count bits set in a value
Adds a generic function that will count the number of bits set in a value. It uses POPCOUNT (e.g. __builtin_popcount for GCC) if available, or else it will use Brian Kernighan’s Algorithm to count bits. POPCOUNT will likely always support unsigned ints, but the function was implemented to use it with uint8_t for the sake of simplicity and compatibility with Brian Kernighan’s Algorithm. A generic solution was chosen rather than a macro/function per type (e.g. uint8_t, uint16_t, etc.) as that is easier to maintain and also supports array types (e.g. counting the number of bits in 128 or 256 octet arrays). Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
1 parent e661a55 commit a8b5926

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

doc/releases/release-notes-4.2.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ New APIs and options
147147

148148
* :c:func:`counter_reset`
149149

150+
* Other
151+
152+
* :c:func:`zephyr_count_bits`
153+
150154
New Boards
151155
**********
152156

include/zephyr/sys/util.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,39 @@ static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const ui
783783
mem_xor_n(dst, src1, src2, 16);
784784
}
785785

786+
/**
787+
* @brief Returns the number of bits set in a value
788+
*
789+
* @param value The value to count number of bits set of
790+
* @param len The number of octets in @p value
791+
*/
792+
static inline size_t zephyr_count_bits(const void *value, size_t len)
793+
{
794+
size_t cnt = 0U;
795+
size_t i = 0;
796+
797+
#ifdef POPCOUNT
798+
for (; i < len / sizeof(unsigned int); i++) {
799+
unsigned int val = ((unsigned int *)value)[i];
800+
801+
cnt += POPCOUNT(val);
802+
}
803+
i *= sizeof(unsigned int); /* convert to a uint8_t index for the remainder (if any) */
804+
#endif
805+
806+
for (; i < len; i++) {
807+
uint8_t value_u8 = ((uint8_t *)value)[i];
808+
809+
/* Implements Brian Kernighan’s Algorithm to count bits */
810+
while (value_u8) {
811+
value_u8 &= (value_u8 - 1);
812+
cnt++;
813+
}
814+
}
815+
816+
return cnt;
817+
}
818+
786819
#ifdef __cplusplus
787820
}
788821
#endif

tests/unit/util/main.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
/*
22
* Copyright (c) 2019 Oticon A/S
3+
* Copyright (c) 2025 Nordic Semiconductor ASA
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
67

7-
#include <zephyr/ztest.h>
8-
#include <zephyr/sys/util.h>
8+
#include <stdint.h>
99
#include <stdio.h>
1010
#include <string.h>
1111

12+
#include <zephyr/ztest.h>
13+
#include <zephyr/sys/util.h>
14+
#include <zephyr/ztest_assert.h>
15+
#include <zephyr/ztest_test.h>
16+
1217
ZTEST(util, test_u8_to_dec) {
1318
char text[4];
1419
uint8_t len;
@@ -768,6 +773,25 @@ ZTEST(util, test_mem_xor_128)
768773
zassert_mem_equal(expected_result, dst, 16);
769774
}
770775

776+
ZTEST(util, test_zephyr_count_bits)
777+
{
778+
uint8_t zero = 0U;
779+
uint8_t u8 = 29U;
780+
uint16_t u16 = 29999U;
781+
uint32_t u32 = 2999999999U;
782+
uint64_t u64 = 123456789012345ULL;
783+
uint8_t u8_arr[] = {u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8,
784+
u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8};
785+
786+
zassert_equal(zephyr_count_bits(&zero, sizeof(zero)), 0);
787+
zassert_equal(zephyr_count_bits(&u8, sizeof(u8)), 4);
788+
zassert_equal(zephyr_count_bits(&u16, sizeof(u16)), 10);
789+
zassert_equal(zephyr_count_bits(&u32, sizeof(u32)), 20);
790+
zassert_equal(zephyr_count_bits(&u64, sizeof(u64)), 23);
791+
792+
zassert_equal(zephyr_count_bits(u8_arr, sizeof(u8_arr)), 128);
793+
}
794+
771795
ZTEST(util, test_CONCAT)
772796
{
773797
#define _CAT_PART1 1

0 commit comments

Comments
 (0)