-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] »

srvaldez wrote:,,,if one wanted to go that route...
I have written quite a bit of Z80, Motorola 68000 and Intel. It was not a difficult transition. I cannot see myself getting into att. Whoever designed att at AT&T needs locking up, unless they were locked up when they designed it. :)
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

deltarho[1859], here's my suggestion, use as little asm as possible

Code: Select all

#if __FB_ASM__ = "intel"
    function rdtsc naked cdecl() as ulong
        asm
            rdtsc
            ret
        end asm
    end function
#else
    function rdtsc naked cdecl() as ulong
        asm "rdtsc \n ret"
    end function
#endif

' ***********************************
' Seeding functions

Function HammingSeed64() As Uint64
Dim As Uint32 tscSeed0, tscSeed1, numBits, m, n
Dim As Uint64 Seed, CopySeed
    
    While Not ((numBits > 30) And (numBits < 34)) ' Gd quality entropy - we don't need exactly 32
        m=rdtsc
        n=m+1073741824
        tscSeed0=(m shr 24) or ((m shr 8) and &h0000ff00) or ((m shl 8) and &h00ff0000) or (m shl 24)
        tscSeed1=(n shr 24) or ((n shr 8) and &h0000ff00) or ((n shl 8) and &h00ff0000) or (n shl 24)
        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
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

Thanks, srvaldez.

Your att seeding code is working a treat. “If it ain't broke, then don't fix it.”

With att I find the proliferation of %, ", and \n decided off-putting. At my time of life I try to avoid anything which proliferates :)
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

It may be a while before this hits the streets, as fbc 1.20.0 is still a work in progress.

Image

The above was compiled using clang and the binary was 10% smaller than SCS II.

I have some clang PRNGs outperforming gcc PRNGs in 64-bit mode. gcc still has the edge with 32-bit.

The gcc 9.3 may only end up on two PCs. We shall see. :)
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

Using fbc 1.20.0/gcc 9.3.0 with -O2 optimization.

32-bit binaries in KIB

Code: Select all

             gcc  clang
Dodicat pool 1085 1014
UEZ fireworks 602  566
How about compile times? Pool took 4.5 seconds with gcc and 13.3 seconds with clang.

gas, on the other hand, took 0.5 seconds with a binary of 647 KiB.

We all know that gas has much less to do than gcc. Well gcc has much less to do than gcc/clang.

The secret is to develop with gas. That is what I do if gcc has its work cut out.

I read a good quip the other day: “If you really want your code to run faster then buy a faster computer.” :)
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

Hashing a 500MiB file using SHA512/2 to give a 256-bit hash.

Code: Select all

gcc   2322ms
clang 2328ms
Nothing in it performance wise.

So why am I halving a 512-bit hash? SHA512 was designed for 64-bit machines and is faster than SHA256 which was designed for 32-bit machines. SHA512/2 is about 13% faster than SHA256. :)

Who told me that? jj2007. Remember him? We still keep in touch.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

I have to make a correction
srvaldez wrote: Mar 13, 2024 18:06 ... the variables get ucased and a $1 is appended to the right ...
for a shared variable only $ gets added
for variables in the main module, $0 gets added
in general, $n gets added to the variable name where n is the scope level, example

Code: Select all

#cmdline "-w all -arch native -asm att -gen gcc -Wc -O2"
dim shared as double shared_var, shared_pi=3.141592654

asm
    "fldl     %[SHARED_PI$]            \n" _   'fld   qword ptr [c]
    "fstpl   %[SHARED_VAR$]            \n" _   'fstp qword ptr [result]
    :[shared_var]"=m"(shared_var), [shared_pi]"=m"(shared_pi) _ 'calculation is manually placed into result
    : _
    :
end asm
? "shared ";shared_var

dim as double result
asm
    "fldl     %[SHARED_PI$]            \n" _   'fld   qword ptr [c]
    "fstpl   %[RESULT$0]            \n" _   'fstp qword ptr [result]
    :[result]"=m"(result), [shared_pi]"=m"(shared_pi) _ 'calculation is manually placed into result
    : _
    :
end asm
? "level 0 ";result

sub pi_l2t()
   dim as double result1
      asm
         "fldpi                 \n" _   'fld   qword ptr [c]
         "fstpl   %[RESULT1$1]            \n" _   'fstp qword ptr [result]
         :[result1]"=m"(result1) _ 'calculation is manually placed into result
         : _
         :
      end asm
      ? "level 1 ";result1
      scope
          dim as double result2
          asm
             "FLDL2T                 \n" _   'fld   qword ptr [c]
             "fstl   %[RESULT2$2]            \n" _   'fstp qword ptr [result]
             "fstpl   %[SHARED_VAR$] \n" _
             :[result2]"=m"(result2), [shared_var]"=m"(shared_var) _ 'calculation is manually placed into result
             : _
             :
          end asm
          ? "level 2 ";result2
          ? "shared_var in level 2 ";shared_var
      end scope
end sub

pi_l2t
Sleep
Arachnophilia
Posts: 26
Joined: Sep 04, 2015 13:33

Re: -gen clang

Post by Arachnophilia »

deltarho[1859] wrote: Mar 16, 2024 16:57 It may be a while before this hits the streets, as fbc 1.20.0 is still a work in progress.

Image

The above was compiled using clang and the binary was 10% smaller than SCS II.

I have some clang PRNGs outperforming gcc PRNGs in 64-bit mode. gcc still has the edge with 32-bit.

The gcc 9.3 may only end up on two PCs. We shall see. :)
Hey Robert
When can you offer SCS III for download? :D
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

Arachnophilia wrote:When can you offer SCS III for download? :D
Without my anonymous toolchain author's permission, the answer to that is never, unless someone else goes public with their clang toolchain. With that, SCS III will swiftly follow. One thing is for sure, and that is I cannot do it.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

for anyone on Windows wanting to try FB with clang, I suggest the following
download and extract FreeBASIC-1.10.1-winlibs-gcc-9.3.0.7z from viewtopic.php?t=32498
delete fbc32.exe and fbc64.exe
download the latest build of fbc windows from https://users.freebasic-portal.de/stw/builds/
extract fbc_win32_mingw into the FreeBASIC-1.10.1-winlibs-gcc-9.3.0 folder and rename fbc.exe to fbc32.exe
extract fbc_win64_mingw into the FreeBASIC-1.10.1-winlibs-gcc-9.3.0 folder and rename fbc.exe to fbc64.exe
download llvm-mingw-20240308-msvcrt-i686.zip and llvm-mingw-20240308-msvcrt-x86_64.zip from https://github.com/mstorsjo/llvm-mingw/releases
copy the folowing files from the llvm-mingw bin folder to the respective bin\winXX folder
clang-18.exe
clang.exe
libc++.dll
libclang-cpp.dll
libLLVM.dll
libunwind.dll

that's it
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

That seems easy enough. I wrote: "SCS III will swiftly follow"

Compiled with fbc 1.10.1/gcc 9.3.0 with 32-bit -O2. The clang version was to show clang working.

I use SetCompilerSwitchesIIILA.exe for a larger form font size and 10 point message boxes, as shown in the screenshot above. Sans LA gives the standard 8 point form font size and 8 point message boxes. Too small for an old fart like me. :)

SetCompilerSwitchesIII.zip
Last edited by deltarho[1859] on Mar 17, 2024 16:50, edited 1 time in total.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: -gen clang

Post by srvaldez »

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

Re: -gen clang

Post by deltarho[1859] »

When this project started, it was a rare event to get clang to compile anything. With fbc 1.20.0/9.3.0 and no asm in sight, it is compiling everything I throw at it. 9.3.0 gives much smaller binaries than 13.2.0 and 13.2.0 doesn't do anything for performance, in my experience.

Don't get too excited with clang. I reckon gcc will outperform gcc/clang more often than not. With the addition of gcc/clang FreeBASIC will be given a boost to its coding environment. With gas/gas64, gcc, and gcc/clang we are positively spoiled. :D

This is a good start: #cmdline "-asm att -s console -w all -fpmode fast -fpu sse -arch native -gen clang -Wc -O3"

clang's -O3 is better than gcc's -O3.

Please keep in mind fbc 1.20.0 has some way to go before a release version.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

Regarding SCS III. If you do not have the latest SCSII then you will need to drop DlgMsgBox.dll into your WinFBE Tools folder.

DlgMsgBox.zip

The dll is responsible for pixel perfect message box centring on the parent window and makes the message box multiple monitor aware.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: -gen clang

Post by deltarho[1859] »

After following srvaldez's build instructions, change the name of the toolchain to:

FreeBASIC-1.20.0-gcc-9.3.0cl

That will appear in WinFBE's Compiler Setup and

fbc 1.20.0 / gcc 9.3.0cl

will appear in SCS III's status bar.
Post Reply