Optimize find_best_path C, remove AVX2 RTCD - #5411
urvangjoshi merged 6 commits into
Conversation
6b5c508 to
6692080
Compare
|
@urvangjoshi I'd like to get your advice on how to approach this PR. To give context, I've largely written ARMv7 NEON for most of TCQ and now I am working through structuring the PRs. This is more of a "clean-up" PR that I wanted to get out of the way first. While optimizing I was worried about introducing a regression, so I kept the prior method as a unit test to ensure new-C vs old-C compliance and also tightened the overall unit testing. My plan was that the "prior method" unit test could be dropped after confidence is established. Is this a reasonable approach? |
| #include "av2/encoder/tokenize.h" | ||
|
|
||
| #if defined(__GNUC__) || defined(__clang__) | ||
| #define AVM_PREFETCH(p) __builtin_prefetch((p), 0, 3) |
There was a problem hiding this comment.
Should this be relocated to avm_ports/mem.h for broader use?
There is a error-silencing macro there for __builtin_prefetch which looks to only be used by reconinter_neon.c. Perhaps creating a more usable project-wide AVM_PREFETCH is worth the trouble?
There was a problem hiding this comment.
Yeah I think it's good to move it to mem.h which already has this:
#if HAVE_NEON && defined(_MSC_VER)
#define __builtin_prefetch(x)
#endif
There was a problem hiding this comment.
Done, I moved AVM_PREFETCH to mem.h and updated reconinter_neon.c to use AVM_PREFETCH instead of __builtin_prefetch(x) directly
@yunqingwang1 @jianj-g what are your thoughts on this? |
I think it's reasonable if the C version is faster. You can also remove the RTCD for |
Done, all the RTCD boilerplate is removed for |
1a46e54 to
ea736c7
Compare
Rewrites
find_best_pathas scalar-only code. Also createsAVM_PREFETCHinmem.hand eliminates direct usage of__prefetch_builtin.The AVX2 specialization of find_best_path used SSE2 intrinsics for only the
non-iqmatrix backtrack path. This folds the software-pipelined backtrack loop
from the AVX2 version into the C implementation, then removes the AVX2 RTCD
entry. The backtrack loop is memory-latency-bound (chasing prevId pointers
through the trellis), so prefetch and pointer arithmetic matter more than SIMD
ALU throughput. Removes ~100 lines of AVX2 code and the RTCD dispatch overhead.
Improvements: row pointer advancement instead of index multiply per iteration,
software prefetch of the next trellis row, precomputed dequant shift/round
constants hoisted out of loop, inlined tcq_quant() as (prev_id >> 1) & 1,
consistent XOR sign trick in both code paths.
Unit tests: FindBestPathInvariantTest (4 tests) verifies output against
independently computed TCQ invariants covering both iqmatrix=NULL and
iqmatrix!=NULL paths. FindBestPathRegressionTest (2 tests) provides bitexact
comparison against frozen pre-optimization C reference.
Micro-benchmark results (Apple Silicon P-core):
Micro-benchmark results (Xeon, new C vs SSE2 AVX2-dispatch):
CTC Results (RA, cpu-used=1, 33 frames, A4+A5):