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
diff --git a/src/gprs_rlcmac_ts_alloc.cpp b/src/gprs_rlcmac_ts_alloc.cpp
index e6c8ad4..57197b2 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 @@
 /* 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 @@
 		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 @@
 		 * 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 @@
 			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 @@
 				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 @@
 				"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 @@
 		/* 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;