• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/dsp/x86/common_avx2.h"
16 
17 #include "gtest/gtest.h"
18 
19 #if LIBGAV1_TARGETING_AVX2
20 
21 #include <cstdint>
22 
23 #include "src/utils/common.h"
24 
25 namespace libgav1 {
26 namespace dsp {
27 namespace {
28 
29 // Show that RightShiftWithRounding_S16() is equal to
30 // RightShiftWithRounding() only for values less than or equal to
31 // INT16_MAX - ((1 << bits) >> 1). In particular, if bits == 16, then
32 // RightShiftWithRounding_S16() is equal to RightShiftWithRounding() only for
33 // negative values.
TEST(CommonDspTest,AVX2RightShiftWithRoundingS16)34 TEST(CommonDspTest, AVX2RightShiftWithRoundingS16) {
35   for (int bits = 0; bits < 16; ++bits) {
36     const int bias = (1 << bits) >> 1;
37     for (int32_t value = INT16_MIN; value <= INT16_MAX; ++value) {
38       const __m256i v_val_d = _mm256_set1_epi16(value);
39       const __m256i v_result_d = RightShiftWithRounding_S16(v_val_d, bits);
40       // Note _mm256_extract_epi16 is avoided for compatibility with Visual
41       // Studio < 2017.
42       const int16_t result =
43           _mm_extract_epi16(_mm256_extracti128_si256(v_result_d, 0), 0);
44       const int32_t expected = RightShiftWithRounding(value, bits);
45       if (value <= INT16_MAX - bias) {
46         EXPECT_EQ(result, expected) << "value: " << value << ", bits: " << bits;
47       } else {
48         EXPECT_EQ(expected, 1 << (15 - bits));
49         EXPECT_EQ(result, -expected)
50             << "value: " << value << ", bits: " << bits;
51       }
52     }
53   }
54 }
55 
56 }  // namespace
57 }  // namespace dsp
58 }  // namespace libgav1
59 
60 #else  // !LIBGAV1_TARGETING_AVX2
61 
TEST(CommonDspTest,AVX2)62 TEST(CommonDspTest, AVX2) {
63   GTEST_SKIP() << "Build this module for x86(-64) with AVX2 enabled to enable "
64                   "the tests.";
65 }
66 
67 #endif  // LIBGAV1_TARGETING_AVX2
68