Merge pull request #27368 from fengyuentau:4x/imgproc/CreateHanningWindow-simd

imgproc: vectorize cv::createHanningWindow #27368

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Yuantao Feng 2025-06-20 17:00:37 +08:00 committed by GitHub
parent 7f4c89c7cc
commit 3259863924
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -38,6 +38,7 @@
#include "precomp.hpp"
#include <vector>
#include "opencv2/core/hal/intrin.hpp"
namespace cv
{
@ -614,8 +615,27 @@ void cv::createHanningWindow(OutputArray _dst, cv::Size winSize, int type)
double* const wc = _wc.data();
double coeff0 = 2.0 * CV_PI / (double)(cols - 1), coeff1 = 2.0 * CV_PI / (double)(rows - 1);
for(int j = 0; j < cols; j++)
wc[j] = 0.5 * (1.0 - cos(coeff0 * j));
int c = 0;
#if CV_SIMD_64F || CV_SIMD_SCALABLE_64F
const int nlanes32 = VTraits<v_float32>::vlanes();
const int nlanes64 = VTraits<v_float64>::vlanes();
const int max_nlanes = VTraits<v_float64>::max_nlanes;
std::array<double, max_nlanes> index;
std::iota(index.data(), index.data()+max_nlanes, 0.f);
v_float64 vindex = vx_load(index.data());
v_float64 delta = vx_setall_f64(VTraits<v_float64>::vlanes());
v_float64 vcoeff0 = vx_setall_f64(coeff0);
v_float64 one = vx_setall_f64(1.f);
v_float64 half = vx_setall_f64(0.5f);
for (; c <= cols - nlanes64; c += nlanes64)
{
v_float64 v = v_mul(half, v_sub(one, v_cos(v_mul(vcoeff0, vindex))));
vx_store(wc + c, v);
vindex = v_add(vindex, delta);
}
#endif
for(; c < cols; c++)
wc[c] = 0.5 * (1.0 - cos(coeff0 * c));
if(dst.depth() == CV_32F)
{
@ -623,7 +643,17 @@ void cv::createHanningWindow(OutputArray _dst, cv::Size winSize, int type)
{
float* dstData = dst.ptr<float>(i);
double wr = 0.5 * (1.0 - cos(coeff1 * i));
for(int j = 0; j < cols; j++)
int j = 0;
#if CV_SIMD_64F || CV_SIMD_SCALABLE_64F
v_float64 vwr = vx_setall_f64(wr);
for (; j <= cols - nlanes32; j += nlanes32)
{
v_float64 v0 = v_mul(vwr, vx_load(wc + j));
v_float64 v1 = v_mul(vwr, vx_load(wc + j + nlanes64));
vx_store(dstData + j, v_cvt_f32(v0, v1));
}
#endif
for(; j < cols; j++)
dstData[j] = (float)(wr * wc[j]);
}
}
@ -633,7 +663,16 @@ void cv::createHanningWindow(OutputArray _dst, cv::Size winSize, int type)
{
double* dstData = dst.ptr<double>(i);
double wr = 0.5 * (1.0 - cos(coeff1 * i));
for(int j = 0; j < cols; j++)
int j = 0;
#if CV_SIMD_64F || CV_SIMD_SCALABLE_64F
v_float64 vwr = vx_setall_f64(wr);
for (; j <= cols - nlanes64; j += nlanes64)
{
v_float64 v = v_mul(vwr, vx_load(wc + j));
vx_store(dstData + j, v);
}
#endif
for(; j < cols; j++)
dstData[j] = wr * wc[j];
}
}