In various places we have code that does the equivalent of
do_something(uint x) {
uint next_pow2 = 1;
while (next_pow2 < x) {
next_pow2 <<= 1;
}
...
}
This is brittle (would loop forever if x > (1 << 31)) and also somewhat inefficient. Adding a utility that does the equivalent of
if (x == 0) {
next_pow2 = 1;
} else if (x < (1 << 31)) {
next_pow2 = 1U << (32 - count_leading_zeros(x));
} else {
next_pow2 = uint_max; // might need to be able to allow different behaviors here
might avoid infinite loops at the extreme (although it's debatable what should be returned when x > 2^31) and potentially be more efficient.