Ok, since when I asked about NextPower2 I got so many answers, and everyone seemed to have fun. Lets try a little challenge.
Create the fastest cross platform implementation of NextPower2 that takes and returns a LongInt.
EX:
[pascal]function NextPow2(aVal : LongInt) : LongInt;
begin
if aVal >= (1 shl 31) then
begin
result := 1 shl 31;
exit;
end;
result := 1;
while result < aVal do
result := result shl 1;
end;[/pascal]
Rules:
* Must compile in FPC for Windows, Linux, and Mac
* You can't simply extend cairnswm fixed lookup table method, as this will always be the fastest possible, and eventually memory comes into concern.
Anyone interested?
I haven't thought of a benchmark or anything just yet, any suggestions?
PS: I think that if there is interest in this sort of thing, then WILL or Dom should create a place for us to post the winning code snippets for when people ask how to do something quickly
Bookmarks