/*
 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 3-Clause Clear License
 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
 * License was not distributed with this source code in the LICENSE file, you
 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/.  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
 * aomedia.org/license/patent-license/.
 */

#include <cstdlib>
#include <new>
#include <tuple>

#include "config/avm_config.h"
#include "config/avm_dsp_rtcd.h"

#include "avm/avm_codec.h"
#include "avm/avm_integer.h"
#include "avm_dsp/variance.h"
#include "avm_mem/avm_mem.h"
#include "avm_ports/avm_timer.h"
#include "avm_ports/mem.h"
#include "av2/common/reconinter.h"
#include "test/acm_random.h"
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"
#include "third_party/googletest/src/googletest/include/gtest/gtest.h"

namespace AV2CompMaskVariance {
#if (HAVE_SSSE3 || HAVE_SSE2 || HAVE_AVX2)
const BLOCK_SIZE kValidBlockSize[] = {
  BLOCK_8X8,   BLOCK_8X16,  BLOCK_8X32,   BLOCK_16X8,   BLOCK_16X16,
  BLOCK_16X32, BLOCK_32X8,  BLOCK_32X16,  BLOCK_32X32,  BLOCK_32X64,
  BLOCK_64X32, BLOCK_64X64, BLOCK_64X128, BLOCK_128X64, BLOCK_128X128,
  BLOCK_16X64, BLOCK_64X16
};
#endif

typedef void (*highbd_comp_mask_pred_func)(uint16_t *comp_pred8,
                                           const uint16_t *pred8, int width,
                                           int height, const uint16_t *ref8,
                                           int ref_stride, const uint8_t *mask,
                                           int mask_stride, int invert_mask);

typedef std::tuple<highbd_comp_mask_pred_func, BLOCK_SIZE, int>
    HighbdCompMaskPredParam;

class AV2HighbdCompMaskVarianceTest
    : public ::testing::TestWithParam<HighbdCompMaskPredParam> {
 public:
  ~AV2HighbdCompMaskVarianceTest();
  void SetUp();

  void TearDown();

 protected:
  void RunCheckOutput(highbd_comp_mask_pred_func test_impl, BLOCK_SIZE bsize,
                      int inv);
  void RunSpeedTest(highbd_comp_mask_pred_func test_impl, BLOCK_SIZE bsize);
  bool CheckResult(int width, int height) {
    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
        const int idx = y * width + x;
        if (comp_pred1_[idx] != comp_pred2_[idx]) {
          printf("%dx%d mismatch @%d(%d,%d) ", width, height, idx, y, x);
          printf("%d != %d ", comp_pred1_[idx], comp_pred2_[idx]);
          return false;
        }
      }
    }
    return true;
  }

  libavm_test::ACMRandom rnd_;
  uint16_t *comp_pred1_;
  uint16_t *comp_pred2_;
  uint16_t *pred_;
  uint16_t *ref_buffer_;
  uint16_t *ref_;
};
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AV2HighbdCompMaskVarianceTest);

AV2HighbdCompMaskVarianceTest::~AV2HighbdCompMaskVarianceTest() { ; }

void AV2HighbdCompMaskVarianceTest::SetUp() {
  rnd_.Reset(libavm_test::ACMRandom::DeterministicSeed());
  av2_init_wedge_masks();

  comp_pred1_ =
      (uint16_t *)avm_memalign(16, MAX_SB_SQUARE * sizeof(*comp_pred1_));
  comp_pred2_ =
      (uint16_t *)avm_memalign(16, MAX_SB_SQUARE * sizeof(*comp_pred2_));
  pred_ = (uint16_t *)avm_memalign(16, MAX_SB_SQUARE * sizeof(*pred_));
  ref_buffer_ = (uint16_t *)avm_memalign(
      16, (MAX_SB_SQUARE + (8 * MAX_SB_SIZE)) * sizeof(*ref_buffer_));
  ref_ = ref_buffer_ + (8 * MAX_SB_SIZE);
}

void AV2HighbdCompMaskVarianceTest::TearDown() {
  avm_free(comp_pred1_);
  avm_free(comp_pred2_);
  avm_free(pred_);
  avm_free(ref_buffer_);
  libavm_test::ClearSystemState();
}

void AV2HighbdCompMaskVarianceTest::RunCheckOutput(
    highbd_comp_mask_pred_func test_impl, BLOCK_SIZE bsize, int inv) {
  int bd_ = GET_PARAM(2);
  const int w = block_size_wide[bsize];
  const int h = block_size_high[bsize];
  const int wedge_types = get_wedge_types_lookup(bsize);

  for (int i = 0; i < MAX_SB_SQUARE; ++i) {
    pred_[i] = rnd_.Rand16() & ((1 << bd_) - 1);
  }
  for (int i = 0; i < MAX_SB_SQUARE + (8 * MAX_SB_SIZE); ++i) {
    ref_buffer_[i] = rnd_.Rand16() & ((1 << bd_) - 1);
  }

  for (int wedge_index = 0; wedge_index < wedge_types; ++wedge_index) {
    for (int k = 0; k < MAX_WEDGE_BOUNDARY_TYPES; k++) {
      const uint8_t *mask =
          av2_get_all_contiguous_soft_mask(wedge_index, 1, bsize, k);

      avm_highbd_comp_mask_pred_c(comp_pred1_, pred_, w, h, ref_, MAX_SB_SIZE,
                                  mask, w, inv);

      test_impl(comp_pred2_, pred_, w, h, ref_, MAX_SB_SIZE, mask, w, inv);

      ASSERT_EQ(CheckResult(w, h), true)
          << " wedge " << wedge_index << " inv " << inv;
    }
  }
}

void AV2HighbdCompMaskVarianceTest::RunSpeedTest(
    highbd_comp_mask_pred_func test_impl, BLOCK_SIZE bsize) {
  int bd_ = GET_PARAM(2);

  const int w = block_size_wide[bsize];
  const int h = block_size_high[bsize];
  const int wedge_types = get_wedge_types_lookup(bsize);
  int wedge_index = wedge_types / 2;

  for (int i = 0; i < MAX_SB_SQUARE; ++i) {
    pred_[i] = rnd_.Rand16() & ((1 << bd_) - 1);
  }
  for (int i = 0; i < MAX_SB_SQUARE + (8 * MAX_SB_SIZE); ++i) {
    ref_buffer_[i] = rnd_.Rand16() & ((1 << bd_) - 1);
  }
  int boundary_index = 0;
  const uint8_t *mask =
      av2_get_all_contiguous_soft_mask(wedge_index, 1, bsize, boundary_index);
  const int num_loops = 1000000000 / (w + h);

  highbd_comp_mask_pred_func funcs[2] = { avm_highbd_comp_mask_pred_c,
                                          test_impl };
  double elapsed_time[2] = { 0 };
  for (int i = 0; i < 2; ++i) {
    avm_usec_timer timer;
    avm_usec_timer_start(&timer);
    highbd_comp_mask_pred_func func = funcs[i];
    for (int j = 0; j < num_loops; ++j) {
      func(comp_pred1_, pred_, w, h, ref_, MAX_SB_SIZE, mask, w, 0);
    }
    avm_usec_timer_mark(&timer);
    double time = static_cast<double>(avm_usec_timer_elapsed(&timer));
    elapsed_time[i] = 1000.0 * time / num_loops;
  }
  printf("compMask %3dx%-3d: %7.2f/%7.2fns", w, h, elapsed_time[0],
         elapsed_time[1]);
  printf("(%3.2f)\n", elapsed_time[0] / elapsed_time[1]);
}

TEST_P(AV2HighbdCompMaskVarianceTest, CheckOutput) {
  // inv = 0, 1
  RunCheckOutput(GET_PARAM(0), GET_PARAM(1), 0);
  RunCheckOutput(GET_PARAM(0), GET_PARAM(1), 1);
}

TEST_P(AV2HighbdCompMaskVarianceTest, DISABLED_Speed) {
  RunSpeedTest(GET_PARAM(0), GET_PARAM(1));
}

#if HAVE_AVX2
INSTANTIATE_TEST_SUITE_P(
    AVX2, AV2HighbdCompMaskVarianceTest,
    ::testing::Combine(::testing::Values(&avm_highbd_comp_mask_pred_avx2),
                       ::testing::ValuesIn(kValidBlockSize),
                       ::testing::Range(8, 13, 2)));
#endif

#if HAVE_SSE2
INSTANTIATE_TEST_SUITE_P(
    SSE2, AV2HighbdCompMaskVarianceTest,
    ::testing::Combine(::testing::Values(&avm_highbd_comp_mask_pred_sse2),
                       ::testing::ValuesIn(kValidBlockSize),
                       ::testing::Range(8, 13, 2)));
#endif

#ifndef avm_highbd_comp_mask_pred
// can't run this test if avm_highbd_comp_mask_pred is defined to
// avm_highbd_comp_mask_pred_c
class AV2HighbdCompMaskUpVarianceTest : public AV2HighbdCompMaskVarianceTest {
 public:
  ~AV2HighbdCompMaskUpVarianceTest();

 protected:
  void RunCheckOutput(highbd_comp_mask_pred_func test_impl, BLOCK_SIZE bsize,
                      int inv);
  void RunSpeedTest(highbd_comp_mask_pred_func test_impl, BLOCK_SIZE bsize,
                    int havSub);
};

AV2HighbdCompMaskUpVarianceTest::~AV2HighbdCompMaskUpVarianceTest() { ; }

void AV2HighbdCompMaskUpVarianceTest::RunCheckOutput(
    highbd_comp_mask_pred_func test_impl, BLOCK_SIZE bsize, int inv) {
  int bd_ = GET_PARAM(2);
  const int w = block_size_wide[bsize];
  const int h = block_size_high[bsize];
  const int wedge_types = get_wedge_types_lookup(bsize);

  for (int i = 0; i < MAX_SB_SQUARE; ++i) {
    pred_[i] = rnd_.Rand16() & ((1 << bd_) - 1);
  }
  for (int i = 0; i < MAX_SB_SQUARE + (8 * MAX_SB_SIZE); ++i) {
    ref_buffer_[i] = rnd_.Rand16() & ((1 << bd_) - 1);
  }

  int subpel_search;
  for (subpel_search = 1; subpel_search <= 2; ++subpel_search) {
    // loop through subx and suby
    for (int sub = 0; sub < 8 * 8; ++sub) {
      int subx = sub & 0x7;
      int suby = (sub >> 3);
      for (int wedge_index = 0; wedge_index < wedge_types; ++wedge_index) {
        for (int k = 0; k < MAX_WEDGE_BOUNDARY_TYPES; k++) {
          const uint8_t *mask =
              av2_get_all_contiguous_soft_mask(wedge_index, 1, bsize, k);

          // ref
          avm_highbd_upsampled_pred_c(NULL, NULL, 0, 0, NULL, comp_pred1_, w, h,
                                      subx, suby, ref_, MAX_SB_SIZE, bd_,
                                      subpel_search, 0);

          avm_highbd_comp_mask_pred_c(comp_pred1_, pred_, w, h, comp_pred1_, w,
                                      mask, w, inv);

          // test
          avm_highbd_upsampled_pred(NULL, NULL, 0, 0, NULL, comp_pred2_, w, h,
                                    subx, suby, ref_, MAX_SB_SIZE, bd_,
                                    subpel_search, 0);

          test_impl(comp_pred2_, pred_, w, h, comp_pred2_, w, mask, w, inv);

          ASSERT_EQ(CheckResult(w, h), true)
              << " wedge " << wedge_index << " inv " << inv << "sub (" << subx
              << "," << suby << ")";
        }
      }
    }
  }
}

void AV2HighbdCompMaskUpVarianceTest::RunSpeedTest(
    highbd_comp_mask_pred_func test_impl, BLOCK_SIZE bsize, int havSub) {
  int bd_ = GET_PARAM(2);
  const int w = block_size_wide[bsize];
  const int h = block_size_high[bsize];
  const int subx = havSub ? 3 : 0;
  const int suby = havSub ? 4 : 0;
  const int wedge_types = get_wedge_types_lookup(bsize);
  int wedge_index = wedge_types / 2;
  int boundary_index = 0;
  const uint8_t *mask =
      av2_get_all_contiguous_soft_mask(wedge_index, 1, bsize, boundary_index);

  for (int i = 0; i < MAX_SB_SQUARE; ++i) {
    pred_[i] = rnd_.Rand16() & ((1 << bd_) - 1);
  }
  for (int i = 0; i < MAX_SB_SQUARE + (8 * MAX_SB_SIZE); ++i) {
    ref_buffer_[i] = rnd_.Rand16() & ((1 << bd_) - 1);
  }

  const int num_loops = 1000000000 / (w + h);
  highbd_comp_mask_pred_func funcs[2] = { &avm_highbd_comp_mask_pred_c,
                                          test_impl };
  double elapsed_time[2] = { 0 };
  for (int i = 0; i < 2; ++i) {
    avm_usec_timer timer;
    avm_usec_timer_start(&timer);
    avm_highbd_comp_mask_pred = funcs[i];
    int subpel_search = 2;  // set to 1 to test 4-tap filter.
    for (int j = 0; j < num_loops; ++j) {
      avm_highbd_comp_mask_upsampled_pred(
          NULL, NULL, 0, 0, NULL, comp_pred1_, pred_, w, h, subx, suby, ref_,
          MAX_SB_SIZE, mask, w, 0, bd_, subpel_search, 0);
    }
    avm_usec_timer_mark(&timer);
    double time = static_cast<double>(avm_usec_timer_elapsed(&timer));
    elapsed_time[i] = 1000.0 * time / num_loops;
  }
  printf("CompMaskUp[%d] %3dx%-3d:%7.2f/%7.2fns", havSub, w, h, elapsed_time[0],
         elapsed_time[1]);
  printf("(%3.2f)\n", elapsed_time[0] / elapsed_time[1]);
}

TEST_P(AV2HighbdCompMaskUpVarianceTest, CheckOutput) {
  // inv mask = 0, 1
  RunCheckOutput(GET_PARAM(0), GET_PARAM(1), 0);
  RunCheckOutput(GET_PARAM(0), GET_PARAM(1), 1);
}

TEST_P(AV2HighbdCompMaskUpVarianceTest, DISABLED_Speed) {
  RunSpeedTest(GET_PARAM(0), GET_PARAM(1), 1);
}

#if HAVE_AVX2
INSTANTIATE_TEST_SUITE_P(
    AVX2, AV2HighbdCompMaskUpVarianceTest,
    ::testing::Combine(::testing::Values(&avm_highbd_comp_mask_pred_avx2),
                       ::testing::ValuesIn(kValidBlockSize),
                       ::testing::Range(8, 13, 2)));
#endif

#if HAVE_SSE2
INSTANTIATE_TEST_SUITE_P(
    SSE2, AV2HighbdCompMaskUpVarianceTest,
    ::testing::Combine(::testing::Values(&avm_highbd_comp_mask_pred_sse2),
                       ::testing::ValuesIn(kValidBlockSize),
                       ::testing::Range(8, 13, 2)));
#endif

typedef void (*highbd_convolve8_func)(const uint16_t *src, ptrdiff_t src_stride,
                                      uint16_t *dst, ptrdiff_t dst_stride,
                                      const int16_t *filter_x, int x_step_q4,
                                      const int16_t *filter_y, int y_step_q4,
                                      int w, int h, int bd);

typedef std::tuple<highbd_convolve8_func, BLOCK_SIZE, int> HighbdConvolve8Param;

class AV2HighbdSubPelConv8HorizTest
    : public ::testing::TestWithParam<HighbdConvolve8Param> {
 public:
  ~AV2HighbdSubPelConv8HorizTest();
  void SetUp();
  void TearDown();

 protected:
  void RunCheckOutput(highbd_convolve8_func test_impl, BLOCK_SIZE bsize);
  void RunSpeedTest(highbd_convolve8_func test_impl, BLOCK_SIZE bsize);
  bool CheckResult(int width, int height) {
    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
        const int idx = y * width + x;
        if (comp_pred1_[idx] != comp_pred2_[idx]) {
          printf("%dx%d mismatch @%d(%d,%d) ", width, height, idx, y, x);
          printf("%d != %d ", comp_pred1_[idx], comp_pred2_[idx]);
          return false;
        }
      }
    }
    return true;
  }

  libavm_test::ACMRandom rnd_;
  uint16_t *comp_pred1_;
  uint16_t *comp_pred2_;
  uint16_t *ref_buffer_;
  uint16_t *ref_;
};

AV2HighbdSubPelConv8HorizTest::~AV2HighbdSubPelConv8HorizTest() { ; }

void AV2HighbdSubPelConv8HorizTest::SetUp() {
  rnd_.Reset(libavm_test::ACMRandom::DeterministicSeed());
  comp_pred1_ =
      (uint16_t *)avm_memalign(16, MAX_SB_SQUARE * sizeof(*comp_pred1_));
  comp_pred2_ =
      (uint16_t *)avm_memalign(16, MAX_SB_SQUARE * sizeof(*comp_pred2_));
  ref_buffer_ = (uint16_t *)avm_memalign(
      16, (MAX_SB_SQUARE + (8 * MAX_SB_SIZE)) * sizeof(*ref_buffer_));
  ref_ = ref_buffer_ + (8 * MAX_SB_SIZE);
}

void AV2HighbdSubPelConv8HorizTest::TearDown() {
  avm_free(comp_pred1_);
  avm_free(comp_pred2_);
  avm_free(ref_buffer_);
  libavm_test::ClearSystemState();
}

void AV2HighbdSubPelConv8HorizTest::RunCheckOutput(
    highbd_convolve8_func test_impl, BLOCK_SIZE bsize) {
  const int bd = GET_PARAM(2);
  const int w = block_size_wide[bsize];
  const int h = block_size_high[bsize];

  for (int i = 0; i < MAX_SB_SQUARE + (8 * MAX_SB_SIZE); ++i) {
    ref_buffer_[i] = rnd_.Rand16() & ((1 << bd) - 1);
  }

  for (int subpel_search = 1; subpel_search <= 3; ++subpel_search) {
    const InterpFilterParams *filter = av2_get_filter(subpel_search);
    for (int subpel = 0; subpel < 16; ++subpel) {
      const int16_t *const kernel =
          av2_get_interp_filter_subpel_kernel(filter, subpel);

      avm_highbd_convolve8_horiz_c(ref_, MAX_SB_SIZE, comp_pred1_, w, kernel,
                                   16, NULL, -1, w, h, bd);
      test_impl(ref_, MAX_SB_SIZE, comp_pred2_, w, kernel, 16, NULL, -1, w, h,
                bd);

      ASSERT_EQ(CheckResult(w, h), true);
    }
  }
}

void AV2HighbdSubPelConv8HorizTest::RunSpeedTest(
    highbd_convolve8_func test_impl, BLOCK_SIZE bsize) {
  const int bd = GET_PARAM(2);
  const int w = block_size_wide[bsize];
  const int h = block_size_high[bsize];

  for (int i = 0; i < MAX_SB_SQUARE + (8 * MAX_SB_SIZE); ++i) {
    ref_buffer_[i] = rnd_.Rand16() & ((1 << bd) - 1);
  }

  const int num_loops = 1000000000 / (w + h);
  avm_usec_timer timer;

  for (int subpel_search = 1; subpel_search <= 3; ++subpel_search) {
    const InterpFilterParams *filter = av2_get_filter(subpel_search);
    const int16_t *const kernel =
        av2_get_interp_filter_subpel_kernel(filter, 8);

    avm_usec_timer_start(&timer);
    for (int j = 0; j < num_loops; ++j) {
      avm_highbd_convolve8_horiz_c(ref_, MAX_SB_SIZE, comp_pred1_, w, kernel,
                                   16, NULL, -1, w, h, bd);
    }
    avm_usec_timer_mark(&timer);
    const int time1 = static_cast<int>(avm_usec_timer_elapsed(&timer));

    avm_usec_timer_start(&timer);
    for (int j = 0; j < num_loops; ++j) {
      test_impl(ref_, MAX_SB_SIZE, comp_pred2_, w, kernel, 16, NULL, -1, w, h,
                bd);
    }
    avm_usec_timer_mark(&timer);
    const int time2 = static_cast<int>(avm_usec_timer_elapsed(&timer));

    printf("%3dx%-3d: bd:%d ref: %d mod: %d (%3.2f)\n", w, h, bd, time1, time2,
           (double)time1 / time2);
  }
}

TEST_P(AV2HighbdSubPelConv8HorizTest, CheckOutput) {
  RunCheckOutput(GET_PARAM(0), GET_PARAM(1));
}

TEST_P(AV2HighbdSubPelConv8HorizTest, DISABLED_Speed) {
  RunSpeedTest(GET_PARAM(0), GET_PARAM(1));
}

#if HAVE_AVX2
INSTANTIATE_TEST_SUITE_P(
    AVX2, AV2HighbdSubPelConv8HorizTest,
    ::testing::Combine(::testing::Values(&avm_highbd_convolve8_horiz_avx2),
                       ::testing::ValuesIn(kValidBlockSize),
                       ::testing::Range(8, 13, 2)));
#endif

#if HAVE_SSE2
INSTANTIATE_TEST_SUITE_P(
    SSE2, AV2HighbdSubPelConv8HorizTest,
    ::testing::Combine(::testing::Values(&avm_highbd_convolve8_horiz_sse2),
                       ::testing::ValuesIn(kValidBlockSize),
                       ::testing::Range(8, 13, 2)));
#endif

class AV2HighbdSubPelConv8VertTest : public AV2HighbdSubPelConv8HorizTest {
 public:
  ~AV2HighbdSubPelConv8VertTest();

 protected:
  void RunCheckOutput(highbd_convolve8_func test_impl, BLOCK_SIZE bsize);
  void RunSpeedTest(highbd_convolve8_func test_impl, BLOCK_SIZE bsize);
};

AV2HighbdSubPelConv8VertTest::~AV2HighbdSubPelConv8VertTest() { ; }

void AV2HighbdSubPelConv8VertTest::RunCheckOutput(
    highbd_convolve8_func test_impl, BLOCK_SIZE bsize) {
  const int bd = GET_PARAM(2);
  const int w = block_size_wide[bsize];
  const int h = block_size_high[bsize];

  for (int i = 0; i < MAX_SB_SQUARE + (8 * MAX_SB_SIZE); ++i) {
    ref_buffer_[i] = rnd_.Rand16() & ((1 << bd) - 1);
  }

  for (int subpel_search = 1; subpel_search <= 3; ++subpel_search) {
    const InterpFilterParams *filter = av2_get_filter(subpel_search);
    for (int subpel = 0; subpel < 8; ++subpel) {
      const int16_t *const kernel =
          av2_get_interp_filter_subpel_kernel(filter, subpel << 1);

      avm_highbd_convolve8_vert_c(ref_, MAX_SB_SIZE, comp_pred1_, w, NULL, -1,
                                  kernel, 16, w, h, bd);
      test_impl(ref_, MAX_SB_SIZE, comp_pred2_, w, NULL, -1, kernel, 16, w, h,
                bd);

      ASSERT_EQ(CheckResult(w, h), true);
    }
  }
}

void AV2HighbdSubPelConv8VertTest::RunSpeedTest(highbd_convolve8_func test_impl,
                                                BLOCK_SIZE bsize) {
  const int bd = GET_PARAM(2);
  const int w = block_size_wide[bsize];
  const int h = block_size_high[bsize];

  for (int i = 0; i < MAX_SB_SQUARE + (8 * MAX_SB_SIZE); ++i) {
    ref_buffer_[i] = rnd_.Rand16() & ((1 << bd) - 1);
  }

  const int num_loops = 1000000000 / (w + h);
  avm_usec_timer timer;

  for (int subpel_search = 1; subpel_search <= 3; ++subpel_search) {
    const InterpFilterParams *filter = av2_get_filter(subpel_search);
    const int16_t *const kernel =
        av2_get_interp_filter_subpel_kernel(filter, 8);

    avm_usec_timer_start(&timer);
    for (int j = 0; j < num_loops; ++j) {
      avm_highbd_convolve8_vert_c(ref_, MAX_SB_SIZE, comp_pred1_, w, NULL, -1,
                                  kernel, 16, w, h, bd);
    }
    avm_usec_timer_mark(&timer);
    const int time1 = static_cast<int>(avm_usec_timer_elapsed(&timer));

    avm_usec_timer_start(&timer);
    for (int j = 0; j < num_loops; ++j) {
      test_impl(ref_, MAX_SB_SIZE, comp_pred2_, w, NULL, -1, kernel, 16, w, h,
                bd);
    }
    avm_usec_timer_mark(&timer);
    const int time2 = static_cast<int>(avm_usec_timer_elapsed(&timer));

    printf("%3dx%-3d: bd:%d ref: %d mod: %d (%3.2f)\n", w, h, bd, time1, time2,
           (double)time1 / time2);
  }
}

TEST_P(AV2HighbdSubPelConv8VertTest, CheckOutput) {
  RunCheckOutput(GET_PARAM(0), GET_PARAM(1));
}

TEST_P(AV2HighbdSubPelConv8VertTest, DISABLED_Speed) {
  RunSpeedTest(GET_PARAM(0), GET_PARAM(1));
}

#if HAVE_AVX2
INSTANTIATE_TEST_SUITE_P(
    AVX2, AV2HighbdSubPelConv8VertTest,
    ::testing::Combine(::testing::Values(&avm_highbd_convolve8_vert_avx2),
                       ::testing::ValuesIn(kValidBlockSize),
                       ::testing::Range(8, 13, 2)));
#endif

#if HAVE_SSE2
INSTANTIATE_TEST_SUITE_P(
    SSE2, AV2HighbdSubPelConv8VertTest,
    ::testing::Combine(::testing::Values(&avm_highbd_convolve8_vert_sse2),
                       ::testing::ValuesIn(kValidBlockSize),
                       ::testing::Range(8, 13, 2)));
#endif

#endif  // ifndef avm_highbd_comp_mask_pred
}  // namespace AV2CompMaskVariance
