Commit 96f92b8310 for aom
commit 96f92b8310bc2d2ceca7cf33aa9004b29b69d714
Author: Frank Galligan <fgalligan@google.com>
Date: Tue Nov 10 19:41:49 2026 -0800
intra_mode_search: fast 8to1 NN hadd & colors
In av1/encoder/x86/ml_avx2.c, replace _mm_hadd_ps and _mm_shuffle_ps in
nn_propagate_8to1 with _mm_movehdup_ps, _mm_movehl_ps, and _mm_add_ss
while keeping lane 0 evaluation order ((s2 + s3) + (s0 + s1)) identical.
In av1/encoder/intra_mode_search.c, add an 8-byte uniform-word SWAR
fast path ((w >> 8) == (w & UINT64_C(0x00FFFFFFFFFFFFFF))) and byte
extraction from the uint64_t word in av1_count_colors, eliminating
aliasing reloads while keeping zero-dim guards and per-row offsets.
Add av1_count_colors unit tests in test/encode_internal_test.cc.
Speedup vs origin/main (AMD EPYC 7B13, --cpu-used=6, bit-exact):
-0.68% (1T) / -1.37% (8T) 1080p allintra, -1.85% (1T) / -0.17% (8T)
720p allintra, -1.65% 720p good (1T).
Binary size (x86_64 -O3): libaom.a .text +0.3 KiB (+0.00%),
.rodata +0.1 KiB (+0.01%); stripped aomenc +0.0 KiB (+0.00%).
Memory (peak RSS, 1080p allintra): 1T -612 KiB (-1.28%),
8T +548 KiB (+1.00%).
TAG=agy
CONV=b929a630-a6cd-45a0-979c-1767c30178ee
Change-Id: I7a74e656ddece6c7ac44332dd1076da6f5c39aaa
diff --git a/av1/encoder/intra_mode_search.c b/av1/encoder/intra_mode_search.c
index ff30a2cfa4..8655309900 100644
--- a/av1/encoder/intra_mode_search.c
+++ b/av1/encoder/intra_mode_search.c
@@ -10,6 +10,9 @@
*/
#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
#include "av1/common/av1_common_int.h"
#include "av1/common/cfl.h"
@@ -306,12 +309,30 @@ void av1_count_colors(const uint8_t *src, int stride, int rows, int cols,
int *val_count, int *num_colors) {
const int max_pix_val = 1 << 8;
memset(val_count, 0, max_pix_val * sizeof(val_count[0]));
+ if (rows <= 0 || cols <= 0) {
+ *num_colors = 0;
+ return;
+ }
for (int r = 0; r < rows; ++r) {
- for (int c = 0; c < cols; ++c) {
- const int this_val = src[r * stride + c];
- assert(this_val < max_pix_val);
- ++val_count[this_val];
+ const uint8_t *const row = src + (ptrdiff_t)r * stride;
+ int c = 0;
+ for (; c <= cols - 8; c += 8) {
+ uint64_t w;
+ memcpy(&w, row + c, sizeof(w));
+ if ((w >> 8) == (w & UINT64_C(0x00FFFFFFFFFFFFFF))) {
+ val_count[(uint8_t)w] += 8;
+ } else {
+ ++val_count[(uint8_t)w];
+ ++val_count[(uint8_t)(w >> 8)];
+ ++val_count[(uint8_t)(w >> 16)];
+ ++val_count[(uint8_t)(w >> 24)];
+ ++val_count[(uint8_t)(w >> 32)];
+ ++val_count[(uint8_t)(w >> 40)];
+ ++val_count[(uint8_t)(w >> 48)];
+ ++val_count[(uint8_t)(w >> 56)];
+ }
}
+ for (; c < cols; ++c) ++val_count[row[c]];
}
int n = 0;
for (int i = 0; i < max_pix_val; ++i) {
diff --git a/av1/encoder/x86/ml_avx2.c b/av1/encoder/x86/ml_avx2.c
index cb0a079986..b70d27e687 100644
--- a/av1/encoder/x86/ml_avx2.c
+++ b/av1/encoder/x86/ml_avx2.c
@@ -43,9 +43,10 @@ static inline void nn_propagate_8to1(
const __m128 low_128 = _mm256_castps256_ps128(in_result);
const __m128 high_128 = _mm256_extractf128_ps(in_result, 1);
const __m128 sum_par_0 = _mm_add_ps(low_128, high_128);
- const __m128 sum_par_1 = _mm_hadd_ps(sum_par_0, sum_par_0);
- const __m128 sum_tot =
- _mm_add_ps(_mm_shuffle_ps(sum_par_1, sum_par_1, 0x99), sum_par_1);
+ const __m128 shuf = _mm_movehdup_ps(sum_par_0);
+ const __m128 sums = _mm_add_ps(sum_par_0, shuf);
+ const __m128 high = _mm_movehl_ps(sums, sums);
+ const __m128 sum_tot = _mm_add_ss(high, sums);
bias_val += _mm_cvtss_f32(sum_tot);
if (is_clip_required) bias_val = AOMMAX(bias_val, 0);
diff --git a/test/encode_internal_test.cc b/test/encode_internal_test.cc
index 11e5aec1af..03e2de77b9 100644
--- a/test/encode_internal_test.cc
+++ b/test/encode_internal_test.cc
@@ -15,6 +15,7 @@
#include <cstdlib>
#include <cstring>
#include <memory>
+#include <vector>
#include "gtest/gtest.h"
@@ -32,6 +33,7 @@
#include "av1/common/enums.h"
#include "av1/common/reconintra.h"
#include "av1/encoder/allintra_vis.h"
+#include "av1/encoder/intra_mode_search.h"
#include "test/acm_random.h"
namespace {
@@ -1318,4 +1320,117 @@ TEST(EncodeInternal, IntraPredictHbdFacade) {
}
#endif // CONFIG_AV1_HIGHBITDEPTH
+void RefCountColors(const uint8_t *src, int stride, int rows, int cols,
+ int *val_count, int *num_colors) {
+ memset(val_count, 0, 256 * sizeof(val_count[0]));
+ for (int r = 0; r < rows; ++r) {
+ for (int c = 0; c < cols; ++c) {
+ ++val_count[src[static_cast<ptrdiff_t>(r) * stride + c]];
+ }
+ }
+ int n = 0;
+ for (int i = 0; i < 256; ++i) {
+ if (val_count[i]) ++n;
+ }
+ *num_colors = n;
+}
+
+TEST(EncodeInternal, CountColors) {
+ // Non-positive dimensions must clear val_count and return 0 colors.
+ int val_count[256];
+ int num_colors = -1;
+ static constexpr int kBadDims[][2] = {
+ { 0, 0 }, { 0, 8 }, { 4, 0 }, { -1, 8 }, { 4, -3 },
+ };
+ for (const auto &dim : kBadDims) {
+ for (int &c : val_count) c = -7;
+ num_colors = -1;
+ av1_count_colors(nullptr, 8, dim[0], dim[1], val_count, &num_colors);
+ EXPECT_EQ(num_colors, 0) << "rows=" << dim[0] << " cols=" << dim[1];
+ for (int i = 0; i < 256; ++i) {
+ EXPECT_EQ(val_count[i], 0) << "i=" << i;
+ }
+ }
+
+ static constexpr int kRows[] = { 1, 3, 4, 8, 16 };
+ static constexpr int kCols[] = { 1, 2, 7, 8, 9, 15, 16, 17, 32, 64 };
+ static constexpr int kAligns[] = { 0, 1, 3, 7 };
+ libaom_test::ACMRandom rnd(libaom_test::ACMRandom::DeterministicSeed() + 3);
+ int expected_count[256];
+
+ for (int rows : kRows) {
+ for (int cols : kCols) {
+ // Exact unpadded tight buffer catches 1-byte over-reads under ASan.
+ std::vector<uint8_t> tight(static_cast<size_t>(rows) *
+ static_cast<size_t>(cols));
+ for (int pattern = 0; pattern < 4; ++pattern) {
+ if (pattern == 0) {
+ memset(tight.data(), 0x5a, tight.size());
+ } else if (pattern == 1) {
+ memset(tight.data(), 0x00, tight.size());
+ tight[0] = 0xff;
+ if (tight.size() > 1) tight.back() = 0x7f;
+ } else if (pattern == 2) {
+ for (uint8_t &v : tight) v = rnd.Rand8();
+ } else {
+ for (size_t i = 0; i < tight.size(); ++i) {
+ tight[i] = static_cast<uint8_t>(i & 0xff);
+ }
+ }
+ RefCountColors(tight.data(), cols, rows, cols, expected_count,
+ &num_colors);
+ const int expected_colors = num_colors;
+ for (int &c : val_count) c = 99;
+ av1_count_colors(tight.data(), cols, rows, cols, val_count,
+ &num_colors);
+ ASSERT_EQ(num_colors, expected_colors);
+ ASSERT_EQ(memcmp(val_count, expected_count, sizeof(val_count)), 0);
+
+ // Negative stride walking backward from the final row.
+ for (int &c : val_count) c = 99;
+ const uint8_t *const neg_src =
+ tight.data() + static_cast<ptrdiff_t>(rows - 1) * cols;
+ RefCountColors(neg_src, -cols, rows, cols, expected_count, &num_colors);
+ const int neg_expected = num_colors;
+ av1_count_colors(neg_src, -cols, rows, cols, val_count, &num_colors);
+ ASSERT_EQ(num_colors, neg_expected);
+ ASSERT_EQ(memcmp(val_count, expected_count, sizeof(val_count)), 0);
+ }
+
+ // Unaligned and padded strides.
+ const int stride = cols + 5;
+ std::vector<uint8_t> padded(
+ static_cast<size_t>(rows) * static_cast<size_t>(stride) + 16, 0xa5);
+ for (int align_off : kAligns) {
+ uint8_t *const base = padded.data() + align_off;
+ for (int r = 0; r < rows; ++r) {
+ for (int c = 0; c < cols; ++c) {
+ base[static_cast<ptrdiff_t>(r) * stride + c] = rnd.Rand8();
+ }
+ }
+ RefCountColors(base, stride, rows, cols, expected_count, &num_colors);
+ const int expected_colors = num_colors;
+ for (int &c : val_count) c = -1;
+ av1_count_colors(base, stride, rows, cols, val_count, &num_colors);
+ ASSERT_EQ(num_colors, expected_colors);
+ ASSERT_EQ(memcmp(val_count, expected_count, sizeof(val_count)), 0);
+ }
+ }
+ }
+
+ // Test single-byte difference at every byte offset 0..7 in an 8-byte word.
+ for (int diff_pos = 0; diff_pos < 8; ++diff_pos) {
+ uint8_t buf[16];
+ memset(buf, 0x33, sizeof(buf));
+ buf[diff_pos] = 0xc4;
+ RefCountColors(buf, 16, 1, 16, expected_count, &num_colors);
+ const int expected_colors = num_colors;
+ for (int &c : val_count) c = -1;
+ av1_count_colors(buf, 16, 1, 16, val_count, &num_colors);
+ ASSERT_EQ(num_colors, expected_colors) << "diff_pos=" << diff_pos;
+ ASSERT_EQ(memcmp(val_count, expected_count, sizeof(val_count)), 0)
+ << "diff_pos=" << diff_pos;
+ }
+}
+
} // namespace