| /* | |
| * Copyright (c) 2016, Alliance for Open Media. All rights reserved. | |
| * | |
| * This source code is subject to the terms of the BSD 2 Clause License and | |
| * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License | |
| * was not distributed with this source code in the LICENSE file, you can | |
| * obtain it at www.aomedia.org/license/software. If the Alliance for Open | |
| * Media Patent License 1.0 was not distributed with this source code in the | |
| * PATENTS file, you can obtain it at www.aomedia.org/license/patent. | |
| */ | |
| #include <algorithm> | |
| #include <cstdint> | |
| #include <memory> | |
| #include <tuple> | |
| #include <utility> | |
| #include <vector> | |
| #include "gtest/gtest.h" | |
| #include "config/aom_config.h" | |
| #include "config/aom_dsp_rtcd.h" | |
| #include "config/av1_rtcd.h" | |
| #include "test/acm_random.h" | |
| #include "test/register_state_check.h" | |
| #include "test/util.h" | |
| #include "av1/common/blockd.h" | |
| #include "aom_mem/aom_mem.h" | |
| #include "aom_ports/mem.h" | |
| using SubtractFunc = void (*)(int rows, int cols, int16_t *diff_ptr, | |
| ptrdiff_t diff_stride, const uint8_t *src_ptr, | |
| ptrdiff_t src_stride, const uint8_t *pred_ptr, | |
| ptrdiff_t pred_stride); | |
| namespace { | |
| using std::get; | |
| using std::make_tuple; | |
| using std::tuple; | |
| using libaom_test::ACMRandom; | |
| // <BLOCK_SIZE, optimized subtract func, reference subtract func> | |
| using Params = tuple<BLOCK_SIZE, SubtractFunc, SubtractFunc>; | |
| class AV1SubtractBlockTestBase : public ::testing::Test { | |
| public: | |
| AV1SubtractBlockTestBase(BLOCK_SIZE bs, int bit_depth, SubtractFunc func, | |
| SubtractFunc ref_func) { | |
| block_width_ = block_size_wide[bs]; | |
| block_height_ = block_size_high[bs]; | |
| func_ = func; | |
| ref_func_ = ref_func; | |
| if (bit_depth == -1) { | |
| hbd_ = false; | |
| bit_depth_ = AOM_BITS_8; | |
| } else { | |
| hbd_ = true; | |
| bit_depth_ = static_cast<aom_bit_depth_t>(bit_depth); | |
| } | |
| } | |
| void SetUp() override { | |
| rnd_.Reset(ACMRandom::DeterministicSeed()); | |
| if (hbd_) { | |
| src_ = CONVERT_TO_BYTEPTR(reinterpret_cast<uint16_t *>( | |
| aom_memalign(32, kMaxBufElems * sizeof(uint16_t)))); | |
| ASSERT_NE(src_, nullptr); | |
| pred_ = CONVERT_TO_BYTEPTR(reinterpret_cast<uint16_t *>( | |
| aom_memalign(32, kMaxBufElems * sizeof(uint16_t)))); | |
| ASSERT_NE(pred_, nullptr); | |
| } else { | |
| src_ = reinterpret_cast<uint8_t *>( | |
| aom_memalign(32, kMaxBufElems * sizeof(uint8_t))); | |
| ASSERT_NE(src_, nullptr); | |
| pred_ = reinterpret_cast<uint8_t *>( | |
| aom_memalign(32, kMaxBufElems * sizeof(uint8_t))); | |
| ASSERT_NE(pred_, nullptr); | |
| } | |
| diff_ = reinterpret_cast<int16_t *>( | |
| aom_memalign(32, kMaxBufElems * sizeof(int16_t))); | |
| ASSERT_NE(diff_, nullptr); | |
| } | |
| void TearDown() override { | |
| if (hbd_) { | |
| aom_free(CONVERT_TO_SHORTPTR(src_)); | |
| aom_free(CONVERT_TO_SHORTPTR(pred_)); | |
| } else { | |
| aom_free(src_); | |
| aom_free(pred_); | |
| } | |
| aom_free(diff_); | |
| } | |
| protected: | |
| static constexpr int kMaxHeight = 128; | |
| static constexpr int kMaxStride = 256; | |
| static constexpr int kMaxOffset = 64; | |
| static constexpr size_t kMaxBufElems = | |
| static_cast<size_t>(kMaxHeight * kMaxStride + kMaxOffset); | |
| void CheckResult(); | |
| void RunForSpeed(); | |
| private: | |
| void FillInputs(size_t count); | |
| ACMRandom rnd_; | |
| int block_height_; | |
| int block_width_; | |
| bool hbd_; | |
| aom_bit_depth_t bit_depth_; | |
| SubtractFunc func_; | |
| SubtractFunc ref_func_; | |
| uint8_t *src_; | |
| uint8_t *pred_; | |
| int16_t *diff_; | |
| }; | |
| void AV1SubtractBlockTestBase::FillInputs(size_t count) { | |
| if (hbd_) { | |
| const int mask = (1 << bit_depth_) - 1; | |
| uint16_t *const src16 = CONVERT_TO_SHORTPTR(src_); | |
| uint16_t *const pred16 = CONVERT_TO_SHORTPTR(pred_); | |
| for (size_t i = 0; i < count; ++i) { | |
| src16[i] = rnd_.Rand16() & mask; | |
| pred16[i] = rnd_.Rand16() & mask; | |
| } | |
| } else { | |
| for (size_t i = 0; i < count; ++i) { | |
| src_[i] = rnd_.Rand8(); | |
| pred_[i] = rnd_.Rand8(); | |
| } | |
| } | |
| } | |
| void AV1SubtractBlockTestBase::CheckResult() { | |
| const int test_num = 100; | |
| // Encoder residual sub-blocks keep diff_ptr and diff_stride aligned to | |
| // min(cols, 16) int16_t elements (8B for cols==4, 16B for cols==8, 32B for | |
| // cols>=16). | |
| const int diff_align = (block_width_ < 16) ? block_width_ : 16; | |
| bool sse2_aligned_src = false; | |
| #if HAVE_SSE2 && HAVE_X86_ASM | |
| if (!hbd_ && func_ == &aom_subtract_block_sse2 && block_width_ >= 16) { | |
| sse2_aligned_src = true; | |
| } | |
| #endif | |
| for (int i = 0; i < test_num; ++i) { | |
| ptrdiff_t src_stride = block_width_; | |
| ptrdiff_t pred_stride = block_width_; | |
| ptrdiff_t diff_stride = block_width_; | |
| int src_off = 0; | |
| int pred_off = 0; | |
| int diff_off = 0; | |
| if (i > 0) { | |
| if (sse2_aligned_src) { | |
| src_stride = block_width_ + (rnd_(5) * 16); | |
| src_off = rnd_(4) * 16; | |
| } else { | |
| src_stride = block_width_ + rnd_(65); | |
| src_off = rnd_(33); | |
| } | |
| pred_stride = block_width_ + rnd_(65); | |
| pred_off = rnd_(33); | |
| // Ensure diff_stride >= diff_align so cols<16 sub-block strides can also | |
| // be wider (e.g. diff_stride == 16..192 for cols == 4 or 8). | |
| diff_stride = ((block_width_ + diff_align - 1) & ~(diff_align - 1)) + | |
| rnd_(5) * diff_align; | |
| diff_off = rnd_(4) * diff_align; | |
| } | |
| const size_t src_elems = | |
| static_cast<size_t>(src_off + block_height_ * src_stride); | |
| const size_t pred_elems = | |
| static_cast<size_t>(pred_off + block_height_ * pred_stride); | |
| const size_t diff_elems = | |
| static_cast<size_t>(diff_off + block_height_ * diff_stride); | |
| ASSERT_LE(src_elems, kMaxBufElems); | |
| ASSERT_LE(pred_elems, kMaxBufElems); | |
| ASSERT_LE(diff_elems, kMaxBufElems); | |
| FillInputs(src_elems > pred_elems ? src_elems : pred_elems); | |
| static constexpr int16_t kDiffCanary = 0x7abc; | |
| for (size_t k = 0; k < diff_elems; ++k) diff_[k] = kDiffCanary; | |
| const uint8_t *const src_ptr = | |
| hbd_ ? CONVERT_TO_BYTEPTR(CONVERT_TO_SHORTPTR(src_) + src_off) | |
| : (src_ + src_off); | |
| const uint8_t *const pred_ptr = | |
| hbd_ ? CONVERT_TO_BYTEPTR(CONVERT_TO_SHORTPTR(pred_) + pred_off) | |
| : (pred_ + pred_off); | |
| int16_t *const diff_ptr = diff_ + diff_off; | |
| API_REGISTER_STATE_CHECK(func_(block_height_, block_width_, diff_ptr, | |
| diff_stride, src_ptr, src_stride, pred_ptr, | |
| pred_stride)); | |
| for (int k = 0; k < diff_off; ++k) { | |
| ASSERT_EQ(diff_[k], kDiffCanary) | |
| << "clobbered before diff_ptr, test " << i; | |
| } | |
| if (hbd_) { | |
| const uint16_t *const s16 = CONVERT_TO_SHORTPTR(src_ptr); | |
| const uint16_t *const p16 = CONVERT_TO_SHORTPTR(pred_ptr); | |
| for (int r = 0; r < block_height_; ++r) { | |
| for (int c = 0; c < block_width_; ++c) { | |
| ASSERT_EQ(diff_ptr[r * diff_stride + c], | |
| static_cast<int16_t>(s16[r * src_stride + c] - | |
| p16[r * pred_stride + c])) | |
| << "r = " << r << ", c = " << c << ", test: " << i; | |
| } | |
| for (ptrdiff_t c = block_width_; c < diff_stride; ++c) { | |
| ASSERT_EQ(diff_ptr[r * diff_stride + c], kDiffCanary) | |
| << "padding clobbered r = " << r << ", c = " << c; | |
| } | |
| } | |
| } else { | |
| for (int r = 0; r < block_height_; ++r) { | |
| for (int c = 0; c < block_width_; ++c) { | |
| ASSERT_EQ(diff_ptr[r * diff_stride + c], | |
| src_ptr[r * src_stride + c] - pred_ptr[r * pred_stride + c]) | |
| << "r = " << r << ", c = " << c << ", test: " << i; | |
| } | |
| for (ptrdiff_t c = block_width_; c < diff_stride; ++c) { | |
| ASSERT_EQ(diff_ptr[r * diff_stride + c], kDiffCanary) | |
| << "padding clobbered r = " << r << ", c = " << c; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| void AV1SubtractBlockTestBase::RunForSpeed() { | |
| const int test_num = 200000; | |
| int i; | |
| if (ref_func_ == func_) GTEST_SKIP(); | |
| const size_t block_elems = static_cast<size_t>(block_width_ * block_height_); | |
| FillInputs(block_elems); | |
| aom_usec_timer ref_timer; | |
| aom_usec_timer_start(&ref_timer); | |
| for (i = 0; i < test_num; ++i) { | |
| ref_func_(block_height_, block_width_, diff_, block_width_, src_, | |
| block_width_, pred_, block_width_); | |
| } | |
| aom_usec_timer_mark(&ref_timer); | |
| const int64_t ref_elapsed_time = aom_usec_timer_elapsed(&ref_timer); | |
| FillInputs(block_elems); | |
| aom_usec_timer timer; | |
| aom_usec_timer_start(&timer); | |
| for (i = 0; i < test_num; ++i) { | |
| func_(block_height_, block_width_, diff_, block_width_, src_, block_width_, | |
| pred_, block_width_); | |
| } | |
| aom_usec_timer_mark(&timer); | |
| const int64_t elapsed_time = aom_usec_timer_elapsed(&timer); | |
| printf( | |
| "[%dx%d]: " | |
| "ref_time=%6" PRId64 " \t simd_time=%6" PRId64 | |
| " \t " | |
| "gain=%f \n", | |
| block_width_, block_height_, ref_elapsed_time, elapsed_time, | |
| static_cast<double>(ref_elapsed_time) / | |
| static_cast<double>(elapsed_time)); | |
| } | |
| class AV1SubtractBlockTest : public ::testing::WithParamInterface<Params>, | |
| public AV1SubtractBlockTestBase { | |
| public: | |
| AV1SubtractBlockTest() | |
| : AV1SubtractBlockTestBase(GET_PARAM(0), -1, GET_PARAM(1), GET_PARAM(2)) { | |
| } | |
| }; | |
| GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV1SubtractBlockTest); | |
| TEST_P(AV1SubtractBlockTest, CheckResult) { CheckResult(); } | |
| TEST_P(AV1SubtractBlockTest, DISABLED_Speed) { RunForSpeed(); } | |
| const BLOCK_SIZE kValidBlockSize[] = { | |
| BLOCK_4X4, BLOCK_4X8, BLOCK_8X4, BLOCK_8X8, BLOCK_8X16, | |
| BLOCK_16X8, BLOCK_16X16, BLOCK_16X32, BLOCK_32X16, BLOCK_32X32, | |
| BLOCK_32X64, BLOCK_64X32, BLOCK_64X64, BLOCK_64X128, BLOCK_128X64, | |
| BLOCK_128X128, BLOCK_4X16, BLOCK_16X4, BLOCK_8X32, BLOCK_32X8, | |
| BLOCK_16X64, BLOCK_64X16 | |
| }; | |
| INSTANTIATE_TEST_SUITE_P( | |
| C, AV1SubtractBlockTest, | |
| ::testing::Combine(::testing::ValuesIn(kValidBlockSize), | |
| ::testing::Values(&aom_subtract_block_c), | |
| ::testing::Values(&aom_subtract_block_c))); | |
| #if HAVE_SSE2 && HAVE_X86_ASM | |
| INSTANTIATE_TEST_SUITE_P( | |
| SSE2, AV1SubtractBlockTest, | |
| ::testing::Combine(::testing::ValuesIn(kValidBlockSize), | |
| ::testing::Values(&aom_subtract_block_sse2), | |
| ::testing::Values(&aom_subtract_block_c))); | |
| #endif | |
| #if HAVE_AVX2 | |
| INSTANTIATE_TEST_SUITE_P( | |
| AVX2, AV1SubtractBlockTest, | |
| ::testing::Combine(::testing::ValuesIn(kValidBlockSize), | |
| ::testing::Values(&aom_subtract_block_avx2), | |
| ::testing::Values(&aom_subtract_block_c))); | |
| #endif | |
| #if CONFIG_HIGHWAY && HAVE_AVX512 | |
| INSTANTIATE_TEST_SUITE_P( | |
| AVX512, AV1SubtractBlockTest, | |
| ::testing::Combine(::testing::ValuesIn(kValidBlockSize), | |
| ::testing::Values(&aom_subtract_block_avx512), | |
| ::testing::Values(&aom_subtract_block_c))); | |
| #endif | |
| #if HAVE_NEON | |
| INSTANTIATE_TEST_SUITE_P( | |
| NEON, AV1SubtractBlockTest, | |
| ::testing::Combine(::testing::ValuesIn(kValidBlockSize), | |
| ::testing::Values(&aom_subtract_block_neon), | |
| ::testing::Values(&aom_subtract_block_c))); | |
| #endif | |
| #if CONFIG_AV1_HIGHBITDEPTH | |
| // <BLOCK_SIZE, bit_depth, optimized subtract func, reference subtract func> | |
| using ParamsHBD = tuple<BLOCK_SIZE, int, SubtractFunc, SubtractFunc>; | |
| class AV1HBDSubtractBlockTest : public ::testing::WithParamInterface<ParamsHBD>, | |
| public AV1SubtractBlockTestBase { | |
| public: | |
| AV1HBDSubtractBlockTest() | |
| : AV1SubtractBlockTestBase(GET_PARAM(0), GET_PARAM(1), GET_PARAM(2), | |
| GET_PARAM(3)) {} | |
| }; | |
| GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV1HBDSubtractBlockTest); | |
| INSTANTIATE_TEST_SUITE_P( | |
| C, AV1HBDSubtractBlockTest, | |
| ::testing::Combine(::testing::ValuesIn(kValidBlockSize), | |
| ::testing::Values(12), | |
| ::testing::Values(&aom_highbd_subtract_block_c), | |
| ::testing::Values(&aom_highbd_subtract_block_c))); | |
| #if HAVE_SSE2 | |
| INSTANTIATE_TEST_SUITE_P( | |
| SSE2, AV1HBDSubtractBlockTest, | |
| ::testing::Combine(::testing::ValuesIn(kValidBlockSize), | |
| ::testing::Values(12), | |
| ::testing::Values(&aom_highbd_subtract_block_sse2), | |
| ::testing::Values(&aom_highbd_subtract_block_c))); | |
| #endif // HAVE_SSE2 | |
| #if HAVE_NEON | |
| INSTANTIATE_TEST_SUITE_P( | |
| NEON, AV1HBDSubtractBlockTest, | |
| ::testing::Combine(::testing::ValuesIn(kValidBlockSize), | |
| ::testing::Values(12), | |
| ::testing::Values(&aom_highbd_subtract_block_neon), | |
| ::testing::Values(&aom_highbd_subtract_block_c))); | |
| #endif | |
| #endif // CONFIG_AV1_HIGHBITDEPTH | |
| #if CONFIG_AV1_ENCODER | |
| using SubSumSseLbdFunc = uint64_t (*)(const uint8_t *src, int src_stride, | |
| const uint8_t *dst, int dst_stride, | |
| int16_t *diff, int diff_stride, int width, | |
| int height, int *sum); | |
| class AV1SubSumSseLbdTest : public ::testing::TestWithParam<SubSumSseLbdFunc> { | |
| }; | |
| TEST_P(AV1SubSumSseLbdTest, MatchReference) { | |
| const SubSumSseLbdFunc func = GetParam(); | |
| ACMRandom rnd(ACMRandom::DeterministicSeed()); | |
| static constexpr int kDims[4] = { 4, 8, 16, 32 }; | |
| static constexpr int16_t kCanary = static_cast<int16_t>(0x5a5a); | |
| std::vector<uint8_t> src_buf(128 * 40); | |
| std::vector<uint8_t> dst_buf(128 * 40); | |
| std::vector<int16_t> diff_buf(128 * 40); | |
| for (int wi = 0; wi < 4; ++wi) { | |
| const int bsw = kDims[wi]; | |
| for (int hi = 0; hi < 4; ++hi) { | |
| const int bsh = kDims[hi]; | |
| const int sstrides[4] = { bsw, bsw + 1, 67, 99 }; | |
| const int dstrides[4] = { bsw, bsw + 3, 73, 64 }; | |
| const int fstrides[4] = { bsw, bsw + 5, bsw + 16, 71 }; | |
| for (int si = 0; si < 4; ++si) { | |
| const int sstride = sstrides[si]; | |
| const int dstride = dstrides[si]; | |
| const int fstride = fstrides[si]; | |
| static constexpr size_t kGuard = 37; | |
| uint8_t *const src = src_buf.data() + ((si & 1) ? 3 : 16); | |
| uint8_t *const dst = dst_buf.data() + ((si & 1) ? 7 : 16); | |
| int16_t *const diff = diff_buf.data() + kGuard; | |
| for (int pat = 0; pat < 10; ++pat) { | |
| for (size_t i = 0; i < src_buf.size(); ++i) { | |
| src_buf[i] = rnd.Rand8(); | |
| dst_buf[i] = rnd.Rand8(); | |
| } | |
| std::fill(diff_buf.begin(), diff_buf.end(), kCanary); | |
| int32_t ref_sum = 0; | |
| uint64_t ref_sse = 0; | |
| for (int r = 0; r < bsh; ++r) { | |
| for (int c = 0; c < bsw; ++c) { | |
| uint8_t sv = 0; | |
| uint8_t dv = 0; | |
| if (pat == 0) { | |
| sv = 0; | |
| dv = 255; | |
| } else if (pat == 1) { | |
| sv = 255; | |
| dv = 0; | |
| } else if (pat == 2) { | |
| sv = 128; | |
| dv = 128; | |
| } else if (pat == 3) { | |
| sv = ((r + c) & 1) ? 255 : 0; | |
| dv = ((r + c) & 1) ? 0 : 255; | |
| } else if (pat == 4) { | |
| sv = (r & 1) ? 255 : 0; | |
| dv = (c & 1) ? 255 : 0; | |
| } else if (pat < 7) { | |
| sv = rnd.Rand8Extremes(); | |
| dv = rnd.Rand8Extremes(); | |
| } else { | |
| sv = rnd.Rand8(); | |
| dv = rnd.Rand8(); | |
| } | |
| src[r * sstride + c] = sv; | |
| dst[r * dstride + c] = dv; | |
| const int32_t d = static_cast<int32_t>(sv) - dv; | |
| ref_sum += d; | |
| ref_sse += static_cast<uint64_t>(d * d); | |
| } | |
| } | |
| int got_sum = 12345; | |
| uint64_t got_sse = 0; | |
| API_REGISTER_STATE_CHECK(got_sse = | |
| func(src, sstride, dst, dstride, diff, | |
| fstride, bsw, bsh, &got_sum)); | |
| ASSERT_EQ(ref_sum, got_sum) | |
| << bsw << "x" << bsh << " si=" << si << " pat=" << pat; | |
| ASSERT_EQ(ref_sse, got_sse) | |
| << bsw << "x" << bsh << " si=" << si << " pat=" << pat; | |
| for (size_t i = 0; i < kGuard; ++i) { | |
| ASSERT_EQ(kCanary, diff_buf[i]); | |
| } | |
| for (int r = 0; r < bsh; ++r) { | |
| for (int c = 0; c < fstride; ++c) { | |
| const int16_t got_d = diff[r * fstride + c]; | |
| if (c < bsw) { | |
| const int16_t exp_d = | |
| static_cast<int16_t>(src[r * sstride + c]) - | |
| static_cast<int16_t>(dst[r * dstride + c]); | |
| ASSERT_EQ(exp_d, got_d) | |
| << bsw << "x" << bsh << " r=" << r << " c=" << c; | |
| } else { | |
| ASSERT_EQ(kCanary, got_d) | |
| << "clobbered stride padding " << bsw << "x" << bsh | |
| << " r=" << r << " c=" << c; | |
| } | |
| } | |
| } | |
| for (size_t i = kGuard + static_cast<size_t>(bsh) * fstride; | |
| i < diff_buf.size(); ++i) { | |
| ASSERT_EQ(kCanary, diff_buf[i]); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| TEST_P(AV1SubSumSseLbdTest, TightBufferExactBounds) { | |
| const SubSumSseLbdFunc func = GetParam(); | |
| ACMRandom rnd(ACMRandom::DeterministicSeed()); | |
| static constexpr int kDims[4] = { 4, 8, 16, 32 }; | |
| for (int bsw : kDims) { | |
| for (int bsh : kDims) { | |
| const int sstride = bsw + 7; | |
| const int dstride = bsw + 5; | |
| const int fstride = bsw + 3; | |
| // Exact minimum allocations (no trailing slack past the final row) so | |
| // ASan catches any out-of-bounds read or write. | |
| std::vector<uint8_t> src(static_cast<size_t>((bsh - 1) * sstride + bsw)); | |
| std::vector<uint8_t> dst(static_cast<size_t>((bsh - 1) * dstride + bsw)); | |
| std::vector<int16_t> diff(static_cast<size_t>((bsh - 1) * fstride + bsw)); | |
| std::vector<int16_t> ref_diff(diff.size()); | |
| for (auto &v : src) v = rnd.Rand8(); | |
| for (auto &v : dst) v = rnd.Rand8(); | |
| int ref_sum = -1; | |
| int got_sum = 1; | |
| const uint64_t ref_sse = | |
| av1_sub_sum_sse_lbd_c(src.data(), sstride, dst.data(), dstride, | |
| ref_diff.data(), fstride, bsw, bsh, &ref_sum); | |
| uint64_t got_sse = 0; | |
| API_REGISTER_STATE_CHECK(got_sse = func(src.data(), sstride, dst.data(), | |
| dstride, diff.data(), fstride, | |
| bsw, bsh, &got_sum)); | |
| ASSERT_EQ(ref_sse, got_sse) << bsw << "x" << bsh; | |
| ASSERT_EQ(ref_sum, got_sum) << bsw << "x" << bsh; | |
| for (int r = 0; r < bsh; ++r) { | |
| for (int c = 0; c < bsw; ++c) { | |
| ASSERT_EQ(ref_diff[r * fstride + c], diff[r * fstride + c]); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| TEST_P(AV1SubSumSseLbdTest, DISABLED_Speed) { | |
| const SubSumSseLbdFunc func = GetParam(); | |
| if (func == &av1_sub_sum_sse_lbd_c) GTEST_SKIP(); | |
| ACMRandom rnd(ACMRandom::DeterministicSeed()); | |
| static constexpr int kDims[4] = { 4, 8, 16, 32 }; | |
| static constexpr int kStride = 384; | |
| static constexpr int kRuns = 200000; | |
| static constexpr size_t kBufElems = 32 * kStride; | |
| std::unique_ptr<uint8_t, decltype(&aom_free)> src( | |
| reinterpret_cast<uint8_t *>(aom_memalign(32, kBufElems)), &aom_free); | |
| std::unique_ptr<uint8_t, decltype(&aom_free)> dst( | |
| reinterpret_cast<uint8_t *>(aom_memalign(32, kBufElems)), &aom_free); | |
| std::unique_ptr<int16_t, decltype(&aom_free)> diff( | |
| reinterpret_cast<int16_t *>( | |
| aom_memalign(32, kBufElems * sizeof(int16_t))), | |
| &aom_free); | |
| ASSERT_NE(src.get(), nullptr); | |
| ASSERT_NE(dst.get(), nullptr); | |
| ASSERT_NE(diff.get(), nullptr); | |
| for (size_t i = 0; i < kBufElems; ++i) { | |
| src.get()[i] = rnd.Rand8(); | |
| dst.get()[i] = rnd.Rand8(); | |
| } | |
| using SumSseFunc = uint64_t (*)(const int16_t *src, int src_stride, int width, | |
| int height, int *sum); | |
| SubtractFunc base_sub = aom_subtract_block; | |
| SumSseFunc base_sum_sse = aom_sum_sse_2d_i16; | |
| #if HAVE_SSE2 | |
| if (func == &av1_sub_sum_sse_lbd_sse2) { | |
| #if HAVE_X86_ASM | |
| base_sub = &aom_subtract_block_sse2; | |
| #else | |
| base_sub = &aom_subtract_block_c; | |
| #endif | |
| base_sum_sse = &aom_sum_sse_2d_i16_sse2; | |
| } | |
| #endif // HAVE_SSE2 | |
| volatile uint64_t sink = 0; | |
| for (int bsw : kDims) { | |
| for (int bsh : kDims) { | |
| uint64_t acc = 0; | |
| aom_usec_timer timer; | |
| aom_usec_timer_start(&timer); | |
| for (int i = 0; i < kRuns; ++i) { | |
| int sum = 0; | |
| acc += av1_sub_sum_sse_lbd_c(src.get(), kStride, dst.get(), kStride, | |
| diff.get(), kStride, bsw, bsh, &sum); | |
| acc += static_cast<uint32_t>(sum); | |
| } | |
| aom_usec_timer_mark(&timer); | |
| const double c_us = static_cast<double>(aom_usec_timer_elapsed(&timer)); | |
| aom_usec_timer_start(&timer); | |
| for (int i = 0; i < kRuns; ++i) { | |
| int sum = 0; | |
| base_sub(bsh, bsw, diff.get(), kStride, src.get(), kStride, dst.get(), | |
| kStride); | |
| acc += base_sum_sse(diff.get(), kStride, bsw, bsh, &sum); | |
| acc += static_cast<uint32_t>(sum); | |
| } | |
| aom_usec_timer_mark(&timer); | |
| const double base_us = | |
| static_cast<double>(aom_usec_timer_elapsed(&timer)); | |
| aom_usec_timer_start(&timer); | |
| for (int i = 0; i < kRuns; ++i) { | |
| int sum = 0; | |
| acc += func(src.get(), kStride, dst.get(), kStride, diff.get(), kStride, | |
| bsw, bsh, &sum); | |
| acc += static_cast<uint32_t>(sum); | |
| } | |
| aom_usec_timer_mark(&timer); | |
| const double opt_us = static_cast<double>(aom_usec_timer_elapsed(&timer)); | |
| sink = acc; | |
| printf( | |
| "SubSumSseLbd %2dx%-2d: C=%8.1f us base=%8.1f us opt=%8.1f us " | |
| "(vs C: %.2fx, vs base: %.2fx)\n", | |
| bsw, bsh, c_us, base_us, opt_us, c_us / opt_us, base_us / opt_us); | |
| } | |
| } | |
| static constexpr size_t kScheduleLen = 4096; | |
| static constexpr int kMixedRepeats = 50; | |
| std::vector<std::pair<int, int>> schedule; | |
| schedule.reserve(kScheduleLen); | |
| for (size_t i = 0; i < kScheduleLen; ++i) { | |
| schedule.emplace_back(kDims[i & 3], kDims[(i >> 2) & 3]); | |
| } | |
| for (size_t i = kScheduleLen - 1; i > 0; --i) { | |
| const size_t j = static_cast<size_t>(rnd.Rand16()) % (i + 1); | |
| std::swap(schedule[i], schedule[j]); | |
| } | |
| { | |
| uint64_t acc = 0; | |
| aom_usec_timer timer; | |
| aom_usec_timer_start(&timer); | |
| for (int rep = 0; rep < kMixedRepeats; ++rep) { | |
| for (const auto &dim : schedule) { | |
| int sum = 0; | |
| acc += av1_sub_sum_sse_lbd_c(src.get(), kStride, dst.get(), kStride, | |
| diff.get(), kStride, dim.first, dim.second, | |
| &sum); | |
| acc += static_cast<uint32_t>(sum); | |
| } | |
| } | |
| aom_usec_timer_mark(&timer); | |
| const double c_us = static_cast<double>(aom_usec_timer_elapsed(&timer)); | |
| aom_usec_timer_start(&timer); | |
| for (int rep = 0; rep < kMixedRepeats; ++rep) { | |
| for (const auto &dim : schedule) { | |
| int sum = 0; | |
| base_sub(dim.second, dim.first, diff.get(), kStride, src.get(), kStride, | |
| dst.get(), kStride); | |
| acc += base_sum_sse(diff.get(), kStride, dim.first, dim.second, &sum); | |
| acc += static_cast<uint32_t>(sum); | |
| } | |
| } | |
| aom_usec_timer_mark(&timer); | |
| const double base_us = static_cast<double>(aom_usec_timer_elapsed(&timer)); | |
| aom_usec_timer_start(&timer); | |
| for (int rep = 0; rep < kMixedRepeats; ++rep) { | |
| for (const auto &dim : schedule) { | |
| int sum = 0; | |
| acc += func(src.get(), kStride, dst.get(), kStride, diff.get(), kStride, | |
| dim.first, dim.second, &sum); | |
| acc += static_cast<uint32_t>(sum); | |
| } | |
| } | |
| aom_usec_timer_mark(&timer); | |
| const double opt_us = static_cast<double>(aom_usec_timer_elapsed(&timer)); | |
| sink = acc; | |
| printf( | |
| "SubSumSseLbd mixed(%zu x %d): C=%8.1f us base=%8.1f us opt=%8.1f us " | |
| "(vs C: %.2fx, vs base: %.2fx)\n", | |
| kScheduleLen, kMixedRepeats, c_us, base_us, opt_us, c_us / opt_us, | |
| base_us / opt_us); | |
| } | |
| (void)sink; | |
| } | |
| INSTANTIATE_TEST_SUITE_P(C, AV1SubSumSseLbdTest, | |
| ::testing::Values(&av1_sub_sum_sse_lbd_c)); | |
| #if HAVE_SSE2 | |
| INSTANTIATE_TEST_SUITE_P(SSE2, AV1SubSumSseLbdTest, | |
| ::testing::Values(&av1_sub_sum_sse_lbd_sse2)); | |
| #endif // HAVE_SSE2 | |
| #if HAVE_AVX2 | |
| INSTANTIATE_TEST_SUITE_P(AVX2, AV1SubSumSseLbdTest, | |
| ::testing::Values(&av1_sub_sum_sse_lbd_avx2)); | |
| #endif // HAVE_AVX2 | |
| #endif // CONFIG_AV1_ENCODER | |
| } // namespace |