mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jakub Kicinski <kubakici@wp.pl>
To: Al Viro <viro@ZenIV.linux.org.uk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC][PATCH] new byteorder primitives - ..._{replace,get}_bits()
Date: Tue, 12 Dec 2017 12:04:09 -0800	[thread overview]
Message-ID: <20171212120409.64b6362e@cakuba.netronome.com> (raw)
In-Reply-To: <20171212194532.GA7062@ZenIV.linux.org.uk>

On Tue, 12 Dec 2017 19:45:32 +0000, Al Viro wrote:
> On Tue, Dec 12, 2017 at 06:20:02AM +0000, Al Viro wrote:
> 
> > Umm...  What's wrong with
> > 
> > #define FIELD_FOO 0,4
> > #define FIELD_BAR 6,12
> > #define FIELD_BAZ 18,14
> > 
> > A macro can bloody well expand to any sequence of tokens - le32_get_bits(v, FIELD_BAZ)
> > will become le32_get_bits(v, 18, 14) just fine.  What's the problem with that?  
> 
> FWIW, if you want to use the mask, __builtin_ffsll() is not the only way to do
> it - you don't need the shift.  Multiplier would do just as well, and that can
> be had easier.  If mask = (2*a + 1)<<n = ((2*a)<<n) ^ (1<<n), then
> 	mask - 1 = ((2*a) << n) + ((1<<n) - 1) = ((2*n) << n) ^ ((1<<n) - 1)
> 	mask ^ (mask - 1) = (1<<n) + ((1<<n) - 1)
> and
> 	mask & (mask ^ (mask - 1)) = 1<<n.
> 
> IOW, with
> 
> static __always_inline u64 mask_to_multiplier(u64 mask)
> {
> 	return mask & (mask ^ (mask - 1));
> }
> 
> we could do
> 
> static __always_inline __le64 le64_replace_bits(__le64 old, u64 v, u64 mask)
> {
> 	__le64 m = cpu_to_le64(mask);
> 	return (old & ~m) | (cpu_to_le64(v * mask_to_multiplier(mask)) & m);
> }
> 
> static __always_inline u64 le64_get_bits(__le64 v, u64 mask)
> {
> 	return (le64_to_cpu(v) & mask) / mask_to_multiplier(mask);
> }
> 
> etc.  Compiler will turn those into shifts...  I can live with either calling
> conventions.
> 
> Comments?

Very nice!  The compilation-time check if the value can fit in a field
covered by the mask (if they're both known) did help me catch bugs
early a few times over the years, so if it could be preserved we can
maybe even drop the FIELD_* macros and just use this approach?

  reply	other threads:[~2017-12-12 20:04 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20171210045326.GO21978@ZenIV.linux.org.uk>
     [not found] ` <420a198d-61f8-81cf-646d-10446cb41def@synopsys.com>
     [not found]   ` <20171211050520.GV21978@ZenIV.linux.org.uk>
2017-12-11  5:38     ` [RFC][PATCH] apparent big-endian bugs in dwc-xlgmac Al Viro
2017-12-11  6:46       ` Jie Deng
2017-12-11 15:54       ` [RFC][PATCH] new byteorder primitives - ..._{replace,get}_bits() Al Viro
2017-12-12  4:02         ` Jakub Kicinski
2017-12-12  6:20           ` Al Viro
2017-12-12 19:45             ` Al Viro
2017-12-12 20:04               ` Jakub Kicinski [this message]
2017-12-12 23:48                 ` Al Viro
2017-12-12 23:59                   ` Jakub Kicinski
2017-12-13  0:36                     ` Al Viro
2017-12-13  1:04                       ` Jakub Kicinski
2017-12-13  1:30                         ` Al Viro
2017-12-13  1:35                           ` Jakub Kicinski
2017-12-13  1:51                             ` Al Viro
2017-12-13  2:44                               ` Jakub Kicinski
2017-12-13 14:22                                 ` Al Viro
2017-12-13 17:45                                   ` Al Viro
2017-12-15  2:33                                     ` [RFC][PATCH] Add primitives for manipulating bitfields both in host- and fixed-endian Al Viro
2017-12-15  5:07                                       ` Jakub Kicinski
2017-12-15  5:34                                         ` Al Viro
2017-12-15 16:48                                           ` [RFC][PATCH v2] " Al Viro
2017-12-13 19:04                                   ` [RFC][PATCH] new byteorder primitives - ..._{replace,get}_bits() Jakub Kicinski

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=20171212120409.64b6362e@cakuba.netronome.com \
    --to=kubakici@wp.pl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@ZenIV.linux.org.uk \
    /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

Powered by JetHome