How to show assembly output for builtin functions?

97 Views Asked by At

I tried to use godbolt online compiler to view the generated assembly for builtin functions. Example

#include <cmath>

double br( double x ) {
    return __builtin_roundl(x);
}

double r( double x ) {
    return std::round( x );
}

See https://godbolt.org/z/7e76a6qv4.

But assembly output just showed as

br(double):
        jmp     round
r(double):
        jmp     round

I guess there has to be some compiler parameter to reveal the definition of builtin functions, but I could not find a suitable one.

What code gets generated in the end? Is there a way to get the call to round inlined?

2

There are 2 best solutions below

6
KamilCuk On

How to show assembly output for builtin functions?

You are showing the assembly output for a builting function in your code - it is jmp round. The assembly function __builtin_roundl generated jmp round in this case, and it is correct.

There is no "assembly" for a builtin function. It only exists in the C compiler source code as a series of transformations on some abstraction over the language.

The code is here https://github.com/gcc-mirror/gcc/blob/fca6f6fddb22b8665e840f455a7d0318d4575227/gcc/convert.cc#L550 .

I guess there has to be some compiler parameter to reveal the definition of builtin functions

There is none.

1
gnasher729 On

Check how much knowledge the compiler has, for example by calling __builtin_roundl(3.7) and checking that the result is just the constant 4.0.

Now the implementation is just a single jmp. If the code is stored somewhere so you can use function pointers then this quite optimal. See what happens if you call __builtin_roundl(x) + __builtin_roundl(x+1) where inlining would be more likely.