Files
opencv/modules/core/src
Adrian Kretz 196a8afe76 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
2026-02-15 10:29:29 +03:00
..
2026-02-14 15:37:33 +03:00
2025-07-22 09:47:19 +03:00
2023-01-09 11:08:02 +00:00
2024-11-14 08:33:22 +03:00
2025-11-18 08:51:08 +03:00
2024-04-02 16:39:54 +03:00
2018-09-07 20:33:52 +09:00
2024-06-26 19:01:34 +03:00
2024-08-28 15:06:19 +03:00
2025-03-11 17:23:06 +03:00
2025-11-18 08:51:08 +03:00
2024-04-02 16:39:54 +03:00
2025-11-18 08:51:08 +03:00
2025-02-05 09:28:27 +03:00
2019-03-05 14:47:04 +03:00
2022-03-03 07:21:53 +09:00
2019-12-27 12:46:53 +00:00
2026-02-14 15:37:33 +03:00
2025-09-26 12:10:49 +03:00
2024-11-14 08:33:22 +03:00
2026-02-14 15:37:33 +03:00
2025-07-22 09:47:19 +03:00
2025-05-07 13:17:42 +03:00
2025-05-07 13:17:42 +03:00
2025-07-22 09:47:19 +03:00
2024-02-12 14:20:35 +03:00
2025-09-10 10:46:54 +03:00
2024-10-11 02:18:14 +02:00
2026-02-14 15:37:33 +03:00
2024-11-14 08:33:22 +03:00
2025-01-13 16:34:28 +01:00
2024-11-14 08:33:22 +03:00
2018-02-12 14:14:08 +03:00
2018-08-27 14:42:18 +03:00
2025-06-10 10:16:50 +03:00
2026-02-14 15:37:33 +03:00
2014-08-15 12:09:16 +04:00
2025-11-18 08:51:08 +03:00
2022-02-22 19:55:26 +00:00