1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

Replace pow with std::pow

The C pow casts to double while std::pow has overloads that can be
optimized by the compiler.
Also replace pow(*, 1./3) by cbrt.
This commit is contained in:
Vincent Rabaud
2026-01-04 01:50:00 +01:00
parent 10589eb2c8
commit 5622958189
47 changed files with 131 additions and 140 deletions
+7 -16
View File
@@ -1675,16 +1675,8 @@ int cv::solveCubic( InputArray _coeffs, OutputArray _roots )
}
else if( d == 0 )
{
if(R >= 0)
{
x0 = -2*pow(R, 1./3) - a1/3;
x1 = pow(R, 1./3) - a1/3;
}
else
{
x0 = 2*pow(-R, 1./3) - a1/3;
x1 = -pow(-R, 1./3) - a1/3;
}
x0 = -2*std::cbrt(R) - a1/3;
x1 = std::cbrt(R) - a1/3;
x2 = 0;
n = x0 == x1 ? 1 : 2;
x1 = x0 == x1 ? 0 : x1;
@@ -1693,7 +1685,7 @@ int cv::solveCubic( InputArray _coeffs, OutputArray _roots )
{
double e;
d = sqrt(-d);
e = pow(d + fabs(R), 1./3);
e = std::cbrt(d + fabs(R));
if( R > 0 )
e = -e;
x0 = (e + Q / e) - a1 * (1./3);
@@ -1808,15 +1800,14 @@ double cv::solvePoly( InputArray _coeffs0, OutputArray _roots0, int maxIters )
if( num_same_root % 2 != 0){
Mat cube_coefs(4, 1, CV_64FC1);
Mat cube_roots(3, 1, CV_64FC2);
cube_coefs.at<double>(3) = -(pow(old_num_re, 3));
cube_coefs.at<double>(2) = -(15*pow(old_num_re, 2) + 27*pow(old_num_im, 2));
cube_coefs.at<double>(3) = -(std::pow(old_num_re, 3));
cube_coefs.at<double>(2) = -(15*std::pow(old_num_re, 2) + 27*std::pow(old_num_im, 2));
cube_coefs.at<double>(1) = -48*old_num_re;
cube_coefs.at<double>(0) = 64;
solveCubic(cube_coefs, cube_roots);
if(cube_roots.at<double>(0) >= 0) num.re = pow(cube_roots.at<double>(0), 1./3);
else num.re = -pow(-cube_roots.at<double>(0), 1./3);
num.im = sqrt(pow(num.re, 2) / 3 - old_num_re / (3*num.re));
num.re = std::cbrt(cube_roots.at<double>(0));
num.im = sqrt(std::pow(num.re, 2) / 3 - old_num_re / (3*num.re));
}
}
roots[i] = p - num;
+1 -1
View File
@@ -265,7 +265,7 @@ TEST_P (CountNonZeroND, ndim)
data = 0;
EXPECT_EQ(0, cv::countNonZero(data));
data = Scalar::all(1);
int expected = static_cast<int>(pow(static_cast<float>(ONE_SIZE), dims));
int expected = static_cast<int>(std::pow(static_cast<float>(ONE_SIZE), dims));
EXPECT_EQ(expected, cv::countNonZero(data));
}
+1 -1
View File
@@ -367,7 +367,7 @@ bool Core_EigenTest::check_full(int type)
for (int i = 0; i < ntests; ++i)
{
int src_size = (int)(std::pow(2.0, (rng.uniform(0, MAX_DEGREE) + 1.)));
int src_size = (int)(std::pow(2, (rng.uniform(0, MAX_DEGREE) + 1.)));
cv::Mat src(src_size, src_size, type);
+4 -4
View File
@@ -146,7 +146,7 @@ void Core_PowTest::get_minmax_bounds( int /*i*/, int /*j*/, int type, Scalar& lo
if( power > 0 )
{
double mval = cvtest::getMaxVal(type);
double u1 = pow(mval,1./power)*2;
double u1 = std::pow(mval,1./power)*2;
u = MIN(u,u1);
}
@@ -322,7 +322,7 @@ void Core_PowTest::prepare_to_validation( int /*test_case_idx*/ )
for( j = 0; j < ncols; j++ )
{
double val = ((float*)a_data)[j];
val = pow( fabs(val), power );
val = std::pow( fabs(val), power );
((float*)b_data)[j] = (float)val;
}
else
@@ -340,7 +340,7 @@ void Core_PowTest::prepare_to_validation( int /*test_case_idx*/ )
for( j = 0; j < ncols; j++ )
{
double val = ((double*)a_data)[j];
val = pow( fabs(val), power );
val = std::pow( fabs(val), power );
((double*)b_data)[j] = (double)val;
}
else
@@ -2394,7 +2394,7 @@ void Core_SolvePolyTest::run( int )
for (int j = 0; j < n; ++j)
{
s += fabs(r[j].real()) + fabs(r[j].imag());
div += sqrt(pow(r[j].real() - ar[j].real(), 2) + pow(r[j].imag() - ar[j].imag(), 2));
div += sqrt(std::pow(r[j].real() - ar[j].real(), 2) + std::pow(r[j].imag() - ar[j].imag(), 2));
}
div /= s;
pass = pass && div < err_eps;