-gen clang

General FreeBASIC programming questions.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

This is the command line that I am using with clang.

-asm att -w all -arch native -gen clang -Wc -O2

With gcc '-arch native' impacted on performance on my machine. srvaldez found it to be faster, some time ago, on his machine. No idea why.

With gcc/clang I get better performance with '-arch native'. No idea why.

With gcc -O3 is unpredictable. More often than not, -O3 is not faster and produces larger binaries.

With gcc/clang -O3 tends to be faster. The internet confirms this.

Adding '-fpmode fast -fpu sse' also works.

So, you can use:

-asm att -w all -fpmode fast -fpu sse -arch native -gen clang -Wc -O3

With some code, you will need to wear a seat belt. I say 'some' because I reckon gcc/clang will not have the edge on gcc most of the time.

Added: My fbc 1.20.0/9.3.0 toolchain is 322 MiB (4786 files) The fbc 1.20.0/9.3.0/clang toolchain is 554 MiB (4777 files); there are some big bruisers in there. :)

:)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: -gen clang

Post by dodicat »

srvaldez wrote: ↑Mar 11, 2024 14:59 the following program compiles ok in 32-bit without -asm att
the matrix multiplication is from the Rosetta code, but the demo is mine, it shows how drastically the precision is lost

Code: Select all

type Matrix
    dim as double m( any , any )
    declare constructor ( )
    declare constructor ( byval x as uinteger , byval y as uinteger )
end type

constructor Matrix ( )
end constructor

constructor Matrix ( byval x as uinteger , byval y as uinteger )
    redim this.m( x - 1 , y - 1 )
end constructor

operator * ( byref a as Matrix , byref b as Matrix ) as Matrix
    dim as Matrix ret
    dim as uinteger i, j, k
    if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
        redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
        for i = 0 to ubound( a.m , 1 )
            for j = 0 to ubound( b.m , 2 )
                for k = 0 to ubound( b.m , 1 )
                    ret.m( i , j ) += a.m( i , k ) * b.m( k , j )
                next k
            next j
        next i
    end if
    return ret
end operator

'some garbage matrices for demonstration
dim as Matrix a = Matrix(4 , 4)
a.m(0 , 0) = 1 : a.m(0 , 1) = 1 : a.m(0 , 2) = 1 : a.m(0 , 3) = 1
a.m(1 , 0) = 2 : a.m(1 , 1) = 4 : a.m(1 , 2) = 8 : a.m(1 , 3) = 16
a.m(2 , 0) = 3 : a.m(2 , 1) = 9 : a.m(2 , 2) = 27 : a.m(2 , 3) = 81
a.m(3 , 0) = 4 : a.m(3 , 1) = 16 : a.m(3 , 2) = 64 : a.m(3 , 3) = 256
dim as Matrix b = Matrix( 4 , 4 )
b.m(0 , 0) = 4 : b.m(0 , 1) = -3 : b.m(0 , 2) = 4/3 : b.m (0, 3) = -1/4
b.m(1 , 0) = -13/3 : b.m(1 , 1) = 19/4 : b.m(1 , 2) = -7/3 : b.m (1, 3) = 11/24
b.m(2 , 0) = 3/2 : b.m(2 , 1) = -2 : b.m(2 , 2) = 7/6 : b.m (2, 3) = -1/4
b.m(3 , 0) = -1/6 : b.m(3 , 1) = 1/4 : b.m(3 , 2) = -1/6 : b.m (3, 3) = 1/24
dim as Matrix c = a * a * b
print c.m(0, 0), c.m(0, 1), c.m(0, 2), c.m(0, 3)
print c.m(1, 0), c.m(1, 1), c.m(1, 2), c.m(1, 3)
print c.m(2, 0), c.m(2, 1), c.m(2, 2), c.m(2, 3)
print c.m(3, 0), c.m(3, 1), c.m(3, 2), c.m(3, 3)
?"=============================================="
for i as long=1 to 4
	c = c * c * b
	print c.m(0, 0), c.m(0, 1), c.m(0, 2), c.m(0, 3)
	print c.m(1, 0), c.m(1, 1), c.m(1, 2), c.m(1, 3)
	print c.m(2, 0), c.m(2, 1), c.m(2, 2), c.m(2, 3)
	print c.m(3, 0), c.m(3, 1), c.m(3, 2), c.m(3, 3)
	?"=============================================="
next
Hi srvaldez.
I made up a fractions udt with operators.
It should keep values, it only uses integers.
I kept your matrix type, but had to tweak the multiply slightly to suit type fraction.
All for fun, just runs with no flags in fbc.exe.

Code: Select all


Type fraction
    As String n,d 'numerator,denominator
    Declare Constructor
    Declare Constructor(As String)
    Declare Operator Let(As String)
    Declare Operator Cast() As String
    Declare Function Val() As Double
    #define f Type<fraction>
End Type

Sub Remove(Text As String,Char As String)
    Var index = 0,asci=Asc(char)
    For i As Integer = 0 To Len(Text) - 1
        If Text[i] <> asci Then Text[index] = Text[i] : index =index+ 1
    Next : Text = Left(Text,index)
End Sub

Function hcf(a As Longint,b As Longint) As Longint
 a=abs(a):b=abs(b)
    If b>a Then Swap b,a
    Dim As Longint c
    While b
        c = a
        a = b
        b = c Mod b
    Wend
    Return a
End Function

Constructor fraction
End Constructor

Constructor fraction (s As String)
remove(s," ")  'keep tight,optional if tight anyway
Var i=Instr(s,"/")
n=Mid(s,1,i-1)
d=Mid(s,i+1)
If Instr(s,"/")=0 Then n=s:d="1"
End Constructor

Operator fraction.let(s As String)
Var i=Instr(s,"/")
n=Mid(s,1,i-1)
d=Mid(s,i+1)
End Operator

Operator fraction.cast() As String
if valint(n)<0 and valint(d)<0 then n=ltrim(n,"-"):d=ltrim(d,"-")
If Instr(d,"-") Then d=Ltrim(d,"-"):n="-"+n
Return Rtrim(n+"/"+d,"/1")
End Operator

Function fraction.val() As Double
    Return Valint(n)/Valint(d)
End Function

Operator +(a As fraction,b As fraction) As fraction
Var p=(Valint(a.d)*Valint(b.d))
Var n1=Valint(a.n)*Valint(b.d)
Var n2=Valint(b.n)*Valint(a.d)
Var n3=n1+n2
Var h= hcf(n3,p)
If h>1 Then
    n3/=h:p/=h
End If
Return Str(n3)+"/"+Str(p)
End Operator


Operator -(a As fraction,b As fraction) As fraction
Var p=(Valint(a.d)*Valint(b.d))
Var n1=Valint(a.n)*Valint(b.d)
Var n2=Valint(b.n)*Valint(a.d)
Var n3=n1-n2
Var h= hcf(n3,p)
If h>1 Then
    n3/=h:p/=h
End If
Return Str(n3)+"/"+Str(p)
End Operator

Operator *(a As fraction,b As fraction) As fraction
Var n2=Valint(a.n)*Valint(b.n)
Var d2=Valint(a.d)*Valint(b.d)
Var h= hcf(Abs(n2),Abs(d2))
n2/=h:d2/=h
Return Str(n2)+"/"+Str(d2)
End Operator

Operator /(a As fraction,b As fraction) As fraction
Swap b.n,b.d
Return a*b
End Operator

'=============  srvaldez ==============
Type Matrix
    Dim As fraction m( Any , Any )
    Declare Constructor ( )
    Declare Constructor ( Byval x As Uinteger , Byval y As Uinteger )
End Type

Constructor Matrix ( )
End Constructor

Constructor Matrix ( Byval x As Uinteger ,Byval y As Uinteger )
Redim this.m( x - 1 , y - 1 )
End Constructor

Operator *(m1 As matrix,m2 As matrix) As matrix 
Dim rows As Integer=Ubound(m1.m,1)
Dim columns As Integer=Ubound(m2.m,2)
If Ubound(m1.m,2)<>Ubound(m2.m,1) Then
    Print "Can't do multiply"
    Exit Operator
End If
Dim As matrix ans=matrix(rows+1,columns+1)
Dim rxc As fraction
For r As Integer=0 To rows
    For c As Integer=0 To columns
        rxc=f("0")
        For k As Integer = 0 To Ubound(m1.m,2)
            rxc=rxc+(m1.m(r,k)*m2.m(k,c))
        Next k
        ans.m(r,c)=rxc
    Next c
Next r
Operator= ans
End Operator


'some garbage matrices for demonstration
Dim As Matrix a = Matrix(4 , 4)
a.m(0 , 0) = f("1") : a.m(0 , 1) = f("1") : a.m(0 , 2) = f("1") : a.m(0 , 3) = f("1")
a.m(1 , 0) = f("2") : a.m(1 , 1) = f("4") : a.m(1 , 2) = f("8") : a.m(1 , 3) = f("16")
a.m(2 , 0) = f("3") : a.m(2 , 1) = f("9") : a.m(2 , 2) = f("27"): a.m(2 , 3) = f("81")
a.m(3 , 0) = f("4") : a.m(3 , 1) = f("16") : a.m(3 , 2) = f("64") : a.m(3 , 3) = f("256")
Dim As Matrix b = Matrix( 4 , 4 )
b.m(0 , 0) = f("4") : b.m(0 , 1) = f("-3") : b.m(0 , 2) = f("4/3") : b.m (0, 3) = f("-1/4")
b.m(1 , 0) = f("-13/3") : b.m(1 , 1) = f("19/4") : b.m(1 , 2) = f("-7/3") : b.m (1, 3) = f("11/24")
b.m(2 , 0) = f("3/2") : b.m(2 , 1) = f("-2") : b.m(2 , 2) = f("7/6") : b.m (2, 3) = f("-1/4")
b.m(3 , 0) = f("-1/6") : b.m(3 , 1) = f("1/4") : b.m(3 , 2) = f("-1/6") : b.m (3, 3) = f("1/24")

Dim As Matrix c = a * a * b
Print c.m(0, 0), c.m(0, 1), c.m(0, 2), c.m(0, 3)
Print c.m(1, 0), c.m(1, 1), c.m(1, 2), c.m(1, 3)
Print c.m(2, 0), c.m(2, 1), c.m(2, 2), c.m(2, 3)
Print c.m(3, 0), c.m(3, 1), c.m(3, 2), c.m(3, 3)
?"=============================================="
For i As Long=1 To 4
	c = c * c * b
	Print c.m(0, 0), c.m(0, 1), c.m(0, 2), c.m(0, 3)
	Print c.m(1, 0), c.m(1, 1), c.m(1, 2), c.m(1, 3)
	Print c.m(2, 0), c.m(2, 1), c.m(2, 2), c.m(2, 3)
	Print c.m(3, 0), c.m(3, 1), c.m(3, 2), c.m(3, 3)
	?"=============================================="
Next
Sleep 
πŸ’•
Last edited by dodicat on Mar 15, 2024 2:07, edited 2 times in total.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

thank you deltarho[1859] for your comments
thank you dodicat, it's good to share ideas and code
btw, on Windows if you press the Windows key and the . key then you will get a popup dialog with some emoji that you can use on this forum, example
πŸ‘πŸ˜πŸ‘Œ
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

srvaldez wrote:btw, on Windows...
With the focus on my next character to type, I opened the emoji picker and clicked on a smilie, but nothing was inserted into the forum.

I'm on the latest Win10 update.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

maybe it's the browser?
I normally use Firefox but I am posting this from the edge browser 🀠 seems to work for me
I see that it works for dodicat 😁
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

I tried Edge when it failed to work in Firefox but no joy.

You're on Win 11. It is supposed to work on Win10 - perhaps it doesn't. :(
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

regarding the seeding function by deltarho[1859] in his MsWsII.bas code viewtopic.php?t=31616&hilit=MsWsII.bas
I did some tests and something is wrong, the following works with clang but not with gcc

Code: Select all

#cmdline "-asm att -w all -arch native -gen clang -Wc -O2"

Function HammingSeed64() As Uint64
    Dim As Uint32 tscSeed0, tscSeed1, numBits
    Dim As Uint64 Seed, CopySeed
    
    While Not ((numBits > 30) And (numBits < 34)) ' Gd quality entropy - we don't need exactly 32
        Asm	
            "rdtsc \n" _
            "movl %%eax, %%ecx \n" _
            "bswap %%eax \n" _ ' fast moving to upper bits
            "movl %%eax, %[TSCSEED0$1] \n" _
            "Addl $1073741824, %%ecx \n" _  ' A quarter of a spin
            "bswap %%ecx \n" _ ' fast moving to upper bits
            "movl %%ecx, %[TSCSEED1$1] \n" _
            : _
            :[tscSeed0]"m"(tscSeed0), [tscSeed1]"m"(tscSeed1) _
            :"eax", "ecx"
        End Asm
        '? tscSeed0, tscSeed1  '<------------- if you uncomment this print statement then it works with gcc 
        Seed = (Cast( Uint64, tscSeed0 ) Shl 32) Or Cast( Uint64, tscSeed1 ) 
        CopySeed = Seed : numBits = 0
        While CopySeed <> 0
            CopySeed = CopySeed And CopySeed - 1
            numBits += 1
        Wend
    Wend
    Return Seed
End Function

? HammingSeed64()
with the print statement uncommented it works with gcc otherwise it goes into never-never land
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

I then tried with a helper naked function, the 64-bit works with gcc but not the 32-bit, both work with clang, can you spot the error in my code?

Code: Select all

#cmdline "-asm att -w all -arch native -gen clang -Wc -O2"
#ifdef __FB_WIN32__
   #ifdef __FB_64BIT__
        Function HammingSeed64a naked cdecl(Byref tscSeed0 As Ulong, Byref tscSeed1 As Ulong) As Ulong
            Asm
                "push %r8"
                "push %r9"
                "movq %rcx, %r8"
                "movq %rdx, %r9"
                "rdtsc"
                "mov     %eax, %ecx"
                "bswap %eax"
                "movl %eax, (%r8)"
                "addl $1073741824, %ecx"
                "bswap %ecx"
                "movl %ecx, (%r9)"
                "movq %r8, %rcx"
                "movq %r9, %rdx"
                "pop %r9"
                "pop %r8"
                "ret"
            End Asm
        End Function
    #else
        Function HammingSeed64a naked cdecl(Byref tscSeed0 As Ulong, Byref tscSeed1 As Ulong) As Ulong
            Asm
                "push    %ecx"
                "push    %edx"
                "rdtsc"
                "mov     %eax, %ecx"
                "bswap %eax"
                "movl %eax, 20(%esp)"
                "addl $1073741824, %ecx"
                "bswap %ecx"
                "movl %ecx, 24(%esp)"
                "pop     %edx"
                "pop     %ecx"
                "ret"
            End Asm
        End Function
    #endif
#endif

Function HammingSeed64() As Uint64
    Dim As Uint32 tscSeed0, tscSeed1, numBits
    Dim As Uint64 Seed, CopySeed
    
    While Not ((numBits > 30) And (numBits < 34)) ' Gd quality entropy - we don't need exactly 32
        HammingSeed64a(tscSeed0, tscSeed1)
        '? tscSeed0, tscSeed1
        Seed = (Cast( Uint64, tscSeed0 ) Shl 32) Or Cast( Uint64, tscSeed1 ) 
        CopySeed = Seed : numBits = 0
        While CopySeed <> 0
            CopySeed = CopySeed And CopySeed - 1
            numBits += 1
        Wend
    Wend
    Return Seed
End Function

? HammingSeed64()

Sleep
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

What I did was to keep the original gcc code, in MsWsII, and then added the gcc/clang code.

I then wrote this in MsWsII.bas

#ifdef clang
<gcc/clang code>
#else
<gcc code>
#endif

In my plot.bas with '#define clang' before including MsWsII.bas and using the clang command line, it works.

Commenting the '#define clang' and using the gcc command line, that worked.

All worked with 32-bit or 64-bit without changing the respective command lines.

-------------------------------

With the emoji saga there was need for a registry addition. Still didn't work. Obviously, I'm adding the key in the wrong place. Microsoft - why the hell do I have to do that? I will make it work if it kills me. :)
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

This is the nearest that I have got to getting the emoji picker to work.

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Input\Settings\proc_1\loc_0809\im_1]
"EnableExpressiveInputShellHotkey"=dword:00000001

But that does not work. I had to search for loc_0809 for the English keyboard. The US keyboard is loc_0409.

Come on Microsoft β€” we should not have to do this. When I boot up, you should know what to do. "EnableExpressiveInputShellHotkey" didn't even exist.

@srvaldez

Can you do a registry search for "EnableExpressiveInputShellHotkey"?

If you find it, would you kindly let me know where you found it?

If you cannot find it, then this problem may very well kill me. :)
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

I got the emoji picker working.

I changed 'Touch Keyboard and Handwriting Panel Service' to Automatic from Disabled. However, when I went back to Services (Local) it was on Manual and the status was running.

WHAT???

What the blazes does that service have to do with the emoji picker?

Instead of the picker popping up and the bottom right of my desktop, it pops up to above the next character entry.

Anyway, thanks for the tip, srvaldez.

πŸ‘Œ

To Microsoft 😝

It didn't kill me after all. :D

Added: I had "EnableExpressiveInputShellHotkey" in four different places altogether. I deleted all of them. Did a registry search and it wasn't found. Restarted and the picker still works. That is why I didn't have it in the first place. 'Yer gotta' laugh. It took a while to remove the blood from my keyboard, and it may be a day or two before I can remove the bandage. πŸ€•

Back to clang. No need as fbc 1.20.0/gcc 9.3.0/clang is working a treat; for me and the author of the toolchain. Can I hand out the toolchain? Not without permission I cannot and that may not be forthcoming. I will have a word, but I probably will not be able to disclose the author.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

@srvaldez

I should have mentioned that both sets of code here work fine with fbc 1.20.0/gcc 9.3.0/clang.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

deltarho[1859], I recommend that you use the basic version of the function HammingSeed64, inline asm in FB is like playing the Russian roulette, it's totally unreliable, there might be an exception if you strictly use gcc extended asm but I could never wrap my head around it's intricacies, example one liner in C

Code: Select all

    unsigned long lo, hi;
    asm( "rdtsc" : "=a" (lo), "=d" (hi));
    return lo;
<edit>
but from my tests, inline asm with clang seems quite reliable, though sometimes puzzling, for example

Code: Select all

        Function HammingSeed64a cdecl(Byref tscSeed0 As Ulong, Byref tscSeed1 As Ulong) As Ulong
            Asm
                "rdtsc \n" _
                "movl %%eax, %%ecx \n" _
                "bswap %%eax \n" _
                "movl %[TSCSEED0$1], %%edx \n" _
                "movl %%eax, (%%edx) \n" _
                "addl $1073741824, %%ecx \n" _
                "bswap %%ecx \n" _
                "movl %[TSCSEED1$1], %%edx \n" _
                "movl %%ecx, (%%edx) \n" _
                : _
                :[tscSeed0]"m"(tscSeed0), [tscSeed1]"m"(tscSeed1) _
                :"eax", "ecx", "edx"
            End Asm
            return 0
        End Function
I had to move the address of the parameters into edx in order to store values in them
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

Hi srvaldez.

I cannot actually use HammingSeed64a. Firstly, I need a Uint64 return value. Secondly, if a time stamp fails a Hamming test, we go back and read the time stamp again. HammingSeed64a only passes the asm block once. I could try to adapt it, but I'd be in dangerous territory, not understanding att code.

I looked at the code and have a vague idea what is going on by comparing it with intel version. I'll stay with your previous clang version, which appears to be working OK.

Thanks.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

Hi deltarho[1859]
that function was only an example, it was intended as helper function if one wanted to go that route but the all-in-one looks better
Post Reply