openMSX
Math.cc
Go to the documentation of this file.
1 #include "Math.hh"
2 #include <cstdint>
3 #include <cstdlib>
4 
5 namespace openmsx {
6 namespace Math {
7 
8 unsigned powerOfTwo(unsigned a)
9 {
10  // classical implementation:
11  // unsigned res = 1;
12  // while (a > res) res <<= 1;
13  // return res;
14 
15  // optimized version
16  a += (a == 0); // can be removed if argument is never zero
17  return floodRight(a - 1) + 1;
18 }
19 
20 } // namespace Math
21 } // namespace openmsx
T floodRight(T x)
Returns the smallest number of the form 2^n-1 that is greater or equal to the given number...
Definition: Math.hh:174
unsigned powerOfTwo(unsigned a)
Returns the smallest number that is both >=a and a power of two.
Definition: Math.cc:8