| 1 | Auto-vectorization notes |
| 2 | ======================== |
| 3 | |
| 4 | [2023/01 @okaneco] - Notes on optimizing decoding filters |
| 5 | |
| 6 | Links: |
| 7 | [PR]: https://github.com/image-rs/image-png/pull/382 |
| 8 | [SWAR]: http://aggregate.org/SWAR/over.html |
| 9 | [AVG]: http://aggregate.org/MAGIC/#Average%20of%20Integers |
| 10 | |
| 11 | #382 heavily refactored and optimized the following filters making the |
| 12 | implementation nonobvious. These comments function as a summary of that |
| 13 | PR with an explanation of the choices made below. |
| 14 | |
| 15 | #382 originally started with trying to optimize using a technique called |
| 16 | SWAR, SIMD Within a Register. SWAR uses regular integer types like `u32` |
| 17 | and `u64` as SIMD registers to perform vertical operations in parallel, |
| 18 | usually involving bit-twiddling. This allowed each `BytesPerPixel` (bpp) |
| 19 | pixel to be decoded in parallel: 3bpp and 4bpp in a `u32`, 6bpp and 8pp |
| 20 | in a `u64`. The `Sub` filter looked like the following code block, `Avg` |
| 21 | was similar but used a bitwise average method from [AVG]: |
| 22 | ``` |
| 23 | // See "Unpartitioned Operations With Correction Code" from [SWAR] |
| 24 | fn swar_add_u32(x: u32, y: u32) -> u32 { |
| 25 | // 7-bit addition so there's no carry over the most significant bit |
| 26 | let n = (x & 0x7f7f7f7f) + (y & 0x7f7f7f7f); // 0x7F = 0b_0111_1111 |
| 27 | // 1-bit parity/XOR addition to fill in the missing MSB |
| 28 | n ^ (x ^ y) & 0x80808080 // 0x80 = 0b_1000_0000 |
| 29 | } |
| 30 | |
| 31 | let mut prev = |
| 32 | u32::from_ne_bytes([current[0], current[1], current[2], current[3]]); |
| 33 | for chunk in current[4..].chunks_exact_mut(4) { |
| 34 | let cur = u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]); |
| 35 | let new_chunk = swar_add_u32(cur, prev); |
| 36 | chunk.copy_from_slice(&new_chunk.to_ne_bytes()); |
| 37 | prev = new_chunk; |
| 38 | } |
| 39 | ``` |
| 40 | While this provided a measurable increase, @fintelia found that this idea |
| 41 | could be taken even further by unrolling the chunks component-wise and |
| 42 | avoiding unnecessary byte-shuffling by using byte arrays instead of |
| 43 | `u32::from|to_ne_bytes`. The bitwise operations were no longer necessary |
| 44 | so they were reverted to their obvious arithmetic equivalent. Lastly, |
| 45 | `TryInto` was used instead of `copy_from_slice`. The `Sub` code now |
| 46 | looked like this (with asserts to remove `0..bpp` bounds checks): |
| 47 | ``` |
| 48 | assert!(len > 3); |
| 49 | let mut prev = [current[0], current[1], current[2], current[3]]; |
| 50 | for chunk in current[4..].chunks_exact_mut(4) { |
| 51 | let new_chunk = [ |
| 52 | chunk[0].wrapping_add(prev[0]), |
| 53 | chunk[1].wrapping_add(prev[1]), |
| 54 | chunk[2].wrapping_add(prev[2]), |
| 55 | chunk[3].wrapping_add(prev[3]), |
| 56 | ]; |
| 57 | *TryInto::<&mut [u8; 4]>::try_into(chunk).unwrap() = new_chunk; |
| 58 | prev = new_chunk; |
| 59 | } |
| 60 | ``` |
| 61 | The compiler was able to optimize the code to be even faster and this |
| 62 | method even sped up Paeth filtering! Assertions were experimentally |
| 63 | added within loop bodies which produced better instructions but no |
| 64 | difference in speed. Finally, the code was refactored to remove manual |
| 65 | slicing and start the previous pixel chunks with arrays of `[0; N]`. |
| 66 | ``` |
| 67 | let mut prev = [0; 4]; |
| 68 | for chunk in current.chunks_exact_mut(4) { |
| 69 | let new_chunk = [ |
| 70 | chunk[0].wrapping_add(prev[0]), |
| 71 | chunk[1].wrapping_add(prev[1]), |
| 72 | chunk[2].wrapping_add(prev[2]), |
| 73 | chunk[3].wrapping_add(prev[3]), |
| 74 | ]; |
| 75 | *TryInto::<&mut [u8; 4]>::try_into(chunk).unwrap() = new_chunk; |
| 76 | prev = new_chunk; |
| 77 | } |
| 78 | ``` |
| 79 | While we're not manually bit-twiddling anymore, a possible takeaway from |
| 80 | this is to "think in SWAR" when dealing with small byte arrays. Unrolling |
| 81 | array operations and performing them component-wise may unlock previously |
| 82 | unavailable optimizations from the compiler, even when using the |
| 83 | `chunks_exact` methods for their potential auto-vectorization benefits. |
| 84 | |
| 85 | `std::simd` notes |
| 86 | ================= |
| 87 | |
| 88 | In the past we have experimented with `std::simd` for unfiltering. This |
| 89 | experiment was removed in https://github.com/image-rs/image-png/pull/585 |
| 90 | because: |
| 91 | |
| 92 | * The crate's microbenchmarks showed that `std::simd` didn't have a |
| 93 | significant advantage over auto-vectorization for most filters, except |
| 94 | for Paeth unfiltering - see |
| 95 | https://github.com/image-rs/image-png/pull/414#issuecomment-1736655668 |
| 96 | * In the crate's microbenchmarks `std::simd` seemed to help with Paeth |
| 97 | unfiltering only on x86/x64, with mixed results on ARM - see |
| 98 | https://github.com/image-rs/image-png/pull/539#issuecomment-2512748043 |
| 99 | * In Chromium end-to-end microbenchmarks `std::simd` either didn't help |
| 100 | or resulted in a small regression (as measured on x64). See |
| 101 | https://crrev.com/c/6090592. |
| 102 | * Field trial data from some "real world" scenarios shows that |
| 103 | performance can be quite good without relying on `std::simd` - see |
| 104 | https://github.com/image-rs/image-png/discussions/562#discussioncomment-13303307 |