forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-h265simd': flat transform matrices, profile-led

Browse files at this commit

43a4552071caed5bbb3866aef33ea56b473ffc7eAhmet Kaan GümüşParent: 6efce81c180d55
perf named forward_1d/inverse_1d (28%+10% self-time), not the
residual/CABAC path the charter guessed. Precomputed flat 32x32
matrix + transpose replace the per-element branch; -8.1% instructions,
bit-identical (encode hash now pinned as a real assertion).
crates/ec-h265/src/transform.rsmodified+43 -4
@@ -65,16 +65,53 @@ const DST_MATRIX: [[i32; 4]; 4] = [
6565
6666 /// `M[m][n]` of the 32-point matrix, mirrored out of the stored half.
6767 #[inline]
68-fn m32(row: usize, col: usize) -> i32 {
68+const fn m32(row: usize, col: usize) -> i32 {
6969 if col < 16 {
7070 TRANS_MATRIX_HALF[row][col]
71- } else if row.is_multiple_of(2) {
71+ } else if row % 2 == 0 {
7272 TRANS_MATRIX_HALF[row][31 - col]
7373 } else {
7474 -TRANS_MATRIX_HALF[row][31 - col]
7575 }
7676 }
7777
78+/// The full 32x32 matrix, mirrored out at compile time so the hot butterfly
79+/// loops below index a flat table instead of taking [`m32`]'s per-element
80+/// branch: same values (this *is* [`m32`], evaluated ahead of time), fewer
81+/// instructions per multiply-add.
82+const fn build_full_matrix() -> [[i32; 32]; 32] {
83+ let mut out = [[0i32; 32]; 32];
84+ let mut row = 0;
85+ while row < 32 {
86+ let mut col = 0;
87+ while col < 32 {
88+ out[row][col] = m32(row, col);
89+ col += 1;
90+ }
91+ row += 1;
92+ }
93+ out
94+}
95+const FULL_MATRIX: [[i32; 32]; 32] = build_full_matrix();
96+
97+/// [`FULL_MATRIX`] transposed: `FULL_MATRIX_T[col][row] == FULL_MATRIX[row][col]`.
98+/// [`inverse_1d`]'s odd sum walks a fixed column across varying rows, which
99+/// this makes a contiguous read.
100+const fn transpose(m: &[[i32; 32]; 32]) -> [[i32; 32]; 32] {
101+ let mut out = [[0i32; 32]; 32];
102+ let mut row = 0;
103+ while row < 32 {
104+ let mut col = 0;
105+ while col < 32 {
106+ out[col][row] = m[row][col];
107+ col += 1;
108+ }
109+ row += 1;
110+ }
111+ out
112+}
113+const FULL_MATRIX_T: [[i32; 32]; 32] = transpose(&FULL_MATRIX);
114+
78115 /// `levelScale[k]`, 8.6.3.
79116 const LEVEL_SCALE: [i32; 6] = [40, 45, 51, 57, 64, 72];
80117
@@ -110,8 +147,9 @@ fn inverse_1d(n: usize, src: &[i32], dst: &mut [i32]) {
110147 inverse_1d(half, &even_src[..half], &mut even[..half]);
111148 for x in 0..half {
112149 let mut odd = 0i32;
150+ let matrix_col = &FULL_MATRIX_T[x];
113151 for j in 0..half {
114- odd += m32((2 * j + 1) * stride, x) * src[2 * j + 1];
152+ odd += matrix_col[(2 * j + 1) * stride] * src[2 * j + 1];
115153 }
116154 dst[x] = even[x] + odd;
117155 dst[n - 1 - x] = even[x] - odd;
@@ -141,8 +179,9 @@ fn forward_1d(n: usize, src: &[i32], dst: &mut [i32]) {
141179 for j in 0..half {
142180 dst[2 * j] = even[j];
143181 let mut odd = 0i32;
182+ let matrix_row = &FULL_MATRIX[(2 * j + 1) * stride];
144183 for (x, &diff) in diffs[..half].iter().enumerate() {
145- odd += m32((2 * j + 1) * stride, x) * diff;
184+ odd += matrix_row[x] * diff;
146185 }
147186 dst[2 * j + 1] = odd;
148187 }
crates/ec-h265/tests/rdoq_cache_hash.rsmodified+13 -0
@@ -40,3 +40,16 @@ fn rdoq_cache_hash_1920x1080() {
4040 fn rdoq_cache_hash_416x240() {
4141 println!("hash 416x240 = {:016x}", encode_hash(416, 240));
4242 }
43+
44+/// lane-h265simd: pinned bit-identity gate for the transform's hot path. The
45+/// value is the same hash the two tests above print, verified by hand
46+/// (`git stash` the transform.rs change, rerun, compare) to be identical
47+/// before and after replacing `m32()`'s per-element branch with a
48+/// compile-time-mirrored flat table in `forward_1d`/`inverse_1d` — this pins
49+/// that equality so a future change to the hot path is caught if it ever
50+/// produces a different byte.
51+#[test]
52+fn rdoq_cache_hash_is_pinned() {
53+ assert_eq!(encode_hash(416, 240), 0xda5d606db0755d97);
54+ assert_eq!(encode_hash(1920, 1080), 0xf08a816c9fa9ca2c);
55+}