x86/lib: Optimize num_digits() and fix INT_MIN overflow
PATCH v2 1/1 x86/lib: Optimize num_digits() and fix INT_MIN overflow
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.
---
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(-)
arch/x86/lib/misc.c +25 -10
@@ -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; }
Signature · 1 line
2.43.0
Quoted 64 lines · 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 isINT_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_MINcase without relying on 64-bit types or undefined signed overflow.Removed the outdated comment.
Signed-off-by David Desobry <[email protected]>---
v2:
- Replaced loop with switch statement and GCC case ranges.
- FixedINT_MINoverflow 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(-)arch/x86/lib/misc.c +18
-9@@ -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:
arch/x86/lib/misc.c line 39
Quoted 10 lines
arch/x86/lib/misc.c +7
-1+return d +7 ; + case 10000000 ... 99999999: +return d +8 ; + case 100000000 ... 999999999: +return d +9 ; +default : +return d +10 ; } -return d; }
Looks good to me
Quoted 36 lines · On Tue, 20 Jan 2026 18:47:48 +0100 David Desobry <[email protected]>…
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 isINT_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_MINcase without relying on 64-bit types or undefined signed overflow.Removed the outdated comment.
Signed-off-by David Desobry <[email protected]>---
v2:
- Replaced loop with switch statement and GCC case ranges.
- FixedINT_MINoverflow 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(-)arch/x86/lib/misc.c +0
-3@@ -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 ;
arch/x86/lib/misc.c line 15
Quoted 10 lines
arch/x86/lib/misc.c +4
-3-int d =1 ; +unsigned int v = val; +int d =0 ;if (val <0 ) { - d++; - val = -val; + d =1 ; + v = -v; }
Maybe better to write as:
if (val < 0) {
d = 1;
v = -val;
} else {
d = 0;
v = val;
}The compiler will only generate one jump.
Quoted 15 lines
arch/x86/lib/misc.c +11
-3-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 ;
arch/x86/lib/misc.c line 37
Quoted 10 lines
arch/x86/lib/misc.c +10
-0+ 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 ;
clang generates something really horrid for that.
Either:
if (v <= 9) return d + 1;
if (v <= 99) return d + 2;
if (v <= 999) return d + 3;
if (v <= 9999) return d + 4;
if (v <= 99999) return d + 5;
if (v <= 999999) return d + 6;
if (v <= 9999999) return d + 7;
if (v <= 99999999) return d + 8;
if (v <= 999999999) return d + 9;
return d + 10;
or:
if ((++d && v > 9) &&
(++d && v > 99) &&
(++d && v > 999) &&
(++d && v > 9999) &&
(++d && v > 99999) &&
(++d && v > 999999) &&
(++d && v > 9999999) &&
(++d && v > 99999999) &&
(++d && v > 999999999))
d++;
return d;generate better code.
In particular it is almost certainly best to only have one taken branch. Dumping in a load of unlikely() might help that as well. (Neither compiler does the ++d inline, the add is done before the return.)
David
Quoted 3 lines
arch/x86/lib/misc.c +0
-1} -return d; }
PATCH v3 1/1 x86/lib: Optimize num_digits() and fix INT_MIN overflow
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 linear chain of comparisons. This allows the compiler to generate optimized branch sequences, providing better performance than the original loop and more efficient assembly than a switch statement on compilers like Clang. By using an internal unsigned int to handle the magnitude, we safely handle the INT_MIN case without relying on 64-bit types or triggering undefined signed overflow.
Removed the outdated comment.
---
v3:
- Replaced switch statement with a linear if-chain for better compiler optimization.
- Simplified sign handling logic to a single if/else block.
- (v2) Fixed INT_MIN overflow using unsigned magnitude and removed outdated comment.
arch/x86/lib/misc.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-)
arch/x86/lib/misc.c +27 -11
@@ -3,22 +3,38 @@/* * 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; +int d;if (val <0 ) { - d++; - val = -val; + d =1 ; + v = -val; + }else { + d =0 ; + v = val; } -while (val >= m) { - m *=10 ; - d++; - } -return d; +if (v <=9 ) +return d +1 ; +if (v <=99 ) +return d +2 ; +if (v <=999 ) +return d +3 ; +if (v <=9999 ) +return d +4 ; +if (v <=99999 ) +return d +5 ; +if (v <=999999 ) +return d +6 ; +if (v <=9999999 ) +return d +7 ; +if (v <=99999999 ) +return d +8 ; +if (v <=999999999 ) +return d +9 ; + +return d +10 ; }
Signature · 1 line
2.43.0
Quoted 10 lines · David Laight
Maybe better to write as:
if (val < 0) { d = 1; v = -val; } else { d = 0; v = val; }The compiler will only generate one jump.
Agreed, I've adopted this in v3.
Quoted 52 lines
Quoted 16 lines
arch/x86/lib/misc.c +11
-3-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 ;arch/x86/lib/misc.c line 37
Quoted 10 lines
arch/x86/lib/misc.c +10
-0+ 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 ;clang generates something really horrid for that.
Either:
if (v <= 9) return d + 1; if (v <= 99) return d + 2; if (v <= 999) return d + 3; if (v <= 9999) return d + 4; if (v <= 99999) return d + 5; if (v <= 999999) return d + 6; if (v <= 9999999) return d + 7; if (v <= 99999999) return d + 8; if (v <= 999999999) return d + 9; return d + 10; or: if ((++d && v > 9) && (++d && v > 99) && (++d && v > 999) && (++d && v > 9999) && (++d && v > 99999) && (++d && v > 999999) && (++d && v > 9999999) && (++d && v > 99999999) && (++d && v > 999999999)) d++; return d;
Quoted 4 lines
generate better code.
In particular it is almost certainly best to only have one taken branch. Dumping in a load ofunlikely()might help that as well. (Neither compiler does the ++d inline, the add is done before the return.)
Good catch. I have replaced the switch statement with a linear if-chain in v3 to ensure better code generation for both GCC and Clang.
David
H. Peter Anvin
reviewerQuoted 4 lines · David Desobry
Good catch. I have replaced the switch statement with a linear if-chain in v3 to ensure better code generation for both GCC and Clang.
I think a bigger deal is just to change it to unsigned.
Now, for really silly optimization:
int num_digits(unsigned int x)
{
int n = 0;
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));
return n;
}No branches at all!
-hpa
Quoted 26 lines · H. Peter Anvin
Quoted 4 lines · David Desobry
Good catch. I have replaced the switch statement with a linear if-chain in v3 to ensure better code generation for both GCC and Clang.
I think a bigger deal is just to change it to unsigned.
Now, for really silly optimization:
int
num_digits(unsigned int x){ int n = 0; 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)); return n; }
Quoted 4 lines
No branches at all!
-hpa
Actually, the V3 change:
if (val < 0) {
- d++;
- val = -val;
+ d = 1;
+ v = -val;
+ } else {
+ d = 0;
+ v = val;
}
reintroduced the undefined behavior for val == INT_MIN.
So this V3 version is incorrect.
I'm not familiar enough with the rest of the codebase to know if changing the function signature to unsigned int is correct here.
H. Peter Anvin
reviewerQuoted 15 lines · David Desobry
Actually, the V3 change:
if (val < 0) {
- d++;
- val = -val;
+ d = 1;
+ v = -val;
+ } else {
+ d = 0;
+ v = val;
}
reintroduced the undefined behavior for val ==INT_MIN.
So this V3 version is incorrect.
I'm not familiar enough with the rest of the codebase to know if changing the function signature to unsigned int is correct here.
It is only ever called with a cpu number, a node number, or a CPU count. Never negative.
-hpa
PATCH v4 1/1 x86/lib: Optimize num_digits() and fix INT_MIN overflow
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.
As this function is only used for unsigned magnitudes in the kernel code, convert the interface to take an unsigned int. This naturally resolves the INT_MIN overflow issue.
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 the function comment to reflect that signs are no longer handled.
---
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/lib/misc.c | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-)
arch/x86/include/asm/misc.h +1 -1
@@ -2,6 +2,6 @@#ifndef _ASM_X86_MISC_H#define _ASM_X86_MISC_H -int num_digits (int val); +int num_digits (unsigned int val);#endif /* _ASM_X86_MISC_H */
arch/x86/lib/misc.c +13 -15
@@ -2,23 +2,21 @@#include <asm/misc.h> /* -* 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 (unsigned int x) { -long long m =10 ; -int d =1 ; +int n =0 ; -if (val <0 ) { - d++; - val = -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 )); -while (val >= m) { - m *=10 ; - d++; - } -return d; +return n; }
Signature · 1 line
2.43.0
Quoted 4 lines · On Wed, 21 Jan 2026 10:39:11 +0100 David Desobry <[email protected]>…
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 negatingINT_MIN.
Quoted 8 lines
As this function is only used for unsigned magnitudes in the kernel code, convert the interface to take an unsigned int. This naturally resolves the
INT_MINoverflow issue.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.
Don't do this version, it isn't worth the effort.
David
Quoted 67 lines
Update the function comment to reflect that signs are no longer handled.
Signed-off-by David Desobry <[email protected]>---
v4:
- Switched to branchless inline assembly.
- Changed function signature to unsigned int.
- Updated prototype inarch/x86/include/asm/misc.h.arch/x86/include/asm/misc.h | 2 +- arch/x86/lib/misc.c | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-)arch/x86/include/asm/misc.h +1
-1@@ -2,6 +2,6 @@#ifndef _ASM_X86_MISC_H#define _ASM_X86_MISC_H -int num_digits (int val); +int num_digits (unsigned int val);#endif /* _ASM_X86_MISC_H */ arch/x86/lib/misc.c +13
-15@@ -2,23 +2,21 @@#include <asm/misc.h> /* -* 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 (unsigned int x) { -long long m =10 ; -int d =1 ; +int n =0 ; -if (val <0 ) { - d++; - val = -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 )); -while (val >= m) { - m *=10 ; - d++; - } -return d; +return n; }
H. Peter Anvin
reviewerQuoted 80 lines · David Laight
Quoted 4 lines · On Wed, 21 Jan 2026 10:39:11 +0100 David Desobry <[email protected]>…
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 negatingINT_MIN.Quoted 8 lines
As this function is only used for unsigned magnitudes in the kernel code, convert the interface to take an unsigned int. This naturally resolves the
INT_MINoverflow issue.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.
Don't do this version, it isn't worth the effort.
David
Quoted 60 lines
Update the function comment to reflect that signs are no longer handled.
Signed-off-by David Desobry <[email protected]>---
v4:
- Switched to branchless inline assembly.
- Changed function signature to unsigned int.
- Updated prototype inarch/x86/include/asm/misc.h.arch/x86/include/asm/misc.h | 2 +- arch/x86/lib/misc.c | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-)arch/x86/include/asm/misc.h +1
-1@@ -2,6 +2,6 @@#ifndef _ASM_X86_MISC_H#define _ASM_X86_MISC_H -int num_digits (int val); +int num_digits (unsigned int val);#endif /* _ASM_X86_MISC_H */ arch/x86/lib/misc.c +12
-10@@ -2,23 +2,21 @@#include <asm/misc.h> /* -* 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 (unsigned int x) { -long long m =10 ; -int d =1 ; +int n =0 ; -if (val <0 ) { - d++; - val = -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 ));
Quoted 8 lines
arch/x86/lib/misc.c line 22
Quoted 7 lines
arch/x86/lib/misc.c +1
-5-while (val >= m) { - m *=10 ; - d++; - } -return d; +return n; }
No, please, it was a joke.
Quoted 1 line · On Wed, 21 Jan 2026 01:04:39 +0100 David Desobry <[email protected]>…
On 1/21/26 00:49, H. Peter Anvin wrote:
..
Quoted 10 lines
Actually, the V3 change:
if (val < 0) {arch/x86/lib/misc.c +5
-2- d++; - val = -val; + d =1 ; + v = -val; + }else { + d =0 ; + v = val; }
Quoted 2 lines
reintroduced the undefined behavior for val ==
INT_MIN. So this V3 version is incorrect.
Change to:
v = -(val + 1); v++;
The compiler notices the two '+ 1' cancel each other out.
David
Quoted 2 lines
I'm not familiar enough with the rest of the codebase to know if changing the function signature to unsigned int is correct here.
In theory you'd need to change the name and all the callers.
David
Quoted 8 lines · David Laight
Quoted 2 lines
I'm not familiar enough with the rest of the codebase to know if changing the function signature to unsigned int is correct here.
In theory you'd need to change the name and all the callers.
David
Agreed. It turns out there were very few callers.
I have just submitted V5, which renames the function to num_digits_u32(), switches the interface to unsigned int, and updates all callers accordingly.
David
PATCH v5 1/1 x86/lib: Rename num_digits() to num_digits_u32() and optimize
In reply to David LaightThe 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.
---
v5:
- Changed name to num_digits_u32() and updated all callers to avoid 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(-)
arch/x86/include/asm/misc.h +1 -1
@@ -2,6 +2,6 @@#ifndef _ASM_X86_MISC_H#define _ASM_X86_MISC_H -int num_digits (int val); +int num_digits_u32 (unsigned int x);#endif /* _ASM_X86_MISC_H */
arch/x86/kernel/smpboot.c +4 -4
@@ -849,10 +849,10 @@ static void announce_cpu(int cpu, int apicid)int node =early_cpu_to_node (cpu);if (!width) - width =num_digits (num_possible_cpus ()) +1 ;/* + '#' sign */ + width =num_digits_u32 (num_possible_cpus ()) +1 ;/* + '#' sign */ if (!node_width) - node_width =num_digits (num_possible_nodes ()) +1 ;/* + '#' */ + node_width =num_digits_u32 (num_possible_nodes ()) +1 ;/* + '#' */ if (system_state <SYSTEM_RUNNING ) {if (first)
@@ -864,7 +864,7 @@ static void announce_cpu(int cpu, int apicid) current_node = node;printk (KERN_INFO ".... node %*s#%d, CPUs: " , - node_width -num_digits (node)," " , node); + node_width -num_digits_u32 (node)," " , node); }/* Add padding for the BSP */
@@ -872,7 +872,7 @@ static void announce_cpu(int cpu, int apicid)pr_cont ("%*s" , width +1 ," " ); first =0 ; -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);
arch/x86/lib/misc.c +13 -15
@@ -2,23 +2,21 @@#include <asm/misc.h> /* -* 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 =10 ; -int d =1 ; +int n =0 ; -if (val <0 ) { - d++; - val = -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 )); -while (val >= m) { - m *=10 ; - d++; - } -return d; +return n; }
Signature · 1 line
2.43.0
Quoted 18 lines · H. Peter Anvin
Now, for really silly optimization:
int
num_digits(unsigned int x){ int n = 0; 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)); return n; }
Quoted 1 line
No branches at all!
I guess you chose to use SBB rather than somewhat less mind-twisting ADC
for the entertainment of the reader?
Anyway branchless code can be produced from C code as well, e.g.:
int num_digits(unsigned int x)
{
return (1 + (x > 9) + (x > 99) + (x > 999) + (x > 9999) +
(x > 99999) + (x > 999999) + (x > 9999999) +
(x > 99999999) + (x > 999999999));
}although GCC at least as at version 11 I have here uses SETA rather than ADC/SBB (it doesn't care if you write (x > 9) or (x >= 10), etc.) emitting a longer and likely slower sequence even at -Os. And likewise the POWER backend doesn't take advantage of the carry flag and prefers calculations involving shifting the sign bit into bit 0. Obviously no one must have thought of adding the right transformation to the optimiser, which might be an interesting challenge to someone.
Maciej
H. Peter Anvin
reviewerQuoted 42 lines · Maciej W. Rozycki
Quoted 18 lines · H. Peter Anvin
Now, for really silly optimization:
int
num_digits(unsigned int x){ int n = 0; 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)); return n; }Quoted 1 line
No branches at all!
I guess you chose to use SBB rather than somewhat less mind-twisting ADC for the entertainment of the reader?
Anyway branchless code can be produced from C code as well, e.g.:
int
num_digits(unsigned int x){ return (1 + (x > 9) + (x > 99) + (x > 999) + (x > 9999) + (x > 99999) + (x > 999999) + (x > 9999999) + (x > 99999999) + (x > 999999999)); }although GCC at least as at version 11 I have here uses SETA rather than ADC/SBB (it doesn't care if you write (x > 9) or (x >= 10), etc.) emitting a longer and likely slower sequence even at -Os. And likewise the POWER backend doesn't take advantage of the carry flag and prefers calculations involving shifting the sign bit into bit 0. Obviously no one must have thought of adding the right transformation to the optimiser, which might be an interesting challenge to someone.
Quoted 1 line
Maciej
No, I use it because SBB subtracts CF, whereas ADC adds CF.
Quoted 2 lines · H. Peter Anvin
Quoted 2 lines
I guess you chose to use SBB rather than somewhat less mind-twisting ADC for the entertainment of the reader?
[...]
Quoted 2 lines
No, I use it because SBB subtracts CF, whereas ADC adds CF.
Ah, right, on x86 an immediate needs to be the subtrahend, so you don't
have the freedom to swap the operands of CMP, which would let you use CF either way (as say on VAX), which is what I had in mind. I stand corrected then, it's the only way.
Maciej