mirror of
https://github.com/opencv/opencv.git
synced 2026-11-22 09:23:44 -06:00
Review request: fix the narrow globally rather than clamping at one call site.
v_pack() saturated at 8, 16 and 32 bits but truncated at 64->32, and v_pack_u() had no 64-bit form
at all. That is why convertTo() had to clamp by hand before packing: an out-of-range value wrapped
first, and the later saturating narrow to 8 or 16 bits then saturated the WRAPPED value, which is
not merely imprecise but the wrong end of the range (4294967295 -> -1 -> 0 for uchar, not 255).
The backends did not even agree with each other: RVV narrowed with vnclip/vnclipu for 8, 16 and 32
bits but with the truncating vnsra/vnsrl at 64, and NEON used vqmovn for the narrow widths and the
plain vmovn at 64.
All of them now saturate, and all of them gain v_pack_u() for int64 -> uint32:
- NEON, MSA, RVV, RVV071: the saturating primitive already existed next to the one in use
(vqmovn/vqmovun, msa_qpack*, vnclip/vnclipu), so these are one-word changes.
- VSX: vec_packs/vec_packsu instead of vec_pack. The 64-bit v_pack_u was already written here and
commented out with "not implemented on other platforms", so it is simply enabled.
- AVX-512: vpmovsqd / vpmovusqd narrow with saturation natively.
- SSE and AVX2: decided on the two 32-bit halves, so no 64-bit compare is needed - a value fits
exactly when the high half equals sext(low), and cmpgt on the high half yields 0 / all-ones,
which doubles as the saturated value for pack_u.
- WASM, LSX, LASX: no saturating narrow, so OPENCV_HAL_IMPL_SATURATING_PACK_64 in
intrin_forward.hpp builds it from the backend's truncating narrow, again from the 32-bit halves.
- intrin_cpp.hpp: saturate_cast instead of static_cast, matching every other width.
A 64-bit ordering compare would have been the obvious way to decide saturation and is the one thing
to avoid here: at the SSE and NEON baselines v_gt on 64-bit lanes is the sign of (b - a), so it
overflows - and answers the wrong way - exactly when the operands sit at opposite ends of the 64-bit
range, which is the case a saturating narrow has to get right.
convertTo() now calls v_pack()/v_pack_u() directly and keeps one local helper, for uint64 -> int32:
no width has a v_pack_s, so that direction has no HAL counterpart to use. Its int64 -> uint64 path
also stops using v_gt(x, 0) to clamp negatives, for the reason above, and tests the sign bit instead.
Tests: test_pack64_saturation and test_pack64_u_saturation check the three narrows either side of
the destination range, wired into the uint32 and int32 suites. The pack_saturate_cast carve-out in
test_intrin_utils.hpp that documented 64-bit as non-saturating is removed, so the existing
test_pack<> now holds every width to the same rule.
v_rshr_pack_u is left without a 64-bit form: it has no callers and the rounding step would need its
own decision about where the rounding happens relative to the clamp.