Thread (40 messages) 40 messages, 11 authors, 2021-11-24

Re: [PATCH 01/17] bitfield: Add non-constant field_{prep,get}() helpers

From: Johannes Berg <johannes@sipsolutions.net>
Date: 2021-11-23 16:22:44
Also in: alsa-devel, linux-aspeed, linux-clk, linux-gpio, linux-iio, linux-media, linux-mmc, linux-omap, linux-renesas-soc, linux-wireless, lkml, netdev

On Tue, 2021-11-23 at 09:30 +0100, Geert Uytterhoeven wrote:
quoted
We have the upper-case (constant) versions, and already
{u32,...}_get_bits()/etc.
These don't work for non-const masks.
Obviously, I know that. Still, just saying.

I'm actually in the opposite camp to you I guess - I much prefer the
typed versions (u32_get_bits() and friends) over the FIELD_GET() macros
that are more magic.

Mostly though that's because the typed ones also have le32_/be32_/...
variants, which are tremendously useful, and so I prefer to use them all
across. In fact, I have considered in the past to just remove the upper-
case macros entirely but ... no time I guess.
quoted
Also, you're using __ffs(), which doesn't work for 64-bit on 32-bit
architectures (afaict), so that seems a bit awkward.
That's a valid comment. Can be fixed by using a wrapper macro
that checks if typeof(mask) == u64, and uses an __ffs64() version when
needed.
You can't really do a typeof()==something, but you can check the size,
so yeah, that could be done.
quoted
Maybe we can make {u32,...}_get_bits() be doing compile-time only checks
if it is indeed a constant? The __field_overflow() usage is already only
done if __builtin_constant_p(v), so I guess we can do the same with
__bad_mask()?
Are all compilers smart enough to replace the division by
field_multiplier(field) by a shift?
In the constant case they are, but you'd have to replace
field_multiplier() with the __ffs(), including the size check discussed
above. Then it's no longer a constant, and then I'm not so sure it would
actually be able to translate it, even if it's "1<<__ffs64(...)". I
guess you can check, or just change it to not use the division and
multiplication, but shifts/masks instead manually?

IOW - I would much prefer to make the type_get_bits() and friends work
for non-constant masks.

In fact, you have e.g. code in drivers/usb/chipidea/udc.c that does
things like cpu_to_le32(mul << __ffs(...)) - though in those cases it's
actually constant today, so you could already write it as
le32_encode_bits(...).

johannes
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help