From 1e47f646303fc8ce791262d85e0c54ada4500f51 Mon Sep 17 00:00:00 2001 From: janwas Date: Sat, 19 Jun 2004 14:43:31 +0000 Subject: [PATCH] add log2 This was SVN commit r556. --- source/lib/lib.cpp | 15 +++++++++++++++ source/lib/lib.h | 4 +++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/source/lib/lib.cpp b/source/lib/lib.cpp index e65517cd35..d105efa0d4 100755 --- a/source/lib/lib.cpp +++ b/source/lib/lib.cpp @@ -241,6 +241,21 @@ int ilog2(const int n) } +// return log base 2, rounded up. +uint log2(uint x) +{ + uint bit = 1; + uint l = 0; + while(bit < x) + { + l++; + bit *= 2; + } + + return l; +} + + int ilog2(const float x) { u32 i = (u32&)x; diff --git a/source/lib/lib.h b/source/lib/lib.h index ecc10e3365..00e29384a3 100755 --- a/source/lib/lib.h +++ b/source/lib/lib.h @@ -264,11 +264,13 @@ extern u32 read_le32(const void* p); extern bool is_pow2(long n); - // return -1 if not an integral power of 2, // otherwise the base2 logarithm extern int ilog2(const int n); +// return log base 2, rounded up. +extern uint log2(uint x); + extern uintptr_t round_up(uintptr_t val, uintptr_t multiple);