I cant think of any situation where a power of 2 calculation is not fast enough...
You normally use this when loading textures in which case the disk access is going to slow it down no matter what. I know jdarling didnt ask for the fastest thing out there, but it's a classic example of the 90% - 10% optimisation mantra:
90% of the code you write will be executed once or twice
the other 10% will be executed very many more times (thousands of times per second?)
And to optimize code that falls into the first 90% is a waste of time. You need to figure out if this code really falls into that last 10% before you start worrying that the trunc() procedure is not fast enough
Just for luck, this is how I implemented it.. using the suggested lookup-table.
Code:
const
Pow2: array[0..12] of integer = (2,4,8,16,32,64,128,256,512,1024,2048,4096,8192);
Function GetNextPow2(Wid: integer): integer;
var I: integer;
begin
Result := Wid;
for I := 1 to 12 do
begin
If Pow2[I] >= Wid then
begin
Result := Pow2[I-1];
exit;
end;
end;
end;
Function GetPrevPow2(Wid: integer): integer;
var I: integer;
begin
Result := Wid;
for I := 12 downto 1 do
begin
If Pow2[I] <= Wid then
begin
Result := Pow2[I-1];
exit;
end;
end;
end;
Bookmarks