aom_dsp,neon: Optimize highbd_quantize_b pre-scan Replace the post-loop memset in aom_highbd_quantize_b_neon and aom_highbd_quantize_b_adaptive_neon with in-loop NEON zero stores and vmaxvq_u32(vorrq_u32(a, b)) zero checks, and add TX_4X4/TX_8X8 NEON test coverage in FullPrecisionQuantizeTest. Speedup: 1.25x-4.36x (-20.7% to -77.1%, aom_highbd_quantize_b_neon & adaptive TX_4X4..TX_16X16) in test_libaom on Neoverse N3; -1.03% (1T, -1.15% min) on Neoverse N2 and -0.75% (1T Cortex-A720) / -3.80% (4T) 10b avifenc on Pixel 9 (bit-exact). TAG=agy CONV=490488d4-7005-402a-8cc4-c245227f80fe Change-Id: Idb4f8f2c7be54cf244e501188e2c61e825999d66
diff --git a/aom_dsp/arm/highbd_quantize_neon.c b/aom_dsp/arm/highbd_quantize_neon.c index 7fe236a..4c1e3ef 100644 --- a/aom_dsp/arm/highbd_quantize_neon.c +++ b/aom_dsp/arm/highbd_quantize_neon.c
@@ -11,20 +11,19 @@ #include <arm_neon.h> #include <assert.h> -#include <string.h> #include "config/aom_config.h" #include "config/aom_dsp_rtcd.h" #include "aom_dsp/quantize.h" -static inline uint32_t sum_abs_coeff(const uint32x4_t a) { +static inline int all_zero_u32x8(const uint32x4_t a, const uint32x4_t b) { + const uint32x4_t ab = vorrq_u32(a, b); #if AOM_ARCH_AARCH64 - return vaddvq_u32(a); + return vmaxvq_u32(ab) == 0; #else - const uint64x2_t b = vpaddlq_u32(a); - const uint64x1_t c = vadd_u64(vget_low_u64(b), vget_high_u64(b)); - return (uint32_t)vget_lane_u64(c, 0); + const uint32x2_t d = vorr_u32(vget_low_u32(ab), vget_high_u32(ab)); + return vget_lane_u64(vreinterpret_u64_u32(d), 0) == 0; #endif } @@ -169,8 +168,9 @@ intptr_t non_zero_count = n_coeffs; - assert(n_coeffs > 8); + assert(n_coeffs >= 16 && (n_coeffs & 7) == 0); // Pre-scan pass + const int32x4_t v_zero_s32 = vdupq_n_s32(0); const int32x4_t v_zbin_s32x = vdupq_lane_s32(vget_low_s32(v_zbin_s32), 1); intptr_t i = n_coeffs; do { @@ -181,19 +181,18 @@ const uint32x4_t v_mask_a = vcgeq_s32(v_abs_coeff_a, v_zbin_s32x); const uint32x4_t v_mask_b = vcgeq_s32(v_abs_coeff_b, v_zbin_s32x); // If the coefficient is in the base ZBIN range, then discard. - if (sum_abs_coeff(v_mask_a) + sum_abs_coeff(v_mask_b) == 0) { + if (all_zero_u32x8(v_mask_a, v_mask_b)) { + vst1q_s32(qcoeff_ptr + i - 8, v_zero_s32); + vst1q_s32(qcoeff_ptr + i - 4, v_zero_s32); + vst1q_s32(dqcoeff_ptr + i - 8, v_zero_s32); + vst1q_s32(dqcoeff_ptr + i - 4, v_zero_s32); non_zero_count -= 8; } else { break; } i -= 8; - } while (i > 0); - - const intptr_t remaining_zcoeffs = n_coeffs - non_zero_count; - memset(qcoeff_ptr + non_zero_count, 0, - remaining_zcoeffs * sizeof(*qcoeff_ptr)); - memset(dqcoeff_ptr + non_zero_count, 0, - remaining_zcoeffs * sizeof(*dqcoeff_ptr)); + // The first 8 coefficients are always quantized below. + } while (i != 8); // DC and first 3 AC v_mask_lo = @@ -216,7 +215,7 @@ get_max_lane_eob(iscan, v_eobmax, vcombine_u16(v_mask_lo, v_mask_hi)); intptr_t count = non_zero_count - 8; - for (; count > 0; count -= 8) { + for (; count != 0; count -= 8) { coeff_ptr += 8; qcoeff_ptr += 8; dqcoeff_ptr += 8; @@ -302,13 +301,14 @@ int16x8_t v_eobmax = vdupq_n_s16(-1); int16x8_t v_eobmin = vdupq_n_s16((int16_t)n_coeffs); - assert(n_coeffs > 8); + assert(n_coeffs >= 16 && (n_coeffs & 7) == 0); // Pre-scan pass const int32x4_t v_zbin_s32x = vdupq_lane_s32(vget_low_s32(v_zbin_s32), 1); const int prescan_add_1 = ROUND_POWER_OF_TWO(dequant_ptr[1] * EOB_FACTOR, 7 + AOM_QM_BITS); const int32x4_t v_zbin_prescan = vaddq_s32(v_zbin_s32x, vdupq_n_s32(prescan_add_1)); + const int32x4_t v_zero_s32 = vdupq_n_s32(0); intptr_t non_zero_count = n_coeffs; intptr_t i = n_coeffs; do { @@ -319,19 +319,18 @@ const uint32x4_t v_mask_a = vcgeq_s32(v_abs_coeff_a, v_zbin_prescan); const uint32x4_t v_mask_b = vcgeq_s32(v_abs_coeff_b, v_zbin_prescan); // If the coefficient is in the base ZBIN range, then discard. - if (sum_abs_coeff(v_mask_a) + sum_abs_coeff(v_mask_b) == 0) { + if (all_zero_u32x8(v_mask_a, v_mask_b)) { + vst1q_s32(qcoeff_ptr + i - 8, v_zero_s32); + vst1q_s32(qcoeff_ptr + i - 4, v_zero_s32); + vst1q_s32(dqcoeff_ptr + i - 8, v_zero_s32); + vst1q_s32(dqcoeff_ptr + i - 4, v_zero_s32); non_zero_count -= 8; } else { break; } i -= 8; - } while (i > 0); - - const intptr_t remaining_zcoeffs = n_coeffs - non_zero_count; - memset(qcoeff_ptr + non_zero_count, 0, - remaining_zcoeffs * sizeof(*qcoeff_ptr)); - memset(dqcoeff_ptr + non_zero_count, 0, - remaining_zcoeffs * sizeof(*dqcoeff_ptr)); + // The first 8 coefficients are always quantized below. + } while (i != 8); // DC and first 3 AC v_mask_lo = @@ -354,7 +353,7 @@ vcombine_u16(v_mask_lo, v_mask_hi), n_coeffs); intptr_t count = non_zero_count - 8; - for (; count > 0; count -= 8) { + for (; count != 0; count -= 8) { coeff_ptr += 8; qcoeff_ptr += 8; dqcoeff_ptr += 8;
diff --git a/test/quantize_func_test.cc b/test/quantize_func_test.cc index 7e13313..3e35c8d 100644 --- a/test/quantize_func_test.cc +++ b/test/quantize_func_test.cc
@@ -922,6 +922,10 @@ static_cast<TX_SIZE>(TX_32X32), TYPE_B, AOM_BITS_12), make_tuple(&aom_highbd_quantize_b_64x64_c, &aom_highbd_quantize_b_64x64_neon, static_cast<TX_SIZE>(TX_64X64), TYPE_B, AOM_BITS_12), + make_tuple(&aom_highbd_quantize_b_c, &aom_highbd_quantize_b_neon, + static_cast<TX_SIZE>(TX_4X4), TYPE_B, AOM_BITS_12), + make_tuple(&aom_highbd_quantize_b_c, &aom_highbd_quantize_b_neon, + static_cast<TX_SIZE>(TX_8X8), TYPE_B, AOM_BITS_12), #if !CONFIG_REALTIME_ONLY make_tuple(&aom_highbd_quantize_b_adaptive_c, &aom_highbd_quantize_b_adaptive_neon, @@ -932,6 +936,12 @@ make_tuple(&aom_highbd_quantize_b_64x64_adaptive_c, &aom_highbd_quantize_b_64x64_adaptive_neon, static_cast<TX_SIZE>(TX_64X64), TYPE_B, AOM_BITS_12), + make_tuple(&aom_highbd_quantize_b_adaptive_c, + &aom_highbd_quantize_b_adaptive_neon, static_cast<TX_SIZE>(TX_4X4), + TYPE_B, AOM_BITS_12), + make_tuple(&aom_highbd_quantize_b_adaptive_c, + &aom_highbd_quantize_b_adaptive_neon, static_cast<TX_SIZE>(TX_8X8), + TYPE_B, AOM_BITS_12), #endif // !CONFIG_REALTIME_ONLY #endif // CONFIG_AV1_HIGHBITDEPTH };