I am trying to find the fastest way to compute the following in C:
p = 2^(ceil(log2(x)));
So far, looking at answers in Stack overflow (and other places) I have got this far:
#define LOG2(X) ((int) (8*sizeof (unsigned long long) - __builtin_clzll((X)) - 1))
int p = 1 << LOG2( (unsigned long long)x );
x
will always be an integer (type int
) and greater than zero. I got the LOG2 solution from this stackoverflow question. There are several good answer but all of them seem to be rounding down (including this one). I need to round up. I am not comfortable enough with the solutions to modify them to round up. Any help would be appreciated !!!!