public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Optimize is_power_of_2().
@ 2007-06-15 16:56 Vegard Nossum
  2007-06-15 18:21 ` H. Peter Anvin
  2007-06-15 19:47 ` Jan Engelhardt
  0 siblings, 2 replies; 7+ messages in thread
From: Vegard Nossum @ 2007-06-15 16:56 UTC (permalink / raw)
  To: linux-kernel

From: Vegard Nossum <vegard@peltkore.net>
Date: Fri, 15 Jun 2007 18:35:49 +0200
Subject: [PATCH] Optimize is_power_of_2().

Rationale: Removes one conditional branch and reduces icache footprint.
Proof: If n is false, the product of n and any value is false. If n is
true, the truth of (n * x) is the truth of x.

Signed-off-by: Vegard Nossum <vegard@peltkore.net>
---
 include/linux/log2.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/include/linux/log2.h b/include/linux/log2.h
index 1b8a2c1..56196b1 100644
--- a/include/linux/log2.h
+++ b/include/linux/log2.h
@@ -51,7 +51,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 * !(n & (n - 1));
 }
 
 /*
-- 
1.5.0.6


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-06-15 20:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-15 16:56 [PATCH] Optimize is_power_of_2() Vegard Nossum
2007-06-15 18:21 ` H. Peter Anvin
2007-06-15 18:28   ` Robert P. J. Day
2007-06-15 20:26     ` H. Peter Anvin
2007-06-15 19:47 ` Jan Engelhardt
2007-06-15 19:54   ` David M. Lloyd
2007-06-15 19:57     ` David M. Lloyd

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox