lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260109235430.69ccfff2@pumpkin>
Date: Fri, 9 Jan 2026 23:54:30 +0000
From: David Laight <david.laight.linux@...il.com>
To: "Arnd Bergmann" <arnd@...db.de>
Cc: "Petr Tesarik" <ptesarik@...e.com>, "Yury Norov" <yury.norov@...il.com>,
 "Rasmus Villemoes" <linux@...musvillemoes.dk>, "Richard Henderson"
 <richard.henderson@...aro.org>, "Matt Turner" <mattst88@...il.com>, "Magnus
 Lindholm" <linmag7@...il.com>, "Vineet Gupta" <vgupta@...nel.org>, "Geert
 Uytterhoeven" <geert@...ux-m68k.org>, "Maciej W. Rozycki"
 <macro@...am.me.uk>, "Thomas Bogendoerfer" <tsbogend@...ha.franken.de>,
 "Madhavan Srinivasan" <maddy@...ux.ibm.com>, "Michael Ellerman"
 <mpe@...erman.id.au>, "Heiko Carstens" <hca@...ux.ibm.com>, "Vasily Gorbik"
 <gor@...ux.ibm.com>, "Alexander Gordeev" <agordeev@...ux.ibm.com>, "Chris
 Zankel" <chris@...kel.net>, "Max Filippov" <jcmvbkbc@...il.com>, "Patrik
 Jakobsson" <patrik.r.jakobsson@...il.com>, "Maarten Lankhorst"
 <maarten.lankhorst@...ux.intel.com>, "Maxime Ripard" <mripard@...nel.org>,
 "Thomas Zimmermann" <tzimmermann@...e.de>, "Dave Airlie"
 <airlied@...il.com>, "Simona Vetter" <simona@...ll.ch>, "Robin Murphy"
 <robin.murphy@....com>, "Joerg Roedel" <joro@...tes.org>, "Will Deacon"
 <will@...nel.org>, "Jakub Kicinski" <kuba@...nel.org>, "Andrew Lunn"
 <andrew+netdev@...n.ch>, "David S . Miller" <davem@...emloft.net>, "Eric
 Dumazet" <edumazet@...gle.com>, "Paolo Abeni" <pabeni@...hat.com>, "Oliver
 Neukum" <oliver@...kum.org>, "Kuan-Wei Chiu" <visitorckw@...il.com>,
 "Andrew Morton" <akpm@...ux-foundation.org>, "Marcel Holtmann"
 <marcel@...tmann.org>, "Johan Hedberg" <johan.hedberg@...il.com>, "Luiz
 Augusto von Dentz" <luiz.dentz@...il.com>, "Pablo Neira Ayuso"
 <pablo@...filter.org>, "Florian Westphal" <fw@...len.de>,
 linux-kernel@...r.kernel.org
Subject: Re: [RFC PATCH 1/2] bits: introduce ffs_val()

On Fri, 09 Jan 2026 18:01:02 +0100
"Arnd Bergmann" <arnd@...db.de> wrote:

> On Fri, Jan 9, 2026, at 17:37, Petr Tesarik wrote:
> 
> > + * Returns:
> > + * least significant non-zero bit, 0 if all bits are zero
> > + */
> > +#define ffs_val(x)			\
> > +({					\
> > +	const typeof(x) val__ = (x);	\
> > +	val__ & -val__;			\
> > +})  
> 
> This looks good to me, but I'd suggest using 'const auto val__'
> instead of typeof(), to reduce expanding complex arguments twice.

It is more usual to just use a single _ prefix and the same name.
I wouldn't bother with 'const' either, maybe:
#define ffs_val(val) ({  \
	auto _val = val; \
	_val & -_val;    \
})

However it isn't necessarily better than using __ffs().
FIELD_PREP(mask, val) (for non-constant mask) can be (mask & -mask) * val
or val << __ffs(mask).
So the 'ffs' version is fewer instructions (assuming non-zero mask).
The timings for bsf/bsr are similar to those for imul on intel cpu,
mul wins on zen3 and bsf/bsr on zen4.
On balance the __ffs() version is actually likely to be faster.

Other architectures may fair better or worse.
Clearly you don't want to use __ffs() unless it is a single instruction.

Of course, for FIELD_GET(reg, mask) you'd need reg/(mask & -mask)
and the cost of the integer division is far more than __ffs().
(But that will give you a compile-time error if mask is a constant zero
and the compiler will convert the divide to a shift.)

And for 64bit calculations on 32bit 'all bets are off'.
Even the shift left might be problematic.

	David

> 
>     Arnd
> 


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ