public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix get_order()
@ 2007-03-06 17:39 David Howells
  2007-03-06 17:58 ` H. Peter Anvin
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: David Howells @ 2007-03-06 17:39 UTC (permalink / raw)
  To: torvalds, akpm, benh; +Cc: linux-kernel, hpa, johannes, dhowells

From: David Howells <dhowells@redhat.com>

Fix get_order() to use ilog2() properly.

Signed-Off-By: David Howells <dhowells@redhat.com>
---

 include/asm-generic/page.h |   14 +++++++++++---
 include/linux/log2.h       |   20 ++++++++++++++++++--
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/include/asm-generic/page.h b/include/asm-generic/page.h
index b55052c..c37571d 100644
--- a/include/asm-generic/page.h
+++ b/include/asm-generic/page.h
@@ -17,10 +17,18 @@ static inline __attribute__((const))
 int __get_order(unsigned long size, int page_shift)
 {
 #if BITS_PER_LONG == 32 && defined(ARCH_HAS_ILOG2_U32)
-	int order = __ilog2_u32(size) - page_shift;
+	int order;
+	if (size <= 1)
+		order = 0;
+	else
+		order = __ilog2_u32(size - 1) + 1 - page_shift;
 	return order >= 0 ? order : 0;
 #elif BITS_PER_LONG == 64 && defined(ARCH_HAS_ILOG2_U64)
-	int order = __ilog2_u64(size) - page_shift;
+	int order;
+	if (size <= 1)
+		order = 0;
+	else
+		order = __ilog2_u64(size - 1) + 1 - page_shift;
 	return order >= 0 ? order : 0;
 #else
 	int order;
@@ -46,7 +54,7 @@ int __get_order(unsigned long size, int page_shift)
 #define get_order(n)							\
 (									\
 	__builtin_constant_p(n) ?					\
-	((n < (1UL << PAGE_SHIFT)) ? 0 : ilog2(n) - PAGE_SHIFT) :	\
+	((n < (1UL << PAGE_SHIFT)) ? 0 : ilog2_up(n) - PAGE_SHIFT) :	\
 	__get_order(n, PAGE_SHIFT)					\
  )
 
diff --git a/include/linux/log2.h b/include/linux/log2.h
index 57e641e..bc3a0eb 100644
--- a/include/linux/log2.h
+++ b/include/linux/log2.h
@@ -70,6 +70,8 @@ unsigned long __roundup_pow_of_two(unsigned long n)
  * constant-capable log of base 2 calculation
  * - this can be used to initialise global variables from constant data, hence
  *   the massive ternary operator construction
+ * - the result is rounded down
+ * - the result is undefined when n < 1
  *
  * selects the appropriately-sized optimised version depending on sizeof(n)
  */
@@ -149,6 +151,20 @@ unsigned long __roundup_pow_of_two(unsigned long n)
  )
 
 /**
+ * ilog2_up - rounded up log of base 2 of 32-bit or a 64-bit unsigned value
+ * @n - parameter
+ *
+ * constant-capable log of base 2 calculation
+ * - this can be used to initialise global variables from constant data, hence
+ *   the massive ternary operator construction
+ * - the result is rounded up
+ * - the result is undefined when n < 1
+ *
+ * selects the appropriately-sized optimised version depending on sizeof(n)
+ */
+#define ilog2_up(n) ((n) == 1 ? 0 : ilog2((n) - 1) + 1)
+
+/**
  * roundup_pow_of_two - round the given value up to nearest power of two
  * @n - parameter
  *
@@ -159,8 +175,8 @@ unsigned long __roundup_pow_of_two(unsigned long n)
 #define roundup_pow_of_two(n)			\
 (						\
 	__builtin_constant_p(n) ? (		\
-		(n == 1) ? 0 :			\
-		(1UL << (ilog2((n) - 1) + 1))	\
+		(n == 1) ? 1 :			\
+		(1UL << ilog2_up(n))		\
 				   ) :		\
 	__roundup_pow_of_two(n)			\
  )


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

end of thread, other threads:[~2007-03-10  7:51 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-03-06 17:39 [PATCH] Fix get_order() David Howells
2007-03-06 17:58 ` H. Peter Anvin
2007-03-06 18:21   ` David Howells
2007-03-06 18:41 ` Linus Torvalds
2007-03-06 18:51   ` David Howells
2007-03-07  3:28 ` Linus Torvalds
2007-03-07 11:43   ` David Howells
2007-03-07 16:02     ` ALIGN via ilog2 without gccisms (Re: [PATCH] Fix get_order()) Oleg Verych
2007-03-07 16:38       ` Linus Torvalds
2007-03-07 17:24         ` Oleg Verych
2007-03-07 18:05           ` Linus Torvalds
2007-03-09 23:13         ` ALIGN " Oleg Verych
2007-03-09 23:15           ` Linus Torvalds
2007-03-10  0:31             ` Oleg Verych
2007-03-10  8:01         ` ALIGN Oleg Verych

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