Flatten the transform matrix branch out of the hot butterfly loops
Browse files at this commitc180d551e576913a820d2ac63cc75558b7cc2515Parent: 6efce81- perf
- flatten the transform matrix branch out of the hot butterfly loops
2 changed files +56 -4
crates/ec-h265/src/transform.rsmodified+43 -4
| @@ -65,16 +65,53 @@ const DST_MATRIX: [[i32; 4]; 4] = [ | |||
| 65 | 65 | ||
| 66 | /// `M[m][n]` of the 32-point matrix, mirrored out of the stored half. | 66 | /// `M[m][n]` of the 32-point matrix, mirrored out of the stored half. |
| 67 | #[inline] | 67 | #[inline] |
| 68 | -fn m32(row: usize, col: usize) -> i32 { | 68 | +const fn m32(row: usize, col: usize) -> i32 { |
| 69 | if col < 16 { | 69 | if col < 16 { |
| 70 | TRANS_MATRIX_HALF[row][col] | 70 | TRANS_MATRIX_HALF[row][col] |
| 71 | - } else if row.is_multiple_of(2) { | 71 | + } else if row % 2 == 0 { |
| 72 | TRANS_MATRIX_HALF[row][31 - col] | 72 | TRANS_MATRIX_HALF[row][31 - col] |
| 73 | } else { | 73 | } else { |
| 74 | -TRANS_MATRIX_HALF[row][31 - col] | 74 | -TRANS_MATRIX_HALF[row][31 - col] |
| 75 | } | 75 | } |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 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 | + | ||
| 78 | /// `levelScale[k]`, 8.6.3. | 115 | /// `levelScale[k]`, 8.6.3. |
| 79 | const LEVEL_SCALE: [i32; 6] = [40, 45, 51, 57, 64, 72]; | 116 | const LEVEL_SCALE: [i32; 6] = [40, 45, 51, 57, 64, 72]; |
| 80 | 117 | ||
| @@ -110,8 +147,9 @@ fn inverse_1d(n: usize, src: &[i32], dst: &mut [i32]) { | |||
| 110 | inverse_1d(half, &even_src[..half], &mut even[..half]); | 147 | inverse_1d(half, &even_src[..half], &mut even[..half]); |
| 111 | for x in 0..half { | 148 | for x in 0..half { |
| 112 | let mut odd = 0i32; | 149 | let mut odd = 0i32; |
| 150 | + let matrix_col = &FULL_MATRIX_T[x]; | ||
| 113 | for j in 0..half { | 151 | 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]; |
| 115 | } | 153 | } |
| 116 | dst[x] = even[x] + odd; | 154 | dst[x] = even[x] + odd; |
| 117 | dst[n - 1 - x] = even[x] - odd; | 155 | dst[n - 1 - x] = even[x] - odd; |
| @@ -141,8 +179,9 @@ fn forward_1d(n: usize, src: &[i32], dst: &mut [i32]) { | |||
| 141 | for j in 0..half { | 179 | for j in 0..half { |
| 142 | dst[2 * j] = even[j]; | 180 | dst[2 * j] = even[j]; |
| 143 | let mut odd = 0i32; | 181 | let mut odd = 0i32; |
| 182 | + let matrix_row = &FULL_MATRIX[(2 * j + 1) * stride]; | ||
| 144 | for (x, &diff) in diffs[..half].iter().enumerate() { | 183 | for (x, &diff) in diffs[..half].iter().enumerate() { |
| 145 | - odd += m32((2 * j + 1) * stride, x) * diff; | 184 | + odd += matrix_row[x] * diff; |
| 146 | } | 185 | } |
| 147 | dst[2 * j + 1] = odd; | 186 | dst[2 * j + 1] = odd; |
| 148 | } | 187 | } |
crates/ec-h265/tests/rdoq_cache_hash.rsmodified+13 -0
| @@ -40,3 +40,16 @@ fn rdoq_cache_hash_1920x1080() { | |||
| 40 | fn rdoq_cache_hash_416x240() { | 40 | fn rdoq_cache_hash_416x240() { |
| 41 | println!("hash 416x240 = {:016x}", encode_hash(416, 240)); | 41 | println!("hash 416x240 = {:016x}", encode_hash(416, 240)); |
| 42 | } | 42 | } |
| 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 | +} | ||