about gcc_builtin functions

General FreeBASIC programming questions.
Post Reply
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

about gcc_builtin functions

Post by srvaldez »

the gcc manual documents the gcc __builtin_functions for all the latest versions https://gcc.gnu.org/onlinedocs/ look in https://gcc.gnu.org/onlinedocs/gcc-11.2 ... -Functions
I tried gcc-9.3 and it supports the builtin functions but later versions don't seem to support them, I tried adding the option -fbuiltin but it made no difference, I thought that perhaps it depends on how the toolchain is built but surely the linux version of gcc should support them, well version 10.2 that I tried on linux didn't support them either
the versions I tried support __builtin_cpu_init () but anything else gives an undefined symbol error, can anyone shed some light on this?
[edit]
it seems to be supported when using C, so why does it not work with FB?
why does it work with gcc versions 8.1 and 9.3 but not with later versions?

Code: Select all

#include <stdio.h>

int main(void)
{
  __builtin_cpu_init ();
  if (__builtin_cpu_supports ("ssse3"))
    printf("%s ssse3 supported\n","");
  else
    printf("%s ssse3 NOT supported\n","");
  return 0;
}

Code: Select all

extern "c"
	declare function __builtin_cpu_init () as long
	declare function __builtin_cpu_is (byval as const zstring ptr) as long
	declare function __builtin_cpu_supports (byval as const zstring ptr) as long
end extern

__builtin_cpu_init ()


if __builtin_cpu_is ("amd") then
    Print "AMD CPU."
end if
if __builtin_cpu_is ("intel") then
    Print "Intel CPU."
end if
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: about gcc_builtin functions

Post by dodicat »

I cannot test more than gcc 9.3, I don't have higher gcc.
These builtin functions seem to reside in cc1.exe.
I can look for things with

Code: Select all

shell " MODE CON: COLS=170 LINES=500"
#include "file.bi"
#include "crt.bi"
Declare Function stats Cdecl Alias "_stat"(As zstring Ptr,As Any Ptr) As Integer

Function String_Split(s_in As String,chars As String,result() As String) As Long
    Dim As Long ctr,ctr2,k,n,LC=Len(chars)
    Dim As boolean tally(Len(s_in))
    #macro check_instring()
    n=0
    While n<Lc
        If chars[n]=s_in[k] Then 
            tally(k)=true
            If (ctr2-1) Then ctr+=1
            ctr2=0
            Exit While
        End If
        n+=1
    Wend
    #endmacro
    
    #macro split()
    If tally(k) Then
        If (ctr2-1) Then ctr+=1:result(ctr)=Mid(s_in,k+2-ctr2,ctr2-1)
        ctr2=0
    End If
    #endmacro
    '==================  LOOP TWICE =======================
    For k  =0 To Len(s_in)-1
        ctr2+=1:check_instring()
    Next k
     if ctr=0 then
         if len(s_in) andalso instr(chars,chr(s_in[0])) then ctr=1':beep
         end if
    If ctr Then Redim result(1 To ctr): ctr=0:ctr2=0 Else  Return 0
    For k  =0 To Len(s_in)-1
        ctr2+=1:split()
    Next k
    '===================== Last one ========================
    If ctr2>0 Then
        Redim Preserve result(1 To ctr+1)
        result(ctr+1)=Mid(s_in,k+1-ctr2,ctr2)
    End If
   
    Return Ubound(result)
End Function

Function _Remove(Byval Text As String,Char As String) 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 
    Return Left(Text,index)
End Function

Function loadfile(file As String) As String
    file= _remove(file,Chr(34))
    If file="" Then Exit Function
    If Fileexists(file)=0 Then Print file;" CAN'T READ (OR EMPTY FOLDER)":Exit Function
    Var  f=Freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(f) > 0 Then
        text = String(Lof(f), 0)
        Get #f, , text
    End If
    Close #f
    Return text
End Function

Function isfolder(path As zstring Ptr) As Long
    #define S_ISDIR(m)   (((m) And &hF000) = &h4000)
    Dim As stat statbuf
    If (stats(path, @statbuf) <> 0) Then Return 0
    Return S_ISDIR(statbuf.st_mode)
End Function

Function isfile(fname As String) As boolean
    Return Iif(isfolder(fname),0,1)
End Function

Function pipeout(Byval s As String="") Byref As String
    Var f=Freefile
    Dim As String tmp
    Open Pipe s For Input As #f 
    s=""
    Do Until Eof(f)
        Line Input #f,tmp
        s+=tmp+Chr(10)
    Loop
    Close #f
    Return s
End Function 

Function tally (somestring As String,partstring As String) As Integer
    Dim As Integer i,j,ln,lnp,count,num
    ln=Len(somestring)
    lnp=Len(partstring)
    count=0
    i=-1
    Do
        i+=1
        If somestring[i] <> partstring[0] Then Goto skip 
        If somestring[i] = partstring[0] Then
            For j=0 To lnp-1 
                If somestring[j+i]<>partstring[j] Then Goto skip
            Next j
        End If
        count+=1
        i=i+lnp-1
        skip:
    Loop Until i>=ln-1 
    Return count
End Function

function search(inputs As String,st As String,ext as string="") as long
    st=Lcase(st)
    dim as long dflag
    'Redim As String tmp()
    dim as string path
    If isfile(inputs) Then 'for a single file or a drive:
        if len(inputs)=2 and inputs[1]=asc(":") then dflag=1:goto skip
        Var L=Lcase(loadfile(inputs))
        If Instr(L,st) Then Print inputs:Return 1 Else Return 0
    End If
    skip:
    Dim As String file
    If Instr(inputs," ") Then inputs=Chr(34)+inputs+Chr(34)
    Dim As String s=pipeout("dir /b " + inputs)
    Dim As String a()
    Static As Long counter
    string_split(s,Chr(13,10),a())
    inputs=_remove(inputs,Chr(34))
    if dflag=0 then 'not a drive
    redim as string tmp()
    string_split(inputs,"\/",tmp())
    if instr(tmp(ubound(tmp)),st) then print inputs;" >> (In folder name)":counter+=1
end if
    For n As Long=Lbound(a) To Ubound(a)
       
         path=(inputs+"\"+a(n)) '''
        If isfile(path) Then
            Redim As String tmp()
            string_split(path,"\",tmp())
            file= tmp(Ubound(tmp))
            If Instr(file,st) Then Print path;" >> (In file name)":counter+=1
            If Instr(file," ") Then file=Chr(34)+file+Chr(34)
           ' print path
             if len(ext) then if instr(file,ext)=0 then goto nxt 'path
            Var L=Lcase(loadfile(path))
            If Len(L) Andalso Instr(L,st) Then Print path + "  ("+ str(tally(L,st))+")":counter+=1
        Else
            search(path,st,ext) 'for nested folders
        End If
        nxt:
    Next n
    return counter
End function 

'==========================  fill this bit in ==================================
Dim  As String location= "C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0"'
print "You are using fb version ";__fb_version__

print "output format:"
print "path to file  (number of occurencies in the file)"
Print "Searching  . . .";
print

'search(path,string to look for,file extension - example .txt, or leave blank)

var c=search(location,"builtin","")
Print "Done ",c; "  locations"
Sleep
  
From the freebasic distribution (the location path)
My result for "builtin"

Code: Select all

You are using fb version 1.08.1
output format:
path to file  (number of occurencies in the file)
Searching  . . .
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\i686-w64-mingw32\9.3.0\cc1.exe  (4590)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\i686-w64-mingw32\9.3.0\ld.exe  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\i686-w64-mingw32\9.3.0\libgcc_s_sjlj-1.dll  (4)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\i686-w64-mingw32\9.3.0\libiconv-2.dll  (4)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\i686-w64-mingw32\9.3.0\libisl-22.dll  (16)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\i686-w64-mingw32\9.3.0\libmpfr-6.dll  (22)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\x86_64-w64-mingw32\9.3.0\cc1.def  (103)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\x86_64-w64-mingw32\9.3.0\cc1.exe  (4601)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\x86_64-w64-mingw32\9.3.0\ld.exe  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\x86_64-w64-mingw32\9.3.0\libgcc_s_sjlj-1.dll  (4)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\x86_64-w64-mingw32\9.3.0\libiconv-2.dll  (4)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\x86_64-w64-mingw32\9.3.0\libisl-22.dll  (16)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\libexec\gcc\x86_64-w64-mingw32\9.3.0\libmpfr-6.dll  (22)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\win32\gcc.exe  (19)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\win32\ld.exe  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\win64\gcc.exe  (19)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\bin\win64\ld.exe  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\changelog.txt  (10)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\examples\manual\proguide\pointers\builtin.bas >> (In file name)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\examples\manual\proguide\pointers\builtin.bas  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\examples\manual\proguide\typalias\builtin.bas >> (In file name)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\examples\manual\proguide\typalias\builtin.bas  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\examples\misc\trycatch\trycatch.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\fbc32.exe  (44)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\fbc64.exe  (44)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\allegro5\allegro_font.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\clang-c.bi  (3)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\dos\sys\djtypes.bi  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\fbc-int\memory.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\ffi.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\gtk\gtk2.bi  (5)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\gtk\gtk3.bi  (4)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\jit.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\libxml\schemasInternals.bi  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\libxml\xmlschemastypes.bi  (3)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\win\gdiplus-c.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\win\GdiplusImaging.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\win\intrin.bi  (3)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\win\objidl.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\win\winerror.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\win\winnt.bi  (34)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\win\winspool.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\inc\wx-c\defs.bi  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\crt2.o  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libgcc.a  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libgcc_eh.a  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libgmon.a  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libmincore.dll.a  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libmingw32.a  (3)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libmingwex.a  (62)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libmsvcrt-os.dll.a  (20)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libmsvcrt.dll.a  (20)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libstdc++.a  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libsupc++.a  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libucrt.dll.a  (13)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libucrtbase.dll.a  (13)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libws2_32.dll.a  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win32\libwsdapi.dll.a  (5)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\crt2.o  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libgcc.a  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libgcc_eh.a  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libgmon.a  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libmincore.dll.a  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libmingw32.a  (3)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libmingwex.a  (62)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libmsvcrt-os.dll.a  (20)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libmsvcrt.dll.a  (20)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libnetui1.dll.a  (20)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libnetui2.dll.a  (10)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libstdc++.a  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libsupc++.a  (1)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libucrt.dll.a  (13)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libucrtbase.dll.a  (13)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libws2_32.dll.a  (2)
C:\Users\Computer\Desktop\fb\FreeBASIC-1.08.1-winlibs-gcc-9.3.0\lib\win64\libwsdapi.dll.a  (5)
Done           75  locations
 
searching for "builtin_cpu" gives only 2 locations with 4 examples in each and the locations are in cc1.exe (both).
That's all I can help with just now.
I got all my complex functions from libmingwex.a
It is a hobby in it's own right, tracking down strange functions from the distribution.
I also use nm somefile now and then, if there are symbols then all the functions are listed, usually T tags any usable things.
Also I see that gmp-10.dll (and others) are available, saves Googling for them.
Never a dull moment with freebasic.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: about gcc_builtin functions

Post by srvaldez »

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

Re: about gcc_builtin functions

Post by deltarho[1859] »

dodicat wrote:It is a hobby in it's own right,
Sorry, srvaldez but this is off-topic.

A couple of years ago, a friend 'phoned me and asked if I was anywhere near Heathrow Airport. I live near Heathrow, so when I am 'out and about' I am usually not far away - I was only a few miles away. He asked if I would drive to the south side and see if I could spot a particular aircraft which rarely came to the UK. No doubt you know about train spotters - my friend is an aircraft spotter. I spotted it, and he said he'd drive over straight away. I asked him what he'd do when he got to Heathrow - "Spot it" he said and make notes of time and day. I was chatting to him one day, and he was parked up in the countryside somewhere waiting for an aircraft to fly over. He was tracking it on his smart 'phone and carried a powerful telescope. He was over the moon when he 'spotted it' - he'd been waiting for years, apparently. He has what he calls his 'aircraft program' which is enormous and runs on his PC 24/7. It took hours to parse the database for relevant information. I wrote a program for him to do the parsing for him, reducing the hours to a few minutes. He wanted to see the source code and decided he'd never ba able to do that. I said: "Well, I have been doing this for decades and that is my hobby". Image I met him in 2003, and he had been 'spotting aircraft' for quite a few years before that. He is a member of an aircraft spotting forum. Yes, they really do exist.

I should start a thread in the Community Discussion forum entitled "Hobbies you won't believe and how FreeBASIC plays a role."
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: about gcc_builtin functions

Post by dodicat »

I wonder Deltarho...
Sitting in the beautiful countryside of the Home counties, on a September evening, with a pair of binoculars, among congenial friends with a flask of coffee, or perhaps a hip flask, waiting patiently for the great silver birds to fly over. Or sitting in front of a monitor speed testing random generators, and every week or so another one pops up, and speed testing all over again, and srvaldez, threatened with the complete obliterations of all his hard found built-ins at the whim of the gcc team.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: about gcc_builtin functions

Post by deltarho[1859] »

Or my getting up really early on a winter's Sunday morning when I was a youngster to cycle for a few hours up the River Tees with my tide tables in my back pocket to sit on the river bank with a float in an ice hole to catch chub which are by no means a table fish. Every now and again I would have to suck the rings on my fishing rod as they used to freeze fairly quickly. Seven or eight hours later, I'd cycle home with my catch after not seeing another soul all day. Our bull terrier looked forward to my return because the chub was his favourite meal of the week. It saved my mother opening a tin of Pedigree Chum, one of the most expensive dog foods at the time, which is only what 'Butch' would eat besides my chub. My mother gave me a few 'bob' which paid for my bait for the following Sunday.

I used to call that my hobby until I met a girl and found that I preferred her to the chub. I sold my fishing tackle to a friend of my father who lived in the next street. I never fished again. I live on my own now "sitting in front of a monitor speed testing random generators, and every week or so another one pops up, and speed testing all over again." Perhaps I should take up fishing again. Image

I have devised a method to show that the quality of randomness of PRNGs fluctuates during a generator session, resulting in anomalies in PractRand runs. However, I have decided not to publish the findings, fearing the forum crowdfunding a hit man.
Image
Apologies again, srvaldez.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: about gcc_builtin functions

Post by srvaldez »

deltarho[1859] wrote: Apologies again, srvaldez.
no problem with me, I enjoy a lighthearted conversation :-)
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: about gcc_builtin functions

Post by srvaldez »

this might be useful to somebody, basic integer arithmetic with overflow detection https://gcc.gnu.org/onlinedocs/gcc/Inte ... ltins.html
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: about gcc_builtin functions

Post by srvaldez »

I did a simple test and it seems that there's no slowdown using the overflow_check functions
the times are basically the same for either

Code: Select all

Extern "c"
    Declare Function mul_overflow Alias "__builtin_smulll_overflow" (Byval a As Longint, Byval b As Longint, Byref res As Longint) As boolean
End Extern

Function ipower (Byval x As Longint, Byval e As Ulong) As Longint
    'take x to an integer power
    Dim As Longint z, y
    Dim As Ulong n

    y = x
    n = e
    z = 1
    While n > 0
        While (n And 1) = 0
            n = n \ 2
            If mul_overflow(y, y, y) Then Print "an overflow occured"
        Wend
        n = n - 1
        If mul_overflow(y, z, z) Then Print "an overflow occured"
    Wend
    ipower = z
End Function

'Print ipower(3, 39)
'Print ipower(3, 40)

Dim As Longint i, j, n, m
Dim As Double sum, t

t=Timer
sum=0
For i=1 To 100000000
	n=Rnd
	For j=1 To 20
		n=n*j
		'mul_overflow(n, j, n)
	Next
	sum+=n
Next
t=timer-t
Print sum
Print t
one thing I noticed was how sensitive the linker is about the __builtin functions, first I declared the function return type as long and the linker could not find the function, when I changed the return type to boolean then all is well, this might explain the problem I was experiencing about undefined symbols
Post Reply