sum_squares_test.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2016 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <cmath>
  11. #include <cstdlib>
  12. #include <string>
  13. #include <tuple>
  14. #include "third_party/googletest/src/include/gtest/gtest.h"
  15. #include "./vpx_config.h"
  16. #include "./vpx_dsp_rtcd.h"
  17. #include "test/acm_random.h"
  18. #include "test/clear_system_state.h"
  19. #include "test/register_state_check.h"
  20. #include "test/util.h"
  21. #include "vpx_ports/mem.h"
  22. using libvpx_test::ACMRandom;
  23. namespace {
  24. const int kNumIterations = 10000;
  25. typedef uint64_t (*SSI16Func)(const int16_t *src, int stride, int size);
  26. typedef std::tuple<SSI16Func, SSI16Func> SumSquaresParam;
  27. class SumSquaresTest : public ::testing::TestWithParam<SumSquaresParam> {
  28. public:
  29. virtual ~SumSquaresTest() {}
  30. virtual void SetUp() {
  31. ref_func_ = GET_PARAM(0);
  32. tst_func_ = GET_PARAM(1);
  33. }
  34. virtual void TearDown() { libvpx_test::ClearSystemState(); }
  35. protected:
  36. SSI16Func ref_func_;
  37. SSI16Func tst_func_;
  38. };
  39. TEST_P(SumSquaresTest, OperationCheck) {
  40. ACMRandom rnd(ACMRandom::DeterministicSeed());
  41. DECLARE_ALIGNED(16, int16_t, src[256 * 256]);
  42. const int msb = 11; // Up to 12 bit input
  43. const int limit = 1 << (msb + 1);
  44. for (int k = 0; k < kNumIterations; k++) {
  45. const int size = 4 << rnd(6); // Up to 128x128
  46. int stride = 4 << rnd(7); // Up to 256 stride
  47. while (stride < size) { // Make sure it's valid
  48. stride = 4 << rnd(7);
  49. }
  50. for (int i = 0; i < size; ++i) {
  51. for (int j = 0; j < size; ++j) {
  52. src[i * stride + j] = rnd(2) ? rnd(limit) : -rnd(limit);
  53. }
  54. }
  55. const uint64_t res_ref = ref_func_(src, stride, size);
  56. uint64_t res_tst;
  57. ASM_REGISTER_STATE_CHECK(res_tst = tst_func_(src, stride, size));
  58. ASSERT_EQ(res_ref, res_tst) << "Error: Sum Squares Test"
  59. << " C output does not match optimized output.";
  60. }
  61. }
  62. TEST_P(SumSquaresTest, ExtremeValues) {
  63. ACMRandom rnd(ACMRandom::DeterministicSeed());
  64. DECLARE_ALIGNED(16, int16_t, src[256 * 256]);
  65. const int msb = 11; // Up to 12 bit input
  66. const int limit = 1 << (msb + 1);
  67. for (int k = 0; k < kNumIterations; k++) {
  68. const int size = 4 << rnd(6); // Up to 128x128
  69. int stride = 4 << rnd(7); // Up to 256 stride
  70. while (stride < size) { // Make sure it's valid
  71. stride = 4 << rnd(7);
  72. }
  73. const int val = rnd(2) ? limit - 1 : -(limit - 1);
  74. for (int i = 0; i < size; ++i) {
  75. for (int j = 0; j < size; ++j) {
  76. src[i * stride + j] = val;
  77. }
  78. }
  79. const uint64_t res_ref = ref_func_(src, stride, size);
  80. uint64_t res_tst;
  81. ASM_REGISTER_STATE_CHECK(res_tst = tst_func_(src, stride, size));
  82. ASSERT_EQ(res_ref, res_tst) << "Error: Sum Squares Test"
  83. << " C output does not match optimized output.";
  84. }
  85. }
  86. using std::make_tuple;
  87. #if HAVE_NEON
  88. INSTANTIATE_TEST_CASE_P(
  89. NEON, SumSquaresTest,
  90. ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
  91. &vpx_sum_squares_2d_i16_neon)));
  92. #endif // HAVE_NEON
  93. #if HAVE_SSE2
  94. INSTANTIATE_TEST_CASE_P(
  95. SSE2, SumSquaresTest,
  96. ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
  97. &vpx_sum_squares_2d_i16_sse2)));
  98. #endif // HAVE_SSE2
  99. #if HAVE_MSA
  100. INSTANTIATE_TEST_CASE_P(
  101. MSA, SumSquaresTest,
  102. ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
  103. &vpx_sum_squares_2d_i16_msa)));
  104. #endif // HAVE_MSA
  105. } // namespace