Using GCC's built-in functions instead of self-written ones

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Neroku
Posts: 24
Joined: Tue Dec 01, 2015 4:53 am

Re: Using GCC's built-in functions instead of self-written o

Post by Neroku »

Combuster wrote:
I want to let GCC replace the calls to my library functions with its built-in functions whenever possible.
Why don't you just simply compile with optimisations enabled instead of doing all these ifdefs? That way you can't mess things up.
Thanks a lot for the suggestion :D
It is definitely much more elegant not to pass GCC the option -fno-builtins and just do away with these preprocessor directives.
The problem is that I am using the -ffreestanding option and it also implicitly enables the -fno-builtin one. From "C Dialect-Options":
if you wish to enable built-in functions selectively when using -fno-builtin or -ffreestanding, you may define macros such as:

#define abs(n) __builtin_abs ((n))
#define strcpy(d, s) __builtin_strcpy ((d), (s))
Combuster wrote:Why don't you just simply compile with optimisations enabled instead of doing all these ifdefs? That way you can't mess things up.
I am assuming that you are telling me to enable optimizations in order to disable this -fno-builtin option. Where did you actually find whether -fno-builtin is disabled by enabling optimization? Did you find that in GCC's documentation or just by trial and error?
I have been looking around, but I can't find it :cry:


Thank you
currently working on kboot86, the Genesis of my kernel
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Using GCC's built-in functions instead of self-written o

Post by Combuster »

Actually, setting freestanding makes builtins invalid because that is the correct thing to do. You can either drop the freestanding because you actually do have a "C library" at your disposal, or you can override that assumption.

I just tested the following to make sure it does what one would expect.

Code: Select all

-ffreestanding -fbuiltin
Note that builtins actually allow the compiler to use different functions as substitutions, so be aware of undefined symbols appearing if you implemented the complex function but not the simpler degenerate case.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Using GCC's built-in functions instead of self-written o

Post by Rusky »

If you want to enable only some builtin functions, you can leave -ffreestanding on and just #define those functions to their __builtin versions, without all the #ifdef stuff- like you discovered at the beginning of this thread, those will get linked to the real versions if they don't get optimized.
User avatar
Neroku
Posts: 24
Joined: Tue Dec 01, 2015 4:53 am

Re: Using GCC's built-in functions instead of self-written o

Post by Neroku »

Combuster wrote:Actually, setting freestanding makes builtins invalid because that is the correct thing to do. You can either drop the freestanding because you actually do have a "C library" at your disposal, or you can override that assumption.

I just tested the following to make sure it does what one would expect.

Code: Select all

-ffreestanding -fbuiltin
Note that builtins actually allow the compiler to use different functions as substitutions, so be aware of undefined symbols appearing if you implemented the complex function but not the simpler degenerate case.
Rusky wrote:If you want to enable only some builtin functions, you can leave -ffreestanding on and just #define those functions to their __builtin versions, without all the #ifdef stuff- like you discovered at the beginning of this thread, those will get linked to the real versions if they don't get optimized.
Both suggested solutions quoted above are indeed good. I'm going to stick to Combuster's proposed one (i.e.: passing GCC the option -fbuiltin) since it requires no modification of the source code at all.


Thank you ever so much! :D
currently working on kboot86, the Genesis of my kernel
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Using GCC's built-in functions instead of self-written o

Post by Rusky »

No, using -fbuiltin is equivalent to using the __builtin_* functions. The compiler will still emit calls to the library functions in some situations.
User avatar
Neroku
Posts: 24
Joined: Tue Dec 01, 2015 4:53 am

Re: Using GCC's built-in functions instead of self-written o

Post by Neroku »

Rusky wrote:No, using -fbuiltin is equivalent to using the __builtin_* functions. The compiler will still emit calls to the library functions in some situations.
I'm already aware of that. What I want to achieve is to let GCC use its built-in functions whenever possible.


Thanks
currently working on kboot86, the Genesis of my kernel
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Using GCC's built-in functions instead of self-written o

Post by Rusky »

Ah, I see. :oops: You're saying you wouldn't even need to write "#define memset __builtin_memset", for example. I figured you were comparing it to needing to write the libc functions themselves.
Post Reply