diff options
author | Jacob Erlbeck <jerlbeck@sysmocom.de> | 2016-01-19 15:48:03 +0100 |
---|---|---|
committer | Jacob Erlbeck <jerlbeck@sysmocom.de> | 2016-02-08 00:45:36 +0100 |
commit | 8cba7e9b6d11918c9865006b41f768943e1e44df (patch) | |
tree | a6182e97c2e93cbf609557a1eb2b0ad7cc48cbcf | |
parent | 13965aed74ae1c59cfd4f52275ac3e98f9aa9e3a (diff) |
utils: Add pcu_bitcount and pcu_lsb
These functions are currently defined in src/gprs_rlcmac_ts_alloc.cpp
but will be needed elsewhere.
Turn them into template functions to support different types and move
them to pcu_utils.h.
Sponsored-by: On-Waves ehf
-rw-r--r-- | src/gprs_rlcmac_ts_alloc.cpp | 29 | ||||
-rw-r--r-- | src/pcu_utils.h | 16 |
2 files changed, 24 insertions, 21 deletions
diff --git a/src/gprs_rlcmac_ts_alloc.cpp b/src/gprs_rlcmac_ts_alloc.cpp index e6c8ad4c..57197b22 100644 --- a/src/gprs_rlcmac_ts_alloc.cpp +++ b/src/gprs_rlcmac_ts_alloc.cpp @@ -24,6 +24,7 @@ #include <bts.h> #include <tbf.h> #include <gprs_ms.h> +#include <pcu_utils.h> #include <errno.h> #include <values.h> @@ -80,20 +81,6 @@ static const struct gprs_ms_multislot_class gprs_ms_multislot_class[32] = { /* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA }, }; -static unsigned lsb(unsigned x) -{ - return x & -x; -} - -static unsigned bitcount(unsigned x) -{ - unsigned count = 0; - for (count = 0; x; count += 1) - x &= x - 1; - - return count; -} - static char *set_flag_chars(char *buf, uint8_t val, char set_char, char unset_char = 0) { int i; @@ -640,7 +627,7 @@ static int find_multi_slots(struct gprs_rlcmac_bts *bts, if ((tx_window & (1 << ((ul_ts+num_tx-1) % 8))) == 0) continue; - tx_slot_count = bitcount(tx_window); + tx_slot_count = pcu_bitcount(tx_window); max_rx = OSMO_MIN(ms_class->rx, ms_class->sum - num_tx); rx_valid_win = (1 << max_rx) - 1; @@ -669,7 +656,7 @@ static int find_multi_slots(struct gprs_rlcmac_bts *bts, * testing */ rx_window = rx_good & rx_valid_win; - rx_slot_count = bitcount(rx_window); + rx_slot_count = pcu_bitcount(rx_window); #if 0 LOGP(DRLCMAC, LOGL_DEBUG, "n_tx=%d, n_rx=%d, mask_sel=%d, " @@ -734,7 +721,7 @@ static int find_multi_slots(struct gprs_rlcmac_bts *bts, continue; /* Check number of common slots according to TS 54.002, 6.4.2.2 */ - common_slot_count = bitcount(tx_window & rx_window); + common_slot_count = pcu_bitcount(tx_window & rx_window); req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count); if (ms_class->type == 1) req_common_slots = OSMO_MIN(req_common_slots, 2); @@ -891,7 +878,7 @@ int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, dl_slots & ul_slots, compute_usage_by_num_tbfs, NULL, NULL); if (ts < 0) - ul_slots = dl_slots = lsb(dl_slots & ul_slots); + ul_slots = dl_slots = pcu_lsb(dl_slots & ul_slots); else ul_slots = dl_slots = (dl_slots & ul_slots) & (1<<ts); } @@ -920,9 +907,9 @@ int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, "available\n"); return -EINVAL; } - slotcount = bitcount(dl_slots); + slotcount = pcu_bitcount(dl_slots); first_ts = ffs(dl_slots) - 1; - avail_count = bitcount(reserved_dl_slots); + avail_count = pcu_bitcount(reserved_dl_slots); } else { int free_usf = -1; @@ -958,7 +945,7 @@ int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, /* We will stick to that single UL slot, unreserve the others */ reserved_ul_slots = ul_slots; - avail_count = bitcount(reserved_ul_slots); + avail_count = pcu_bitcount(reserved_ul_slots); } first_common_ts = ffs(dl_slots & ul_slots) - 1; diff --git a/src/pcu_utils.h b/src/pcu_utils.h index 5ca234a4..d6644462 100644 --- a/src/pcu_utils.h +++ b/src/pcu_utils.h @@ -24,3 +24,19 @@ inline void csecs_to_timeval(unsigned csecs, struct timeval *tv) { tv->tv_sec = csecs / 100; tv->tv_usec = (csecs % 100) * 10000; } + +template <typename T> +inline unsigned int pcu_bitcount(T x) +{ + unsigned int count = 0; + for (count = 0; x; count += 1) + x &= x - 1; + + return count; +} + +template <typename T> +inline T pcu_lsb(T x) +{ + return x & -x; +} |