Skip to content

Commit 16d17f3

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 02b1c0d commit 16d17f3

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
@@ -1073,22 +1073,6 @@ int bt_bap_broadcast_sink_create(struct bt_le_per_adv_sync *pa_sync, uint32_t br
10731073
return 0;
10741074
}
10751075

1076-
static uint8_t bit_count(uint32_t bitfield)
1077-
{
1078-
#ifdef POPCOUNT
1079-
return POPCOUNT(bitfield);
1080-
#else
1081-
uint8_t cnt = 0U;
1082-
1083-
while (bitfield != 0U) {
1084-
cnt += bitfield & 1U;
1085-
bitfield >>= 1U;
1086-
}
1087-
1088-
return cnt;
1089-
#endif
1090-
}
1091-
10921076
struct sync_base_info_data {
10931077
struct bt_audio_codec_cfg codec_cfgs[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT];
10941078
struct bt_audio_codec_cfg *subgroup_codec_cfg;
@@ -1273,7 +1257,7 @@ int bt_bap_broadcast_sink_sync(struct bt_bap_broadcast_sink *sink, uint32_t inde
12731257
}
12741258

12751259
/* Validate that number of bits set is within supported range */
1276-
bis_count = bit_count(indexes_bitfield);
1260+
bis_count = zephyr_count_bits(&indexes_bitfield, sizeof(indexes_bitfield));
12771261
if (bis_count > CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT) {
12781262
LOG_DBG("Cannot sync to more than %d streams (%u was requested)",
12791263
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)