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

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


Paolo Carlini wrote:
> Hi again,
> 
> ok, I did my homework, and admittedly, when the exponent is not
> constant, the binary algorithm that we are using in v3 is *much*
> faster.
> This is a tiny testcase:
> 
>  for (int i = 0; i < 100000000; ++i)
>    {
>      double a = M_PI * i;
>      int n = i % 10;
>      std::pow(a, n);
>      // ::pow(a, n);
>    }
> 
> On my P4-2400, -O2:
> 
> ::pow
> -----
> 23.400u 0.010s 0:24.32 96.2%    0+0k 0+0io 154pf+0w
> 
> std::pow
> --------
> 1.580u 0.000s 0:01.58 100.0%    0+0k 0+0io 149pf+0w

I'm not at all surprised if ::pow() gets to call libm - because libm pow 
is (double, double) here and has to fall back to taylor series pow 
approximation probably.  There is no integer overload in libm.

So we can again cheat by using something like

double pow(double x, int n)
{
    if (__builtin_constant_p(n))
      return __builtin_pow(x, n);
    else
      return __pow_helper(x, n);
}

or do the right thing with a special __builtin_powi() or use a 
pow_helper in libgcc to which we can dispatch from __bultin_pow() in 
non-optimized/inlined integer exponent case.

I'd be happily doing any of the above if only the copyright assignment 
papers would arrive...

Richard.



More information about the Libstdc++ mailing list