From cd3dc424fbec0e1bc54bf58cd8a2385d09065be6 Mon Sep 17 00:00:00 2001 From: Shiven Solanki Date: Tue, 27 Oct 2026 02:23:59 +0800 Subject: [PATCH] Merge pull request #30463 from sshivensolanki:fix-countnonzero-fp8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit core: support fp8 depths in countNonZero and hasNonZero - #30463 Fixes #30461 ### Problem `countNonZeroTab` (`count_non_zero.simd.hpp`) and `hasNonZeroTab` (`has_non_zero.simd.hpp`) have `0` in the `CV_8F_E4M3FN` and `CV_8F_E4M3FNUZ` slots, so `cv::countNonZero` and `cv::hasNonZero` fail with `func != 0` for fp8 input. `cv::findNonZero` already supports both depths since #30341. ### Fix - `CV_8F_E4M3FN`: new `countNonZero8f` / `hasNonZero8f` kernels. Same as the 8u kernels, but the sign bit is ignored so `-0.0` (`0x80`) counts as zero: the vector path reuses the `v_add_wrap(x, x)` compare of the 16f kernels, and the scalar tail uses `CHECK_NZ_FP8(x) = ((x) & 0x7f) != 0`. - `CV_8F_E4M3FNUZ`: the existing 8u kernels. This format has no `-0`; `0x80` is its NaN and counts as non-zero, the same as in `findNonZero`. ### Tests - `CountNonZero1D` now also runs on both fp8 depths. - `HasNonZeroNegZeros` now also covers `CV_8F_E4M3FNC1`. - New `CountNonZeroFP8.sign_bit`, a `TEST_P` over both fp8 depths and n = 1, 100, 1031 (scalar tail, single-vector loop, unrolled loop). It fills a `CV_32F` Mat with random values in ±[0.5, 4), sets every 5th element and the last one to `-0.0`, `NaN` or `+0.0`, converts it to fp8, and checks `countNonZero` / `hasNonZero` against the number of non-zero values set. `-0.0` becomes `0x80` in E4M3FN and must count as zero; `NaN` becomes `0x80` in E4M3FNUZ and must count as non-zero. These fail on current 5.x (`func != 0`) and pass with this patch. All `*NonZero*` and `*FP8*` tests in `opencv_test_core` pass locally on Apple M1 (190 tests), with OpenCL enabled and with `OPENCV_OPENCL_DEVICE=disabled`. ### 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 - [x] There is a reference to the original bug report and related work - [x] 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/core/src/count_non_zero.simd.hpp | 7 ++++- modules/core/src/has_non_zero.simd.hpp | 6 +++- modules/core/test/test_countnonzero.cpp | 38 +++++++++++++++++++++++- modules/core/test/test_hasnonzero.cpp | 2 +- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/modules/core/src/count_non_zero.simd.hpp b/modules/core/src/count_non_zero.simd.hpp index 9a4ebaa0d3..b9039197a8 100644 --- a/modules/core/src/count_non_zero.simd.hpp +++ b/modules/core/src/count_non_zero.simd.hpp @@ -155,6 +155,9 @@ static int funcname( const void* src_ptr, int len ) \ // 16-bit float: mask the sign bit so -0.0 (0x8000) reads as zero. #undef CHECK_NZ_FP16 #define CHECK_NZ_FP16(x) (((x) & 0x7fff) != 0) +// 8-bit float: mask the sign bit so -0.0 (0x80) reads as zero. +#undef CHECK_NZ_FP8 +#define CHECK_NZ_FP8(x) (((x) & 0x7f) != 0) #undef VEC_CMP_EQ_Z_FP16 #define VEC_CMP_EQ_Z_FP16(x, z) v_eq(v_add_wrap(x, x), z) #undef VEC_CMP_EQ_Z_FP @@ -183,6 +186,7 @@ DEFINE_NONZERO_FUNC(countNonZero16u, u16, u32, s16, ushort, v_uint16, v_uint32, DEFINE_NONZERO_FUNC(countNonZero32s, s32, s32, s32, int, v_int32, v_int32, v_eq, v_add, UPDATE_SUM_S32, CHECK_NZ_INT) DEFINE_NONZERO_FUNC(countNonZero32f, u32, u32, s32, uint, v_uint32, v_uint32, VEC_CMP_EQ_Z_FP, v_add, UPDATE_SUM_S32, CHECK_NZ_FP) DEFINE_NONZERO_FUNC(countNonZero16f, u16, u32, s16, ushort, v_uint16, v_uint32, VEC_CMP_EQ_Z_FP16, v_add_wrap, UPDATE_SUM_U16, CHECK_NZ_FP16) +DEFINE_NONZERO_FUNC(countNonZero8f, u8, u32, s8, uchar, v_uint8, v_uint32, VEC_CMP_EQ_Z_FP16, v_add_wrap, UPDATE_SUM_U8, CHECK_NZ_FP8) #undef DEFINE_NONZERO_FUNC_NOSIMD #define DEFINE_NONZERO_FUNC_NOSIMD(funcname, T) \ @@ -237,7 +241,8 @@ CountNonZeroFunc getCountNonZeroTab(int depth) countNonZero64s, countNonZero64s, countNonZero32s, - 0 + countNonZero8f, + countNonZero8u }; return countNonZeroTab[depth]; diff --git a/modules/core/src/has_non_zero.simd.hpp b/modules/core/src/has_non_zero.simd.hpp index 13b582fa97..fdcbd62aae 100644 --- a/modules/core/src/has_non_zero.simd.hpp +++ b/modules/core/src/has_non_zero.simd.hpp @@ -79,6 +79,8 @@ static bool funcname( const void* _src, size_t len ) \ #define CHECK_NZ_FP(x) (((x)<<1) != 0) #undef CHECK_NZ_FP16 #define CHECK_NZ_FP16(x) (((x)&0x7fff) != 0) +#undef CHECK_NZ_FP8 +#define CHECK_NZ_FP8(x) (((x)&0x7f) != 0) #undef VEC_CMP_EQ_Z_FP16 #define VEC_CMP_EQ_Z_FP16(x, z) v_ne(v_add_wrap(x, x), z) #undef VEC_CMP_EQ_Z_FP @@ -92,6 +94,7 @@ DEFINE_HASNONZERO_FUNC(hasNonZero64s, s64, int64, v_int64, v_ne, CHECK_NZ_INT) DEFINE_HASNONZERO_FUNC(hasNonZero32f, s32, int, v_int32, VEC_CMP_EQ_Z_FP, CHECK_NZ_FP) DEFINE_HASNONZERO_FUNC(hasNonZero64f, s64, int64, v_int64, VEC_CMP_EQ_Z_FP, CHECK_NZ_FP) DEFINE_HASNONZERO_FUNC(hasNonZero16f, u16, ushort, v_uint16, VEC_CMP_EQ_Z_FP16, CHECK_NZ_FP16) +DEFINE_HASNONZERO_FUNC(hasNonZero8f, u8, uchar, v_uint8, VEC_CMP_EQ_Z_FP16, CHECK_NZ_FP8) HasNonZeroFunc getHasNonZeroFunc(int depth) { @@ -110,7 +113,8 @@ HasNonZeroFunc getHasNonZeroFunc(int depth) hasNonZero64s, hasNonZero64s, hasNonZero32s, - 0 + hasNonZero8f, + hasNonZero8u }; return hasNonZeroTab[depth]; diff --git a/modules/core/test/test_countnonzero.cpp b/modules/core/test/test_countnonzero.cpp index 9a58d81f6b..3f6bac2155 100644 --- a/modules/core/test/test_countnonzero.cpp +++ b/modules/core/test/test_countnonzero.cpp @@ -335,7 +335,43 @@ TEST_P(CountNonZero1D, /**/) } INSTANTIATE_TEST_CASE_P(Core, CountNonZero1D, - testing::Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32U, CV_32S, CV_64U, CV_64S, CV_32F, CV_64F, CV_16F, CV_16BF, CV_Bool) + testing::Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32U, CV_32S, CV_64U, CV_64S, CV_32F, CV_64F, CV_16F, CV_16BF, CV_Bool, CV_8F_E4M3FN, CV_8F_E4M3FNUZ) ); +// -0.0 becomes 0x80 in E4M3FN (counts as zero) and NaN becomes 0x80 in E4M3FNUZ (counts as non-zero). +// The sizes reach the scalar tail (1), the single-vector loop (100) and the unrolled loop (1031). +typedef testing::TestWithParam > CountNonZeroFP8; + +TEST_P(CountNonZeroFP8, sign_bit) +{ + const int depth = get<0>(GetParam()); + const int n = get<1>(GetParam()); + + // random values in +-[0.5, 4): fp8 never rounds them to zero + Mat src(1, n, CV_32F), m; + randu(src, 0.5f, 4.f); + for (int i = 1; i < n; i += 2) + src.at(i) = -src.at(i); + + // every 5th element and the last one become -0.0, NaN or +0.0 + const float special[] = { -0.f, std::numeric_limits::quiet_NaN(), 0.f }; + int nz = n; + for (int i = 0; i < n; i++) + { + if (i % 5 != 0 && i != n - 1) + continue; + src.at(i) = special[(i / 5) % 3]; + nz -= src.at(i) == 0.f; // -0.0 and +0.0 are zero, NaN is not + } + src.convertTo(m, depth); + + EXPECT_EQ(nz, countNonZero(m)); + EXPECT_EQ(nz != 0, hasNonZero(m)); +} + +INSTANTIATE_TEST_CASE_P(Core, CountNonZeroFP8, testing::Combine( + testing::Values(CV_8F_E4M3FN, CV_8F_E4M3FNUZ), + testing::Values(1, 100, 1031) +)); + }} // namespace diff --git a/modules/core/test/test_hasnonzero.cpp b/modules/core/test/test_hasnonzero.cpp index 00634b26a7..904ead8b3d 100644 --- a/modules/core/test/test_hasnonzero.cpp +++ b/modules/core/test/test_hasnonzero.cpp @@ -76,7 +76,7 @@ TEST_P(HasNonZeroNegZeros, hasNonZeroNegZeros) INSTANTIATE_TEST_CASE_P(Core, HasNonZeroNegZeros, testing::Combine( - testing::Values(CV_32FC1, CV_64FC1, CV_16FC1, CV_16BFC1), + testing::Values(CV_32FC1, CV_64FC1, CV_16FC1, CV_16BFC1, CV_8F_E4M3FNC1), testing::Values(Size(1, 1), Size(320, 240), Size(127, 113), Size(1, 113)) ) );