Understanding std::pow Implementation in GCC

Viewed 1316

I am trying to understand how the std::pow function works in GCC. I was looking at the source code for the cmath library here https://code.woboq.org/gcc/libstdc++-v3/include/c_global/cmath.html#_ZSt3powff.

On line 388 of that file, they give the definition of pow when float types are passed as the argument as shown here

#ifndef __CORRECT_ISO_CPP_MATH_H_PROTO
  inline _GLIBCXX_CONSTEXPR float
  pow(float __x, float __y)
  { return __builtin_powf(__x, __y); }

This pow function calls another function __builtin_powf.

What I want to know is, where is __builtin_powf defined? I read that it is a built in function, but that still does not help me understand where its implementation lives. I can not seem to find __builtin_powf anywhere else in the GCC source code. Also, how is it translated to assembly?

1 Answers

where is __builtin_powf defined?

It is defined in your C standard library implementation. There no implementation of powf in gcc.

The __builtin_powf exists so that gcc could detect pow usage and compute the result at compile time if it can. Well, the string "__builtin_powf" is "defined" in gcc/builtins.def#L541 (but really only exists as function argument) and builtins seem to be handled at gcc/builtins.c#L2100, but on the end pow is handled by standard lirbary. When fold_const_call_sss() in fold-const-call.c#L1393 returned success, that means pow was folded constantly. The call could be vectorized/substituted in tree-vect-patterns.c. But on the end the "implementation" is in your C standard library.

Related