Skip to content

Commit

Permalink
Added IsPowerOfTwo and RoundUp functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenliang committed May 26, 2011
1 parent daf071f commit a44dfba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions util/math_base.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#pragma once

#include "util.h"
#include <assert.h>

namespace util
{
#define UTIL_POW2(x) ((x)*(x))

inline bool IsPowerOfTwo(int n);

// align ±ØÐëÊÇ 2 µÄÃÝ
inline unsigned int RoundUp(unsigned int n, unsigned int align);

template <class T1, class T2, class T3>
inline T1 Clam(const T1& num, const T2& low, const T3& high);
template <class T1, class T2>
Expand Down
12 changes: 12 additions & 0 deletions util/math_base.inl
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@

inline bool IsPowerOfTwo(int n)
{
return (n > 0) ? (!(n & (n - 1))) : false;
}

// align ±ØÐëÊÇ 2 µÄÃÝ
inline unsigned int RoundUp(unsigned int n, unsigned int align)
{
assert(IsPowerOfTwo(align));
return (n + (align - 1)) & ~(align - 1);
}

template <class T1, class T2, class T3>
inline T1 Clam(const T1& num, const T2& low, const T3& high)
{
Expand Down
2 changes: 1 addition & 1 deletion util/util.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
</File>
</Filter>
<Filter
Name="test"
Name="dbgs"
>
<File
RelativePath=".\dbg_new.h"
Expand Down

0 comments on commit a44dfba

Please sign in to comment.