Skip to content

Commit da5f4e6

Browse files
committed
Bluetooth: Audio: Use generic zephyr_count_bits to count bits
Instead of re-implementing or assuming that POPCOUNT is available we now use the generic function from Zephyr. Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
1 parent beafc2b commit da5f4e6

File tree

4 files changed

+8
-32
lines changed

4 files changed

+8
-32
lines changed

samples/bluetooth/cap_acceptor/src/cap_acceptor_broadcast.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @file
22
* @brief Bluetooth Common Audio Profile (CAP) Acceptor broadcast.
33
*
4-
* Copyright (c) 2024 Nordic Semiconductor ASA
4+
* Copyright (c) 2024-2025 Nordic Semiconductor ASA
55
*
66
* SPDX-License-Identifier: Apache-2.0
77
*/
@@ -459,7 +459,8 @@ static int bis_sync_req_cb(struct bt_conn *conn,
459459

460460
LOG_INF("BIS sync request received for %p: 0x%08x", recv_state, bis_sync_req[0]);
461461

462-
if (new_bis_sync_req != BT_BAP_BIS_SYNC_NO_PREF && POPCOUNT(new_bis_sync_req) > 1U) {
462+
if (new_bis_sync_req != BT_BAP_BIS_SYNC_NO_PREF &&
463+
zephyr_count_bits(&new_bis_sync_req, sizeof(new_bis_sync_req)) > 1U) {
463464
LOG_WRN("Rejecting BIS sync request for 0x%08X as we do not support that",
464465
new_bis_sync_req);
465466

subsys/bluetooth/audio/audio.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/*
44
* Copyright (c) 2022 Codecoup
5+
* Copyright (c) 2025 Nordic Semiconductor ASA
56
*
67
* SPDX-License-Identifier: Apache-2.0
78
*/
@@ -23,6 +24,7 @@
2324
#include <zephyr/bluetooth/hci_types.h>
2425
#include <zephyr/logging/log.h>
2526
#include <zephyr/sys/check.h>
27+
#include <zephyr/sys/util.h>
2628
#include <zephyr/toolchain.h>
2729

2830
#include "audio_internal.h"
@@ -146,18 +148,7 @@ uint8_t bt_audio_get_chan_count(enum bt_audio_location chan_allocation)
146148
return 1;
147149
}
148150

149-
#ifdef POPCOUNT
150-
return POPCOUNT(chan_allocation);
151-
#else
152-
uint8_t cnt = 0U;
153-
154-
while (chan_allocation != 0U) {
155-
cnt += chan_allocation & 1U;
156-
chan_allocation >>= 1U;
157-
}
158-
159-
return cnt;
160-
#endif
151+
return zephyr_count_bits(&chan_allocation, sizeof(chan_allocation));
161152
}
162153

163154
static bool valid_ltv_cb(struct bt_data *data, void *user_data)

subsys/bluetooth/audio/bap_broadcast_sink.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,22 +1081,6 @@ int bt_bap_broadcast_sink_create(struct bt_le_per_adv_sync *pa_sync, uint32_t br
10811081
return 0;
10821082
}
10831083

1084-
static uint8_t bit_count(uint32_t bitfield)
1085-
{
1086-
#ifdef POPCOUNT
1087-
return POPCOUNT(bitfield);
1088-
#else
1089-
uint8_t cnt = 0U;
1090-
1091-
while (bitfield != 0U) {
1092-
cnt += bitfield & 1U;
1093-
bitfield >>= 1U;
1094-
}
1095-
1096-
return cnt;
1097-
#endif
1098-
}
1099-
11001084
struct sync_base_info_data {
11011085
struct bt_audio_codec_cfg codec_cfgs[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT];
11021086
struct bt_audio_codec_cfg *subgroup_codec_cfg;
@@ -1281,7 +1265,7 @@ int bt_bap_broadcast_sink_sync(struct bt_bap_broadcast_sink *sink, uint32_t inde
12811265
}
12821266

12831267
/* Validate that number of bits set is within supported range */
1284-
bis_count = bit_count(indexes_bitfield);
1268+
bis_count = zephyr_count_bits(&indexes_bitfield, sizeof(indexes_bitfield));
12851269
if (bis_count > CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT) {
12861270
LOG_DBG("Cannot sync to more than %d streams (%u was requested)",
12871271
CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT, bis_count);

tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ static void test_broadcast_sync(const uint8_t broadcast_code[BT_ISO_BROADCAST_CO
801801
return;
802802
}
803803

804-
stream_sync_cnt = POPCOUNT(bis_index_bitfield);
804+
stream_sync_cnt = zephyr_count_bits(&bis_index_bitfield, sizeof(bis_index_bitfield));
805805
}
806806

807807
static void test_broadcast_sync_inval(void)

0 commit comments

Comments
 (0)