1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

Merge pull request #28267 from akretz:absdiff-int

Fix absdiff with int arguments #28267

I believe the fix to the undefined behavior described in #27080 is simply casting to unsigned before subtraction, because

1. casting int to unsigned is well-defined; negative values get represented modulo $2^{32}$
2. overflow in unsigned subtraction is well-defined and the results are modulo $2^{32}$

Since we are computing everything modulo $2^{32}$ and the result must always be a non-negative number below $2^{32}$, this computation should be well-defined and correct.

I have verified this on ARM Apple Clang and on x64 Linux gcc with `-O3` and both produce the correct values in the reproducer of #27080. The test I have added fails with the integer overflow on both platforms I have tested. Perhaps @fengyuentau could verify if this also fixes the issue on the platforms he has tested?

I have added a fix and removed the workarounds. I recommended this approach in #28229, but he decided to revert it.


### 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.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Adrian Kretz
2026-02-15 08:29:29 +01:00
committed by GitHub
parent 82ff8e45e9
commit 196a8afe76
3 changed files with 17 additions and 48 deletions
-47
View File
@@ -77,32 +77,6 @@ struct NormDiffL1_SIMD {
}
};
// This specialization is needed because https://github.com/opencv/opencv/issues/27080
template <>
struct NormDiffL1_SIMD<int, double> {
inline double operator() (const int* src1, const int* src2, int n) const {
double s = 0;
int j = 0;
#if (CV_SIMD_64F || CV_SIMD_SCALABLE_64F)
v_float64 r0 = vx_setzero_f64(), r1 = vx_setzero_f64();
for (; j <= n - VTraits<v_int32>::vlanes(); j += VTraits<v_int32>::vlanes()) {
v_int32 v01 = vx_load(src1 + j), v02 = vx_load(src2 + j);
v_uint32 v0 = v_absdiff(v01, v02);
v_uint64 ev0, ev1;
v_expand(v0, ev0, ev1);
r0 = v_add(r0, v_cvt_f64(v_reinterpret_as_s64(ev0)));
r1 = v_add(r1, v_cvt_f64(v_reinterpret_as_s64(ev1)));
}
s += v_reduce_sum(v_add(r0, r1));
#endif
for (; j < n; j++) {
double d1 = (double)src1[j], d2 = (double)src2[j];
s += (double)std::abs(d1 - d2);
}
return s;
}
};
template <typename T, typename ST>
struct NormDiffL2_SIMD {
inline ST operator() (const T* src1, const T* src2, int n) const {
@@ -1348,27 +1322,6 @@ normDiffL1_(const T* src1, const T* src2, const uchar* mask, ST* _result, int le
return 0;
}
// This specialization is needed because https://github.com/opencv/opencv/issues/27080
template<> int
normDiffL1_(const int* src1, const int* src2, const uchar* mask, double* _result, int len, int cn) {
double result = *_result;
if( !mask ) {
NormDiffL1_SIMD<int, double> op;
result += op(src1, src2, len*cn);
} else {
for( int i = 0; i < len; i++, src1 += cn, src2 += cn ) {
if( mask[i] ) {
for( int k = 0; k < cn; k++ ) {
double d1 = (double)src1[k], d2 = (double)src2[k];
result += (double)std::abs(d1 - d2);
}
}
}
}
*_result = result;
return 0;
}
template<typename T, typename ST> int
normDiffL2_(const T* src1, const T* src2, const uchar* mask, ST* _result, int len, int cn) {
ST result = *_result;