From 155f4e18fda883b96cb4157faeb90b24979e84b1 Mon Sep 17 00:00:00 2001 From: Shiven Solanki Date: Wed, 21 Oct 2026 15:22:28 +0800 Subject: [PATCH] core: SIMD fast path for findNonZero on 8-bit inputs --- modules/core/perf/perf_stat.cpp | 22 +++++++++++++++++ modules/core/src/count_non_zero.dispatch.cpp | 15 ++++++++++- modules/core/test/test_arithm.cpp | 26 ++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/modules/core/perf/perf_stat.cpp b/modules/core/perf/perf_stat.cpp index 42f157124e..c42f5ea3b2 100644 --- a/modules/core/perf/perf_stat.cpp +++ b/modules/core/perf/perf_stat.cpp @@ -136,4 +136,26 @@ PERF_TEST_P(Size_MatType, hasNonZero, testing::Combine( testing::Values( TYPICAL SANITY_CHECK_NOTHING(); } +typedef tuple Size_Density_t; +typedef perf::TestBaseWithParam Size_Density; + +PERF_TEST_P(Size_Density, findNonZero, testing::Combine( + testing::Values(TYPICAL_MAT_SIZES), + testing::Values(0.0, 0.001, 0.01, 0.1, 0.5))) +{ + Size sz = get<0>(GetParam()); + double density = get<1>(GetParam()); + + Mat r(sz, CV_32FC1); + randu(r, 0, 1); + Mat src = r < density; // CV_8UC1: 255 where r < density, 0 elsewhere + std::vector idx; + + declare.in(src); + + TEST_CYCLE() findNonZero(src, idx); + + SANITY_CHECK_NOTHING(); +} + } // namespace diff --git a/modules/core/src/count_non_zero.dispatch.cpp b/modules/core/src/count_non_zero.dispatch.cpp index e9472a9d8b..f4aa0d66af 100644 --- a/modules/core/src/count_non_zero.dispatch.cpp +++ b/modules/core/src/count_non_zero.dispatch.cpp @@ -167,7 +167,20 @@ void findNonZero(InputArray _src, OutputArray _idx) const uchar* ptr8 = src.ptr(i); if( depth == CV_8U || depth == CV_8S || depth == CV_Bool || depth == CV_8F_E4M3FNUZ ) { - for( j = 0; j < cols; j++ ) + j = 0; +#if (CV_SIMD || CV_SIMD_SCALABLE) + // skip whole SIMD blocks of zeros; only scan blocks that contain a non-zero byte + const int vlanes = VTraits::vlanes(); + const v_uint8 vzero = vx_setzero_u8(); + for( ; j <= cols - vlanes; j += vlanes ) + { + if( !v_check_any(v_ne(vx_load(ptr8 + j), vzero)) ) + continue; + for( int t = j; t < j + vlanes; t++ ) + if( ptr8[t] != 0 ) buf[k++] = t; + } +#endif + for( ; j < cols; j++ ) if( ptr8[j] != 0 ) buf[k++] = j; } else if( depth == CV_16U || depth == CV_16S ) diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index bdc9718181..ed76b8ff6d 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -2414,6 +2414,32 @@ TEST(Core_FindNonZero, fp8) } } +// widths around and above the SIMD block size, so both the vector loop and the scalar tail run +TEST(Core_FindNonZero, wide_rows_8u) +{ + RNG& rng = theRNG(); + for (int cols : { 15, 16, 17, 63, 64, 65, 127, 1000 }) + { + for (double density : { 0.0, 0.01, 0.3 }) + { + Mat r(3, cols + 5, CV_32FC1); + rng.fill(r, RNG::UNIFORM, 0, 1); + Mat full = r < density; + Mat src = full.colRange(2, 2 + cols); // ROI: rows are not continuous + + std::vector expected; + for (int y = 0; y < src.rows; y++) + for (int x = 0; x < src.cols; x++) + if (src.at(y, x) != 0) + expected.push_back(Point(x, y)); + + std::vector pts; + findNonZero(src, pts); + EXPECT_EQ(expected, pts) << "cols=" << cols << " density=" << density; + } + } +} + TEST(Core_BoolVector, support) { std::vector test;