git: f0a0420dfd36 - main - powerof2: replace loops with fls or ilog2

From: Doug Moore <dougm_at_FreeBSD.org>
Date: Wed, 12 Jun 2024 10:01:04 UTC
The branch main has been updated by dougm:

URL: https://cgit.FreeBSD.org/src/commit/?id=f0a0420dfd36ae90f86cc9bfb1342b8862b8c9ec

commit f0a0420dfd36ae90f86cc9bfb1342b8862b8c9ec
Author:     Doug Moore <dougm@FreeBSD.org>
AuthorDate: 2024-06-12 09:26:42 +0000
Commit:     Doug Moore <dougm@FreeBSD.org>
CommitDate: 2024-06-12 10:00:48 +0000

    powerof2: replace loops with fls or ilog2
    
    In several places, a loop tests for powers of two, or iterates through
    powers of two.  In those places, replace the loop with an invocation
    of fls or ilog2 without changing the meaning of the code.
    
    Reviewed by:    alc, markj, kib, np, erj, avg (previous version)
    Differential Revision:  https://reviews.freebsd.org/D45494
---
 sys/dev/aic7xxx/aic79xx.c           |  3 +--
 sys/dev/cxgb/cxgb_sge.c             |  7 ++-----
 sys/dev/cxgbe/t4_sge.c              |  4 +---
 sys/dev/irdma/irdma_ctrl.c          | 16 +++++-----------
 sys/dev/mlx5/mlx5_en/mlx5_en_main.c |  4 +---
 sys/netpfil/ipfw/ip_fw_table.c      | 10 +---------
 6 files changed, 11 insertions(+), 33 deletions(-)

diff --git a/sys/dev/aic7xxx/aic79xx.c b/sys/dev/aic7xxx/aic79xx.c
index 0c4b615c5b24..ab68c3d8b088 100644
--- a/sys/dev/aic7xxx/aic79xx.c
+++ b/sys/dev/aic7xxx/aic79xx.c
@@ -8593,8 +8593,7 @@ ahd_loadseq(struct ahd_softc *ahd)
 	if (sg_prefetch_align == 0)
 		sg_prefetch_align = 8;
 	/* Round down to the nearest power of 2. */
-	while (powerof2(sg_prefetch_align) == 0)
-		sg_prefetch_align--;
+	sg_prefetch_align = 1 << ilog2(sg_prefetch_align);
 
 	cacheline_mask = sg_prefetch_align - 1;
 
diff --git a/sys/dev/cxgb/cxgb_sge.c b/sys/dev/cxgb/cxgb_sge.c
index f57494065aec..0c5be9dd6614 100644
--- a/sys/dev/cxgb/cxgb_sge.c
+++ b/sys/dev/cxgb/cxgb_sge.c
@@ -553,9 +553,7 @@ t3_sge_prep(adapter_t *adap, struct sge_params *p)
 	nqsets *= adap->params.nports;
 
 	fl_q_size = min(nmbclusters/(3*nqsets), FL_Q_SIZE);
-
-	while (!powerof2(fl_q_size))
-		fl_q_size--;
+	fl_q_size = 1 << ilog2(fl_q_size);
 
 	use_16k = cxgb_use_16k_clusters != -1 ? cxgb_use_16k_clusters :
 	    is_offload(adap);
@@ -567,8 +565,7 @@ t3_sge_prep(adapter_t *adap, struct sge_params *p)
 		jumbo_q_size = min(nmbjumbo9/(3*nqsets), JUMBO_Q_SIZE);
 		jumbo_buf_size = MJUM9BYTES;
 	}
-	while (!powerof2(jumbo_q_size))
-		jumbo_q_size--;
+	jumbo_q_size = 1 << ilog2(jumbo_q_size);
 
 	if (fl_q_size < (FL_Q_SIZE / 4) || jumbo_q_size < (JUMBO_Q_SIZE / 2))
 		device_printf(adap->dev,
diff --git a/sys/dev/cxgbe/t4_sge.c b/sys/dev/cxgbe/t4_sge.c
index e1705ae063e2..505f2d1bf677 100644
--- a/sys/dev/cxgbe/t4_sge.c
+++ b/sys/dev/cxgbe/t4_sge.c
@@ -4220,9 +4220,7 @@ qsize_to_fthresh(int qsize)
 {
 	u_int fthresh;
 
-	while (!powerof2(qsize))
-		qsize++;
-	fthresh = ilog2(qsize);
+	fthresh = qsize == 0 ? 0 : fls(qsize - 1);
 	if (fthresh > X_CIDXFLUSHTHRESH_128)
 		fthresh = X_CIDXFLUSHTHRESH_128;
 
diff --git a/sys/dev/irdma/irdma_ctrl.c b/sys/dev/irdma/irdma_ctrl.c
index dc42b15392c5..6078ac43815e 100644
--- a/sys/dev/irdma/irdma_ctrl.c
+++ b/sys/dev/irdma/irdma_ctrl.c
@@ -4909,7 +4909,7 @@ irdma_cfg_fpm_val(struct irdma_sc_dev *dev, u32 qp_count)
 	struct irdma_virt_mem virt_mem;
 	u32 i, mem_size;
 	u32 qpwanted, mrwanted, pblewanted;
-	u32 powerof2, hte;
+	u32 hte;
 	u32 sd_needed;
 	u32 sd_diff;
 	u32 loop_count = 0;
@@ -4938,12 +4938,8 @@ irdma_cfg_fpm_val(struct irdma_sc_dev *dev, u32 qp_count)
 		    hmc_info->sd_table.sd_cnt, max_sds);
 
 	qpwanted = min(qp_count, hmc_info->hmc_obj[IRDMA_HMC_IW_QP].max_cnt);
-
-	powerof2 = 1;
-	while (powerof2 <= qpwanted)
-		powerof2 *= 2;
-	powerof2 /= 2;
-	qpwanted = powerof2;
+	if (qpwanted != 0)
+		qpwanted = 1 << ilog2(qpwanted);
 
 	mrwanted = hmc_info->hmc_obj[IRDMA_HMC_IW_MR].max_cnt;
 	pblewanted = hmc_info->hmc_obj[IRDMA_HMC_IW_PBLE].max_cnt;
@@ -4986,11 +4982,9 @@ irdma_cfg_fpm_val(struct irdma_sc_dev *dev, u32 qp_count)
 		hmc_info->hmc_obj[IRDMA_HMC_IW_MR].cnt = mrwanted;
 
 		hte = round_up(qpwanted + hmc_info->hmc_obj[IRDMA_HMC_IW_FSIMC].cnt, 512);
-		powerof2 = 1;
-		while (powerof2 < hte)
-			powerof2 *= 2;
+		hte = hte == 0 ? 1 : 1 << fls(hte - 1);
 		hmc_info->hmc_obj[IRDMA_HMC_IW_HTE].cnt =
-		    powerof2 * hmc_fpm_misc->ht_multiplier;
+		    hte * hmc_fpm_misc->ht_multiplier;
 		if (dev->hw_attrs.uk_attrs.hw_rev == IRDMA_GEN_1)
 			cfg_fpm_value_gen_1(dev, hmc_info, qpwanted);
 		else
diff --git a/sys/dev/mlx5/mlx5_en/mlx5_en_main.c b/sys/dev/mlx5/mlx5_en/mlx5_en_main.c
index 962705e6d258..4d9d1048448b 100644
--- a/sys/dev/mlx5/mlx5_en/mlx5_en_main.c
+++ b/sys/dev/mlx5/mlx5_en/mlx5_en_main.c
@@ -2331,9 +2331,7 @@ mlx5e_get_wqe_sz(struct mlx5e_priv *priv, u32 *wqe_sz, u32 *nsegs)
 	 * Stride size is 16 * (n + 1), as the first segment is
 	 * control.
 	 */
-	for (n = howmany(r, MLX5E_MAX_RX_BYTES); !powerof2(n + 1); n++)
-		;
-
+	n = (1 << fls(howmany(r, MLX5E_MAX_RX_BYTES))) - 1;
 	if (n > MLX5E_MAX_BUSDMA_RX_SEGS)
 		return (-ENOMEM);
 
diff --git a/sys/netpfil/ipfw/ip_fw_table.c b/sys/netpfil/ipfw/ip_fw_table.c
index b6847d43081b..5cf948ce39a2 100644
--- a/sys/netpfil/ipfw/ip_fw_table.c
+++ b/sys/netpfil/ipfw/ip_fw_table.c
@@ -1516,15 +1516,7 @@ static uint32_t
 roundup2p(uint32_t v)
 {
 
-	v--;
-	v |= v >> 1;
-	v |= v >> 2;
-	v |= v >> 4;
-	v |= v >> 8;
-	v |= v >> 16;
-	v++;
-
-	return (v);
+	return (1 << fls(v - 1));
 }
 
 /*