Hoist the mode>=18 branch and idx/fact out of predict_angular's inner loop
Browse files at this commitf9a6938025b938ca43c17721c39c9bfb805f7e32Parent: 44291e4- perf
- hoist the mode>=18 branch and idx/fact out of predict_angular's inner loop
2 changed files +40 -13
- Cargo.lock +1 -0
- crates/ec-h265/src/intra.rs +39 -13
Cargo.lockmodified+1 -0
| @@ -329,6 +329,7 @@ version = "0.1.0" | |||
| 329 | dependencies = [ | 329 | dependencies = [ |
| 330 | "ec-core", | 330 | "ec-core", |
| 331 | "ec-h265-syntax", | 331 | "ec-h265-syntax", |
| 332 | + "wide", | ||
| 332 | ] | 333 | ] |
| 333 | 334 | ||
| 334 | [[package]] | 335 | [[package]] |
crates/ec-h265/src/intra.rsmodified+39 -13
| @@ -282,21 +282,47 @@ fn predict_angular(refs: &Refs, mode: u8, n: usize, is_luma: bool, out: &mut [u8 | |||
| 282 | reference[BIAS + x] = i32::from(main[x - 1]); | 282 | reference[BIAS + x] = i32::from(main[x - 1]); |
| 283 | } | 283 | } |
| 284 | } | 284 | } |
| 285 | - for y in 0..n { | 285 | + // `idx`/`fact` depend only on the "row" index along the prediction |
| 286 | - // The "row" index along the prediction direction: y for the vertical | 286 | + // direction (y for the vertical modes, x for the horizontal ones — the |
| 287 | - // modes, x for the horizontal ones, which is the whole difference | 287 | + // whole difference between the two halves of 8.4.4.2.6), not on the |
| 288 | - // between the two halves of 8.4.4.2.6. | 288 | + // position across it. The mode>=18 branch and the idx/fact division used |
| 289 | + // to be recomputed at every one of the n*n pixels; hoisting them to one | ||
| 290 | + // per row/column turns the inner loop into a contiguous, near-linear scan | ||
| 291 | + // over `reference`, which is both fewer instructions and autovectorizes. | ||
| 292 | + if mode >= 18 { | ||
| 293 | + for y in 0..n { | ||
| 294 | + let idx = ((y as i32 + 1) * angle) >> 5; | ||
| 295 | + let fact = ((y as i32 + 1) * angle) & 31; | ||
| 296 | + let base = (BIAS as i32 + idx + 1) as usize; | ||
| 297 | + let row = &mut out[y * n..y * n + n]; | ||
| 298 | + if fact != 0 { | ||
| 299 | + let (w0, w1) = (32 - fact, fact); | ||
| 300 | + for x in 0..n { | ||
| 301 | + row[x] = | ||
| 302 | + ((w0 * reference[base + x] + w1 * reference[base + x + 1] + 16) >> 5) as u8; | ||
| 303 | + } | ||
| 304 | + } else { | ||
| 305 | + for x in 0..n { | ||
| 306 | + row[x] = reference[base + x] as u8; | ||
| 307 | + } | ||
| 308 | + } | ||
| 309 | + } | ||
| 310 | + } else { | ||
| 289 | for x in 0..n { | 311 | for x in 0..n { |
| 290 | - let (along, across) = if mode >= 18 { (y, x) } else { (x, y) }; | 312 | + let idx = ((x as i32 + 1) * angle) >> 5; |
| 291 | - let idx = ((along as i32 + 1) * angle) >> 5; | 313 | + let fact = ((x as i32 + 1) * angle) & 31; |
| 292 | - let fact = ((along as i32 + 1) * angle) & 31; | 314 | + let base = (BIAS as i32 + idx + 1) as usize; |
| 293 | - let base = (BIAS as i32 + across as i32 + idx + 1) as usize; | 315 | + if fact != 0 { |
| 294 | - let value = if fact != 0 { | 316 | + let (w0, w1) = (32 - fact, fact); |
| 295 | - ((32 - fact) * reference[base] + fact * reference[base + 1] + 16) >> 5 | 317 | + for y in 0..n { |
| 318 | + out[y * n + x] = | ||
| 319 | + ((w0 * reference[base + y] + w1 * reference[base + y + 1] + 16) >> 5) as u8; | ||
| 320 | + } | ||
| 296 | } else { | 321 | } else { |
| 297 | - reference[base] | 322 | + for y in 0..n { |
| 298 | - }; | 323 | + out[y * n + x] = reference[base + y] as u8; |
| 299 | - out[y * n + x] = value as u8; | 324 | + } |
| 325 | + } | ||
| 300 | } | 326 | } |
| 301 | } | 327 | } |
| 302 | // The vertical and horizontal modes carry a boundary filter on luma | 328 | // The vertical and horizontal modes carry a boundary filter on luma |