Re: [PATCH] bitops: simplify get_count_order_long()

From: Andrew Morton
Date: Mon May 25 2020 - 16:41:14 EST


On Mon, 25 May 2020 18:32:16 +0300 Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:

> On Mon, May 25, 2020 at 02:43:12PM +0000, Wei Yang wrote:
> > On Mon, May 25, 2020 at 12:14:58PM +0300, Andy Shevchenko wrote:
> > >On Sun, May 24, 2020 at 12:35:51PM +0000, Wei Yang wrote:
> > >> These two cases could be unified into one.
> > >
> > >Care to provide a test case which supports your change?

I hurt my brain convincing myself, so I got practical:


int fls(unsigned int x)
{
return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
}

static int get_count_order(unsigned l)
{
if (l == 0)
return -1;
else if (l & (l - 1UL))
return fls(l);
else
return fls(l) - 1;
}

static int get_count_order2(unsigned long l)
{
if (l == 0)
return -1;
return fls(--l);
}

main()
{
unsigned i;

for (i = 1; i < 64; i++) {
printf("%d %d\n", get_count_order(i),
get_count_order2(i));
}
}


> >
> > Hmm.. where should I put the test? tools/testing/selftests/ ?
>
> I guess into test_bitops.c [1]? I though it eventually should make kernel, but I don't see it.
>
> Andrew, can you apply that or do you need Jesse to resend?
>

Got it.