From bbb81c96b85585b345c94cb7bf67dab458586af7 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 13 Feb 2016 17:52:00 -1000 Subject: [PATCH] Add ceilingPowerOf2 --- include/Math.hpp | 1 + src/Math.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/Math.hpp b/include/Math.hpp index fe5ea42..4087e42 100644 --- a/include/Math.hpp +++ b/include/Math.hpp @@ -92,6 +92,7 @@ namespace Math float fastCosR(float val); float fastSinR(float val); int floorPowerOfTwo(int x); + int ceilingPowerOfTwo(int x); template inline int PopCount(T x) diff --git a/src/Math.cpp b/src/Math.cpp index 864f813..fa86843 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -198,6 +198,22 @@ int floorPowerOfTwo(int x) return x - (x >> 1); } +int ceilingPowerOfTwo(int x) +{ + if (x == 0) + return 0; + + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + x++; + + return x; +} + float fastCosR(float val) { if (fabs(val) > M_PI)