Code: Select all
Function foo Naked cdecl ( ByVal x As Ulong ) As Double
Asm
mov edx, Dword Ptr [esp+4]
cvtsi2sd xmm0, edx
ret
End Asm
End Function
Print foo(66)
Sleep
Code: Select all
Function foo Naked cdecl ( ByVal x As Ulong ) As Double
Asm
mov edx, Dword Ptr [esp+4]
cvtsi2sd xmm0, edx
ret
End Asm
End Function
Print foo(66)
Sleep
Code: Select all
Function foo Naked cdecl ( ByVal x As Ulong ) As Double
Asm
' mov edx, Dword Ptr [esp+4]
' cvtsi2sd xmm0, edx
fild Dword Ptr [esp+4]
ret
End Asm
End Function
How?jj2007 wrote:Indeed, you can return a "naked" double via xmm0
deltarho[1859] wrote:Suppose after a few lines of code we get a scalar double result in xmm0 how do we put xmm0 into the FPU?
Code: Select all
Function foo1 Naked cdecl ( ByVal x As Ulong ) As Double
Asm
fild Dword Ptr [esp+4] ' simple...
ret
End Asm
End Function
Function foo2 Naked cdecl ( ByVal x As Ulong ) As Double
Asm
cvtsi2sd xmm0, dword Ptr [esp+4] ' get value as integer, convert to double
sub esp, 8 ' create a qword slot
movlps qword ptr [esp], xmm0 ' load double into memory
fld qword ptr [esp] ' transfer to the FPU
add esp, 8 ' correct the stack
ret
End Asm
End Function
Print "directly to the FPU:", foo1(12345)
Print "with acrobatics:", foo2(12345)
Sleep
Code: Select all
#macro fld_xmm0
sub esp, 8 ' create a qword slot
movlps qword ptr [esp], xmm0 ' load double into memory
fld qword ptr [esp] ' transfer to the FPU
add esp, 8
#endmacro
Function foo2 Naked cdecl ( ByVal x As Ulong ) As Double
Asm
cvtsi2sd xmm0, dword Ptr [esp+4]
fld_xmm0
ret
End Asm
End Function
Code: Select all
fld_xmm0
ret
Code: Select all
Function foo2 Naked ( ByVal x As Ulong ) As double
Asm
cvtsi2sd xmm0, ecx
ret
End Asm
End Function
print foo2(57897897)
sleep
Codesize-wise, it's identical:SARG wrote:With 64bit it's shorter.
Code: Select all
Function foo Naked cdecl ( ByVal x As Ulong ) As Double
Asm
int 3
#ifdef __FB_64BIT__
cvtsi2sd xmm0, ecx
#else
fild Dword Ptr [esp+4]
ret
#endif
nop
End Asm
End Function
Dim as double my123
asm int 3
my123=foo(1234567)
asm nop
asm nop
Print "here it is: ";my123
Sleep
marcov wrote:On Windows, XMM for FPU is default for 64-bit, not for 32-bit.
What he actually meant is "On Windows, XMM for returning doubles is default for 64-bit". The OS uses xmmregs for doubles. As a coder, you can use the fpu for whatever you like, though.deltarho[1859] wrote:Ah, I wondered what marcov was getting at.
I agree.jj2007 wrote:Codesize-wise, it's identical: