mirror of
https://github.com/opencv/opencv.git
synced 2026-11-15 22:08:03 -06:00
core: accept both float->int saturation results in OCL ConvertToSaturate test - #30448 Fixes the `macOS-x64 / BuildAndTest` failure from #30386, which makes 5.x macOS-x64 CI red for every PR (e.g. #30302): ``` [ RUN ] OCL_MatrixOperation/ConvertToSaturate.Accuracy/16, where GetParam() = (CV_32F, CV_32S) test_matrix_operation.cpp:247: Failure cv::countNonZero(diff) Which is: 2 ``` INT_MAX isn't representable in float, so a saturated `CV_32F -> CV_32S` result can legitimately be either value: - x86 CPU (`v_round` / `cvRound`) clamps to `CV__FLT2INT_MAX_F` = 2147483520 before converting (since #30007). - OpenCL `convert_int_sat_rte` and NEON `vcvtnq_s32_f32` give INT_MAX = 2147483647. So on an x86 host the out-of-range inputs `3e9` and `1e20` differ by 127 between `Mat` and `UMat`. On Apple M1 both sides give INT_MAX, which is why it only shows up on macOS-x64. For this pair the test now clamps both results to 2147483520 before the exact comparison, i.e. it accepts any value in [2147483520, INT_MAX] as saturated. In-range values and all other depth pairs are still compared exactly. **Testing** (Apple M1 Pro, OpenCL on; native arm64 build and an x86_64 build run under Rosetta 2) `Mat::convertTo` vs `UMat::convertTo` for `CV_32F -> CV_32S`: | input | arm64 CPU | x86_64 CPU | OpenCL | |---|---|---|---| | 3e9 | 2147483647 | 2147483520 | 2147483647 | | 1e20 | 2147483647 | 2147483520 | 2147483647 | The x86_64 build reproduces the CI mismatch. With this change, on both builds: - `opencv_test_core --gtest_filter=*ConvertToSaturate*`: 135 passed. - `opencv_test_core --gtest_filter=OCL_*:*ConvertTo*:*convertTo*:*Core_ConvertScale*`: 7549 passed. ### 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