[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20221207231728.2331166-3-jesse.brandeburg@intel.com>
Date: Wed, 7 Dec 2022 15:17:28 -0800
From: Jesse Brandeburg <jesse.brandeburg@...el.com>
To: davem@...emloft.net, kuba@...nel.org, pabeni@...hat.com
Cc: netdev@...r.kernel.org, mkubecek@...e.cz,
Jesse Brandeburg <jesse.brandeburg@...el.com>
Subject: [PATCH net-next v1 2/2] ethtool: refactor bit-shifts
While coding up the BIT() conversions for ethtool, some ugly conversions
and code were noticed and we can fix them with a little GENMASK and a
small refactor to use local variables to simplify some bitset.c code.
These changes should have no functional effect.
Signed-off-by: Jesse Brandeburg <jesse.brandeburg@...el.com>
---
net/ethtool/bitset.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/net/ethtool/bitset.c b/net/ethtool/bitset.c
index 0515d6604b3b..ac289f335582 100644
--- a/net/ethtool/bitset.c
+++ b/net/ethtool/bitset.c
@@ -14,12 +14,12 @@
static u32 ethnl_lower_bits(unsigned int n)
{
- return ~(u32)0 >> (32 - n % 32);
+ return GENMASK((n % 32), 0);
}
static u32 ethnl_upper_bits(unsigned int n)
{
- return ~(u32)0 << (n % 32);
+ return GENMASK(31, (n % 32));
}
/**
@@ -452,8 +452,8 @@ ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits,
ethnl_bitmap32_clear(bitmap, 0, nbits, mod);
nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
+ unsigned int idx, bitmap_idx, mod_idx;
bool old_val, new_val;
- unsigned int idx;
if (nla_type(bit_attr) != ETHTOOL_A_BITSET_BITS_BIT) {
NL_SET_ERR_MSG_ATTR(extack, bit_attr,
@@ -464,12 +464,14 @@ ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits,
names, extack);
if (ret < 0)
return ret;
- old_val = bitmap[idx / 32] & ((u32)1 << (idx % 32));
+ bitmap_idx = idx / 32;
+ mod_idx = idx % 32;
+ old_val = bitmap[bitmap_idx] & BIT(mod_idx);
if (new_val != old_val) {
if (new_val)
- bitmap[idx / 32] |= ((u32)1 << (idx % 32));
+ bitmap[bitmap_idx] |= BIT(mod_idx);
else
- bitmap[idx / 32] &= ~((u32)1 << (idx % 32));
+ bitmap[bitmap_idx] &= ~BIT(mod_idx);
*mod = true;
}
}
--
2.31.1
Powered by blists - more mailing lists