FreePascal 64 bit question

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Dann Corbit
Posts: 12542
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

FreePascal 64 bit question

Post by Dann Corbit »

According to this:
http://wiki.freepascal.org/FPC_New_Feat ... intrinsics

FreePascal should have intrinsics:
BsfQWord() and BsrQWord()

However, I can't seem to get the compiler to recognize them.

Do any FreePascal experts frequent this forum and can you tell me how to get them to work? I want to make a 64 bit version of Booot.

If need be, I can use these primitives to build my own versions:
not Bitwise negation (unary)
and Bitwise and
or Bitwise or
xor Bitwise xor
shl Bitwise shift to the left
shr Bitwise shift to the right

but I would much rather use the intrinsics if possible.
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: FreePascal 64 bit question

Post by JuLieN »

Dann, this is a quick search answer, as I'm off to bed now, but here is a sample program that use these functions I found. Hopefully it will help :

Code: Select all

program testbitscan;

function test_byte: boolean;
var
  x8,f,r: byte;
  i: integer;
begin
  for i:=0 to 7 do
  begin
    x8:=1 shl i;
    f:=BsfByte(x8);
    if &#40;f<>i&#41; then
    begin
      writeln&#40;'BsfByte&#40;',x8,') returned ',f,', should be ',i&#41;;
      exit&#40;false&#41;;
    end;
    r&#58;=BsrByte&#40;x8&#41;;
    if r<>i then
    begin
      writeln&#40;'BsrByte&#40;',x8,') returned ',f,', should be ',i&#41;;
      exit&#40;false&#41;;
    end;
  end;
  result&#58;=true;
end;

function test_word&#58; boolean;
var
  x16&#58; word;
  i,f,r&#58; integer;
begin
  for i&#58;=0 to 15 do
  begin
    x16&#58;=1 shl i;
    f&#58;=BsfWord&#40;x16&#41;;
    if &#40;f<>i&#41; then
    begin
      writeln&#40;'BsfWord&#40;',x16,') returned ',f,', should be ',i&#41;;
      exit&#40;false&#41;;
    end;
    r&#58;=BsrWord&#40;x16&#41;;
    if r<>i then
    begin
      writeln&#40;'BsrWord&#40;',x16,') returned ',f,', should be ',i&#41;;
      exit&#40;false&#41;;
    end;
  end;
  result&#58;=true;
end;

function test_dword&#58; boolean;
var
  x32&#58; cardinal;
  i,f,r&#58; integer;
begin
  for i&#58;=0 to 31 do
  begin
    x32&#58;=1 shl i;
    f&#58;=BsfDWord&#40;x32&#41;;
    if &#40;f<>i&#41; then
    begin
      writeln&#40;'BsfDWord&#40;',x32,') returned ',f,', should be ',i&#41;;
      exit&#40;false&#41;;
    end;
    r&#58;=BsrDWord&#40;x32&#41;;
    if r<>i then
    begin
      writeln&#40;'BsrDWord&#40;',x32,') returned ',f,', should be ',i&#41;;
      exit&#40;false&#41;;
    end;
  end;
  result&#58;=true;
end;

function test_qword&#58; boolean;
var
  x64&#58; qword;
  i, f, r&#58; integer;
begin
  for i&#58;=0 to 63 do
  begin
    x64&#58;=uint64&#40;1&#41; shl i;
    f&#58;=BsfQWord&#40;x64&#41;;
    if f<>i then begin
      writeln&#40;'BsfQWord&#40;',x64,') returned ',f,', should be ',i&#41;;
      exit&#40;false&#41;;
    end;
    r&#58;=BsrQWord&#40;x64&#41;;
    if r<>i then begin
      writeln&#40;'BsrQWord&#40;',x64,') returned ',r,', should be ',i&#41;;
      exit&#40;false&#41;;
    end;
  end;
  result&#58;=true;
end;

begin
  if test_byte then writeln&#40;'passed') else writeln&#40;'failed');
  if test_word then writeln&#40;'passed') else writeln&#40;'failed');
  if test_dword then writeln&#40;'passed') else writeln&#40;'failed');
  if test_qword then writeln&#40;'passed') else writeln&#40;'failed');
end.
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: FreePascal 64 bit question

Post by JuLieN »

Hmmm... ok, so the problem is : in which unit are those functions found, so you can add it to your "uses" clause? Well... I can't find it either :(
And the documentation about that is just non-existant at all. All I can say is that it's not in the LCL. Weird. :(
I guess you'll have to emulate them.

EDIT: Yahoo's cache has a reference to these functions in the free pascal. It might save you some time to copy/paste this code :

http://74.6.239.67/search/cache?ei=UTF- ... B3xcFhWA--

For instance :

Code: Select all

function BsfQWord&#40;Const AValue &#58; QWord&#41;&#58; cardinal; assembler; nostackframe;
asm
     bsfl    4&#40;%esp&#41;,%eax
     jnz     .L2
.L1&#58; bsfl    8&#40;%esp&#41;,%eax
     add     $32,%eax
.L2&#58;
end;

function BsrQWord&#40;Const AValue &#58; QWord&#41;&#58; cardinal; assembler; nostackframe;
asm
     bsrl    8&#40;%esp&#41;,%eax
     jz     .L1
     add     $32,%eax
     jmp     .L2
.L1&#58; bsrl    4&#40;%esp&#41;,%eax
.L2&#58;
end;
But that's in asm, and you probably don't want that ;)
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
User avatar
rvida
Posts: 481
Joined: Thu Apr 16, 2009 12:00 pm
Location: Slovakia, EU

Re: FreePascal 64 bit question

Post by rvida »

Hi,

You probably need a more recent version of freepascal, at least FPC 2.5.1 revision 16174 or newer. The patch implementing bitscan intrinsics was submitted 2010-10-09 and merged to trunk 2010-10-16. More details on freepascal bugtracker. (By the way, I am the author of this patch ;))

Richard
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: FreePascal 64 bit question

Post by JuLieN »

@Richard

I say, this is a small world! :lol:

Btw, thanks for Critter, you are the answer to C/C++ programmers looking down at us Pascal lovers :)
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
Dann Corbit
Posts: 12542
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: FreePascal 64 bit question

Post by Dann Corbit »

Where can the most recent versions be downloaded?
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: FreePascal 64 bit question

Post by JuLieN »

Dann Corbit wrote:Where can the most recent versions be downloaded?
There : http://www.hu.freepascal.org/lazarus/

(I always use the daily builds, that way I can blame the FPC/Lazarus team for Prédateur's bugs.)
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
Dann Corbit
Posts: 12542
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: FreePascal 64 bit question

Post by Dann Corbit »

JuLieN wrote:
Dann Corbit wrote:Where can the most recent versions be downloaded?
There : http://www.hu.freepascal.org/lazarus/

(I always use the daily builds, that way I can blame the FPC/Lazarus team for Prédateur's bugs.)
The latest one there for Windows 64 bit appears to be 2.4.3 and Richard said I would need FPC 2.5.1 revision 16174 or newer so I am not sure if it will work
User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

Re: FreePascal 64 bit question

Post by JuLieN »

Dann Corbit wrote: The latest one there for Windows 64 bit appears to be 2.4.3 and Richard said I would need FPC 2.5.1 revision 16174 or newer so I am not sure if it will work
Oh, sorry, my mistake. A quick search with people having the same concerns points to Code Typhoon, presumably always up to date to the latest betas of FPC and Lazarus :

http://www.pilotlogic.com/sitejoom/inde ... Itemid=147

I didn't try it, as I always download Lazarus from the link I gave you, but as far as I can see it looks big and serious.

Please report how it goes if you try it, so maybe I'll switch to it. :)

EDIT: I found that FreePascal's official repository is more up to date than Lazaru's one. Weird. here's the link to the latests official win64 Lazarus 0.9.31 + FPC 2.5.1 installer :
http://ftp.freepascal.org/snapshots/Laz ... -win64.exe
Last edited by JuLieN on Fri Jan 21, 2011 9:12 pm, edited 1 time in total.
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
User avatar
Steve Maughan
Posts: 1221
Joined: Wed Mar 08, 2006 8:28 pm
Location: Florida, USA

Re: FreePascal 64 bit question

Post by Steve Maughan »

Hi Richard,

I'm a big fan of Delphi. How does FreePascal compare in terms of speed to Delphi. Is FreePascal 64 significantly faster than Delphi 32 bit for bitboard type programs?

Thanks,

Steve