From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BADFE3D4122 for ; Wed, 21 Jan 2026 11:36:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768995408; cv=none; b=eEU6/WTiRE+r7WatcekWk1LMwNNJ8TgNG1c50q7ujH8CHe9F6NXsC02V1yWvp5QW4fggSHiBTNGlm16Lsb7YJqNC/9IfLQ08UicYKdMctxfIGfOTwsRsE0yn5D9L8NbjKlsQd5a2ZaQKs1hGsnopUx0UA54PlatcQ9+Vi4k+634= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768995408; c=relaxed/simple; bh=hrVe+OjmLlh/t27iOK2MKLNWT3gxGGblnAgEW03iW+4=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QI6WbPffFrYdMf5Uu1SDlXS/twDkc9nMfksttV2LEs1zAFvQ0ONub/Zz9LHmg/0M2AjhjV1+ADzBd2MzU9oUG8O9+lVGxOFpcBEGEOtzbR34NK0WO08e9CWt04O1KvVMPYZRMN/9w+lCPOsNwBA8rtCqxCC5rXoUVgz+h+AOufQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=du2VS7Ht; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="du2VS7Ht" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-43246af170aso562247f8f.0 for ; Wed, 21 Jan 2026 03:36:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768995405; x=1769600205; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=3ia9p++fXGQzmzCll0KQvQQFYw9cnahFnp7h2GseUBA=; b=du2VS7HtQQR/0txOKuWIpRBSomWO2axd5ulW9+CPr2G0k4QJysQ0pqpTthnSsn0plk RhvvVSNc69IiaVk0eHtScV5qkvxsjYEMj9m8C6+FzrWe571OFt/pVIfZIDesQauNGh+e EDgeeFg75V2Nl2qJYJLnSmYkVa6lsDXOa5Dn732elNpswtRrta3GH+R/IXXjTUwnpyXd pbw+zhe+TyPyyDEHUhLC+v5x+9mz9ax0frgiEh5T2U12MNasRfdL2I3KytpjCfRwijmv +mrZwSY41mWlcTP6/dYgEikNcfrFXvhjyjDrUZCf/ES8W88WVW0ggvqnVrR6EZUTEklu Czrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768995405; x=1769600205; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=3ia9p++fXGQzmzCll0KQvQQFYw9cnahFnp7h2GseUBA=; b=TFQskSLzEOjzFrOo6+PzSMl3s6TKigYrBxTvj6oVnCeOnnl4gOxYvxp18OcRMu/yU5 Oi6g86H5bLZYuOW8iZgQ20cwo/ENNvqIPVK0mdppA2QRNnC7m/UNqYnDgLtcYOjqZWpm kkDxkHbQOOVQcWB1PfCwj14PpMd6/e9b9rJAAh0+p+Ml2fdhDdmOyvcrVT1qdu4fc6RK l4hXChqxA3j5dc7SZy0Pb4nRiurnO5EfSdYtp+dXVJ+2Dbo2GCeMekwuTxg7fB7bzaFt 6dCZMHUJCZCC3nSq4yAorRw6zYIy6wc8Z3/Hx7T90aFnGAUptohw6Cm3LEdFp0y0lqq1 ARJQ== X-Forwarded-Encrypted: i=1; AJvYcCVWWpqMHRENrAw1Eu3Q9A8h8ZkcuPlF1/tiJ0S8kTNlqLy0Nu/giXfPW6U5CK2wFG3So1ZCh8+mlmy0noU=@vger.kernel.org X-Gm-Message-State: AOJu0YwD2bBG4sxipaOQNVvWZ7Yh88/4zTv+coMBG9g9orpxR3DrF8Bz fPxq5rHC4e3DfrAxuNE/MulUGI1KpvnCX+YwQhL8WEBbSyo51RKZ7T9h X-Gm-Gg: AZuq6aJMvRGySxybMzWBfU3HP4zG8q4cARS/o8OT11w4sytnto6iDRpnLFytqimGvSh XPTL8fY3johOoGzVglQAtgVjj2hPGeulSCjpLeyyHWKvZUzMseJpfkUrpF576A/y8Bbbep0uUHD UwupWpnossF7/08TPHEfpvs3hlPFga/npC2fZ7zE70fVmPBSZLHM+FCMbASMM+2OSwS5mPs1Mi8 1lpYOjjzdHzOZr0N7gFFGxSMWUUSf/E19CCxvX/AbWjW03GY8D8ga9AYamho2CPFC8IbvxDw4tG HVD6ZQr7l/5wUnHGBH/uiKnW0BUWFmqGRrcQ+2LewUNFyiV2WUxRe8b/s4lRtUuDEUhTy6lVEAZ xBhWEURONU6w3/zHU7ShIGEZp09z0x560F9HHYb5K/FoiSaAMIbcZPEBEAOC6jJWhVQygEGT1Y+ ZCaGKZDB+MWk8RIlFwmyOENC/GwoYBBOnYto8ALY1H7+jDmODx5c7p X-Received: by 2002:a05:6000:22c1:b0:3ea:6680:8fb9 with SMTP id ffacd0b85a97d-4356953f8d7mr26723246f8f.3.1768995404938; Wed, 21 Jan 2026 03:36:44 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43569926ffcsm35231367f8f.18.2026.01.21.03.36.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jan 2026 03:36:44 -0800 (PST) Date: Wed, 21 Jan 2026 11:36:43 +0000 From: David Laight To: David Desobry Cc: tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4] x86/lib: Optimize num_digits() and fix INT_MIN overflow Message-ID: <20260121113643.22718a3d@pumpkin> In-Reply-To: <20260121093911.27042-1-david.desobry@formalgen.com> References: <02817c9a-e512-427f-bd64-b1420132adf6@zytor.com> <20260121093911.27042-1-david.desobry@formalgen.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 21 Jan 2026 10:39:11 +0100 David Desobry wrote: > 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. Don't do this version, it isn't worth the effort. David > > Update the function comment to reflect that signs are no longer handled. > > Signed-off-by: David Desobry > --- > 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(-) > > diff --git a/arch/x86/include/asm/misc.h b/arch/x86/include/asm/misc.h > index bb049cca3729..48b6bd7c08b9 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 > > -int num_digits(int val); > +int num_digits(unsigned int val); > > #endif /* _ASM_X86_MISC_H */ > diff --git a/arch/x86/lib/misc.c b/arch/x86/lib/misc.c > index 40b81c338ae5..9623795b059f 100644 > --- a/arch/x86/lib/misc.c > +++ b/arch/x86/lib/misc.c > @@ -2,23 +2,21 @@ > #include > > /* > - * 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; > }