Debian Codegraph @

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

raw symbols SCIP
1 // Copyright 2019 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 #include <stdint.h>
18
19 #include "hwy/aligned_allocator.h"
20
21 #undef HWY_TARGET_INCLUDE
22 #define HWY_TARGET_INCLUDE "tests/if_test.cc"
23 #include "hwy/foreach_target.h" // IWYU pragma: keep
24 #include "hwy/highway.h"
25 #include "hwy/tests/test_util-inl.h"
26
27 HWY_BEFORE_NAMESPACE();
28 namespace hwy {
29 namespace HWY_NAMESPACE {
30
31 struct TestIfThenElse {
32 template <class T, class D>
33 HWY_NOINLINE void operator()(T /*unused*/, D d) {
34 RandomState rng;
35
36 using TI = MakeSigned<T>; // For mask > 0 comparison
37 const Rebind<TI, D> di;
38 const size_t N = Lanes(d);
39 auto in1 = AllocateAligned<T>(N);
40 auto in2 = AllocateAligned<T>(N);
41 auto bool_lanes = AllocateAligned<TI>(N);
42 auto expected = AllocateAligned<T>(N);
43
44 // Each lane should have a chance of having mask=true.
45 for (size_t rep = 0; rep < AdjustedReps(200); ++rep) {
46 for (size_t i = 0; i < N; ++i) {
47 in1[i] = static_cast<T>(Random32(&rng));
48 in2[i] = static_cast<T>(Random32(&rng));
49 bool_lanes[i] = (Random32(&rng) & 16) ? TI(1) : TI(0);
50 }
51
52 const auto v1 = Load(d, in1.get());
53 const auto v2 = Load(d, in2.get());
54 const auto mask = RebindMask(d, Gt(Load(di, bool_lanes.get()), Zero(di)));
55
56 for (size_t i = 0; i < N; ++i) {
57 expected[i] = bool_lanes[i] ? in1[i] : in2[i];
58 }
59 HWY_ASSERT_VEC_EQ(d, expected.get(), IfThenElse(mask, v1, v2));
60
61 for (size_t i = 0; i < N; ++i) {
62 expected[i] = bool_lanes[i] ? in1[i] : T(0);
63 }
64 HWY_ASSERT_VEC_EQ(d, expected.get(), IfThenElseZero(mask, v1));
65
66 for (size_t i = 0; i < N; ++i) {
67 expected[i] = bool_lanes[i] ? T(0) : in2[i];
68 }
69 HWY_ASSERT_VEC_EQ(d, expected.get(), IfThenZeroElse(mask, v2));
70 }
71 }
72 };
73
74 HWY_NOINLINE void TestAllIfThenElse() {
75 ForAllTypes(ForPartialVectors<TestIfThenElse>());
76 }
77
78 struct TestIfVecThenElse {
79 template <class T, class D>
80 HWY_NOINLINE void operator()(T /*unused*/, D d) {
81 RandomState rng;
82
83 using TU = MakeUnsigned<T>; // For all-one mask
84 const Rebind<TU, D> du;
85 const size_t N = Lanes(d);
86 auto in1 = AllocateAligned<T>(N);
87 auto in2 = AllocateAligned<T>(N);
88 auto vec_lanes = AllocateAligned<TU>(N);
89 auto expected = AllocateAligned<T>(N);
90
91 // Each lane should have a chance of having mask=true.
92 for (size_t rep = 0; rep < AdjustedReps(200); ++rep) {
93 for (size_t i = 0; i < N; ++i) {
94 in1[i] = static_cast<T>(Random32(&rng));
95 in2[i] = static_cast<T>(Random32(&rng));
96 vec_lanes[i] = (Random32(&rng) & 16) ? static_cast<TU>(~TU(0)) : TU(0);
97 }
98
99 const auto v1 = Load(d, in1.get());
100 const auto v2 = Load(d, in2.get());
101 const auto vec = BitCast(d, Load(du, vec_lanes.get()));
102
103 for (size_t i = 0; i < N; ++i) {
104 expected[i] = vec_lanes[i] ? in1[i] : in2[i];
105 }
106 HWY_ASSERT_VEC_EQ(d, expected.get(), IfVecThenElse(vec, v1, v2));
107 }
108 }
109 };
110
111 HWY_NOINLINE void TestAllIfVecThenElse() {
112 ForAllTypes(ForPartialVectors<TestIfVecThenElse>());
113 }
114
115 struct TestZeroIfNegative {
116 template <class T, class D>
117 HWY_NOINLINE void operator()(T /*unused*/, D d) {
118 const auto v0 = Zero(d);
119 const auto vp = Iota(d, 1);
120 const auto vn = Iota(d, T(-1E5)); // assumes N < 10^5
121
122 // Zero and positive remain unchanged
123 HWY_ASSERT_VEC_EQ(d, v0, ZeroIfNegative(v0));
124 HWY_ASSERT_VEC_EQ(d, vp, ZeroIfNegative(vp));
125
126 // Negative are all replaced with zero
127 HWY_ASSERT_VEC_EQ(d, v0, ZeroIfNegative(vn));
128 }
129 };
130
131 HWY_NOINLINE void TestAllZeroIfNegative() {
132 ForFloatTypes(ForPartialVectors<TestZeroIfNegative>());
133 }
134
135 struct TestIfNegative {
136 template <class T, class D>
137 HWY_NOINLINE void operator()(T /*unused*/, D d) {
138 const auto v0 = Zero(d);
139 const auto vp = Iota(d, 1);
140 const auto vn = Or(vp, SignBit(d));
141
142 // Zero and positive remain unchanged
143 HWY_ASSERT_VEC_EQ(d, v0, IfNegativeThenElse(v0, vn, v0));
144 HWY_ASSERT_VEC_EQ(d, vn, IfNegativeThenElse(v0, v0, vn));
145 HWY_ASSERT_VEC_EQ(d, vp, IfNegativeThenElse(vp, vn, vp));
146 HWY_ASSERT_VEC_EQ(d, vn, IfNegativeThenElse(vp, vp, vn));
147
148 // Negative are replaced with 2nd arg
149 HWY_ASSERT_VEC_EQ(d, v0, IfNegativeThenElse(vn, v0, vp));
150 HWY_ASSERT_VEC_EQ(d, vn, IfNegativeThenElse(vn, vn, v0));
151 HWY_ASSERT_VEC_EQ(d, vp, IfNegativeThenElse(vn, vp, vn));
152 }
153 };
154
155 HWY_NOINLINE void TestAllIfNegative() {
156 ForFloatTypes(ForPartialVectors<TestIfNegative>());
157 ForSignedTypes(ForPartialVectors<TestIfNegative>());
158 }
159
160 // NOLINTNEXTLINE(google-readability-namespace-comments)
161 } // namespace HWY_NAMESPACE
162 } // namespace hwy
163 HWY_AFTER_NAMESPACE();
164
165 #if HWY_ONCE
166
167 namespace hwy {
168 HWY_BEFORE_TEST(HwyIfTest);
169 HWY_EXPORT_AND_TEST_P(HwyIfTest, TestAllIfThenElse);
170 HWY_EXPORT_AND_TEST_P(HwyIfTest, TestAllIfVecThenElse);
171 HWY_EXPORT_AND_TEST_P(HwyIfTest, TestAllZeroIfNegative);
172 HWY_EXPORT_AND_TEST_P(HwyIfTest, TestAllIfNegative);
173 } // namespace hwy
174
175 #endif