This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
std::pow implementation
- From: Richard Guenther <rguenth at tat dot physik dot uni-tuebingen dot de>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 29 Jul 2003 13:04:41 +0200 (CEST)
- Subject: std::pow implementation
Hi!
The current std::pow implementation causes very weak code to be emitted
for the overloads pow(T, int) for both gcc 3.3 and gcc 3.4. These powers
are computed going through the std::__power_helper() function template
which ends up emitting a call to std::__cmath_power() always. For
reference:
template<typename _Tp>
inline _Tp
__pow_helper(_Tp __x, int __n)
{
return __n < 0
? _Tp(1)/__cmath_power(__x, -__n)
: __cmath_power(__x, __n);
}
If we change this to read like
template<typename _Tp>
inline _Tp __attribute__((always_inline))
__power_helper(_Tp __x, int __n)
{
if (__builtin_constant_p(__n)) {
switch (__n) {
case 0:
return 1;
case 1:
return __x;
case 2:
return __x*__x;
#if ! __OPTIMIZE_SIZE__
case 3:
return __x*__x*__x;
case 4: {
_Tp __y = __x*__x;
return __y*__y;
}
/* etc. */
#endif
}
}
return __n < 0
? _Tp(1)/std::__cmath_power(__x, -__n)
: std::__cmath_power(__x, __n);
}
we will win a lot.
Can such be done for 3.4 and possibly 3.3, too?
Thanks,
Richard.
--
Richard Guenther <richard dot guenther at uni-tuebingen dot de>
WWW: http://www.tat.physik.uni-tuebingen.de/~rguenth/