Skip to content

libvmaf/adm: shift the adm_cm cube arithmetically in the AVX2 path - #1729

Open
lusoris wants to merge 1 commit into
Netflix:masterfrom
vidiko:fix/adm-cm-avx2-cube-shift
Open

lusoris wants to merge 1 commit into
Netflix:masterfrom
vidiko:fix/adm-cm-avx2-cube-shift

Conversation

@lusoris

@lusoris lusoris commented Nov 18, 2026

Copy link
Copy Markdown

The AVX2 path of adm_cm shifts the 64-bit cube product with a logical shift where the C code shifts a signed value. This is the second concern split out of PR 1602; the threshold-width change is PR 1728.

The defect

adm_cm cubes the masked coefficient as (x_sq * x + round) >> shift, with x_sq the rounded square held in a 32-bit lane. The C code does this on a signed int64_t. The two AVX2 macros use _mm256_srli_epi64, which shifts zeros in from the top. The results differ when the product is negative, and that happens when the rounded square is above INT32_MAX and wraps in its lane.

Fix

Use the sra_epi64 helper that adm_avx2.c already has, in the scale 0 macro (both 64-bit halves) and in the macro of scales 1 to 3. Three lines change. The AVX-512 path already uses _mm512_srai_epi64 there.

Can a real frame reach it?

I did not find one, and for the default options a bound on the coefficient range says no: with 8- to 16-bit input the largest rounded square stays below INT32_MAX at every scale (the closest is scale 3 at 2109771130 against 2147483647).

That bound does not hold for every option value. With adm_csf_mode=1 and adm_norm_view_dist=24 the same worst-case coefficient at scale 2 gives a value far outside the 32-bit lane. This is a bound, not an input that I have shown to occur.

So this PR is about making the AVX2 path compute what the C code computes, not about a score that is known to be wrong today.

Tests

check_adm gains cases for adm_cm (scale 0) and i4_adm_cm (scales 1 to 3) that put the rounded square above INT32_MAX, plus scale 0 cases just below and just above the limit. The inputs are synthetic and say so in comments: they exercise the arithmetic, they are not what the extractor produces from a frame.

With the logical shift put back and the tests kept, each of these cases fails against the C reference on seeds 1 to 20; with the fix they pass.

Validation

  • x86-64 Linux, release build, GCC 16.2.1 and Clang 23.1.5: checkasm --test=adm passes with seeds 1 to 20; the libvmaf unit suite passes 28 of 28 on both.
  • ADM scores of the 576x324 src01_hrc00/src01_hrc01 pair, 8-bit and 16-bit, are byte-equal to master cf677baa on the AVX-512, AVX2 (--cpumask 16) and scalar (--cpumask 63) paths.
  • CI on a fork branch with this commit plus one fork-only commit that pins the workflow actions to commit SHAs (the fork requires pinned actions): libvmaf, Windows, Docker, ffmpeg, checkasm, all green.

adm_cm cubes the masked coefficient as (x_sq * x + round) >> shift on a
64-bit product. The C code shifts a signed int64_t; the AVX2 path used
_mm256_srli_epi64, a logical shift. The two differ when the product is
negative, which happens when the rounded square passes INT32_MAX and
wraps in its 32-bit lane. Use the existing sra_epi64 helper so the
AVX2 result matches the C code for every input.

Applies to the scale 0 macro (both 64-bit halves) and the macro of
scales 1 to 3.

check_adm gains cases that put the rounded square on both sides of
INT32_MAX at scale 0 and above it at scales 1 to 3. The inputs are
synthetic: they exercise the arithmetic, not a decoded frame. With the
logical shift each of these cases fails against the C reference.

Signed-off-by: Lusoris <lusoris@pm.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant