Debian Codegraph @

src/lib/core/highway/hwy/tests/reverse_test.cc

raw symbols SCIP
1 // Copyright 2022 Google LLC
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include <stddef.h>
17
18 #include "hwy/base.h"
19
20 #undef HWY_TARGET_INCLUDE
21 #define HWY_TARGET_INCLUDE "tests/reverse_test.cc"
22 #include "hwy/foreach_target.h" // IWYU pragma: keep
23 #include "hwy/highway.h"
24 #include "hwy/tests/test_util-inl.h"
25
26 HWY_BEFORE_NAMESPACE();
27 namespace hwy {
28 namespace HWY_NAMESPACE {
29
30 struct TestReverse {
31 template <class T, class D>
32 HWY_NOINLINE void operator()(T /*unused*/, D d) {
33 const size_t N = Lanes(d);
34 const RebindToUnsigned<D> du; // Iota does not support float16_t.
35 const auto v = BitCast(d, Iota(du, 1));
36 auto expected = AllocateAligned<T>(N);
37
38 // Can't set float16_t value directly, need to permute in memory.
39 auto copy = AllocateAligned<T>(N);
40 Store(v, d, copy.get());
41 for (size_t i = 0; i < N; ++i) {
42 expected[i] = copy[N - 1 - i];
43 }
44 HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse(d, v));
45 }
46 };
47
48 struct TestReverse2 {
49 template <class T, class D>
50 HWY_NOINLINE void operator()(T /*unused*/, D d) {
51 const size_t N = Lanes(d);
52 const RebindToUnsigned<D> du; // Iota does not support float16_t.
53 const auto v = BitCast(d, Iota(du, 1));
54 auto expected = AllocateAligned<T>(N);
55 if (N == 1) {
56 Store(v, d, expected.get());
57 HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse2(d, v));
58 return;
59 }
60
61 // Can't set float16_t value directly, need to permute in memory.
62 auto copy = AllocateAligned<T>(N);
63 Store(v, d, copy.get());
64 for (size_t i = 0; i < N; ++i) {
65 expected[i] = copy[i ^ 1];
66 }
67 HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse2(d, v));
68 }
69 };
70
71 struct TestReverse4 {
72 template <class T, class D>
73 HWY_NOINLINE void operator()(T /*unused*/, D d) {
74 const size_t N = Lanes(d);
75 const RebindToUnsigned<D> du; // Iota does not support float16_t.
76 const auto v = BitCast(d, Iota(du, 1));
77 auto expected = AllocateAligned<T>(N);
78
79 // Can't set float16_t value directly, need to permute in memory.
80 auto copy = AllocateAligned<T>(N);
81 Store(v, d, copy.get());
82 for (size_t i = 0; i < N; ++i) {
83 expected[i] = copy[i ^ 3];
84 }
85 HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse4(d, v));
86 }
87 };
88
89 struct TestReverse8 {
90 template <class T, class D>
91 HWY_NOINLINE void operator()(T /*unused*/, D d) {
92 const size_t N = Lanes(d);
93 const RebindToUnsigned<D> du; // Iota does not support float16_t.
94 const auto v = BitCast(d, Iota(du, 1));
95 auto expected = AllocateAligned<T>(N);
96
97 // Can't set float16_t value directly, need to permute in memory.
98 auto copy = AllocateAligned<T>(N);
99 Store(v, d, copy.get());
100 for (size_t i = 0; i < N; ++i) {
101 expected[i] = copy[i ^ 7];
102 }
103 HWY_ASSERT_VEC_EQ(d, expected.get(), Reverse8(d, v));
104 }
105 };
106
107 HWY_NOINLINE void TestAllReverse() {
108 // 8-bit is not supported because Risc-V uses rgather of Lanes - Iota,
109 // which requires 16 bits.
110 ForUIF163264(ForPartialVectors<TestReverse>());
111 }
112
113 HWY_NOINLINE void TestAllReverse2() {
114 // 8-bit is not supported because Risc-V uses rgather of Lanes - Iota,
115 // which requires 16 bits.
116 ForUIF64(ForGEVectors<128, TestReverse2>());
117 ForUIF32(ForGEVectors<64, TestReverse2>());
118 ForUIF16(ForGEVectors<32, TestReverse2>());
119
120 #if HWY_TARGET == HWY_SSSE3
121 // Implemented mainly for internal use.
122 ForUI8(ForPartialVectors<TestReverse2>());
123 #endif
124 }
125
126 HWY_NOINLINE void TestAllReverse4() {
127 // 8-bit is not supported because Risc-V uses rgather of Lanes - Iota,
128 // which requires 16 bits.
129 ForUIF64(ForGEVectors<256, TestReverse4>());
130 ForUIF32(ForGEVectors<128, TestReverse4>());
131 ForUIF16(ForGEVectors<64, TestReverse4>());
132 }
133
134 HWY_NOINLINE void TestAllReverse8() {
135 // 8-bit is not supported because Risc-V uses rgather of Lanes - Iota,
136 // which requires 16 bits.
137 ForUIF64(ForGEVectors<512, TestReverse8>());
138 ForUIF32(ForGEVectors<256, TestReverse8>());
139 ForUIF16(ForGEVectors<128, TestReverse8>());
140 }
141
142 struct TestReverseBlocks {
143 template <class T, class D>
144 HWY_NOINLINE void operator()(T /*unused*/, D d) {
145 const size_t N = Lanes(d);
146 const RebindToUnsigned<D> du; // Iota does not support float16_t.
147 const auto v = BitCast(d, Iota(du, 1));
148 auto expected = AllocateAligned<T>(N);
149
150 constexpr size_t kLanesPerBlock = 16 / sizeof(T);
151 const size_t num_blocks = N / kLanesPerBlock;
152 HWY_ASSERT(num_blocks != 0);
153
154 // Can't set float16_t value directly, need to permute in memory.
155 auto copy = AllocateAligned<T>(N);
156 Store(v, d, copy.get());
157 for (size_t i = 0; i < N; ++i) {
158 const size_t idx_block = i / kLanesPerBlock;
159 const size_t base = (num_blocks - 1 - idx_block) * kLanesPerBlock;
160 expected[i] = copy[base + (i % kLanesPerBlock)];
161 }
162 HWY_ASSERT_VEC_EQ(d, expected.get(), ReverseBlocks(d, v));
163 }
164 };
165
166 HWY_NOINLINE void TestAllReverseBlocks() {
167 ForAllTypes(ForGEVectors<128, TestReverseBlocks>());
168 }
169
170 // NOLINTNEXTLINE(google-readability-namespace-comments)
171 } // namespace HWY_NAMESPACE
172 } // namespace hwy
173 HWY_AFTER_NAMESPACE();
174
175 #if HWY_ONCE
176
177 namespace hwy {
178 HWY_BEFORE_TEST(HwyReverseTest);
179 HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverse);
180 HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverse2);
181 HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverse4);
182 HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverse8);
183 HWY_EXPORT_AND_TEST_P(HwyReverseTest, TestAllReverseBlocks);
184 } // namespace hwy
185
186 #endif