Sheed
Sheed

Reputation: 619

Is it possible to see builtin function definition?

This is pure curiosity, I'm using a cmath function, that is cos, whatever. After looking at the cmath header, I see that kind of things:

 inline _GLIBCXX_CONSTEXPR float
  acos(float __x)
  { return __builtin_acosf(__x); }

And there is that "__ builtin__" and I don't find any source of that with google. I'm interested in seeing the source code of how they implement a math function, I mean I guess they use taylor and stuff, but I wanted to see how they do it. Is it hidden for proprietary reasons or can it be found?

Upvotes: 3

Views: 951

Answers (1)

javidcf
javidcf

Reputation: 59701

GCC __builtin_ functions are not actual functions, that is, they are not really declared and implemented anywhere as such, but instead they are detected by the compiler and mapped to some implementation. See builtins.h, builtins.c and builtins.def. It is rather hard to track down where the actual implementations reside, but it seems that these mathematical functions are taken from the implementation of libc that you are using. For example, digging through GLibc source code, one can find at least a couple of implementations of __ieee754_acosf (which seems to be then aliased and wrapped by other function) one in C e_acosf.c and one in x86 assembly a_acosf.S (backup links, since these are unofficial GitHub mirrors e_acosf.c e_acosf.S). You could say that readability was not their first priority. You can find similar code in the musl libc source tree: acosf.c in C an acosf.s in x86.

Upvotes: 5

Related Questions