mirror of
https://github.com/opencv/opencv.git
synced 2026-11-30 13:22:08 -06:00
Merge pull request #30463 from sshivensolanki:fix-countnonzero-fp8
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
This commit is contained in:
@@ -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];
|
||||
|
||||
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user