Repository navigation
Conversation
The vertical pass adds four tap products in an int32_t and subtracts the rounding offset last. With 16-bit samples of 42456 or more the sum passes INT32_MAX after the third tap, which is signed overflow. Start the accumulator from the offset instead: the partial sums then stay within +/-1796403056 and the result is unchanged. Applies to adm_dwt2_16 and adm_dwt2_16_lo, the vertical pass of adm_dwt2_16_avx2 and adm_dwt2_16_avx512, and the CUDA vertical kernel in its uint16_t instance. test_adm_dwt2_16 compares each dispatch against a 64-bit reference and builds the three C files with -fsanitize=signed-integer-overflow where the compiler supports it, so the old order fails the test. Signed-off-by: Lusoris <lusoris@pm.me>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The vertical pass of
adm_dwt2_16()adds four tap products in anint32_tand subtracts the rounding offset last. With 16-bit samples of 42456 or more the sum passesINT32_MAXafter the third tap, which is signed integer overflow. This is the second of the two concerns PR 1601 mixed together; the tap-shift change is in a separate PR.Fix
Start the accumulator from the offset, then add the four taps. The low-pass taps are {15826, 27411, 7345, -4240} and the offset is 46342 * 2^(bits-1), so at 16 bits the partial sums now stay within -1,796,403,056 to 1,796,356,714. The old order reached 3,314,891,370. The final value is the same.
The same order is applied at every site with this sum:
adm_dwt2_16()andadm_dwt2_16_lo()ininteger_adm.cadm_dwt2_16_avx2()andadm_dwt2_16_avx512()adm_dwt2_8_vert_hori_kernelinadm_dwt2.cu, which is also instantiated foruint16_tThe high-pass sum (at most 27411 * 65535) and the horizontal passes (int16 inputs) stay inside
int32_tand are not changed.Before and after
UBSan build (
-Db_sanitize=undefined), 16-bit 576x324 pairsrc01_hrc00/src01_hrc01(yuv420p16le, from the clips that lived inpython/test/resource/yuvbeforebac8b607),--feature adm:cf677baa--cpumask 16)Tests
test_adm_dwt2_16runs each available dispatch over inputs that cover the full 16-bit sample range and compares the output against a 64-bit reference.Overflow wraps back to the right value on the usual targets, so comparing values alone cannot fail on the old code. The test therefore builds its own copies of
integer_adm.c,x86/adm_avx2.candx86/adm_avx512.cwith-fsanitize=signed-integer-overflow -fno-sanitize-recover=signed-integer-overflowwhen the compiler accepts and links those flags; the rest of the library comes from the regular build. Where the flags are not available the test still does the value comparison. This is most of thetest/meson.buildchange.With the fix reverted in one file at a time the test fails at that file's site (
integer_adm.c:2626,adm_avx2.c:2942,adm_avx512.c:3085), with GCC 16.2.1 and Clang 23.1.5.Validation
integer_adm_cuda.cmarks theuint16_tkernel as untested.