Skip to contentSkip to conversation

include/log2.h: Fix rounddown_pow_of_two(1)

PATCH In review Dmitry Torokhov 4 participants 7 messages Dec 12, 2011 – Dec 13, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 include/log2.h: Fix rounddown_pow_of_two(1)

From: Andrei Warkentin <[email protected]>

1 is a power of two, therefore rounddown_pow_of_two(1) should return 1. It does in case the argument is a variable but in case it's a constant it behaves incorrectly and returns 0. Probably nobody ever did it so this was never noticed, however drivers/net/vmxnet3 with latest GCC does and breaks on unicpu systems.

This is similar to Rolf's patch to roundup_pow_of_two(1).

---
 include/linux/log2.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
include/linux/log2.h +1 -1
index 25b8086..ccda848 100644
@@ -185,7 +185,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
 #define rounddown_pow_of_two(n)			\
 (						\
 	__builtin_constant_p(n) ? (		\
-		(n == 1) ? 0 :			\
+		(n == 1) ? 1 :			\
 		(1UL << ilog2(n))) :		\
 	__rounddown_pow_of_two(n)		\
  )
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 9 lines · Dmitry Torokhov

From: Andrei Warkentin <[email protected]>

1 is a power of two, therefore rounddown_pow_of_two(1) should return 1. It does in case the argument is a variable but in case it's a constant it behaves incorrectly and returns 0. Probably nobody ever did it so this was never noticed, however drivers/net/vmxnet3 with latest GCC does and breaks on unicpu systems.

This is similar to Rolf's patch to roundup_pow_of_two(1).

Umm. I already applied this patch, but then I started looking at it more, and asked myself:

- Why is that "n == 1" test there AT ALL?

Afaik, that whole test is just plain stupid. It seems to have been copied from the "roundup()" case (where it exists due to the "-1/+1" hackery that breaks ilog2()) without any thought about the actual math of the function at all.

I think the *real* fix is to just remove that incorrect line, no?

It's a bit sad that we apparently have several reviewers for this trivial patch, and nobody reacted to the math just not making any sense.

Linus

#2 lore raw
Quoted 14 lines · Linus Torvalds
Quoted 9 lines · Dmitry Torokhov

From: Andrei Warkentin <[email protected]>

1 is a power of two, therefore rounddown_pow_of_two(1) should return 1. It does in case the argument is a variable but in case it's a constant it behaves incorrectly and returns 0. Probably nobody ever did it so this was never noticed, however drivers/net/vmxnet3 with latest GCC does and breaks on unicpu systems.

This is similar to Rolf's patch to roundup_pow_of_two(1).

Umm. I already applied this patch, but then I started looking at it more, and asked myself:

Quoted 8 lines

- Why is that "n == 1" test there AT ALL?

Afaik, that whole test is just plain stupid. It seems to have been copied from the "roundup()" case (where it exists due to the "-1/+1" hackery that breaks ilog2()) without any thought about the actual math of the function at all.

I think the *real* fix is to just remove that incorrect line, no?

Yes, you are right, special-casing for 1 is not necessary in rounddown case.

Thanks,
Dmitry

#4 lore raw
Quoted 7 lines · Peter Zijlstra
Quoted 1 line · Dmitry Torokhov

1 is a power of two, therefore rounddown_pow_of_two(1) should return 1.

x^0 = 1
x^1 = x

Seems to me 0 was the right answer, no?

n/m head-up-arse, missed that this wasn't actually a log variant.

#5 lore raw
In reply to Peter Zijlstra
Quoted 7 lines · Dmitry Torokhov
Quoted 1 line

1 is a power of two, therefore rounddown_pow_of_two(1) should return 1.

x^0 = 1
x^1 = x

Seems to me 0 was the right answer, no?

Eh? The answer should be the power-of-two (1), not the exponent (0).

#6 lore raw
Quoted 9 lines · Bhavesh Davda
Quoted 7 lines · Dmitry Torokhov
Quoted 1 line

1 is a power of two, therefore rounddown_pow_of_two(1) should return 1.

x^0 = 1
x^1 = x

Seems to me 0 was the right answer, no?

Eh? The answer should be the power-of-two (1), not the exponent (0).

Yeah, just noticed that, see my other reply.