git: 9ff1462976fc - main - x86: simplify ceil(log2(x)) function

From: Doug Moore <dougm_at_FreeBSD.org>
Date: Tue, 04 Jun 2024 18:02:23 UTC
The branch main has been updated by dougm:

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

commit 9ff1462976fce4f4389be9a3357eadd22d04d308
Author:     Doug Moore <dougm@FreeBSD.org>
AuthorDate: 2024-06-04 18:00:25 +0000
Commit:     Doug Moore <dougm@FreeBSD.org>
CommitDate: 2024-06-04 18:00:25 +0000

    x86: simplify ceil(log2(x)) function
    
    A function called mask_width in one place and log2 in the other
    calculates its value in a more complex way than necessary. A simpler
    implementation offered here saves a few bytes in the functions that
    call it.
    
    Reviewed by:    alc, avg
    Differential Revision:  https://reviews.freebsd.org/D45483
---
 sys/amd64/vmm/x86.c  | 5 ++---
 sys/x86/x86/mp_x86.c | 6 ++----
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/sys/amd64/vmm/x86.c b/sys/amd64/vmm/x86.c
index 3cf689217590..aa8f87c98fe8 100644
--- a/sys/amd64/vmm/x86.c
+++ b/sys/amd64/vmm/x86.c
@@ -61,14 +61,13 @@ SYSCTL_INT(_hw_vmm_topology, OID_AUTO, cpuid_leaf_b, CTLFLAG_RDTUN,
     &cpuid_leaf_b, 0, NULL);
 
 /*
- * Round up to the next power of two, if necessary, and then take log2.
- * Returns -1 if argument is zero.
+ * Compute ceil(log2(x)).  Returns -1 if x is zero.
  */
 static __inline int
 log2(u_int x)
 {
 
-	return (fls(x << (1 - powerof2(x))) - 1);
+	return (x == 0 ? -1 : fls(x - 1));
 }
 
 int
diff --git a/sys/x86/x86/mp_x86.c b/sys/x86/x86/mp_x86.c
index 1027c2c8972b..3c08f7fa987d 100644
--- a/sys/x86/x86/mp_x86.c
+++ b/sys/x86/x86/mp_x86.c
@@ -183,15 +183,13 @@ mem_range_AP_init(void)
 }
 
 /*
- * Round up to the next power of two, if necessary, and then
- * take log2.
- * Returns -1 if argument is zero.
+ * Compute ceil(log2(x)).  Returns -1 if x is zero.
  */
 static __inline int
 mask_width(u_int x)
 {
 
-	return (fls(x << (1 - powerof2(x))) - 1);
+	return (x == 0 ? -1 : fls(x - 1));
 }
 
 /*