diff options
author | RJ Ryan <rryan@mixxx.org> | 2014-12-03 19:24:07 -0500 |
---|---|---|
committer | RJ Ryan <rryan@mixxx.org> | 2014-12-03 19:30:03 -0500 |
commit | ad779affbcc683c83c5bafea8b79cc51d3803dfa (patch) | |
tree | 74bd7a6c64cd17ac33ec1aa1a121b812112093fb /src/util/math.h | |
parent | a969befe5b2495162d3b20356db5c8bc8692f2a4 (diff) |
Replace bit-twiddling hacks with a portable solution.
Diffstat (limited to 'src/util/math.h')
-rw-r--r-- | src/util/math.h | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/src/util/math.h b/src/util/math.h index 17d928b190..4b317ec724 100644 --- a/src/util/math.h +++ b/src/util/math.h @@ -71,15 +71,16 @@ inline bool even(T value) { #endif inline int roundUpToPowerOf2(int v) { - // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 - v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v++; - return v; + int power = 1; + while (power < v && power > 0) { + power *= 2; + } + // There is not a power of 2 higher than v representable by our + // architecture's integer size. + if (power < 0) { + return -1; + } + return power; } // MSVS 2013 (_MSC_VER 1800) introduced C99 support. |