diff --git a/modules/core/perf/perf_stat.cpp b/modules/core/perf/perf_stat.cpp index 3f33f7f7db..c3ba3d1235 100644 --- a/modules/core/perf/perf_stat.cpp +++ b/modules/core/perf/perf_stat.cpp @@ -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 ) )) { diff --git a/modules/core/src/sum.simd.hpp b/modules/core/src/sum.simd.hpp index f790fc733a..eb572da940 100644 --- a/modules/core/src/sum.simd.hpp +++ b/modules/core/src/sum.simd.hpp @@ -293,7 +293,7 @@ struct Sum_SIMD #endif #endif -template +template 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_(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); } diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index ec120816ab..c4a561111d 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -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 > 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) { {