Re: [PATCH] lib: bitrev.c micro-optimization

From: Linus Torvalds
Date: Tue Apr 29 2008 - 11:51:01 EST




Not very good. Why do it in memory, when doing it in a register is much
clearer?

This generates one byte longer code for me, but avoids the partial memory
stalls and looks more obvious. It is also portable, unlike your version
(you need to use a union to avoid aliasing issues - admittedly we disable
the gcc alias code anyway because of other kernel code, but still).

Linus

---
lib/bitrev.c | 10 ++++------
1 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/lib/bitrev.c b/lib/bitrev.c
index 989aff7..e0cb44e 100644
--- a/lib/bitrev.c
+++ b/lib/bitrev.c
@@ -42,17 +42,15 @@ const u8 byte_rev_table[256] = {
};
EXPORT_SYMBOL_GPL(byte_rev_table);

-static __always_inline u16 bitrev16(u16 x)
-{
- return (bitrev8(x & 0xff) << 8) | bitrev8(x >> 8);
-}
-
/**
* bitrev32 - reverse the order of bits in a u32 value
* @x: value to be bit-reversed
*/
u32 bitrev32(u32 x)
{
- return (bitrev16(x & 0xffff) << 16) | bitrev16(x >> 16);
+ return (bitrev8(x) << 24) |
+ (bitrev8(x >> 8) << 16) |
+ (bitrev8(x >> 16) << 8) |
+ (bitrev8(x >> 24));
}
EXPORT_SYMBOL(bitrev32);
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/