Skip to content

Commit 5734d7e

Browse files
Thalleykartben
authored andcommitted
Bluetooth: Audio: Use generic sys_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 <[email protected]>
1 parent b201cb7 commit 5734d7e

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+
sys_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 sys_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
@@ -1096,22 +1096,6 @@ int bt_bap_broadcast_sink_create(struct bt_le_per_adv_sync *pa_sync, uint32_t br
10961096
return 0;
10971097
}
10981098

1099-
static uint8_t bit_count(uint32_t bitfield)
1100-
{
1101-
#ifdef POPCOUNT
1102-
return POPCOUNT(bitfield);
1103-
#else
1104-
uint8_t cnt = 0U;
1105-
1106-
while (bitfield != 0U) {
1107-
cnt += bitfield & 1U;
1108-
bitfield >>= 1U;
1109-
}
1110-
1111-
return cnt;
1112-
#endif
1113-
}
1114-
11151099
struct sync_base_info_data {
11161100
struct bt_audio_codec_cfg codec_cfgs[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT];
11171101
struct bt_audio_codec_cfg *subgroup_codec_cfg;
@@ -1296,7 +1280,7 @@ int bt_bap_broadcast_sink_sync(struct bt_bap_broadcast_sink *sink, uint32_t inde
12961280
}
12971281

12981282
/* Validate that number of bits set is within supported range */
1299-
bis_count = bit_count(indexes_bitfield);
1283+
bis_count = sys_count_bits(&indexes_bitfield, sizeof(indexes_bitfield));
13001284
if (bis_count > CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT) {
13011285
LOG_DBG("Cannot sync to more than %d streams (%u was requested)",
13021286
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 = sys_count_bits(&bis_index_bitfield, sizeof(bis_index_bitfield));
805805
}
806806

807807
static void test_broadcast_sync_inval(void)

0 commit comments

Comments
 (0)