Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-03

IFDEF usage in x86_64's SwapEndian() implementation

6 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

Here is the x86_64's SwapEndian() function implementation.

=============[ src/rtl/x86_64/x86_64.inc ]==================== function SwapEndian(const AValue: DWord): DWord; assembler; nostackframe; asm
{$ifdef win64}
movl %ecx, %eax
{$else win64}
movl %edi, %eax
{$endif win64}
bswap %eax
end;
=============================================

Surely the usage of IFDEF WIN64 is not 100% correct or accurate. Should the above not rather read IFDEF CPU64?

Regards,
- Graeme -

Reply by email

Bart
to Graeme Geldenhuys
Graeme Geldenhuys wrote
Surely the usage of IFDEF WIN64 is not 100% correct or accurate. Should the above not rather read IFDEF CPU64?

Me thinks:
It's in ($pfcsource)rtl\x86_64\x86_64.inc, so CPU64 will be defined in that procedure always.
So this code distincts between 64-bit Windows and 64-bit "any other OS"

Bart

Reply by email

Graeme Geldenhuys
to Bart
Bart wrote
So this code distincts between 64-bit Windows and 64-bit "any other OS"

Say for example I have two identical Intel i7 based systems. One with 64-bit FreeBSD installed, and one with 64-bit Win7. What you are saying is that SwapEndian() will execute different code based on the two systems. That doesn't sound right to me. Both systems have the same CPU type, thus same Endianness.

Regards,
- Graeme -

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys: Here is the x86_64's SwapEndian() function implementation. =============[…
Graeme Geldenhuys wrote 18 lines
Hi, Here is the x86_64's SwapEndian() function implementation. =============[ src/rtl/x86_64/x86_64.inc ]==================== function SwapEndian(const AValue: DWord): DWord; assembler; nostackframe; asm {$ifdef win64} movl %ecx, %eax {$else win64} movl %edi, %eax {$endif win64} bswap %eax end; ============================================= Surely the usage of IFDEF WIN64 is not 100% correct or accurate. Should the above not rather read IFDEF CPU64?

No, the code is exclusively for all 64-bit platforms.

The code is there to differentiate between the windows ABI and other ABIS, which specify in what register the AValue parameter is passed.

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
The code is there to differentiate between the windows ABI and other ABIS, which specify in what register the AValue parameter is passed.

Ah okay. One could possibly write it in a less ambiguous way, as follows? No IFDEF's needed.

function SwapEndian(const AValue: DWord): DWord; assembler; nostackframe; asm
movl AValue, %eax
bswap %eax
end;

The generated assembler seems to be identical to the original code (at least on my FreeBSD system).

.Lc1:
# Var AValue located in register edi
# Var $result located in register eax
# [project1.lpr]
# [10] asm
# [11] movl AValue, %eax
movl %edi,%eax
# [12] bswap %eax
bswapl %eax
# [13] end;
ret
.Lc2:

Regards,
- Graeme -

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 12 lines
On 2016-03-30 11:58, Michael Van Canneyt wrote: > The code is there to differentiate between the windows ABI and other ABIS, > which specify in what register the AValue parameter is passed. Ah okay. One could possibly write it in a less ambiguous way, as follows? No IFDEF's needed. function SwapEndian(const AValue: DWord): DWord; assembler; nostackframe; asm movl AValue, %eax bswap %eax end;

I am not proficient enough in the assembler parts of the compiler to comment on this. Maybe one of the compiler people knows why it is the way it is...

Michael.

Reply by email