xref: /aosp_15_r20/external/libgav1/src/dsp/x86/common_sse4_test.cc (revision 095378508e87ed692bf8dfeb34008b65b3735891)
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_sse4_test.h"
16 
17 #include "gtest/gtest.h"
18 #include "src/utils/cpu.h"
19 
20 #if LIBGAV1_TARGETING_SSE4_1
21 
22 #include <cstdint>
23 
24 #include "src/dsp/x86/common_sse4.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.
SSE41RightShiftWithRoundingS16Test()35 void SSE41RightShiftWithRoundingS16Test() {
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 __m128i v_val_d = _mm_set1_epi16(value);
40       const __m128i v_result_d = RightShiftWithRounding_S16(v_val_d, bits);
41       const int16_t result = _mm_extract_epi16(v_result_d, 0);
42       const int32_t expected = RightShiftWithRounding(value, bits);
43       if (value <= INT16_MAX - bias) {
44         EXPECT_EQ(result, expected) << "value: " << value << ", bits: " << bits;
45       } else {
46         EXPECT_EQ(expected, 1 << (15 - bits));
47         EXPECT_EQ(result, -expected)
48             << "value: " << value << ", bits: " << bits;
49       }
50     }
51   }
52 }
53 
54 }  // namespace dsp
55 }  // namespace libgav1
56 
57 #else  // !LIBGAV1_TARGETING_SSE4_1
58 
59 namespace libgav1 {
60 namespace dsp {
61 
SSE41RightShiftWithRoundingS16Test()62 void SSE41RightShiftWithRoundingS16Test() {
63   GTEST_SKIP() << "Build this module for x86(-64) with SSE4 enabled to enable "
64                   "the tests.";
65 }
66 
67 }  // namespace dsp
68 }  // namespace libgav1
69 
70 #endif  // LIBGAV1_TARGETING_SSE4_1
71