From 32598639244ada2b5095baf08118a2ec80c6bb37 Mon Sep 17 00:00:00 2001 From: Yuantao Feng Date: Fri, 20 Jun 2025 17:00:37 +0800 Subject: [PATCH] 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 --- modules/imgproc/src/phasecorr.cpp | 47 ++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/modules/imgproc/src/phasecorr.cpp b/modules/imgproc/src/phasecorr.cpp index d2f88420be..3fbe62883d 100644 --- a/modules/imgproc/src/phasecorr.cpp +++ b/modules/imgproc/src/phasecorr.cpp @@ -38,6 +38,7 @@ #include "precomp.hpp" #include +#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::vlanes(); + const int nlanes64 = VTraits::vlanes(); + const int max_nlanes = VTraits::max_nlanes; + std::array 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::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(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(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]; } }