public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Al Viro <viro@ZenIV.linux.org.uk>
To: Jie Deng <Jie.Deng1@synopsys.com>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [RFC][PATCH] apparent big-endian bugs in dwc-xlgmac
Date: Mon, 11 Dec 2017 05:38:03 +0000	[thread overview]
Message-ID: <20171211053803.GW21978@ZenIV.linux.org.uk> (raw)
In-Reply-To: <20171211050520.GV21978@ZenIV.linux.org.uk>

On Mon, Dec 11, 2017 at 05:05:20AM +0000, Al Viro wrote:

> What for?  Sure, this variant will work, but why bother with
> 	a = le32_to_cpu(b);
> 	(cpu_to_le32(a) & ....) | ....
> and how is that better than
> 	(b & ...) | ...
> 
> IDGI...  Mind you, I'm not sure if there is any point keeping _var in that thing,
> seeing that we use var only once - might be better off with
> 	((var) & ~cpu_to_le32(GENMASK(_pos + _len - 1, _pos))) | 	\
> 		cpu_to_le32(_val);					\

FWIW, seeing how many drivers end up open-coding that, I'm rather tempted to
add to linux/bitops.h or linux/byteorder/generic.h the following:

static inline __le16 le16_replace_bits(__le16 old, u16 v, int bit, int size)
{
	__le16 mask = cpu_to_le16(GENMASK(bit + size - 1, bit));
	return (old & ~mask) | (cpu_to_le16(v << bit) & mask);
}

static inline __le32 le32_replace_bits(__le32 old, u32 v, int bit, int size)
{
	__le32 mask = cpu_to_le32(GENMASK(bit + size - 1, bit));
	return (old & ~mask) | (cpu_to_le32(v << bit) & mask);
}

static inline __le64 le64_replace_bits(__le64 old, u64 v, int bit, int size)
{
	__le64 mask = cpu_to_le64(GENMASK_ULL(bit + size - 1, bit));
	return (old & ~mask) | (cpu_to_le64(v << bit) & mask);
}

static inline __be16 be16_replace_bits(__be16 old, u16 v, int bit, int size)
{
	__be16 mask = cpu_to_be16(GENMASK(bit + size - 1, bit));
	return (old & ~mask) | (cpu_to_be16(v << bit) & mask);
}

static inline __be32 be32_replace_bits(__be32 old, u32 v, int bit, int size)
{
	__be32 mask = cpu_to_be32(GENMASK(bit + size - 1, bit));
	return (old & ~mask) | (cpu_to_be32(v << bit) & mask);
}

static inline __be64 be64_replace_bits(__be64 old, u64 v, int bit, int size)
{
	__be64 mask = cpu_to_be64(GENMASK_ULL(bit + size - 1, bit));
	return (old & ~mask) | (cpu_to_be64(v << bit) & mask);
}

static inline u16 le16_get_bits(__le16 v, int bit, int size)
{
	return (le16_to_cpu(v) >> bit) & (BIT(size) - 1);
}

static inline u32 le32_get_bits(__le32 v, int bit, int size)
{
	return (le32_to_cpu(v) >> bit) & (BIT(size) - 1);
}

static inline u64 le64_get_bits(__le64 v, int bit, int size)
{
	return (le64_to_cpu(v) >> bit) & (BIT_ULL(size) - 1);
}

static inline u16 be16_get_bits(__be16 v, int bit, int size)
{
	return (be16_to_cpu(v) >> bit) & (BIT(size) - 1);
}

static inline u32 be32_get_bits(__be32 v, int bit, int size)
{
	return (be32_to_cpu(v) >> bit) & (BIT(size) - 1);
}

static inline u64 be64_get_bits(__be64 v, int bit, int size)
{
	return (be64_to_cpu(v) >> bit) & (BIT_ULL(size) - 1);
}

and let drivers use those...

       reply	other threads:[~2017-12-11  5:38 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     ` Al Viro [this message]
2017-12-11  6:46       ` [RFC][PATCH] apparent big-endian bugs in dwc-xlgmac 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
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=20171211053803.GW21978@ZenIV.linux.org.uk \
    --to=viro@zeniv.linux.org.uk \
    --cc=Jie.Deng1@synopsys.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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