strange behaviour with naked function

New to FreeBASIC? Post your questions here.
Post Reply
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

strange behaviour with naked function

Post by srvaldez »

in reference to viewtopic.php?t=31711
I tried writing a naked version of this function

Code: Select all

Function BytSwap8( Byval qw As Ulongint ) As Ulongint
    Dim Swap8Mask As Const Ulongint = &H0607040502030001
    Asm
        movq mm1, [Swap8Mask]
        movq mm0, [qw]
        pshufb mm0, mm1	        ' Swap8Mask
        movq [Function], mm0
        emms
    End Asm
End Function

'my naked version

Sub BytSwap8n Naked Cdecl( Byref result As Ulongint, Byref qw As Ulongint, Byref Swap8Mask As Ulongint = &H0607040502030001ull )
    Asm
        mov eax,[esp+12]
        movq mm1, [eax]
        mov eax,[esp+8]
        movq mm0, [eax]
        pshufb mm0, mm1         ' Swap8Mask
        mov eax,[esp+4]
        movq [eax], mm0
        emms
        ret
    End Asm
End Sub

'simple benchmark

Dim As Ulongint i, r
Dim As Double t, sum

Print"starting"
sum=0
t=Timer
For i=1 To 80000000
    sum-=BytSwap8(i)
    sum+=BytSwap8(i+1)
Next
t=timer-t
Print sum
Print t

Print"starting"
sum=0
t=Timer
For i=1 To 80000000
    BytSwap8n(r, i)
    sum-=r
    BytSwap8n(r,i+1)
    sum+=r
Next
t=timer-t
Print sum
Print t
Sleep
if I don't use cdecl it will crash, also if you comment-out the regular BytSwap8 function and then run the program there's a significant delay before the message "starting" is displayed
what is wrong here?
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: strange behaviour with naked function

Post by adeyblue »

The default calling convention on Windows is stdcall, so you need to
ret <amount of bytes of arguments>
to pop the arguments off the stack and balance it.

On Linux it's cdecl, so you don't.
Last edited by adeyblue on Jun 13, 2022 16:53, edited 1 time in total.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: strange behaviour with naked function

Post by fxm »

Have you looked at the first example on the Naked documentation page which compare for a similar function the 3 types of calling conventions with their constraints ?
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: strange behaviour with naked function

Post by srvaldez »

thanks all, it's obvious that I need to take into account the calling convention in dealing with the function arguments
but one question remains, why the 3 second delay before displaying "starting" ?

Code: Select all

Sub BytSwap8n Naked Cdecl( Byref result As Ulongint, Byref qw As Ulongint, Byref Swap8Mask As Ulongint = &H0607040502030001ull )
    Asm
        mov eax,[esp+12]
        movq mm1, [eax]
        mov eax,[esp+8]
        movq mm0, [eax]
        pshufb mm0, mm1         ' Swap8Mask
        mov eax,[esp+4]
        movq [eax], mm0
        emms
        ret
    End Asm
End Sub

'simple benchmark

Dim As Ulongint i, r
Dim As Double t, sum

Print"starting"
sum=0
t=Timer
For i=1 To 80000000
    BytSwap8n(r, i)
    sum-=r
    BytSwap8n(r,i+1)
    sum+=r
Next
t=timer-t
Print sum
Print t
Sleep
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: strange behaviour with naked function

Post by fxm »

Due to anti-virus.
Post Reply