Skip to contentSkip to conversation

log2.h: Define order_base_2() macro for convenience.

PATCH New Robert P. J. Day 1 participant 1 message Nov 12, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 log2.h: Define order_base_2() macro for convenience.

Given a number of places in the tree that need to calculate this value explicitly, might as well just create a macro for it.

---

run-time tested for the first several values. note that this macro
is defined strictly in terms of rounding *up* -- i've seen no places in the tree that require this expression to be rounded down, so i'm not drawing that distinction.

include/linux/log2.h +16 -0
index c8cf5e8..ed121c0 100644
@@ -190,4 +190,20 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
 	__rounddown_pow_of_two(n)		\
  )
 
+/**
+ * order_base_2 - calculate the (rounded up) base 2 order of the argument
+ * @n: parameter
+ *
+ * The first few values calculated by this routine:
+ *  ob2(0) = 0
+ *  ob2(1) = 0
+ *  ob2(2) = 1
+ *  ob2(3) = 2
+ *  ob2(4) = 2
+ *  ob2(5) = 3
+ *  ... and so on.
+ */
+
+#define order_base_2(n) ilog2(roundup_pow_of_two(n))
+
 #endif /* _LINUX_LOG2_H */
Signature · 7 lines
========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA

http://crashcourse.ca
========================================================================