[PATCH] PR11706, optimize std::pow(T, int)

Richard Guenther rguenth@tat.physik.uni-tuebingen.de
Wed Jan 12 15:43:00 GMT 2005


On Wed, 12 Jan 2005, Richard Guenther wrote:

> On Wed, 12 Jan 2005, Paolo Carlini wrote:
>
> > Richard Guenther wrote:
> >
> > Maybe Gaby wants to add something here, but I seem to remember that
> > we really dislike this kind of approach, calling back pow from __pow_helper
> > via a cast, thus giving away the info that the exponent is integer.
>
> We can dispatch directly to __builtin_pow[lf](), too, and the frontends

Like with

      inline _Tp
      __pow_helper(_Tp __x, int __n)
      {
#if __FAST_MATH__
       if (__builtin_constant_p(__n))
#else
       if (__builtin_constant_p(__n) && __n >= -2 && __n <= -2)
#endif
         {
           if (__builtin_types_compatible_p(_Tp, float))
             return __builtin_powf(__x, __n);
           else if (__builtin_types_compatible_p(_Tp, double))
             return __builtin_pow(__x, __n);
           else if (__builtin_types_compatible_p(_Tp, long double))
             return __builtin_powl(__x, __n);
         }
       return __n < 0
          ? _Tp(1)/__cmath_power(__x, -__n)
          : __cmath_power(__x, __n);

(untested, maybe only __typeof(__x) works), which would even work if
someone cared to call std::__pow_helper with _Tp other than float, double
or long double.

Richard.

--
Richard Guenther <richard dot guenther at uni-tuebingen dot de>
WWW: http://www.tat.physik.uni-tuebingen.de/~rguenth/



More information about the Libstdc++ mailing list