Commit 2af5809b67 for aom
commit 2af5809b67b000b13d150042d1ff738a9012134d
Author: Frank Galligan <fgalligan@google.com>
Date: Fri Nov 20 14:21:47 2026 -0800
av1,arm_crc32: Unroll crc32c and combine hashes
Use memcpy in CRC_LOOP/CRC_SINGLE to avoid unaligned pointer casts,
unroll av1_get_crc32c_value_arm_crc32() by 32 bytes (beating
memcpy-only by -16%..-50% on Cortex-X4/A720 and -6%..-19% on
Cortex-A520; cmake/cpu.cmake uses -march=armv8-a+crc, Android NDK
uses armv8.2-a+crc), and peel width & 3 before a 4-block interleaved
loop in av1_combine_block_hashes_arm_crc32() with advancing pointers
so independent __crc32cd chains overlap with zero stack spills.
Add unaligned/tail-length and extra width/sub_size coverage in
hash_test.cc.
Speedup (crc32c 64B-1KB / combine_hashes w16-w512, 1T, bit-exact):
- AmpereOne: -4.7%..-14.2% / -28.6%..-39.7% (1.66x)
- Pixel 9 Cortex-X4: -13.5%..-30.9% / +0.9%..-18.9% (1.23x)
- Pixel 9 Cortex-A720: -24.5%..-28.6% / -5.7%..-15.1% (1.18x)
- Pixel 9 Cortex-A520: -5.7%..-19.9% / -25.3%..-27.3% (1.38x)
- Apple M4 Pro: -24.2%..-25.0% (64B), -0.3%..-2.5% (256B) / -0.7%..-7.8%
- Arm Neoverse N3: -1.8%..-18.6% / -1.6%..+1.1%
- Apple M2 Ultra: -37.8% (64B), -0.5% (256B) (crc32c)
- Arm Neoverse V2: -1.8%..-19.2% (crc32c)
TAG=agy
CONV=490488d4-7005-402a-8cc4-c245227f80fe
Change-Id: I41b29ab3e076f0148b641f599bd225871f3ddb80
diff --git a/av1/encoder/arm/hash_arm_crc32.c b/av1/encoder/arm/hash_arm_crc32.c
index 18fc79229f..5adc42ec46 100644
--- a/av1/encoder/arm/hash_arm_crc32.c
+++ b/av1/encoder/arm/hash_arm_crc32.c
@@ -15,25 +15,35 @@
#include <arm_acle.h>
#endif
+#include <assert.h>
#include <stddef.h>
#include <stdint.h>
+#include <string.h>
#include "config/aom_config.h"
#include "config/av1_rtcd.h"
-#define CRC_LOOP(op, crc, type, buf, len) \
- while ((len) >= sizeof(type)) { \
- (crc) = op((crc), *(type *)(buf)); \
- (len) -= sizeof(type); \
- buf += sizeof(type); \
- }
+#define CRC_LOOP(op, crc, type, buf, len) \
+ do { \
+ while ((len) >= sizeof(type)) { \
+ type crc_val_; \
+ memcpy(&crc_val_, (buf), sizeof(type)); \
+ (crc) = op((crc), crc_val_); \
+ (len) -= sizeof(type); \
+ (buf) += sizeof(type); \
+ } \
+ } while (0)
-#define CRC_SINGLE(op, crc, type, buf, len) \
- if ((len) >= sizeof(type)) { \
- (crc) = op((crc), *(type *)(buf)); \
- (len) -= sizeof(type); \
- buf += sizeof(type); \
- }
+#define CRC_SINGLE(op, crc, type, buf, len) \
+ do { \
+ if ((len) >= sizeof(type)) { \
+ type crc_val_; \
+ memcpy(&crc_val_, (buf), sizeof(type)); \
+ (crc) = op((crc), crc_val_); \
+ (len) -= sizeof(type); \
+ (buf) += sizeof(type); \
+ } \
+ } while (0)
/* Return 32-bit CRC for the input buffer.
* Polynomial is 0x1EDC6F41.
@@ -42,6 +52,7 @@
uint32_t av1_get_crc32c_value_arm_crc32(void *crc_calculator, const uint8_t *p,
size_t len) {
(void)crc_calculator;
+ assert(p != NULL || len == 0);
const uint8_t *buf = p;
uint32_t crc = 0xFFFFFFFF;
@@ -53,25 +64,78 @@ uint32_t av1_get_crc32c_value_arm_crc32(void *crc_calculator, const uint8_t *p,
}
#endif
- CRC_LOOP(__crc32cd, crc, uint64_t, buf, len)
- CRC_SINGLE(__crc32cw, crc, uint32_t, buf, len)
- CRC_SINGLE(__crc32ch, crc, uint16_t, buf, len)
- CRC_SINGLE(__crc32cb, crc, uint8_t, buf, len)
+ while (len >= 32) {
+ uint64_t v0, v1, v2, v3;
+ memcpy(&v0, buf, sizeof(v0));
+ memcpy(&v1, buf + 8, sizeof(v1));
+ memcpy(&v2, buf + 16, sizeof(v2));
+ memcpy(&v3, buf + 24, sizeof(v3));
+ crc = __crc32cd(crc, v0);
+ crc = __crc32cd(crc, v1);
+ crc = __crc32cd(crc, v2);
+ crc = __crc32cd(crc, v3);
+ buf += 32;
+ len -= 32;
+ }
+ CRC_LOOP(__crc32cd, crc, uint64_t, buf, len);
+ CRC_SINGLE(__crc32cw, crc, uint32_t, buf, len);
+ CRC_SINGLE(__crc32ch, crc, uint16_t, buf, len);
+ CRC_SINGLE(__crc32cb, crc, uint8_t, buf, len);
return ~crc;
}
+// Hash the 4 values from registers, low byte first, matching the C version.
void av1_combine_block_hashes_arm_crc32(void *crc_calculator,
const uint32_t *top,
const uint32_t *bottom, uint32_t *dst,
int width, int sub_size) {
(void)crc_calculator;
- for (int x = 0; x < width; x++) {
- // Hash the 4 values from registers, in the same byte order as the C
- // version reads them from memory.
+ assert(width >= 0);
+ if (width <= 0) return;
+ assert(sub_size > 0);
+ assert(top != NULL && bottom != NULL && dst != NULL);
+ assert(dst != top && dst != bottom);
+ assert(dst != top + sub_size && dst != bottom + sub_size);
+
+ const int rem = width & 3;
+ for (int x = 0; x < rem; ++x) {
uint32_t crc = 0xFFFFFFFF;
- crc = __crc32cd(crc, top[x] | (uint64_t)top[x + sub_size] << 32);
- crc = __crc32cd(crc, bottom[x] | (uint64_t)bottom[x + sub_size] << 32);
+ crc = __crc32cd(crc, top[x] | ((uint64_t)top[x + sub_size] << 32));
+ crc = __crc32cd(crc, bottom[x] | ((uint64_t)bottom[x + sub_size] << 32));
dst[x] = ~crc;
}
+ int n = width >> 2;
+ if (n == 0) return;
+
+ top += rem;
+ bottom += rem;
+ dst += rem;
+ const uint32_t *top_right = top + sub_size;
+ const uint32_t *bottom_right = bottom + sub_size;
+
+ do {
+ // Process 4 positions per iteration so independent CRC chains overlap.
+ uint32_t c0 =
+ __crc32cd(0xFFFFFFFF, top[0] | ((uint64_t)top_right[0] << 32));
+ uint32_t c1 =
+ __crc32cd(0xFFFFFFFF, top[1] | ((uint64_t)top_right[1] << 32));
+ uint32_t c2 =
+ __crc32cd(0xFFFFFFFF, top[2] | ((uint64_t)top_right[2] << 32));
+ uint32_t c3 =
+ __crc32cd(0xFFFFFFFF, top[3] | ((uint64_t)top_right[3] << 32));
+ c0 = __crc32cd(c0, bottom[0] | ((uint64_t)bottom_right[0] << 32));
+ c1 = __crc32cd(c1, bottom[1] | ((uint64_t)bottom_right[1] << 32));
+ c2 = __crc32cd(c2, bottom[2] | ((uint64_t)bottom_right[2] << 32));
+ c3 = __crc32cd(c3, bottom[3] | ((uint64_t)bottom_right[3] << 32));
+ dst[0] = ~c0;
+ dst[1] = ~c1;
+ dst[2] = ~c2;
+ dst[3] = ~c3;
+ top += 4;
+ top_right += 4;
+ bottom += 4;
+ bottom_right += 4;
+ dst += 4;
+ } while (--n != 0);
}
diff --git a/test/hash_test.cc b/test/hash_test.cc
index d728419d03..bab208443f 100644
--- a/test/hash_test.cc
+++ b/test/hash_test.cc
@@ -9,6 +9,8 @@
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
+#include <cstddef>
+#include <cstdint>
#include <cstdlib>
#include <new>
#include <tuple>
@@ -41,6 +43,7 @@ class AV1Crc32cHashTest : public ::testing::TestWithParam<HashParam> {
protected:
void RunCheckOutput(get_crc32c_value_func test_impl);
+ void RunUnalignedAndTailTest(get_crc32c_value_func test_impl);
void RunSpeedTest(get_crc32c_value_func test_impl);
void RunZeroTest(get_crc32c_value_func test_impl);
@@ -85,6 +88,24 @@ void AV1Crc32cHashTest::RunCheckOutput(get_crc32c_value_func test_impl) {
ASSERT_EQ(crc3, crc4);
}
+void AV1Crc32cHashTest::RunUnalignedAndTailTest(
+ get_crc32c_value_func test_impl) {
+ constexpr size_t kMaxOff = 8;
+ constexpr size_t kMaxLen = 72;
+ uint8_t buf[kMaxOff + kMaxLen];
+ static_assert(sizeof(buf) >= (kMaxOff - 1) + kMaxLen, "");
+ for (size_t i = 0; i < sizeof(buf); ++i) {
+ buf[i] = rnd_.Rand8();
+ }
+ for (size_t off = 0; off < kMaxOff; ++off) {
+ for (size_t len = 0; len <= kMaxLen; ++len) {
+ ASSERT_EQ(test_impl(&calc_, buf + off, len),
+ av1_get_crc32c_value_c(&calc_, buf + off, len))
+ << "off=" << off << " len=" << len;
+ }
+ }
+}
+
void AV1Crc32cHashTest::RunSpeedTest(get_crc32c_value_func test_impl) {
get_crc32c_value_func impls[] = { av1_get_crc32c_value_c, test_impl };
const int repeat = 10000000 / (bsize_ + bsize_);
@@ -116,6 +137,10 @@ void AV1Crc32cHashTest::RunZeroTest(get_crc32c_value_func test_impl) {
TEST_P(AV1Crc32cHashTest, CheckOutput) { RunCheckOutput(GET_PARAM(0)); }
+TEST_P(AV1Crc32cHashTest, UnalignedAndTailLengths) {
+ RunUnalignedAndTailTest(GET_PARAM(0));
+}
+
TEST_P(AV1Crc32cHashTest, CheckZero) { RunZeroTest(GET_PARAM(0)); }
TEST_P(AV1Crc32cHashTest, DISABLED_Speed) { RunSpeedTest(GET_PARAM(0)); }
@@ -185,8 +210,8 @@ class AV1CombineBlockHashesTest
};
TEST_P(AV1CombineBlockHashesTest, CheckOutput) {
- const int kSubSizes[] = { 2, 4, 8, 16, 32, 64 };
- const int kWidths[] = { 0, 1, 7, 64, 129 };
+ constexpr int kSubSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
+ constexpr int kWidths[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 64, 129 };
for (int sub_size : kSubSizes) {
for (int width : kWidths) {
for (int i = 0; i < 5; ++i) {