This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
::pow(T, n) vs std::pow(T, n) for non-constant n
- From: Paolo Carlini <pcarlini at suse dot de>
- To: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Cc: Gabriel Dos Reis <gdr at integrable-solutions dot net>,Richard Guenther <rguenth at tat dot physik dot uni-tuebingen dot de>
- Date: Sun, 14 Mar 2004 13:17:24 +0100
- Subject: ::pow(T, n) vs std::pow(T, n) for non-constant n
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
std::pow for the lno-branch
---------------------------
0.010u 0.000s 0:00.00 0.0% 0+0k 0+0io 146pf+0w
============
On the other hand, for:
for (int i = 0; i < 300000000; ++i)
{
double a = M_PI * i;
std::pow(a, 3);
//::pow(a, 3);
}
-O2 -ffast-math:
::pow
-----
0.190u 0.000s 0:00.19 100.0% 0+0k 0+0io 149pf+0w
std::pow
--------
0.570u 0.000s 0:00.58 98.2% 0+0k 0+0io 149pf+0w
std::pow for the lno-branch
---------------------------
0.000u 0.000s 0:00.00 0.0% 0+0k 0+0io 146pf+0w
Therefore, the superiority of the binary algorithm vs library call is
much larger than the gain of the optimized builtin for constant argument
vs the binary algorithm.
At the outset, I couldn't imagine that.
In practical applications, which is the mix? Are there more non-constant
or constant arguments in the actual applications? I suspect more constant
calls, but cannot be sure about that.
As the lno-branch indicates, a __builtin_powi, omptimizing also non-constant
calls to the unrolled optimal code would be the best, but I'm not sure that
what we currently have is the best trade-off, taking also into accoount the
concerns about the precision of the binary algorithm (when the user doesn't
want -ffast-math type behavior)
Paolo.