diff --git a/crates/ec-h265/src/transform.rs b/crates/ec-h265/src/transform.rs index d68be9f2..2038c86e 100644 --- a/crates/ec-h265/src/transform.rs +++ b/crates/ec-h265/src/transform.rs @@ -65,16 +65,53 @@ const DST_MATRIX: [[i32; 4]; 4] = [ /// `M[m][n]` of the 32-point matrix, mirrored out of the stored half. #[inline] -fn m32(row: usize, col: usize) -> i32 { +const fn m32(row: usize, col: usize) -> i32 { if col < 16 { TRANS_MATRIX_HALF[row][col] - } else if row.is_multiple_of(2) { + } else if row % 2 == 0 { TRANS_MATRIX_HALF[row][31 - col] } else { -TRANS_MATRIX_HALF[row][31 - col] } } +/// The full 32x32 matrix, mirrored out at compile time so the hot butterfly +/// loops below index a flat table instead of taking [`m32`]'s per-element +/// branch: same values (this *is* [`m32`], evaluated ahead of time), fewer +/// instructions per multiply-add. +const fn build_full_matrix() -> [[i32; 32]; 32] { + let mut out = [[0i32; 32]; 32]; + let mut row = 0; + while row < 32 { + let mut col = 0; + while col < 32 { + out[row][col] = m32(row, col); + col += 1; + } + row += 1; + } + out +} +const FULL_MATRIX: [[i32; 32]; 32] = build_full_matrix(); + +/// [`FULL_MATRIX`] transposed: `FULL_MATRIX_T[col][row] == FULL_MATRIX[row][col]`. +/// [`inverse_1d`]'s odd sum walks a fixed column across varying rows, which +/// this makes a contiguous read. +const fn transpose(m: &[[i32; 32]; 32]) -> [[i32; 32]; 32] { + let mut out = [[0i32; 32]; 32]; + let mut row = 0; + while row < 32 { + let mut col = 0; + while col < 32 { + out[col][row] = m[row][col]; + col += 1; + } + row += 1; + } + out +} +const FULL_MATRIX_T: [[i32; 32]; 32] = transpose(&FULL_MATRIX); + /// `levelScale[k]`, 8.6.3. const LEVEL_SCALE: [i32; 6] = [40, 45, 51, 57, 64, 72]; @@ -110,8 +147,9 @@ fn inverse_1d(n: usize, src: &[i32], dst: &mut [i32]) { inverse_1d(half, &even_src[..half], &mut even[..half]); for x in 0..half { let mut odd = 0i32; + let matrix_col = &FULL_MATRIX_T[x]; for j in 0..half { - odd += m32((2 * j + 1) * stride, x) * src[2 * j + 1]; + odd += matrix_col[(2 * j + 1) * stride] * src[2 * j + 1]; } dst[x] = even[x] + odd; dst[n - 1 - x] = even[x] - odd; @@ -141,8 +179,9 @@ fn forward_1d(n: usize, src: &[i32], dst: &mut [i32]) { for j in 0..half { dst[2 * j] = even[j]; let mut odd = 0i32; + let matrix_row = &FULL_MATRIX[(2 * j + 1) * stride]; for (x, &diff) in diffs[..half].iter().enumerate() { - odd += m32((2 * j + 1) * stride, x) * diff; + odd += matrix_row[x] * diff; } dst[2 * j + 1] = odd; } diff --git a/crates/ec-h265/tests/rdoq_cache_hash.rs b/crates/ec-h265/tests/rdoq_cache_hash.rs index 6dcce82d..3519942c 100644 --- a/crates/ec-h265/tests/rdoq_cache_hash.rs +++ b/crates/ec-h265/tests/rdoq_cache_hash.rs @@ -40,3 +40,16 @@ fn rdoq_cache_hash_1920x1080() { fn rdoq_cache_hash_416x240() { println!("hash 416x240 = {:016x}", encode_hash(416, 240)); } + +/// lane-h265simd: pinned bit-identity gate for the transform's hot path. The +/// value is the same hash the two tests above print, verified by hand +/// (`git stash` the transform.rs change, rerun, compare) to be identical +/// before and after replacing `m32()`'s per-element branch with a +/// compile-time-mirrored flat table in `forward_1d`/`inverse_1d` — this pins +/// that equality so a future change to the hot path is caught if it ever +/// produces a different byte. +#[test] +fn rdoq_cache_hash_is_pinned() { + assert_eq!(encode_hash(416, 240), 0xda5d606db0755d97); + assert_eq!(encode_hash(1920, 1080), 0xf08a816c9fa9ca2c); +}