I want to learn how can I find source codes of __builtin functions like __builtin_cbrt()
in C++. If you know, Can you help me?

- 11
- 3
-
Functions that start with `__` are special implementation-specific functions. They aren't necessarily implemented by C++ source code. They can be implemented as assembly or just treated uniquely by the compiler. – François Andrieux Feb 16 '21 at 19:14
-
You would need to check the source code for the compiler you are using. – Richard Critten Feb 16 '21 at 19:15
-
By definition, platform-specific functions `__builtin_*` are _builtin_ in the compiler. They are not implemented in C++ in a library. Of course, you can look in the source code of _Clang_ or _GCC_ themselves, but it will be hard to spot precisely where those functions are handled. – prapin Feb 16 '21 at 19:26
-
what research did you do on your own? Have you at least found out your compiler sources? – KamilCuk Feb 16 '21 at 19:40
-
@KamilCuk I was trying to find source code of ```std::cbrt()```, i find it in STL library on local, it calss ```__builtin_cbrt()``` but I couldn't find it's source code... – Halit Feb 17 '21 at 14:05
1 Answers
I want to learn how can I find
First become acquainted with the language you are working with - learn C and C++ programming languages. Learn about the tools like git
and autotools
and the environment around these programming languages. Become familiar with the tool set needed browsing files - at least grep
, but I recommend it's (way) faster alternatives - "the silver searcher" ag
or ack
, but be aware of tools like ctags or GNU Global.
Then research. GNU projects are available open source - it's very easy to find source code of GNU projects, nowadays they are even mirrored on github.
Then it's just a "feeling" or "experience". Surely a project will have builtins functions in a file named "builtins.c" or something similar. Be curious, reasonable and inventive. If you would want to add a builtin function to a codebase, where would you put it? Become familiar with the project structure you are working with. And expect big and old projects to have stuff scattered all over the place.
First I find gcc sources with builtins.def (BUILT_IN_CBRT, "cbrt"
, and some references of BUILT_IN_CBRT in builtins.c.
After cloning the gcc
repository I scan for BUILT_IN_CBRT
macro name. Browsing the code leads me to CASE_CFN_CBRT
macro name, which leads me to fold-const-call.c:
CASE_CFN_CBRT:
return do_mpfr_arg1 (result, mpfr_cbrt, arg, format);
By the name of the file fold-const-call.c
I suspect this part of code is taken only when folding a constant call.
From there I can browse google about mpfr_cbrt
symbol, which leads me to GNU MPFR library. I find clone of MPRF library on github and search for a file named cbrt
, I find cbrt.c with mpfr_cbrt() sources with the source of cbrt within MPRF library. This is the code that will be called and will compute cbrt
of a number when __builtin_cbrt
is folded inside a constant expression, I suspect.
When not in constnat expression, I suspect that [fold_const_call_ss]https://code.woboq.org/gcc/gcc/fold-const-call.c.html#_ZL18fold_const_call_ssP10real_value11combined_fnPKS_PK11real_format) is not called at all, instead some fold_const_*
function returns to gcc that the expression cannot be constantly folded so a call to the actual cbrt()
standard function is generated.

- 120,984
- 8
- 59
- 111