* [PATCH] lib: count_zeros: unify count_{leading,trailing}_zeros()
@ 2026-03-23 17:00 Yury Norov
2026-03-23 17:10 ` Andy Shevchenko
0 siblings, 1 reply; 3+ messages in thread
From: Yury Norov @ 2026-03-23 17:00 UTC (permalink / raw)
To: linux-kernel, Bibo Mao, Huacai Chen, Rasmus Villemoes,
Tianrui Zhao, WANG Xuerui, Yury Norov, David Howells, kvm,
loongarch
Cc: Yury Norov, Andy Shevchenko
The 'leading' helper returns BITS_PER_LONG if x == 0, while 'trailing'
one returns COUNT_TRAILING_ZEROS_0, which turns to be -1.
None of the current users explicitly check the returned value for
COUNT_TRAILING_ZEROS_0, except the loongarch, which tests implicitly
for the '>= 0'.
So, align count_trailing_zeros() with the count_leading_zeros(), and
simplify the loongarch handling.
Signed-off-by: Yury Norov <ynorov@nvidia.com>
---
On top of bitmap-for-next.
arch/loongarch/kvm/intc/eiointc.c | 4 ++--
include/linux/count_zeros.h | 6 ++----
2 files changed, 4 insertions(+), 6 deletions(-)
diff --git a/arch/loongarch/kvm/intc/eiointc.c b/arch/loongarch/kvm/intc/eiointc.c
index d2acb4d09e73..3b5268116727 100644
--- a/arch/loongarch/kvm/intc/eiointc.c
+++ b/arch/loongarch/kvm/intc/eiointc.c
@@ -16,7 +16,7 @@ static void eiointc_set_sw_coreisr(struct loongarch_eiointc *s)
ipnum = (s->ipmap >> (irq / 32 * 8)) & 0xff;
if (!(s->status & BIT(EIOINTC_ENABLE_INT_ENCODE))) {
ipnum = count_trailing_zeros(ipnum);
- ipnum = (ipnum >= 0 && ipnum < 4) ? ipnum : 0;
+ ipnum = ipnum < 4 ? ipnum : 0;
}
cpuid = ((u8 *)s->coremap)[irq];
@@ -41,7 +41,7 @@ static void eiointc_update_irq(struct loongarch_eiointc *s, int irq, int level)
ipnum = (s->ipmap >> (irq / 32 * 8)) & 0xff;
if (!(s->status & BIT(EIOINTC_ENABLE_INT_ENCODE))) {
ipnum = count_trailing_zeros(ipnum);
- ipnum = (ipnum >= 0 && ipnum < 4) ? ipnum : 0;
+ ipnum = ipnum < 4 ? ipnum : 0;
}
cpu = s->sw_coremap[irq];
diff --git a/include/linux/count_zeros.h b/include/linux/count_zeros.h
index 5034a30b5c7c..b72ba3faa036 100644
--- a/include/linux/count_zeros.h
+++ b/include/linux/count_zeros.h
@@ -10,8 +10,6 @@
#include <asm/bitops.h>
-#define COUNT_TRAILING_ZEROS_0 (-1)
-
/**
* count_leading_zeros - Count the number of zeros from the MSB back
* @x: The value
@@ -38,11 +36,11 @@ static inline int count_leading_zeros(unsigned long x)
*
* If the LSB of @x is set, the result is 0.
* If only the MSB of @x is set, then the result is BITS_PER_LONG-1.
- * If @x is 0 then the result is COUNT_TRAILING_ZEROS_0.
+ * If @x is 0 then the result is BITS_PER_LONG.
*/
static inline int count_trailing_zeros(unsigned long x)
{
- return (x != 0) ? __ffs(x) : COUNT_TRAILING_ZEROS_0;
+ return x ? __ffs(x) : BITS_PER_LONG;
}
#endif /* _LINUX_BITOPS_COUNT_ZEROS_H_ */
--
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] lib: count_zeros: unify count_{leading,trailing}_zeros()
2026-03-23 17:00 [PATCH] lib: count_zeros: unify count_{leading,trailing}_zeros() Yury Norov
@ 2026-03-23 17:10 ` Andy Shevchenko
2026-03-23 17:12 ` Yury Norov
0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2026-03-23 17:10 UTC (permalink / raw)
To: Yury Norov
Cc: linux-kernel, Bibo Mao, Huacai Chen, Rasmus Villemoes,
Tianrui Zhao, WANG Xuerui, Yury Norov, David Howells, kvm,
loongarch
On Mon, Mar 23, 2026 at 01:00:32PM -0400, Yury Norov wrote:
> The 'leading' helper returns BITS_PER_LONG if x == 0, while 'trailing'
> one returns COUNT_TRAILING_ZEROS_0, which turns to be -1.
>
> None of the current users explicitly check the returned value for
> COUNT_TRAILING_ZEROS_0, except the loongarch, which tests implicitly
> for the '>= 0'.
>
> So, align count_trailing_zeros() with the count_leading_zeros(), and
> simplify the loongarch handling.
Makes sense,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] lib: count_zeros: unify count_{leading,trailing}_zeros()
2026-03-23 17:10 ` Andy Shevchenko
@ 2026-03-23 17:12 ` Yury Norov
0 siblings, 0 replies; 3+ messages in thread
From: Yury Norov @ 2026-03-23 17:12 UTC (permalink / raw)
To: Andy Shevchenko
Cc: linux-kernel, Bibo Mao, Huacai Chen, Rasmus Villemoes,
Tianrui Zhao, WANG Xuerui, Yury Norov, David Howells, kvm,
loongarch
On Mon, Mar 23, 2026 at 07:10:05PM +0200, Andy Shevchenko wrote:
> On Mon, Mar 23, 2026 at 01:00:32PM -0400, Yury Norov wrote:
> > The 'leading' helper returns BITS_PER_LONG if x == 0, while 'trailing'
> > one returns COUNT_TRAILING_ZEROS_0, which turns to be -1.
> >
> > None of the current users explicitly check the returned value for
> > COUNT_TRAILING_ZEROS_0, except the loongarch, which tests implicitly
> > for the '>= 0'.
> >
> > So, align count_trailing_zeros() with the count_leading_zeros(), and
> > simplify the loongarch handling.
>
> Makes sense,
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Thanks, Andy. Moving in -next for testing then.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-03-23 17:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-23 17:00 [PATCH] lib: count_zeros: unify count_{leading,trailing}_zeros() Yury Norov
2026-03-23 17:10 ` Andy Shevchenko
2026-03-23 17:12 ` Yury Norov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox