sum_squares_test.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "third_party/googletest/src/include/gtest/gtest.h"
  14. #include "./vpx_config.h"
  15. #include "./vpx_dsp_rtcd.h"
  16. #include "test/acm_random.h"
  17. #include "test/clear_system_state.h"
  18. #include "test/register_state_check.h"
  19. #include "test/util.h"
  20. #include "vpx_ports/mem.h"
  21. using libvpx_test::ACMRandom;
  22. namespace {
  23. const int kNumIterations = 10000;
  24. typedef uint64_t (*SSI16Func)(const int16_t *src, int stride, int size);
  25. typedef std::tr1::tuple<SSI16Func, SSI16Func> SumSquaresParam;
  26. class SumSquaresTest : public ::testing::TestWithParam<SumSquaresParam> {
  27. public:
  28. virtual ~SumSquaresTest() {}
  29. virtual void SetUp() {
  30. ref_func_ = GET_PARAM(0);
  31. tst_func_ = GET_PARAM(1);
  32. }
  33. virtual void TearDown() { libvpx_test::ClearSystemState(); }
  34. protected:
  35. SSI16Func ref_func_;
  36. SSI16Func tst_func_;
  37. };
  38. TEST_P(SumSquaresTest, OperationCheck) {
  39. ACMRandom rnd(ACMRandom::DeterministicSeed());
  40. DECLARE_ALIGNED(16, int16_t, src[256 * 256]);
  41. const int msb = 11; // Up to 12 bit input
  42. const int limit = 1 << (msb + 1);
  43. for (int k = 0; k < kNumIterations; k++) {
  44. const int size = 4 << rnd(6); // Up to 128x128
  45. int stride = 4 << rnd(7); // Up to 256 stride
  46. while (stride < size) { // Make sure it's valid
  47. stride = 4 << rnd(7);
  48. }
  49. for (int i = 0; i < size; ++i) {
  50. for (int j = 0; j < size; ++j) {
  51. src[i * stride + j] = rnd(2) ? rnd(limit) : -rnd(limit);
  52. }
  53. }
  54. const uint64_t res_ref = ref_func_(src, stride, size);
  55. uint64_t res_tst;
  56. ASM_REGISTER_STATE_CHECK(res_tst = tst_func_(src, stride, size));
  57. ASSERT_EQ(res_ref, res_tst) << "Error: Sum Squares Test"
  58. << " C output does not match optimized output.";
  59. }
  60. }
  61. TEST_P(SumSquaresTest, ExtremeValues) {
  62. ACMRandom rnd(ACMRandom::DeterministicSeed());
  63. DECLARE_ALIGNED(16, int16_t, src[256 * 256]);
  64. const int msb = 11; // Up to 12 bit input
  65. const int limit = 1 << (msb + 1);
  66. for (int k = 0; k < kNumIterations; k++) {
  67. const int size = 4 << rnd(6); // Up to 128x128
  68. int stride = 4 << rnd(7); // Up to 256 stride
  69. while (stride < size) { // Make sure it's valid
  70. stride = 4 << rnd(7);
  71. }
  72. const int val = rnd(2) ? limit - 1 : -(limit - 1);
  73. for (int i = 0; i < size; ++i) {
  74. for (int j = 0; j < size; ++j) {
  75. src[i * stride + j] = val;
  76. }
  77. }
  78. const uint64_t res_ref = ref_func_(src, stride, size);
  79. uint64_t res_tst;
  80. ASM_REGISTER_STATE_CHECK(res_tst = tst_func_(src, stride, size));
  81. ASSERT_EQ(res_ref, res_tst) << "Error: Sum Squares Test"
  82. << " C output does not match optimized output.";
  83. }
  84. }
  85. using std::tr1::make_tuple;
  86. #if HAVE_SSE2
  87. INSTANTIATE_TEST_CASE_P(
  88. SSE2, SumSquaresTest,
  89. ::testing::Values(make_tuple(&vpx_sum_squares_2d_i16_c,
  90. &vpx_sum_squares_2d_i16_sse2)));
  91. #endif // HAVE_SSE2
  92. #if HAVE_MSA
  93. INSTANTIATE_TEST_CASE_P(MSA, SumSquaresTest, ::testing::Values(make_tuple(
  94. &vpx_sum_squares_2d_i16_c,
  95. &vpx_sum_squares_2d_i16_msa)));
  96. #endif // HAVE_MSA
  97. } // namespace