lkml.org 
[lkml]   [2022]   [May]   [10]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    /
    From
    Date
    SubjectRe: [PATCH 08/22] bitops: introduce MANY_BITS() macro
    On Tue, May 10, 2022 at 8:48 AM Yury Norov <yury.norov@gmail.com> wrote:
    >
    > arch/xtensa/kernel/traps.c and include/linux/log2.h define very similar
    > functions with different behaviour. XTENSA defines IS_POW2(), and
    > log2.h defines is_power_of_2(). The difference is that IS_POW2()
    > considers 0 as power of 2, while is_power_of_2() - does not.
    >
    > This discrepancy may confuse reader. From mathematical point of view,
    > 0 is not a power of 2. So let's introduce macro MANY_BITS(), which
    > returns true if 2 or more bits are set in a number (which is what
    > XTENSA actually needs), and use it in is_power_of_2().
    >
    > CC: Alexei Starovoitov <ast@kernel.org>
    > CC: Andrew Morton <akpm@linux-foundation.org>
    > CC: Chris Zankel <chris@zankel.net>
    > CC: Christophe Leroy <christophe.leroy@csgroup.eu>
    > CC: Eric W. Biederman <ebiederm@xmission.com>
    > CC: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    > CC: Max Filippov <jcmvbkbc@gmail.com>
    > CC: Toke Høiland-Jørgensen <toke@redhat.com>
    > CC: linux-xtensa@linux-xtensa.org
    > CC: linux-kernel@vger.kernel.org
    > Signed-off-by: Yury Norov <yury.norov@gmail.com>
    > ---
    > arch/xtensa/kernel/traps.c | 5 +----
    > include/linux/bitops.h | 3 +++
    > include/linux/log2.h | 2 +-
    > 3 files changed, 5 insertions(+), 5 deletions(-)
    >
    > diff --git a/arch/xtensa/kernel/traps.c b/arch/xtensa/kernel/traps.c
    > index 138a86fbe9d7..040ec38bfce2 100644
    > --- a/arch/xtensa/kernel/traps.c
    > +++ b/arch/xtensa/kernel/traps.c
    > @@ -203,10 +203,7 @@ static void do_multihit(struct pt_regs *regs)
    >
    > #if XTENSA_FAKE_NMI
    >
    > -#define IS_POW2(v) (((v) & ((v) - 1)) == 0)
    > -
    > -#if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
    > - IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
    > +#if (MANY_BITS(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)) || PROFILING_INTLEVEL != XCHAL_EXCM_LEVEL)
    > #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
    > #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
    >
    > diff --git a/include/linux/bitops.h b/include/linux/bitops.h
    > index 7aaed501f768..96bc6a2552d6 100644
    > --- a/include/linux/bitops.h
    > +++ b/include/linux/bitops.h
    > @@ -21,6 +21,9 @@
    > #define BITS_TO_U32(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
    > #define BITS_TO_BYTES(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
    >
    > +/* Return: nonzero if 2 or more bits are set */
    > +#define MANY_BITS(n) ((n) & ((n) - 1))
    > +
    > extern unsigned int __sw_hweight8(unsigned int w);
    > extern unsigned int __sw_hweight16(unsigned int w);
    > extern unsigned int __sw_hweight32(unsigned int w);
    > diff --git a/include/linux/log2.h b/include/linux/log2.h
    > index 9f30d087a128..335b9dbd302d 100644
    > --- a/include/linux/log2.h
    > +++ b/include/linux/log2.h
    > @@ -44,7 +44,7 @@ int __ilog2_u64(u64 n)
    > static inline __attribute__((const))
    > bool is_power_of_2(unsigned long n)
    > {
    > - return (n != 0 && ((n & (n - 1)) == 0));
    > + return n != 0 && !MANY_BITS(n);
    > }

    Please don't. Open coded version is much easier to read.

    \
     
     \ /
      Last update: 2022-05-10 18:51    [W:4.173 / U:0.088 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site