int MathUtilities::nearestPowerOfTwo(int x) { if (isPowerOfTwo(x)) return x; int n0 = previousPowerOfTwo(x), n1 = nextPowerOfTwo(x); if (x - n0 < n1 - x) return n0; else return n1; }
inline unsigned int nearestPowerOfTwo(unsigned int v) { const unsigned int np2 = nextPowerOfTwo(v); const unsigned int pp2 = previousPowerOfTwo(v); if (np2 - v <= v - pp2) { return np2; } else { return pp2; } }