From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from 2.mo576.mail-out.ovh.net (2.mo576.mail-out.ovh.net [178.33.251.80]) (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 13A1C306491 for ; Wed, 21 Jan 2026 01:20:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.33.251.80 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768958432; cv=none; b=uyFQaBGoOyUnEOVJAi3hc3ORSulMnexH4Y/OWIdT59e+JaMnN0+bgLbBJlqTrFipJlmjsO3OYXM77r2If07XUpOZVP7i3sTvZ32ElzkLIAOM5o0aNc9kemfLRcWSpqvJ4hJgztlnX4ch050s3AyhpZYBpPKfGj9qN2cwhe5Rhc8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768958432; c=relaxed/simple; bh=Bbgk+WdgGjHfM+H0Z+vCS8/o/lKv7mzVFW38Tn6oLE4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=RmLkNqdiIK9zv/kT5i+GpCWuGpfN1NV/wRm5qKw5JDKvhL1B70cMObakT1VA6Utc3E41/a85B3DZVFOnwF5BXovD/KYFMps2RO6EMKlfm/BRtWBEmquD+EsiX1haAixAnol/GhZ9Y/cBpPLt6DRvi/bxR+Lvys+/nBUB9xLejlc= 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=Z0Xc+JLi; arc=none smtp.client-ip=178.33.251.80 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="Z0Xc+JLi" Received: from director2.ghost.mail-out.ovh.net (unknown [10.110.37.5]) by mo576.mail-out.ovh.net (Postfix) with ESMTP id 4dwkvs3bJMz5x3w for ; Wed, 21 Jan 2026 00:04:41 +0000 (UTC) Received: from ghost-submission-7d8d68f679-zfx2s (unknown [10.110.168.221]) by director2.ghost.mail-out.ovh.net (Postfix) with ESMTPS id C2F11C06B6; Wed, 21 Jan 2026 00:04:40 +0000 (UTC) Received: from formalgen.com ([37.59.142.109]) by ghost-submission-7d8d68f679-zfx2s with ESMTPSA id Ikn8GhgYcGmNdwAArOirtg (envelope-from ); Wed, 21 Jan 2026 00:04:40 +0000 Authentication-Results:garm.ovh; auth=pass (GARM-109S003adaef9c0-3e85-4591-9b37-083a2ab7c127, 831801AFC7761530D51FB11603EF4B8E66579AB0) smtp.auth=david.desobry@formalgen.com X-OVh-ClientIp:90.91.42.105 Message-ID: Date: Wed, 21 Jan 2026 01:04:39 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] x86/lib: Optimize num_digits() and fix INT_MIN overflow To: "H. Peter Anvin" , David Laight Cc: tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, linux-kernel@vger.kernel.org References: <20260120174748.302078-1-david.desobry@formalgen.com> <20260120214612.73b83cbe@pumpkin> <8923623d-1a23-483d-add7-2fd03a3a2c3d@zytor.com> Content-Language: en-US From: David Desobry In-Reply-To: <8923623d-1a23-483d-add7-2fd03a3a2c3d@zytor.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Ovh-Tracer-Id: 1736419133890677723 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: 0 X-VR-SPAMCAUSE: dmFkZTF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ceRsvDfXmNC2nsMjfV/WtkUVYBM NcLRbgXuT4T12DJzSNkK8klk1bfpAp+P0HmeTALw/WYEQ== 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.