All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qian Cai <quic_qiancai@quicinc.com>
To: Yury Norov <yury.norov@gmail.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	<linux-kernel@vger.kernel.org>,
	Qian Cai <quic_qiancai@quicinc.com>
Subject: [PATCH] bitmap: simplify GENMASK(size - 1, 0) lines
Date: Tue, 26 Oct 2021 10:41:08 -0400	[thread overview]
Message-ID: <20211026144108.35373-1-quic_qiancai@quicinc.com> (raw)

Since "size" is an "unsigned int", the rvalue "size - 1" will still be
"unsigned int" according to the C standard (3.2.1.5 Usual arithmetic
conversions). Therefore, GENMASK(size - 1, 0) will always return 0UL. Those
are also caught by GCC (W=2):

./include/linux/find.h: In function 'find_first_bit':
./include/linux/bits.h:25:22: warning: comparison of unsigned expression in '< 0' is always false [-Wtype-limits]
   25 |   __is_constexpr((l) > (h)), (l) > (h), 0)))
      |                      ^
./include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO'
   16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
      |                                                              ^
./include/linux/bits.h:25:3: note: in expansion of macro '__is_constexpr'
   25 |   __is_constexpr((l) > (h)), (l) > (h), 0)))
      |   ^~~~~~~~~~~~~~
./include/linux/bits.h:38:3: note: in expansion of macro 'GENMASK_INPUT_CHECK'
   38 |  (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
      |   ^~~~~~~~~~~~~~~~~~~
./include/linux/find.h:119:31: note: in expansion of macro 'GENMASK'
  119 |   unsigned long val = *addr & GENMASK(size - 1, 0);
      |                               ^~~~~~~
./include/linux/bits.h:25:34: warning: comparison of unsigned expression in '< 0' is always false [-Wtype-limits]
   25 |   __is_constexpr((l) > (h)), (l) > (h), 0)))
      |                                  ^
./include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO'
   16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
      |                                                              ^
./include/linux/bits.h:38:3: note: in expansion of macro 'GENMASK_INPUT_CHECK'
   38 |  (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
      |   ^~~~~~~~~~~~~~~~~~~
./include/linux/find.h:119:31: note: in expansion of macro 'GENMASK'
  119 |   unsigned long val = *addr & GENMASK(size - 1, 0);
      |                               ^~~~~~~

Signed-off-by: Qian Cai <quic_qiancai@quicinc.com>
---
 include/linux/find.h | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)

diff --git a/include/linux/find.h b/include/linux/find.h
index 5bb6db213bcb..5ce2b17aea42 100644
--- a/include/linux/find.h
+++ b/include/linux/find.h
@@ -115,11 +115,8 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
 static inline
 unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
 {
-	if (small_const_nbits(size)) {
-		unsigned long val = *addr & GENMASK(size - 1, 0);
-
-		return val ? __ffs(val) : size;
-	}
+	if (small_const_nbits(size))
+		return size;
 
 	return _find_first_bit(addr, size);
 }
@@ -140,11 +137,8 @@ unsigned long find_first_and_bit(const unsigned long *addr1,
 				 const unsigned long *addr2,
 				 unsigned long size)
 {
-	if (small_const_nbits(size)) {
-		unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
-
-		return val ? __ffs(val) : size;
-	}
+	if (small_const_nbits(size))
+		return size;
 
 	return _find_first_and_bit(addr1, addr2, size);
 }
@@ -162,11 +156,8 @@ unsigned long find_first_and_bit(const unsigned long *addr1,
 static inline
 unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
 {
-	if (small_const_nbits(size)) {
-		unsigned long val = *addr | ~GENMASK(size - 1, 0);
-
-		return val == ~0UL ? size : ffz(val);
-	}
+	if (small_const_nbits(size))
+		return size;
 
 	return _find_first_zero_bit(addr, size);
 }
@@ -183,11 +174,8 @@ unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
 static inline
 unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
 {
-	if (small_const_nbits(size)) {
-		unsigned long val = *addr & GENMASK(size - 1, 0);
-
-		return val ? __fls(val) : size;
-	}
+	if (small_const_nbits(size))
+		return size;
 
 	return _find_last_bit(addr, size);
 }
-- 
2.30.2


             reply	other threads:[~2021-10-26 14:41 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-26 14:41 Qian Cai [this message]
2021-10-26 15:10 ` [PATCH] bitmap: simplify GENMASK(size - 1, 0) lines Andy Shevchenko
2021-10-26 15:26   ` Qian Cai
2021-10-26 18:54 ` Yury Norov
2021-10-26 19:21   ` Andy Shevchenko
2021-10-26 19:28     ` Yury Norov
2021-10-26 19:42       ` Andy Shevchenko
2021-10-26 19:54         ` Qian Cai
2021-10-27  9:44           ` Andy Shevchenko
2021-10-27 15:06             ` Qian Cai
2021-10-26 19:33     ` Qian Cai
2021-10-27  9:34       ` Andy Shevchenko
2021-10-29  1:36 ` [bitmap] ca95e676a1: BUG:unable_to_handle_page_fault_for_address kernel test robot
2021-10-29  1:36   ` kernel test robot

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=20211026144108.35373-1-quic_qiancai@quicinc.com \
    --to=quic_qiancai@quicinc.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=yury.norov@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.