From nobody Sun Feb 8 05:19:56 2026 Received: from 3.mo582.mail-out.ovh.net (3.mo582.mail-out.ovh.net [178.33.253.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 299212E8B75 for ; Wed, 21 Jan 2026 17:41:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.33.253.26 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769017320; cv=none; b=sK/SW2w6spqX07emK/X6Oi4t/00aimEIBI7dRvnu8asG7WyMOIvDfS4ZfTHSLMqvZYnKC3tVAyB6eF/J45maxFGk3PnKw8hlAlaLqLdMbAh9x0WmOwi2QCyV5HcWcr54HXvBX0AJb+IsjmBtYabmhmpuHypGjTDtkO4TrLMQYdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769017320; c=relaxed/simple; bh=Xun+X1m7ZFdFfPY2avmUdGDR2euwjZCYQMcle3nhaXA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Tm//BbnK130ZscCe8hl0oluqQeGlXyh8Jr/mYC8u4sb2zuq78BDRmqWkB/mXx1DyqJMO+PTRChqc0dQVXIvS3Og35mHZxYJIYL2GOBcL0wIIjtVeut06Aqfv2bIFgrSCPV6/ZCy/j9oL7XVlePlUT5YZXDvy5a5TRZoPzx1V1Xs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=formalgen.com; spf=pass smtp.mailfrom=formalgen.com; dkim=pass (2048-bit key) header.d=formalgen.com header.i=@formalgen.com header.b=hNr6XjuV; arc=none smtp.client-ip=178.33.253.26 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=formalgen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=formalgen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=formalgen.com header.i=@formalgen.com header.b="hNr6XjuV" Received: from director11.ghost.mail-out.ovh.net (unknown [10.110.58.226]) by mo582.mail-out.ovh.net (Postfix) with ESMTP id 4dx0rg4qgJz5xw2 for ; Wed, 21 Jan 2026 10:32:51 +0000 (UTC) Received: from ghost-submission-7d8d68f679-6hxkw (unknown [10.110.118.75]) by director11.ghost.mail-out.ovh.net (Postfix) with ESMTPS id BBE51C2DD8; Wed, 21 Jan 2026 10:32:50 +0000 (UTC) Received: from formalgen.com ([37.59.142.113]) by ghost-submission-7d8d68f679-6hxkw with ESMTPSA id SjgmI1KrcGlcAQAAyVf5bQ (envelope-from ); Wed, 21 Jan 2026 10:32:50 +0000 Authentication-Results: garm.ovh; auth=pass (GARM-113S007351535fe-0ae1-449d-9efe-0fe41ead9597, 831801AFC7761530D51FB11603EF4B8E66579AB0) smtp.auth=david.desobry@formalgen.com X-OVh-ClientIp: 90.91.42.105 From: David Desobry To: tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com Cc: x86@kernel.org, hpa@zytor.com, linux-kernel@vger.kernel.org, David Desobry Subject: [PATCH v5] x86/lib: Rename num_digits() to num_digits_u32() and optimize Date: Wed, 21 Jan 2026 11:32:04 +0100 Message-ID: <20260121103204.48713-1-david.desobry@formalgen.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260121095405.58fa513f@pumpkin> References: <20260121095405.58fa513f@pumpkin> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Ovh-Tracer-Id: 12345211005592980427 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: 0 X-VR-SPAMCAUSE: dmFkZTEJS6MjQfni21CXfm8en1kAf69L64SHIs0z8dcUHzNtjYUHOzWhgOP5SQeUVZ1XDJisc0LEbweItWFPMxtK4h710B70YAEdrf3epgQNNvPhHxTKx83st/dUrU5n7OKy725ZUO52CeH+JTinufNj3ei3ivmQbA87nymNSEnC3XUWUNK/EUx1aaBXwR/hmcRfVV5J115c6h3xnthN2ulrvXxYmWSKBnYSAq0KonCAgebSX3Reh+BwCaEY+tlrpPiFTVKONPyVmf1TH2yxoVViS90ji6CK7Hms7IV3V3WAOARkS0O+qYrSUF7EPQm8YE/6R2hQEZROZBeCtIYyDkGBrzXPqswWlZZlpxUgtAJ8ipqqin5OnG2PGo4uwBqDSl1o/aTJzThhITaegTseGpbsX/GYduY9K0wnhGpre1GIcJvgk6ay3WruZowFnAtJQ+hzjUjgky8vLN9x+XpCbk7AsH8bifTYtPBZ3vKTpTeKFF5zY8265iZFB5E9Dhs6H76uFFk8IxETpkEhFE7onqMZ1Bz1w5VTTwdSHdTEsqD0WAdnOkrlk/Szgb7eyy+C69njUVfmye/nYmMRLB77j3vFEQTpG6M3blQmMM/Orm650Hm6nMBcfJEnbf3RJd83ZHCYIsX6da/2fWvLuN0OnHgMVq3BKIqJu9xjnAv/tHhHL1vezg DKIM-Signature: a=rsa-sha256; bh=17u9I4OlZpG7XNJ3HEZzKMScNG5Pd0nzgS8E19ZKbB0=; c=relaxed/relaxed; d=formalgen.com; h=From; s=ovhmo-selector-1; t=1768991571; v=1; b=hNr6XjuVXvByw0VOcE4CKtH99E2CYUKwsgJOEWKODXWzZk43XKRXDllu0SjfD/PoV9s8KH2o bZLU0e49YMHL8EuBcyw0JpCkMi6VWP3bXTQ4mfCUudokjoyGGKic5eRXv0iJ0kFAkkj9kHP22Hy FsmVfVbDydCRB0DW/moKlYNUfkfeG+BKY1HOqr0fDfrLxwJ2CtklHY8pPaqOFjxRYb5d2udRNCh 5K6DwskvByPvtj61dMvzq9L54jR1zbis0OhzuyxLLKYpgf5oTXH6xiL7JNsZBT61aQBsDehsotZ 3HFME9Uib8ThwJ4jFULIZdPf3Bw+eCSGtRSHNNhgFbbtQ== Content-Type: text/plain; charset="utf-8" The current implementation of num_digits() uses a loop with repeated multiplication, which is inefficient. Furthermore, it takes a signed integer which leads to undefined behavior when negating INT_MIN. Since this function is only used for unsigned magnitudes, change it to num_digits_u32() and convert the interface to take an unsigned int. This naturally resolves the INT_MIN overflow issue and clarifies the function's intended use. Replace the loop with a branchless sequence using inline assembly. By using the 'sbb' instruction against the carry flag after comparisons, we eliminate all conditional branches. This provides constant-time performance and avoids CPU branch misprediction penalties. Update all existing callers and the function comment to reflect the new name and the fact that signs are no longer handled. Signed-off-by: David Desobry --- v5: - Changed name to num_digits_u32() and updated all callers to avoid=20 semantic confusion v4: - Switched to branchless inline assembly. - Changed function signature to unsigned int. - Updated prototype in arch/x86/include/asm/misc.h. arch/x86/include/asm/misc.h | 2 +- arch/x86/kernel/smpboot.c | 8 ++++---- arch/x86/lib/misc.c | 28 +++++++++++++--------------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/arch/x86/include/asm/misc.h b/arch/x86/include/asm/misc.h index bb049cca3729..78d96b81d985 100644 --- a/arch/x86/include/asm/misc.h +++ b/arch/x86/include/asm/misc.h @@ -2,6 +2,6 @@ #ifndef _ASM_X86_MISC_H #define _ASM_X86_MISC_H =20 -int num_digits(int val); +int num_digits_u32(unsigned int x); =20 #endif /* _ASM_X86_MISC_H */ diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c index 5cd6950ab672..fc92f04c7f04 100644 --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -849,10 +849,10 @@ static void announce_cpu(int cpu, int apicid) int node =3D early_cpu_to_node(cpu); =20 if (!width) - width =3D num_digits(num_possible_cpus()) + 1; /* + '#' sign */ + width =3D num_digits_u32(num_possible_cpus()) + 1; /* + '#' sign */ =20 if (!node_width) - node_width =3D num_digits(num_possible_nodes()) + 1; /* + '#' */ + node_width =3D num_digits_u32(num_possible_nodes()) + 1; /* + '#' */ =20 if (system_state < SYSTEM_RUNNING) { if (first) @@ -864,7 +864,7 @@ static void announce_cpu(int cpu, int apicid) current_node =3D node; =20 printk(KERN_INFO ".... node %*s#%d, CPUs: ", - node_width - num_digits(node), " ", node); + node_width - num_digits_u32(node), " ", node); } =20 /* Add padding for the BSP */ @@ -872,7 +872,7 @@ static void announce_cpu(int cpu, int apicid) pr_cont("%*s", width + 1, " "); first =3D 0; =20 - pr_cont("%*s#%d", width - num_digits(cpu), " ", cpu); + pr_cont("%*s#%d", width - num_digits_u32(cpu), " ", cpu); } else pr_info("Booting Node %d Processor %d APIC 0x%x\n", node, cpu, apicid); diff --git a/arch/x86/lib/misc.c b/arch/x86/lib/misc.c index 40b81c338ae5..b21ca6f0dbf8 100644 --- a/arch/x86/lib/misc.c +++ b/arch/x86/lib/misc.c @@ -2,23 +2,21 @@ #include =20 /* - * Count the digits of @val including a possible sign. - * - * (Typed on and submitted from hpa's mobile phone.) + * Count the decimal digits of an unsigned integer. */ -int num_digits(int val) +int num_digits_u32(unsigned int x) { - long long m =3D 10; - int d =3D 1; + int n =3D 0; =20 - if (val < 0) { - d++; - val =3D -val; - } + asm("cmp %2,%1; sbb $-2,%0" : "+r" (n) : "r" (x), "g" (10)); + asm("cmp %2,%1; sbb $-1,%0" : "+r" (n) : "r" (x), "g" (100)); + asm("cmp %2,%1; sbb $-1,%0" : "+r" (n) : "r" (x), "g" (1000)); + asm("cmp %2,%1; sbb $-1,%0" : "+r" (n) : "r" (x), "g" (10000)); + asm("cmp %2,%1; sbb $-1,%0" : "+r" (n) : "r" (x), "g" (100000)); + asm("cmp %2,%1; sbb $-1,%0" : "+r" (n) : "r" (x), "g" (1000000)); + asm("cmp %2,%1; sbb $-1,%0" : "+r" (n) : "r" (x), "g" (10000000)); + asm("cmp %2,%1; sbb $-1,%0" : "+r" (n) : "r" (x), "g" (100000000)); + asm("cmp %2,%1; sbb $-1,%0" : "+r" (n) : "r" (x), "g" (1000000000)); =20 - while (val >=3D m) { - m *=3D 10; - d++; - } - return d; + return n; } --=20 2.43.0