mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Desobry <david.desobry@formalgen.com>
To: "H. Peter Anvin" <hpa@zytor.com>,
	David Laight <david.laight.linux@gmail.com>
Cc: tglx@kernel.org, mingo@redhat.com, bp@alien8.de,
	dave.hansen@linux.intel.com, x86@kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] x86/lib: Optimize num_digits() and fix INT_MIN overflow
Date: Wed, 21 Jan 2026 01:04:39 +0100	[thread overview]
Message-ID: <ef852c16-3736-4bd5-bc65-69211b05506e@formalgen.com> (raw)
In-Reply-To: <8923623d-1a23-483d-add7-2fd03a3a2c3d@zytor.com>

On 1/21/26 00:49, H. Peter Anvin wrote:
> On 2026-01-20 15:32, David Desobry wrote:
>>
>> 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
> 

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.

  reply	other threads:[~2026-01-21  1:20 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-20 17:47 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ef852c16-3736-4bd5-bc65-69211b05506e@formalgen.com \
    --to=david.desobry@formalgen.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=david.laight.linux@gmail.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®