public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] x86/lib: Optimize num_digits() and fix INT_MIN overflow
@ 2026-01-20 17:47 David Desobry
  2026-01-20 18:03 ` H. Peter Anvin
  2026-01-20 21:46 ` David Laight
  0 siblings, 2 replies; 17+ messages in thread
From: David Desobry @ 2026-01-20 17:47 UTC (permalink / raw)
  To: tglx, mingo, bp, dave.hansen; +Cc: x86, hpa, linux-kernel, David Desobry

The current implementation of num_digits() uses a loop with repeated
multiplication, which is inefficient. Furthermore, the negation of
the input value "val = -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 <david.desobry@formalgen.com>
---
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 @@
 
 /*
  * 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 = 10;
-	int d = 1;
+	unsigned int v = val;
+	int d = 0;
 
 	if (val < 0) {
-		d++;
-		val = -val;
+		d = 1;
+		v = -v;
 	}
 
-	while (val >= m) {
-		m *= 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;
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2026-01-23 10:55 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-20 17:47 [PATCH v2] x86/lib: Optimize num_digits() and fix INT_MIN overflow David Desobry
2026-01-20 18:03 ` H. Peter Anvin
2026-01-20 21:46 ` David Laight
2026-01-20 23:19   ` [PATCH v3] " David Desobry
2026-01-20 23:32   ` [PATCH v2] " David Desobry
2026-01-20 23:49     ` H. Peter Anvin
2026-01-21  0:04       ` David Desobry
2026-01-21  0:17         ` H. Peter Anvin
2026-01-21  9:39           ` [PATCH v4] " David Desobry
2026-01-21 11:36             ` David Laight
2026-01-21 15:46               ` H. Peter Anvin
2026-01-21  9:54         ` [PATCH v2] " David Laight
2026-01-21 10:32           ` [PATCH v5] x86/lib: Rename num_digits() to num_digits_u32() and optimize David Desobry
2026-01-21 10:40           ` [PATCH v2] x86/lib: Optimize num_digits() and fix INT_MIN overflow David Desobry
2026-01-21 11:51       ` Maciej W. Rozycki
2026-01-23  7:06         ` H. Peter Anvin
2026-01-23 10:55           ` Maciej W. Rozycki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox