Page 1 of 2
FreePascal 64 bit question
Posted: Fri Jan 21, 2011 2:21 am
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.
Re: FreePascal 64 bit question
Posted: Fri Jan 21, 2011 2:29 am
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 (f<>i) then
begin
writeln('BsfByte(',x8,') returned ',f,', should be ',i);
exit(false);
end;
r:=BsrByte(x8);
if r<>i then
begin
writeln('BsrByte(',x8,') returned ',f,', should be ',i);
exit(false);
end;
end;
result:=true;
end;
function test_word: boolean;
var
x16: word;
i,f,r: integer;
begin
for i:=0 to 15 do
begin
x16:=1 shl i;
f:=BsfWord(x16);
if (f<>i) then
begin
writeln('BsfWord(',x16,') returned ',f,', should be ',i);
exit(false);
end;
r:=BsrWord(x16);
if r<>i then
begin
writeln('BsrWord(',x16,') returned ',f,', should be ',i);
exit(false);
end;
end;
result:=true;
end;
function test_dword: boolean;
var
x32: cardinal;
i,f,r: integer;
begin
for i:=0 to 31 do
begin
x32:=1 shl i;
f:=BsfDWord(x32);
if (f<>i) then
begin
writeln('BsfDWord(',x32,') returned ',f,', should be ',i);
exit(false);
end;
r:=BsrDWord(x32);
if r<>i then
begin
writeln('BsrDWord(',x32,') returned ',f,', should be ',i);
exit(false);
end;
end;
result:=true;
end;
function test_qword: boolean;
var
x64: qword;
i, f, r: integer;
begin
for i:=0 to 63 do
begin
x64:=uint64(1) shl i;
f:=BsfQWord(x64);
if f<>i then begin
writeln('BsfQWord(',x64,') returned ',f,', should be ',i);
exit(false);
end;
r:=BsrQWord(x64);
if r<>i then begin
writeln('BsrQWord(',x64,') returned ',r,', should be ',i);
exit(false);
end;
end;
result:=true;
end;
begin
if test_byte then writeln('passed') else writeln('failed');
if test_word then writeln('passed') else writeln('failed');
if test_dword then writeln('passed') else writeln('failed');
if test_qword then writeln('passed') else writeln('failed');
end.
Re: FreePascal 64 bit question
Posted: Fri Jan 21, 2011 2:52 am
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(Const AValue : QWord): cardinal; assembler; nostackframe;
asm
bsfl 4(%esp),%eax
jnz .L2
.L1: bsfl 8(%esp),%eax
add $32,%eax
.L2:
end;
function BsrQWord(Const AValue : QWord): cardinal; assembler; nostackframe;
asm
bsrl 8(%esp),%eax
jz .L1
add $32,%eax
jmp .L2
.L1: bsrl 4(%esp),%eax
.L2:
end;
But that's in asm, and you probably don't want that

Re: FreePascal 64 bit question
Posted: Fri Jan 21, 2011 5:23 am
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
Re: FreePascal 64 bit question
Posted: Fri Jan 21, 2011 10:47 am
by JuLieN
@Richard
I say, this is a small world!
Btw, thanks for Critter, you are the answer to C/C++ programmers looking down at us Pascal lovers

Re: FreePascal 64 bit question
Posted: Fri Jan 21, 2011 8:39 pm
by Dann Corbit
Where can the most recent versions be downloaded?
Re: FreePascal 64 bit question
Posted: Fri Jan 21, 2011 8:48 pm
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.)
Re: FreePascal 64 bit question
Posted: Fri Jan 21, 2011 9:00 pm
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
Re: FreePascal 64 bit question
Posted: Fri Jan 21, 2011 9:04 pm
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
Re: FreePascal 64 bit question
Posted: Fri Jan 21, 2011 9:12 pm
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