Skip to contentSkip to conversation

bitops: simplify get_count_order_long()

PATCH In review Wei Yang 3 participants 10 messages May 24, 2020 – May 28, 2020 Bitops API

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 bitops: simplify get_count_order_long()

These two cases could be unified into one.

---
 include/linux/bitops.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
include/linux/bitops.h +1 -4
index 9acf654f0b19..5b5609e81a84 100644
@@ -206,10 +206,7 @@ static inline int get_count_order_long(unsigned long l)
 {
 	if (l == 0UL)
 		return -1;
-	else if (l & (l - 1UL))
-		return (int)fls_long(l);
-	else
-		return (int)fls_long(l) - 1;
+	return (int)fls_long(--l);
 }
 
 /**
Signature · 1 line
2.23.0
#1 lore raw
Quoted 1 line · Wei Yang

These two cases could be unified into one.

Care to provide a test case which supports your change?

Quoted 19 lines
---
 include/linux/bitops.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
include/linux/bitops.h +1 -4
index 9acf654f0b19..5b5609e81a84 100644
@@ -206,10 +206,7 @@ static inline int get_count_order_long(unsigned long l)
 {
 	if (l == 0UL)
 		return -1;
-	else if (l & (l - 1UL))
-		return (int)fls_long(l);
-	else
-		return (int)fls_long(l) - 1;
+	return (int)fls_long(--l);
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#2 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 1 line · Wei Yang

These two cases could be unified into one.

Care to provide a test case which supports your change?

Hmm.. where should I put the test? tools/testing/selftests/ ?

Quoted 24 lines
Quoted 19 lines
---
 include/linux/bitops.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
include/linux/bitops.h +1 -4
index 9acf654f0b19..5b5609e81a84 100644
@@ -206,10 +206,7 @@ static inline int get_count_order_long(unsigned long l)
 {
 	if (l == 0UL)
 		return -1;
-	else if (l & (l - 1UL))
-		return (int)fls_long(l);
-	else
-		return (int)fls_long(l) - 1;
+	return (int)fls_long(--l);
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 2 lines
Wei Yang
Help you, Help me
#3 lore raw
Quoted 8 lines · Wei Yang
Quoted 5 lines · Andy Shevchenko
Quoted 1 line · Wei Yang

These two cases could be unified into one.

Care to provide a test case which supports your change?

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?

[1]: https://lore.kernel.org/lkml/[email protected]/

Quoted 29 lines
Quoted 25 lines
Quoted 20 lines
---
 include/linux/bitops.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
include/linux/bitops.h +1 -4
index 9acf654f0b19..5b5609e81a84 100644
@@ -206,10 +206,7 @@ static inline int get_count_order_long(unsigned long l)
 {
 	if (l == 0UL)
 		return -1;
-	else if (l & (l - 1UL))
-		return (int)fls_long(l);
-	else
-		return (int)fls_long(l) - 1;
+	return (int)fls_long(--l);
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 2 lines
Wei Yang
Help you, Help me
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 6 lines · On Mon, 25 May 2020 18:32:16 +0300 Andy Shevchenko…
Quoted 5 lines · Wei Yang
Quoted 4 lines · Andy Shevchenko
Quoted 1 line · Wei Yang

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));
	}
}
Quoted 7 lines
Quoted 2 lines

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.

author
#5 lore raw
Quoted 46 lines · Andrew Morton
Quoted 6 lines · On Mon, 25 May 2020 18:32:16 +0300 Andy Shevchenko…
Quoted 5 lines · Wei Yang
Quoted 4 lines · Andy Shevchenko
Quoted 1 line · Wei Yang

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));
	}
}
Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

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.

I see the patch just merged, so I suppose to add the above test code into that one?

Signature · 2 lines
Wei Yang
Help you, Help me
#6 lore raw
Quoted 2 lines · On Mon, 25 May 2020 21:57:41 +0000 Wei Yang <[email protected]> wrote:

I see the patch just merged, so I suppose to add the above test code into that one?

Well, that's not really test code.

But yes, something which tests both the 32-bit and 64-bit functions would be nice, sometime.

author
#7 lore raw
Quoted 9 lines · Andrew Morton
Quoted 2 lines · On Mon, 25 May 2020 21:57:41 +0000 Wei Yang <[email protected]> wrote:

I see the patch just merged, so I suppose to add the above test code into that one?

Well, that's not really test code.

But yes, something which tests both the 32-bit and 64-bit functions would be nice, sometime.

Mimic the test_bitops.c, I wrote a test like this:

/* a tiny module only meant to test get_count_order/long */

unsigned int order_comb[][2] = {
	{0x00000003,  2},
	{0x00000004,  2},
	{0x00001fff, 13},
	{0x00002000, 13},
	{0x50000000, 32},
	{0x80000000, 32},
};

static int __init test_getorder_startup(void)

{
	int i;

	for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
		if (order_comb[i][1] != get_count_order(order_comb[i][0]))
			pr_warn("get_count_order wrong for %lx\n",
					order_comb[i][0]);
	}

	return 0;
}

Since I don't get a way to iterate all the possibilities, some random combination is chosen. Is this one looks good?

Signature · 2 lines
Wei Yang
Help you, Help me
#8 lore raw
Quoted 23 lines · On Wed, 27 May 2020 22:45:42 +0000 Wei Yang <[email protected]> wrote:

/* a tiny module only meant to test get_count_order/long */

unsigned int order_comb[][2] = {
	{0x00000003,  2},
	{0x00000004,  2},
	{0x00001fff, 13},
	{0x00002000, 13},
	{0x50000000, 32},
	{0x80000000, 32},
};

static int __init test_getorder_startup(void)

{
	int i;

	for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
		if (order_comb[i][1] != get_count_order(order_comb[i][0]))
			pr_warn("get_count_order wrong for %lx\n",
					order_comb[i][0]);
	}

	return 0;
}
Quoted 2 lines

Since I don't get a way to iterate all the possibilities, some random combination is chosen. Is this one looks good?

Looks good.

You might want to add a less-negative number as well? 0x80030000. Something that won't turn positive if it has 1 subtracted from it.

author
#9 lore raw
Quoted 25 lines · Andrew Morton
Quoted 23 lines · On Wed, 27 May 2020 22:45:42 +0000 Wei Yang <[email protected]> wrote:

/* a tiny module only meant to test get_count_order/long */

unsigned int order_comb[][2] = {
	{0x00000003,  2},
	{0x00000004,  2},
	{0x00001fff, 13},
	{0x00002000, 13},
	{0x50000000, 32},
	{0x80000000, 32},
};

static int __init test_getorder_startup(void)

{
	int i;

	for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
		if (order_comb[i][1] != get_count_order(order_comb[i][0]))
			pr_warn("get_count_order wrong for %lx\n",
					order_comb[i][0]);
	}

	return 0;
}
Quoted 7 lines
Quoted 2 lines

Since I don't get a way to iterate all the possibilities, some random combination is chosen. Is this one looks good?

Looks good.

You might want to add a less-negative number as well? 0x80030000. Something that won't turn positive if it has 1 subtracted from it.

Thanks, this is a good suggestion.

Signature · 2 lines
Wei Yang
Help you, Help me