View Poll Results: Are you interested in seeing little things like this pop up on PGD every week? Quick contests with

Voters
15. You may not vote on this poll
  • Yes

    11 73.33%
  • No

    0 0%
  • It depends on the price of tea in China

    4 26.67%
Results 1 to 9 of 9

Thread: Challenge: NextPower2 for LongInt

  1. #1

    Challenge: NextPower2 for LongInt

    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

  2. #2

    Challenge: NextPower2 for LongInt

    Why not try something other than Power of 2...
    Try to pick an algorithm that, while simple, needs to be run many number of times...
    My site: DelphiTuts.com (coming soon)...

    Download Font Studio 4.21 here.

  3. #3

    Challenge: NextPower2 for LongInt

    Quote Originally Posted by Nitrogen
    Why not try something other than Power of 2...
    Try to pick an algorithm that, while simple, needs to be run many number of times...
    Actually, I have a few in mind, but I thought this would be a good start since its a common need and we only solved for an integer.

  4. #4

    Challenge: NextPower2 for LongInt

    Well, all methods except the lookup table can be used for longint as well?

  5. #5

    Challenge: NextPower2 for LongInt

    [pascal]
    function NextPowerOf2(x: Longint): Longint;
    begin
    Result := x-1;
    Result := Result or Result shr 1;
    Result := Result or Result shr 2;
    Result := Result or Result shr 4;
    Result := Result or Result shr 8;
    Result := Result or Result shr 16;
    Inc(Result);
    end;
    [/pascal]

    upd: tags corrected

  6. #6
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Challenge: NextPower2 for LongInt

    I really like the idea of these code offs - my problem is that I just dont work at a low enough level! All my solutions will be working out the best/quickest method using only high level Pascal code (like the lookup solution).
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  7. #7

    Challenge: NextPower2 for LongInt

    isn't a longint the same as an integer?
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  8. #8

    Challenge: NextPower2 for LongInt

    Well, in Delphi this is true. In FPC it depends on the compiler mode. In modes objfpc and delphi it is the same as longint. In modes fpc, tp, macpas and it is the same as smallint.

    But, for all proposed solutions except the table lookup, it doesn't matter wether the integer is 16 or 32 bits.

  9. #9

    Challenge: NextPower2 for LongInt

    Quote Originally Posted by cairnswm
    I really like the idea of these code offs - my problem is that I just dont work at a low enough level! All my solutions will be working out the best/quickest method using only high level Pascal code (like the lookup solution).
    As we've seen, sometimes those can be better than an assembler rountine...
    My site: DelphiTuts.com (coming soon)...

    Download Font Studio 4.21 here.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •