From 9ff1462976fce4f4389be9a3357eadd22d04d308 Mon Sep 17 00:00:00 2001 From: Doug Moore Date: Tue, 4 Jun 2024 13:00:25 -0500 Subject: [PATCH] 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 3cf68921759..aa8f87c98fe 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 1027c2c8972..3c08f7fa987 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)); } /*