You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2022/03/10 11:52:52 UTC

[mynewt-nimble] 01/07: nimble/ll: Optimize num used channels calculation

This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 6faa4027dea1d1ed58e344a7eba938ca308242e1
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 28 11:05:06 2022 +0100

    nimble/ll: Optimize num used channels calculation
    
    Using single loop and uint32 should be more efficient than nested loops
    with uint8.
---
 .../controller/include/controller/ble_ll_utils.h   |  2 +-
 nimble/controller/src/ble_ll_utils.c               | 37 ++++++++--------------
 2 files changed, 15 insertions(+), 24 deletions(-)

diff --git a/nimble/controller/include/controller/ble_ll_utils.h b/nimble/controller/include/controller/ble_ll_utils.h
index e0b1646..48d76a6 100644
--- a/nimble/controller/include/controller/ble_ll_utils.h
+++ b/nimble/controller/include/controller/ble_ll_utils.h
@@ -23,7 +23,7 @@ uint32_t ble_ll_utils_calc_access_addr(void);
 uint8_t ble_ll_utils_remapped_channel(uint8_t remap_index, const uint8_t *chanmap);
 uint8_t ble_ll_utils_calc_dci_csa2(uint16_t event_cntr, uint16_t channel_id,
                                    uint8_t num_used_chans, const uint8_t *chanmap);
-uint8_t ble_ll_utils_calc_num_used_chans(const uint8_t *chanmap);
+uint8_t ble_ll_utils_calc_num_used_chans(const uint8_t *chan_map);
 uint32_t ble_ll_utils_calc_window_widening(uint32_t anchor_point,
                                            uint32_t last_anchor_point,
                                            uint8_t central_sca);
diff --git a/nimble/controller/src/ble_ll_utils.c b/nimble/controller/src/ble_ll_utils.c
index 66ca3e2..c312626 100644
--- a/nimble/controller/src/ble_ll_utils.c
+++ b/nimble/controller/src/ble_ll_utils.c
@@ -182,32 +182,23 @@ ble_ll_utils_remapped_channel(uint8_t remap_index, const uint8_t *chanmap)
 }
 
 uint8_t
-ble_ll_utils_calc_num_used_chans(const uint8_t *chmap)
+ble_ll_utils_calc_num_used_chans(const uint8_t *chan_map)
 {
-    int i;
-    int j;
-    uint8_t mask;
-    uint8_t chanbyte;
-    uint8_t used_channels;
-
-    used_channels = 0;
-    for (i = 0; i < BLE_LL_CHMAP_LEN; ++i) {
-        chanbyte = chmap[i];
-        if (chanbyte) {
-            if (chanbyte == 0xff) {
-                used_channels += 8;
-            } else {
-                mask = 0x01;
-                for (j = 0; j < 8; ++j) {
-                    if (chanbyte & mask) {
-                        ++used_channels;
-                    }
-                    mask <<= 1;
-                }
-            }
+    uint32_t u32 = 0;
+    uint32_t num_used_chans = 0;
+    unsigned idx;
+
+    for (idx = 0; idx < 37; idx++) {
+        if ((idx % 8) == 0) {
+            u32 = chan_map[idx / 8];
         }
+        if (u32 & 1) {
+            num_used_chans++;
+        }
+        u32 >>= 1;
     }
-    return used_channels;
+
+    return num_used_chans;
 }
 
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CSA2)