From nobody Sun Feb 8 05:35:40 2026 Received: from 7.mo575.mail-out.ovh.net (7.mo575.mail-out.ovh.net [46.105.63.230]) (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 B65B336403C for ; Tue, 20 Jan 2026 18:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.105.63.230 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768932378; cv=none; b=LSW2QEsrWqQBzSmmoGzMbECfW4cV90Uo8q7dLflgKMc19alvlXp0S4iBuiItuElLmcmPm9DeGDKybRoxgiwAUVCfNoU+IL+5LnRKzuCgJ6ZCoaMnN8RJhX2Fe6nT83xXlt5VfKB15J+wzr0oBJU9hb1RxpKQ5akO+EuqSYNK2No= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768932378; c=relaxed/simple; bh=EBmVZIHjluOGk3tEyXy7QvO/kCDgYUPQFvtgLBvAJts=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=FqI1jg4dlqyBCVSCfkWA4CovHSRGXVAh6/luyWrimkAbh3SMv8DLSv14NGvlCroVGpFbpJkyPw5hX3830vhB9nFiZBewyZsEOWgVA9065Yp4F0jHSSs4dj12dR+CurdXq5CEoHEnmLij8hRUPANptHeHh3wIZh2YyNG01Nh3i7o= 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=CmPghh7v; arc=none smtp.client-ip=46.105.63.230 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="CmPghh7v" Received: from director4.ghost.mail-out.ovh.net (unknown [10.109.249.109]) by mo575.mail-out.ovh.net (Postfix) with ESMTP id 4dwZYK6jdmz5y9h for ; Tue, 20 Jan 2026 17:48:05 +0000 (UTC) Received: from ghost-submission-7d8d68f679-t5mzt (unknown [10.110.168.242]) by director4.ghost.mail-out.ovh.net (Postfix) with ESMTPS id 6064FC28F5; Tue, 20 Jan 2026 17:48:05 +0000 (UTC) Received: from formalgen.com ([37.59.142.113]) by ghost-submission-7d8d68f679-t5mzt with ESMTPSA id GdQmDdW/b2ltHQAAHKa83w (envelope-from ); Tue, 20 Jan 2026 17:48:05 +0000 Authentication-Results: garm.ovh; auth=pass (GARM-113S00773d8edf1-cd26-48ff-bfd3-b9cb0077b33c, BD04578FE93AF9D298F1B82426988FE6DFB30C64) 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 v2] x86/lib: Optimize num_digits() and fix INT_MIN overflow Date: Tue, 20 Jan 2026 18:47:48 +0100 Message-ID: <20260120174748.302078-1-david.desobry@formalgen.com> X-Mailer: git-send-email 2.43.0 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: 13822954632189627339 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: 0 X-VR-SPAMCAUSE: dmFkZTF0+vJFwzw5091iw2RX2YPDBU7gY4onkGgCkmi3PMtiFTT4HJ5Bt8Rfn+DjJoMVMelbTx+4QC26GMndHfNDSpI6+a9dzQBTB4rqXfCChI9iIrWix6gk+u6pi8isTKTB0NR/EDAHorhRWplkSIubsRNnQ/CejyptdaNuBXBHtyu+qfQqElIEjPnFmOM1qDANFacuxuuO2E38RYjpIAnXgvauo4f9IQyz/00dK0dmHhcAr9pDicZdEbY88Z65nR1LzSTwyeNiLZzkYZu1/ExtdMsxNTGAp3od7nvboTseoZ8XxyOn1BUJcQmp2cXf7xpfm09N2Ik5Bu4xlo+yjGeD4T4nEdtK8TJgE0I4AZ7vozTv4JI8Icy/ZRMoZ3SV1h4jGavx78/f/016+PlpW3DPCIuouzAacM/wDhusY+yUIELR4bR4kfdnfnqKbxXrzQrR1tlCWIg+IbRjMwDrFJw3n2/w2UWIE+NndAknWRplUerWhURkB1Fx90TW9oAaz/TyUDu89/s2zFjOr/IBbEgUNmgGySmiZQuqvoNaQwCsf5P1VhvMbHLjKgE3JIy1hmofyM2Unv/g80VlzjnPH0UzryH+M0O138GKq96A5Q9woiaIHlrVQvIaGAuZWxkShq+ngPyqf7NvZR31p+pUvD4gs1W9+piMJKlKn2gFpbvjsUugLA DKIM-Signature: a=rsa-sha256; bh=BE03z49gsKLzLoJYvIJaS8/2DlI5ay6ebmG7hmmO2vw=; c=relaxed/relaxed; d=formalgen.com; h=From; s=ovhmo-selector-1; t=1768931286; v=1; b=CmPghh7vuiWq1W8JNxT6Gx72OCFHru/vdCfja9YxC74LQG5CoizRywcjFlB+9nC7GkPJPNJi upvmUOtoAtyg2dDUCks9Q/RjVAmQ+Y+TAkMDeUsmgn1F3WCz0yk0r/u+HShNzVnPD/C7R0tgh0p y4SNDndxGNzQVhQvRe9bZIuy9jio8uSmP3kpLHXa3C/sjqMDNDA6gkhWJ3N4URajNLsNVtcuNam AHXJaUkFtYGTztaJs6Wh856WW+/EptN4jci8A0C/kGpLHSoUprajQayeadg7Me5tMveHTL43you 5mO4uEKfNEmwsoiQfkGKd829d6Fe6lYNv1qFEJjUBcgUw== Content-Type: text/plain; charset="utf-8" The current implementation of num_digits() uses a loop with repeated multiplication, which is inefficient. Furthermore, the negation of the input value "val =3D -val" causes undefined behavior when val is INT_MIN, as its absolute value cannot be represented as a 32-bit integer. Replace the loop with a switch statement using GCC case ranges. This allows the compiler to generate a jump table or a series of optimized comparisons, providing O(1) performance. By using an unsigned int to handle the magnitude, we safely handle the INT_MIN case without relying on 64-bit types or undefined signed overflow. Removed the outdated comment. Signed-off-by: David Desobry Acked-by: H. Peter Anvin (Intel) --- v2: - Replaced loop with switch statement and GCC case ranges. - Fixed INT_MIN overflow using unsigned int cast instead of s64/long long. - Removed outdated comment regarding mobile submission. arch/x86/lib/misc.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/arch/x86/lib/misc.c b/arch/x86/lib/misc.c index 40b81c338ae5..03ba028d5326 100644 --- a/arch/x86/lib/misc.c +++ b/arch/x86/lib/misc.c @@ -3,22 +3,37 @@ =20 /* * Count the digits of @val including a possible sign. - * - * (Typed on and submitted from hpa's mobile phone.) */ int num_digits(int val) { - long long m =3D 10; - int d =3D 1; + unsigned int v =3D val; + int d =3D 0; =20 if (val < 0) { - d++; - val =3D -val; + d =3D 1; + v =3D -v; } =20 - while (val >=3D m) { - m *=3D 10; - d++; + switch (v) { + case 0 ... 9: + return d + 1; + case 10 ... 99: + return d + 2; + case 100 ... 999: + return d + 3; + case 1000 ... 9999: + return d + 4; + case 10000 ... 99999: + return d + 5; + case 100000 ... 999999: + return d + 6; + case 1000000 ... 9999999: + return d + 7; + case 10000000 ... 99999999: + return d + 8; + case 100000000 ... 999999999: + return d + 9; + default: + return d + 10; } - return d; } --=20 2.43.0