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_test.h"
16
17 #include "gtest/gtest.h"
18 #include "src/utils/cpu.h"
19
20 #if LIBGAV1_TARGETING_AVX2
21
22 #include <cstdint>
23
24 #include "src/dsp/x86/common_avx2.h"
25 #include "src/utils/common.h"
26
27 namespace libgav1 {
28 namespace dsp {
29
30 // Show that RightShiftWithRounding_S16() is equal to
31 // RightShiftWithRounding() only for values less than or equal to
32 // INT16_MAX - ((1 << bits) >> 1). In particular, if bits == 16, then
33 // RightShiftWithRounding_S16() is equal to RightShiftWithRounding() only for
34 // negative values.
AVX2RightShiftWithRoundingS16Test()35 void AVX2RightShiftWithRoundingS16Test() {
36 for (int bits = 0; bits < 16; ++bits) {
37 const int bias = (1 << bits) >> 1;
38 for (int32_t value = INT16_MIN; value <= INT16_MAX; ++value) {
39 const __m256i v_val_d = _mm256_set1_epi16(value);
40 const __m256i v_result_d = RightShiftWithRounding_S16(v_val_d, bits);
41 // Note _mm256_extract_epi16 is avoided for compatibility with Visual
42 // Studio < 2017.
43 const int16_t result =
44 _mm_extract_epi16(_mm256_extracti128_si256(v_result_d, 0), 0);
45 const int32_t expected = RightShiftWithRounding(value, bits);
46 if (value <= INT16_MAX - bias) {
47 EXPECT_EQ(result, expected) << "value: " << value << ", bits: " << bits;
48 } else {
49 EXPECT_EQ(expected, 1 << (15 - bits));
50 EXPECT_EQ(result, -expected)
51 << "value: " << value << ", bits: " << bits;
52 }
53 }
54 }
55 }
56
57 } // namespace dsp
58 } // namespace libgav1
59
60 #else // !LIBGAV1_TARGETING_AVX2
61
62 namespace libgav1 {
63 namespace dsp {
64
AVX2RightShiftWithRoundingS16Test()65 void AVX2RightShiftWithRoundingS16Test() {
66 GTEST_SKIP() << "Build this module for x86(-64) with AVX2 enabled to enable "
67 "the tests.";
68 }
69
70 } // namespace dsp
71 } // namespace libgav1
72
73 #endif // LIBGAV1_TARGETING_AVX2
74