Merge pull request #30800 from sohailshk/fix/sum-32s-wraparound

core: stop cv::sum() wrapping at 32 bits for CV_32S
This commit is contained in:
Abhishek Gola
2026-11-14 09:21:00 +05:30
committed by GitHub
3 changed files with 46 additions and 3 deletions

View File

@@ -25,6 +25,25 @@ PERF_TEST_P(Size_MatType, sum, testing::Combine( testing::Values( TYPICAL_MAT_SI
SANITY_CHECK(s, 1e-6, ERROR_RELATIVE);
}
// CV_32S is not in TYPICAL_MAT_TYPES_STAT; it is the depth whose scalar accumulation
// step is widened.
PERF_TEST_P(Size_MatType, sum_wide,
testing::Combine(testing::Values(TYPICAL_MAT_SIZES),
testing::Values(CV_32SC1, CV_32SC3)))
{
Size sz = get<0>(GetParam());
int type = get<1>(GetParam());
Mat arr(sz, type);
Scalar s;
declare.in(arr, WARMUP_RNG).out(s);
TEST_CYCLE() s = sum(arr);
SANITY_CHECK_NOTHING();
}
PERF_TEST_P(Size_MatType, mean, testing::Combine( testing::Values( TYPICAL_MAT_SIZES ),
testing::Values( TYPICAL_MAT_TYPES_STAT ) ))
{

View File

@@ -293,7 +293,7 @@ struct Sum_SIMD<float, double>
#endif
#endif
template<typename T, typename ST>
template<typename T, typename ST, typename WT=T>
static int sum_(const T* src0, const uchar* mask, ST* dst, int len, int cn )
{
const T* src = src0;
@@ -309,7 +309,7 @@ static int sum_(const T* src0, const uchar* mask, ST* dst, int len, int cn )
#if CV_ENABLE_UNROLLED
for(; i <= len - 4; i += 4, src += cn*4 )
s0 += src[0] + src[cn] + src[cn*2] + src[cn*3];
s0 += (WT)src[0] + (WT)src[cn] + (WT)src[cn*2] + (WT)src[cn*3];
#endif
for( ; i < len; i++, src += cn )
s0 += src[0];
@@ -424,7 +424,7 @@ static int sum16s( const short* src, const uchar* mask, int* dst, int len, int c
{ CV_INSTRUMENT_REGION(); return sum_(src, mask, dst, len, cn); }
static int sum32s( const int* src, const uchar* mask, double* dst, int len, int cn )
{ CV_INSTRUMENT_REGION(); return sum_(src, mask, dst, len, cn); }
{ CV_INSTRUMENT_REGION(); return sum_<int, double, int64>(src, mask, dst, len, cn); }
static int sum32f( const float* src, const uchar* mask, double* dst, int len, int cn )
{ CV_INSTRUMENT_REGION(); return sum_(src, mask, dst, len, cn); }

View File

@@ -3004,6 +3004,30 @@ TEST(Core_AbsDiff, regression_29639_integer_overflow)
}
}
// cv::sum() returns doubles, so a CV_32S array whose total exceeds the 32-bit range must still
// come back exact. The scalar step of the accumulation added four elements to each other as int
// before reaching the double accumulator, so large values wrapped (signed overflow).
typedef testing::TestWithParam< tuple<int, int> > Core_SumWide;
TEST_P(Core_SumWide, no_32bit_wraparound)
{
const int cn = get<0>(GetParam());
const int len = get<1>(GetParam());
const double value = 2147483647.0;
Mat flat(1, len * cn, CV_32S, Scalar(value));
Mat src = flat.reshape(cn);
const Scalar got = cv::sum(src);
const double expected = (double)len * value;
for (int c = 0; c < cn; c++)
EXPECT_DOUBLE_EQ(expected, got[c])
<< "channels " << cn << ", length " << len << ", channel " << c;
}
INSTANTIATE_TEST_CASE_P(/**/, Core_SumWide,
testing::Combine(testing::Values(1, 2, 3, 4), testing::Values(4, 7, 127)));
TEST(Core_MeanStdDev, regression_multichannel)
{
{