Commit 9ed77bc80d for aom
commit 9ed77bc80d20db75a499ffdd4a2af50e42aade7c
Author: Nithya V S <Nithya.VS@amd.com>
Date: Tue Nov 24 13:45:25 2026 +0530
Replace long scalar tails in cost batch kernels
When n>=8 and at least 3 contexts remain after the vector loop, process
one more 8-context group instead of the scalar tail. Overlapping
contexts are rewritten with identical values.
Kernel time, AVX2 (Zen 5c, AOCC 5.2.0), before -> after:
cdf_4_batch br_rate (n=21, stride 4): 85.7 -> 70.6 ns (1.21x)
cdf_2_batch txb_skip (n=13, stride 2): 27.3 -> 24.7 ns (1.11x)
base_cost (n=42) and eob_extra (n=9) keep the scalar tail.
Change-Id: I052327ddc1ea23ccbfc057bdd548819fa71210c9
diff --git a/av1/encoder/x86/cost_avx2.c b/av1/encoder/x86/cost_avx2.c
index 6a71d5e2cd..bcea1be6ea 100644
--- a/av1/encoder/x86/cost_avx2.c
+++ b/av1/encoder/x86/cost_avx2.c
@@ -16,6 +16,10 @@
#include "av1/encoder/cost.h"
+// Below this many leftover contexts the scalar tail is cheaper than one more
+// 8-context SIMD group.
+#define MIN_OVERLAP_TAIL 3
+
// Vector form of av1_cost_symbol_clamped for 8 lanes. Bit-exact with the
// scalar path:
// p15c = clamp(p15, EC_MIN_PROB, CDF_PROB_TOP - 1)
@@ -46,6 +50,53 @@ static inline __m256i cost_symbol_x8(__m256i p15) {
return _mm256_add_epi32(tbl, lit);
}
+// Fills the N=4 costs of 8 CDFs starting at cdf; lane holds their offsets.
+static inline void cost_tokens_cdf4_x8(int *costs, int costs_stride,
+ const uint16_t *cdf, __m256i lane) {
+ const __m256i mask16 = _mm256_set1_epi32(0xFFFF);
+ const __m256i top = _mm256_set1_epi32(CDF_PROB_TOP);
+ // Gather {c0,c1} and {c2,c3} pairs (16-bit values packed in 32-bit lanes).
+ const __m256i g01 = _mm256_i32gather_epi32((const int *)cdf, lane, 2);
+ const __m256i g23 = _mm256_i32gather_epi32(
+ (const int *)cdf, _mm256_add_epi32(lane, _mm256_set1_epi32(2)), 2);
+ // ICDF(x) = CDF_PROB_TOP - x; p15[i] = ICDF[i] - ICDF[i-1], ICDF[-1] = 0.
+ const __m256i i0 = _mm256_sub_epi32(top, _mm256_and_si256(g01, mask16));
+ const __m256i i1 = _mm256_sub_epi32(top, _mm256_srli_epi32(g01, 16));
+ const __m256i i2 = _mm256_sub_epi32(top, _mm256_and_si256(g23, mask16));
+ const __m256i i3 = _mm256_sub_epi32(top, _mm256_srli_epi32(g23, 16));
+ const __m256i r0 = cost_symbol_x8(i0);
+ const __m256i r1 = cost_symbol_x8(_mm256_sub_epi32(i1, i0));
+ const __m256i r2 = cost_symbol_x8(_mm256_sub_epi32(i2, i1));
+ const __m256i r3 = cost_symbol_x8(_mm256_sub_epi32(i3, i2));
+
+ // Transpose the 4 SoA cost vectors (8 ctx each) into 8 AoS rows of 4.
+ const __m256i t0 = _mm256_unpacklo_epi32(r0, r1);
+ const __m256i t1 = _mm256_unpackhi_epi32(r0, r1);
+ const __m256i t2 = _mm256_unpacklo_epi32(r2, r3);
+ const __m256i t3 = _mm256_unpackhi_epi32(r2, r3);
+ const __m256i q0 = _mm256_unpacklo_epi64(t0, t2); // ctx 0 | ctx 4
+ const __m256i q1 = _mm256_unpackhi_epi64(t0, t2); // ctx 1 | ctx 5
+ const __m256i q2 = _mm256_unpacklo_epi64(t1, t3); // ctx 2 | ctx 6
+ const __m256i q3 = _mm256_unpackhi_epi64(t1, t3); // ctx 3 | ctx 7
+
+ _mm_storeu_si128((__m128i *)(costs + 0 * costs_stride),
+ _mm256_castsi256_si128(q0));
+ _mm_storeu_si128((__m128i *)(costs + 1 * costs_stride),
+ _mm256_castsi256_si128(q1));
+ _mm_storeu_si128((__m128i *)(costs + 2 * costs_stride),
+ _mm256_castsi256_si128(q2));
+ _mm_storeu_si128((__m128i *)(costs + 3 * costs_stride),
+ _mm256_castsi256_si128(q3));
+ _mm_storeu_si128((__m128i *)(costs + 4 * costs_stride),
+ _mm256_extracti128_si256(q0, 1));
+ _mm_storeu_si128((__m128i *)(costs + 5 * costs_stride),
+ _mm256_extracti128_si256(q1, 1));
+ _mm_storeu_si128((__m128i *)(costs + 6 * costs_stride),
+ _mm256_extracti128_si256(q2, 1));
+ _mm_storeu_si128((__m128i *)(costs + 7 * costs_stride),
+ _mm256_extracti128_si256(q3, 1));
+}
+
void av1_cost_tokens_from_cdf_4_batch_avx2(int *costs, int costs_stride,
const uint16_t *cdf, int cdf_stride,
int n) {
@@ -53,97 +104,71 @@ void av1_cost_tokens_from_cdf_4_batch_avx2(int *costs, int costs_stride,
// Per-lane element offset of the 8 CDFs processed together.
const __m256i lane =
_mm256_setr_epi32(0, s, 2 * s, 3 * s, 4 * s, 5 * s, 6 * s, 7 * s);
- const __m256i mask16 = _mm256_set1_epi32(0xFFFF);
- const __m256i top = _mm256_set1_epi32(CDF_PROB_TOP);
int c = 0;
- for (; c + 8 <= n; c += 8) {
- const __m256i base = _mm256_add_epi32(_mm256_set1_epi32(c * s), lane);
- // Gather {c0,c1} and {c2,c3} pairs (16-bit values packed in 32-bit lanes).
- const __m256i g01 = _mm256_i32gather_epi32((const int *)cdf, base, 2);
- const __m256i g23 = _mm256_i32gather_epi32(
- (const int *)cdf, _mm256_add_epi32(base, _mm256_set1_epi32(2)), 2);
- // ICDF(x) = CDF_PROB_TOP - x; p15[i] = ICDF[i] - ICDF[i-1], ICDF[-1] = 0.
- const __m256i i0 = _mm256_sub_epi32(top, _mm256_and_si256(g01, mask16));
- const __m256i i1 = _mm256_sub_epi32(top, _mm256_srli_epi32(g01, 16));
- const __m256i i2 = _mm256_sub_epi32(top, _mm256_and_si256(g23, mask16));
- const __m256i i3 = _mm256_sub_epi32(top, _mm256_srli_epi32(g23, 16));
- const __m256i r0 = cost_symbol_x8(i0);
- const __m256i r1 = cost_symbol_x8(_mm256_sub_epi32(i1, i0));
- const __m256i r2 = cost_symbol_x8(_mm256_sub_epi32(i2, i1));
- const __m256i r3 = cost_symbol_x8(_mm256_sub_epi32(i3, i2));
-
- // Transpose the 4 SoA cost vectors (8 ctx each) into 8 AoS rows of 4.
- const __m256i t0 = _mm256_unpacklo_epi32(r0, r1);
- const __m256i t1 = _mm256_unpackhi_epi32(r0, r1);
- const __m256i t2 = _mm256_unpacklo_epi32(r2, r3);
- const __m256i t3 = _mm256_unpackhi_epi32(r2, r3);
- const __m256i q0 = _mm256_unpacklo_epi64(t0, t2); // ctx 0 | ctx 4
- const __m256i q1 = _mm256_unpackhi_epi64(t0, t2); // ctx 1 | ctx 5
- const __m256i q2 = _mm256_unpacklo_epi64(t1, t3); // ctx 2 | ctx 6
- const __m256i q3 = _mm256_unpackhi_epi64(t1, t3); // ctx 3 | ctx 7
-
- int *o = costs + c * costs_stride;
- _mm_storeu_si128((__m128i *)(o + 0 * costs_stride),
- _mm256_castsi256_si128(q0));
- _mm_storeu_si128((__m128i *)(o + 1 * costs_stride),
- _mm256_castsi256_si128(q1));
- _mm_storeu_si128((__m128i *)(o + 2 * costs_stride),
- _mm256_castsi256_si128(q2));
- _mm_storeu_si128((__m128i *)(o + 3 * costs_stride),
- _mm256_castsi256_si128(q3));
- _mm_storeu_si128((__m128i *)(o + 4 * costs_stride),
- _mm256_extracti128_si256(q0, 1));
- _mm_storeu_si128((__m128i *)(o + 5 * costs_stride),
- _mm256_extracti128_si256(q1, 1));
- _mm_storeu_si128((__m128i *)(o + 6 * costs_stride),
- _mm256_extracti128_si256(q2, 1));
- _mm_storeu_si128((__m128i *)(o + 7 * costs_stride),
- _mm256_extracti128_si256(q3, 1));
+ for (; c + 8 <= n; c += 8)
+ cost_tokens_cdf4_x8(costs + c * costs_stride, costs_stride, cdf + c * s,
+ lane);
+ if (c > 0 && n - c >= MIN_OVERLAP_TAIL) {
+ // Redoes the last 8 contexts; the overlapping ones get identical values.
+ cost_tokens_cdf4_x8(costs + (n - 8) * costs_stride, costs_stride,
+ cdf + (n - 8) * s, lane);
+ return;
}
for (; c < n; ++c)
av1_cost_tokens_from_cdf_4(costs + c * costs_stride, cdf + c * cdf_stride);
}
+// Fills the N=2 costs of 8 CDFs starting at cdf; lane holds their offsets.
+static inline void cost_tokens_cdf2_x8(int *costs, int costs_stride,
+ const uint16_t *cdf, __m256i lane) {
+ const __m256i top = _mm256_set1_epi32(CDF_PROB_TOP);
+ // Only cdf[0] is needed for a 2-symbol CDF; the 32-bit read's high half is
+ // the terminator and is masked away.
+ const __m256i g = _mm256_i32gather_epi32((const int *)cdf, lane, 2);
+ const __m256i c0 = _mm256_and_si256(g, _mm256_set1_epi32(0xFFFF));
+ // p15[0] = ICDF[0] = CDF_PROB_TOP - cdf[0]; p15[1] = cdf[0].
+ const __m256i r0 = cost_symbol_x8(_mm256_sub_epi32(top, c0));
+ const __m256i r1 = cost_symbol_x8(c0);
+
+ // Transpose the 2 SoA cost vectors into 8 AoS pairs and store 2 ints/ctx.
+ const __m256i t0 = _mm256_unpacklo_epi32(r0, r1); // ctx 0,1 | ctx 4,5
+ const __m256i t1 = _mm256_unpackhi_epi32(r0, r1); // ctx 2,3 | ctx 6,7
+ const __m128i lo0 = _mm256_castsi256_si128(t0);
+ const __m128i hi0 = _mm256_extracti128_si256(t0, 1);
+ const __m128i lo1 = _mm256_castsi256_si128(t1);
+ const __m128i hi1 = _mm256_extracti128_si256(t1, 1);
+ _mm_storel_epi64((__m128i *)(costs + 0 * costs_stride), lo0);
+ _mm_storel_epi64((__m128i *)(costs + 1 * costs_stride),
+ _mm_unpackhi_epi64(lo0, lo0));
+ _mm_storel_epi64((__m128i *)(costs + 2 * costs_stride), lo1);
+ _mm_storel_epi64((__m128i *)(costs + 3 * costs_stride),
+ _mm_unpackhi_epi64(lo1, lo1));
+ _mm_storel_epi64((__m128i *)(costs + 4 * costs_stride), hi0);
+ _mm_storel_epi64((__m128i *)(costs + 5 * costs_stride),
+ _mm_unpackhi_epi64(hi0, hi0));
+ _mm_storel_epi64((__m128i *)(costs + 6 * costs_stride), hi1);
+ _mm_storel_epi64((__m128i *)(costs + 7 * costs_stride),
+ _mm_unpackhi_epi64(hi1, hi1));
+}
+
void av1_cost_tokens_from_cdf_2_batch_avx2(int *costs, int costs_stride,
const uint16_t *cdf, int cdf_stride,
int n) {
const int s = cdf_stride;
+ // Per-lane element offset of the 8 CDFs processed together.
const __m256i lane =
_mm256_setr_epi32(0, s, 2 * s, 3 * s, 4 * s, 5 * s, 6 * s, 7 * s);
- const __m256i top = _mm256_set1_epi32(CDF_PROB_TOP);
int c = 0;
- for (; c + 8 <= n; c += 8) {
- const __m256i base = _mm256_add_epi32(_mm256_set1_epi32(c * s), lane);
- // Only cdf[0] is needed for a 2-symbol CDF; the 32-bit read's high half is
- // the terminator and is masked away.
- const __m256i g = _mm256_i32gather_epi32((const int *)cdf, base, 2);
- const __m256i c0 = _mm256_and_si256(g, _mm256_set1_epi32(0xFFFF));
- // p15[0] = ICDF[0] = CDF_PROB_TOP - cdf[0]; p15[1] = cdf[0].
- const __m256i r0 = cost_symbol_x8(_mm256_sub_epi32(top, c0));
- const __m256i r1 = cost_symbol_x8(c0);
-
- // Transpose the 2 SoA cost vectors into 8 AoS pairs and store 2 ints/ctx.
- const __m256i t0 = _mm256_unpacklo_epi32(r0, r1); // ctx 0,1 | ctx 4,5
- const __m256i t1 = _mm256_unpackhi_epi32(r0, r1); // ctx 2,3 | ctx 6,7
- const __m128i lo0 = _mm256_castsi256_si128(t0);
- const __m128i hi0 = _mm256_extracti128_si256(t0, 1);
- const __m128i lo1 = _mm256_castsi256_si128(t1);
- const __m128i hi1 = _mm256_extracti128_si256(t1, 1);
- int *o = costs + c * costs_stride;
- _mm_storel_epi64((__m128i *)(o + 0 * costs_stride), lo0);
- _mm_storel_epi64((__m128i *)(o + 1 * costs_stride),
- _mm_unpackhi_epi64(lo0, lo0));
- _mm_storel_epi64((__m128i *)(o + 2 * costs_stride), lo1);
- _mm_storel_epi64((__m128i *)(o + 3 * costs_stride),
- _mm_unpackhi_epi64(lo1, lo1));
- _mm_storel_epi64((__m128i *)(o + 4 * costs_stride), hi0);
- _mm_storel_epi64((__m128i *)(o + 5 * costs_stride),
- _mm_unpackhi_epi64(hi0, hi0));
- _mm_storel_epi64((__m128i *)(o + 6 * costs_stride), hi1);
- _mm_storel_epi64((__m128i *)(o + 7 * costs_stride),
- _mm_unpackhi_epi64(hi1, hi1));
+ for (; c + 8 <= n; c += 8)
+ cost_tokens_cdf2_x8(costs + c * costs_stride, costs_stride, cdf + c * s,
+ lane);
+ if (c > 0 && n - c >= MIN_OVERLAP_TAIL) {
+ // Redoes the last 8 contexts; the overlapping ones get identical values.
+ cost_tokens_cdf2_x8(costs + (n - 8) * costs_stride, costs_stride,
+ cdf + (n - 8) * s, lane);
+ return;
}
for (; c < n; ++c)
av1_cost_tokens_from_cdf_2(costs + c * costs_stride, cdf + c * cdf_stride);