::pow(T, n) vs std::pow(T, n) for non-constant n

Richard Guenther rguenth@tat.physik.uni-tuebingen.de
Sun Mar 14 12:40:00 GMT 2004


Paolo Carlini wrote:
> ... however, for the very same loop that I used at the beginning:
> 
>  for (int i = 0; i < 100000000; ++i)
>    {
>      double a = M_PI * i;
>      int n = i % 10;
>      std::pow(a, n);
>      //::pow(a, n);
>    }
> 
> With -O2 -ffast-math <- IMPORTANT
> 
> ::pow
> -----
> 0.090u 0.000s 0:00.09 100.0%    0+0k 0+0io 149pf+0w
> 
> std::pow
> --------
> 1.540u 0.000s 0:01.54 100.0%    0+0k 0+0io 149pf+0w
> 
> Therefore, in the situation displayed above, gcc + builtin pow,
> when requested to do so (-ffast-math) is able to establish the
> constness of the argument and use the fast optimized code!!!

Hmm, I can't get it to recognize the constant-ness.  You should look at 
the generated assembler.  Probably the loop is just optimized away 
because you don't use the result of ::pow() and ::pow() is probably 
const or pure while std::pow() is not (it should probably be).

> For sure, in other, more complex, situations this will not happen,
> but when, exactly?
> 
> The more I try out the behavior of gcc + builtin pow, the more I
> become convinced that, for -ffast-math, is so faster than the current
> v3 binary algorithm, otherwise, more accurate.

It should be more accurate than v3 in any case.  The only thing is, 
std::pow() defaults to something like -ffast-math, while __builtin_pow() 
does not.  This can be surprising to users.



More information about the Libstdc++ mailing list