Debian Codegraph @

gcc/testsuite/g++.target/riscv/pr115458.C

raw
1 /* { dg-do compile } */
2 /* { dg-options "-march=rv64gcv1p0 -mabi=lp64d -misa-spec=20191213 -mtls-dialect=trad -march=rv64imafdc_v1p0_zmmul_zca_zcd_zve32f_zve32x_zve64d_zve64f_zve64x_zvl128b_zvl32b_zvl64b -O2 -std=c++17 -fno-exceptions -w" } */
3
4 typedef signed char int8_t;
5 typedef unsigned char uint8_t;
6 void Abort(...);
7 template <bool> struct EnableIfT;
8 template <> struct EnableIfT<true> {
9 using type = void;
10 };
11 template <bool Condition> using EnableIf = typename EnableIfT<Condition>::type;
12 template <typename> using MakeUnsigned = unsigned char;
13 template <typename> using MakeSigned = signed char;
14 template <int __v> struct integral_constant {
15 static constexpr int value = __v;
16 };
17 template <bool __v> using __bool_constant = integral_constant<__v>;
18 template <bool, typename _Tp> using __enable_if_t = _Tp;
19 char *TargetName();
20 template <typename _Tp> struct __uniq_ptr_impl {
21 template <typename _Up> struct _Ptr {
22 using type = _Up *;
23 };
24 using pointer = typename _Ptr<_Tp>::type;
25 };
26 template <typename _Tp, typename = _Tp> class unique_ptr;
27 template <typename _Tp, typename _Dp> struct unique_ptr<_Tp[], _Dp> {
28 template <typename _Up, typename _Del>
29 unique_ptr(_Up, __enable_if_t<__bool_constant<false>::value, _Del>);
30 typename __uniq_ptr_impl<_Tp>::pointer get();
31 operator bool();
32 };
33 using AllocPtr = void *;
34 using FreePtr = void();
35 template <typename T> T AllocateAlignedItems(int, AllocPtr, void *);
36 struct AlignedFreer {
37 AlignedFreer(FreePtr, void *);
38 };
39 template <typename T> using AlignedFreeUniquePtr = unique_ptr<T>;
40 AllocPtr AllocateAligned_alloc;
41 template <typename T>
42 AlignedFreeUniquePtr<T[]> AllocateAligned(int items, void *opaque) {
43 FreePtr free;
44 return AlignedFreeUniquePtr<T[]>(
45 AllocateAlignedItems<T>(items, AllocateAligned_alloc, opaque),
46 AlignedFreer(free, opaque));
47 }
48 template <typename T> AlignedFreeUniquePtr<T[]> AllocateAligned(int items) {
49 return AllocateAligned<T>(items, nullptr);
50 }
51 template <typename> void MakeTypeInfo();
52 void AssertArrayEqual(void *, void *, char *, int);
53 #pragma riscv intrinsic "vector"
54 template <typename Lane, int, int kPow2> struct Simd {
55 using T = Lane;
56 constexpr int Pow2() { return kPow2; }
57 template <typename> static constexpr int RebindPow2() { return kPow2; }
58 template <typename NewT> using Rebind = Simd<NewT, 0, RebindPow2<NewT>()>;
59 };
60 template <typename T, int kPow2> struct ClampNAndPow2 {
61 using type = Simd<T, 6, kPow2>;
62 };
63 template <typename T, int kPow2> struct ScalableTagChecker {
64 using type = typename ClampNAndPow2<T, kPow2>::type;
65 };
66 template <typename T, int kPow2>
67 using ScalableTag = typename ScalableTagChecker<T, kPow2>::type;
68 template <class D> using TFromD = typename D::T;
69 template <class T, class D> using Rebind = typename D::Rebind<T>;
70 template <class D> using RebindToSigned = Rebind<MakeSigned<D>, D>;
71 template <class D> using RebindToUnsigned = Rebind<MakeUnsigned<D>, D>;
72 template <class> struct DFromV_t;
73 template <class V> using DFromV = typename DFromV_t<V>::type;
74 template <> struct DFromV_t<vint8mf8_t> {
75 using Lane = int8_t;
76 using type = ScalableTag<Lane, -3>;
77 };
78 template <> struct DFromV_t<vint8mf4_t> {
79 using Lane = int8_t;
80 using type = ScalableTag<Lane, -2>;
81 };
82 template <> struct DFromV_t<vint8mf2_t> {
83 using Lane = int8_t;
84 using type = ScalableTag<Lane, -1>;
85 };
86 template <> struct DFromV_t<vint8m1_t> {
87 using Lane = int8_t;
88 using type = ScalableTag<Lane, 0>;
89 };
90 template <> struct DFromV_t<vint8m2_t> {
91 using Lane = int8_t;
92 using type = ScalableTag<Lane, 1>;
93 };
94 template <> struct DFromV_t<vint8m4_t> {
95 using Lane = int8_t;
96 using type = ScalableTag<Lane, 2>;
97 };
98 template <> struct DFromV_t<vint8m8_t> {
99 using Lane = int8_t;
100 using type = ScalableTag<Lane, 3>;
101 };
102 template <int N> int Lanes(Simd<int8_t, N, -3>);
103 template <int N> int Lanes(Simd<int8_t, N, -2>);
104 template <int N> int Lanes(Simd<int8_t, N, -1>);
105 template <int N> int Lanes(Simd<int8_t, N, 0>);
106 template <int N> int Lanes(Simd<int8_t, N, 1>);
107 template <int N> int Lanes(Simd<int8_t, N, 2>);
108 template <int N> int Lanes(Simd<int8_t, N, 3>);
109 template <int N> vuint8mf8_t Set(Simd<uint8_t, N, -3>, uint8_t);
110 template <int N> vuint8mf4_t Set(Simd<uint8_t, N, -2>, uint8_t);
111 template <int N> vuint8mf2_t Set(Simd<uint8_t, N, -1>, uint8_t);
112 template <int N> vuint8m1_t Set(Simd<uint8_t, N, 0>, uint8_t);
113 template <int N> vuint8m2_t Set(Simd<uint8_t, N, 1>, uint8_t);
114 template <int N> vuint8m4_t Set(Simd<uint8_t, N, 2>, uint8_t);
115 template <int N> vuint8m8_t Set(Simd<uint8_t, N, 3>, uint8_t arg) {
116 return __riscv_vmv_v_x_u8m8(arg, 0);
117 }
118 template <int N> vint8mf8_t Set(Simd<int8_t, N, -3>, int8_t);
119 template <int N> vint8mf4_t Set(Simd<int8_t, N, -2>, int8_t);
120 template <int N> vint8mf2_t Set(Simd<int8_t, N, -1>, int8_t);
121 template <int N> vint8m1_t Set(Simd<int8_t, N, 0>, int8_t);
122 template <int N> vint8m2_t Set(Simd<int8_t, N, 1>, int8_t);
123 template <int N> vint8m4_t Set(Simd<int8_t, N, 2>, int8_t);
124 template <int N> vint8m8_t Set(Simd<int8_t, N, 3>, int8_t);
125 template <class D> using VFromD = decltype(Set(D(), TFromD<D>()));
126 template <class D> VFromD<D> Zero(D d) {
127 RebindToUnsigned<decltype(d)> du;
128 return BitCast(d, Set(du, 0));
129 }
130 template <typename T, int N>
131 vuint8mf8_t BitCastToByte(Simd<T, N, -3>, vuint8mf8_t);
132 template <typename T, int N>
133 vuint8mf4_t BitCastToByte(Simd<T, N, -2>, vuint8mf4_t);
134 template <typename T, int N>
135 vuint8mf2_t BitCastToByte(Simd<T, N, -1>, vuint8mf2_t);
136 template <typename T, int N>
137 vuint8m1_t BitCastToByte(Simd<T, N, 0>, vuint8m1_t);
138 template <typename T, int N>
139 vuint8m2_t BitCastToByte(Simd<T, N, 1>, vuint8m2_t);
140 template <typename T, int N>
141 vuint8m4_t BitCastToByte(Simd<T, N, 2>, vuint8m4_t);
142 template <typename T, int N>
143 vuint8m8_t BitCastToByte(Simd<T, N, 3>, vuint8m8_t v) {
144 return v;
145 }
146 template <typename T, int N>
147 vuint8mf8_t BitCastToByte(Simd<T, N, -3>, vint8mf8_t);
148 template <int N> vint8mf8_t BitCastFromByte(Simd<int8_t, N, -3>, vuint8mf8_t);
149 template <typename T, int N>
150 vuint8mf4_t BitCastToByte(Simd<T, N, -2>, vint8mf4_t);
151 template <int N> vint8mf4_t BitCastFromByte(Simd<int8_t, N, -2>, vuint8mf4_t);
152 template <typename T, int N>
153 vuint8mf2_t BitCastToByte(Simd<T, N, -1>, vint8mf2_t);
154 template <int N> vint8mf2_t BitCastFromByte(Simd<int8_t, N, -1>, vuint8mf2_t);
155 template <typename T, int N> vuint8m1_t BitCastToByte(Simd<T, N, 0>, vint8m1_t);
156 template <int N> vint8m1_t BitCastFromByte(Simd<int8_t, N, 0>, vuint8m1_t);
157 template <typename T, int N> vuint8m2_t BitCastToByte(Simd<T, N, 1>, vint8m2_t);
158 template <int N> vint8m2_t BitCastFromByte(Simd<int8_t, N, 1>, vuint8m2_t);
159 template <typename T, int N> vuint8m4_t BitCastToByte(Simd<T, N, 2>, vint8m4_t);
160 template <int N> vint8m4_t BitCastFromByte(Simd<int8_t, N, 2>, vuint8m4_t);
161 template <typename T, int N> vuint8m8_t BitCastToByte(Simd<T, N, 3>, vint8m8_t);
162 template <int N> vint8m8_t BitCastFromByte(Simd<int8_t, N, 3>, vuint8m8_t v) {
163 return __riscv_vreinterpret_v_u8m8_i8m8(v);
164 }
165 template <class D, class FromV> VFromD<D> BitCast(D d, FromV v) {
166 return BitCastFromByte(d, BitCastToByte(d, v));
167 }
168 vint8mf8_t And(vint8mf8_t, vint8mf8_t);
169 vint8mf4_t And(vint8mf4_t, vint8mf4_t);
170 vint8mf2_t And(vint8mf2_t, vint8mf2_t);
171 vint8m1_t And(vint8m1_t, vint8m1_t);
172 vint8m2_t And(vint8m2_t, vint8m2_t);
173 vint8m4_t And(vint8m4_t, vint8m4_t);
174 vint8m8_t And(vint8m8_t, vint8m8_t);
175 vint8mf8_t Xor(vint8mf8_t, vint8mf8_t);
176 vint8mf4_t Xor(vint8mf4_t, vint8mf4_t);
177 vint8mf2_t Xor(vint8mf2_t, vint8mf2_t);
178 vint8m1_t Xor(vint8m1_t, vint8m1_t);
179 vint8m2_t Xor(vint8m2_t, vint8m2_t);
180 vint8m4_t Xor(vint8m4_t, vint8m4_t);
181 vint8m8_t Xor(vint8m8_t, vint8m8_t);
182 template <class V> V AndNot(V);
183 template <class V> V Xor3(V);
184 template <class V> V Neg(V);
185 template <int> vuint8mf8_t ShiftLeft(vuint8mf8_t);
186 template <int> vuint8mf4_t ShiftLeft(vuint8mf4_t);
187 template <int> vuint8mf2_t ShiftLeft(vuint8mf2_t);
188 template <int> vuint8m1_t ShiftLeft(vuint8m1_t);
189 template <int> vuint8m2_t ShiftLeft(vuint8m2_t);
190 template <int> vuint8m4_t ShiftLeft(vuint8m4_t);
191 template <int> vuint8m8_t ShiftLeft(vuint8m8_t);
192 vint8mf8_t MaskedSubOr(vint8mf8_t, vbool64_t, vint8mf8_t, vint8mf8_t);
193 vint8mf4_t MaskedSubOr(vint8mf4_t, vbool32_t, vint8mf4_t, vint8mf4_t);
194 vint8mf2_t MaskedSubOr(vint8mf2_t, vbool16_t, vint8mf2_t, vint8mf2_t);
195 vint8m1_t MaskedSubOr(vint8m1_t, vbool8_t, vint8m1_t, vint8m1_t);
196 vint8m2_t MaskedSubOr(vint8m2_t, vbool4_t, vint8m2_t, vint8m2_t);
197 vint8m4_t MaskedSubOr(vint8m4_t, vbool2_t, vint8m4_t, vint8m4_t);
198 vint8m8_t MaskedSubOr(vint8m8_t no, vbool1_t m, vint8m8_t a, vint8m8_t b) {
199 return __riscv_vsub_vv_i8m8_mu(m, no, a, b, 0);
200 }
201 vbool64_t Lt(vint8mf8_t, vint8mf8_t);
202 vbool32_t Lt(vint8mf4_t, vint8mf4_t);
203 vbool16_t Lt(vint8mf2_t, vint8mf2_t);
204 vbool8_t Lt(vint8m1_t, vint8m1_t);
205 vbool4_t Lt(vint8m2_t, vint8m2_t);
206 vbool2_t Lt(vint8m4_t, vint8m4_t);
207 vbool1_t Lt(vint8m8_t a, vint8m8_t b) {
208 return __riscv_vmslt_vv_i8m8_b1(a, b, 0);
209 }
210 template <class V> V BroadcastSignBit(V);
211 template <class V> V IfNegativeThenElse(V);
212 template <int N> void Store(vint8mf8_t, Simd<int8_t, N, -3>, int8_t *);
213 template <int N> void Store(vint8mf4_t, Simd<int8_t, N, -2>, int8_t *);
214 template <int N> void Store(vint8mf2_t, Simd<int8_t, N, -1>, int8_t *);
215 template <int N> void Store(vint8m1_t, Simd<int8_t, N, 0>, int8_t *);
216 template <int N> void Store(vint8m2_t, Simd<int8_t, N, 1>, int8_t *);
217 template <int N> void Store(vint8m4_t, Simd<int8_t, N, 2>, int8_t *);
218 template <int N> void Store(vint8m8_t, Simd<int8_t, N, 3>, int8_t *);
219 template <class D, class V, EnableIf<D().Pow2() <= 2> * = nullptr>
220 V InterleaveUpperBlocks(D, V, V) {}
221 template <class D, class V, EnableIf<(D().Pow2() > 2)> * = nullptr>
222 V InterleaveUpperBlocks(D, V, V);
223 template <typename T, int N, int kPow2>
224 constexpr bool IsGE128(Simd<T, N, kPow2>) {
225 return kPow2 >= 0;
226 }
227 template <class D, class V, EnableIf<IsGE128(D())> * = nullptr>
228 V InterleaveLower(D, V, V);
229 template <class D, class V, EnableIf<!IsGE128(D())> * = nullptr>
230 V InterleaveLower(D, V, V);
231 template <class D, class V, EnableIf<IsGE128(D())> * = nullptr>
232 V InterleaveUpper(D d, V a, V b) {
233 return InterleaveUpperBlocks(d, a, b);
234 }
235 template <class D, class V, EnableIf<!IsGE128(D())> * = nullptr>
236 V InterleaveUpper(D, V, V);
237 template <class D, typename T2> VFromD<D> Iota(D, T2);
238 template <class D> using Vec = decltype(Zero(D()));
239 template <class D> Vec<D> SignBit(D);
240 template <class V> V IfNegativeThenElseZero(V);
241 template <class V> V IfNegativeThenZeroElse(V);
242 template <class V> V BitwiseIfThenElse(V, V, V);
243 template <class V> inline V IfNegativeThenNegOrUndefIfZero(V mask, V v) {
244 auto zero = Zero(DFromV<V>());
245 return MaskedSubOr(v, Lt(mask, zero), zero, v);
246 }
247 template <class D> Vec<D> PositiveIota(D);
248 int AssertVecEqual_line;
249 template <class D, typename T = TFromD<D>>
250 inline void AssertVecEqual(D d, Vec<D> expected, Vec<D> actual, char *) {
251 int N = Lanes(d);
252 auto expected_lanes = AllocateAligned<T>(N),
253 actual_lanes = AllocateAligned<T>(N);
254 if (expected_lanes && actual_lanes)
255 Abort("", "");
256 Store(expected, d, expected_lanes.get());
257 Store(actual, d, actual_lanes.get());
258 MakeTypeInfo<T>();
259 char *target_name = TargetName();
260 AssertArrayEqual(expected_lanes.get(), actual_lanes.get(), target_name,
261 AssertVecEqual_line);
262 }
263 template <typename> constexpr int MinPow2() { return sizeof(int) ? -3 : 0; }
264 template <typename T, int kPow2, int kMaxPow2, int, class Test>
265 struct ForeachPow2 {
266 static void Do(int min_lanes) {
267 ScalableTag<T, kPow2> d;
268 Lanes(d);
269 Test()(T(), d);
270 ForeachPow2<T, kPow2 + 1, kMaxPow2, kPow2 + 1 <= kMaxPow2, Test>::Do(
271 min_lanes);
272 }
273 };
274 template <typename T, int kPow2, int kMaxPow2, class Test>
275 struct ForeachPow2<T, kPow2, kMaxPow2, false, Test> {
276 static void Do(int);
277 };
278 template <typename T, int kAddMin, int kSubMax, class Test>
279 using ForeachPow2Trim =
280 ForeachPow2<T, MinPow2<T>(), 3, kAddMin <= kSubMax, Test>;
281 template <class Test, int kPow2> struct ForExtendableVectors {
282 template <typename T> void operator()(T) {
283 ForeachPow2Trim<T, 0, kPow2, Test>::Do(1);
284 }
285 };
286 template <class Test> struct ForPartialVectors {
287 template <typename T> void operator()(T t) {
288 ForExtendableVectors<Test, 0>()(t);
289 }
290 };
291 template <class Func> void ForSignedTypes(Func func) { func(int8_t()); }
292 struct TestIfNegative {
293 template <class T, class D> void operator()(T, D d) {
294 auto vp = Iota(d, 1), vsignbit = SignBit(d);
295 RebindToSigned<decltype(d)> di;
296 RebindToUnsigned<decltype(d)> du;
297 BitCast(d, ShiftLeft<sizeof(TFromD<decltype(d)>)>(Iota(du, 1)));
298 auto m1 = Xor3(BitCast(d, Set(du, {})));
299 auto x1 = Xor(vp, BitCast(d, Set(d, {})));
300 auto x2 = Xor(vp, BitCast(d, Set(d, {})));
301 Xor(m1, vsignbit);
302 auto m1_s = BitCast(d, BroadcastSignBit(BitCast(di, m1)));
303 auto expected_2 = BitwiseIfThenElse(m1_s, x2, x1);
304 AssertVecEqual(d, expected_2, IfNegativeThenElse(x2), "");
305 auto expected_3 = And(m1_s, x1);
306 auto expected_4 = AndNot(x2);
307 AssertVecEqual(d, expected_3, IfNegativeThenElseZero(x1), "");
308 AssertVecEqual(d, expected_3, IfNegativeThenZeroElse(x1), "");
309 AssertVecEqual(d, expected_4, IfNegativeThenZeroElse(x2), "");
310 AssertVecEqual(d, expected_4, IfNegativeThenElseZero(x2), "");
311 }
312 };
313 void TestAllIfNegative() {
314 ForSignedTypes(ForPartialVectors<TestIfNegative>());
315 }
316 template <class D>
317 void TestMoreThan1LaneIfNegativeThenNegOrUndefIfZero(D d, Vec<D> v1) {
318 Vec<D> v2, v3 = InterleaveLower(d, v1, v1), v5 = InterleaveLower(d, v1, v2);
319 if (Lanes(d) < 2)
320 return;
321 Vec<D> v4 = InterleaveUpper(d, v1, v1);
322 Vec<D> v6 = InterleaveUpper(d, v1, v2);
323 Vec<D> v7 = InterleaveLower(d, v2, v1);
324 Vec<D> v8 = InterleaveUpper(d, v2, v1);
325 AssertVecEqual(d, v3, IfNegativeThenNegOrUndefIfZero(v3, v3), "");
326 AssertVecEqual(d, v4, IfNegativeThenNegOrUndefIfZero(v4, v4), "");
327 AssertVecEqual(d, v4, IfNegativeThenNegOrUndefIfZero(v8, v8), "");
328 AssertVecEqual(d, v6, IfNegativeThenNegOrUndefIfZero(v4, v6), "");
329 AssertVecEqual(d, v7, IfNegativeThenNegOrUndefIfZero(v3, v7), "");
330 AssertVecEqual(d, v8, IfNegativeThenNegOrUndefIfZero(v4, v8), "");
331 Vec<D> zero = Zero(d);
332 AssertVecEqual(d, zero, IfNegativeThenNegOrUndefIfZero(v3, zero), "");
333 AssertVecEqual(d, zero, IfNegativeThenNegOrUndefIfZero(v4, zero), "");
334 AssertVecEqual(d, zero, IfNegativeThenNegOrUndefIfZero(v5, zero), "");
335 AssertVecEqual(d, zero, IfNegativeThenNegOrUndefIfZero(v6, zero), "");
336 AssertVecEqual(d, zero, IfNegativeThenNegOrUndefIfZero(v7, zero), "");
337 AssertVecEqual(d, zero, IfNegativeThenNegOrUndefIfZero(v8, zero), "");
338 }
339 struct TestIfNegativeThenNegOrUndefIfZero {
340 template <typename T, class D> void operator()(T, D d) {
341 auto v1 = PositiveIota(d), v2 = Neg(v1), zero = Zero(d), vmin = Set(d, 0),
342 vmax = Set(d, 0);
343 AssertVecEqual(d, v2, IfNegativeThenNegOrUndefIfZero(v1, v2), "");
344 AssertVecEqual(d, v2, IfNegativeThenNegOrUndefIfZero(v2, v1), "");
345 AssertVecEqual(d, v1, IfNegativeThenNegOrUndefIfZero(v2, v2), "");
346 AssertVecEqual(d, zero, IfNegativeThenNegOrUndefIfZero(zero, zero), "");
347 AssertVecEqual(d, zero, IfNegativeThenNegOrUndefIfZero(v1, zero), "");
348 AssertVecEqual(d, zero, IfNegativeThenNegOrUndefIfZero(v2, zero), "");
349 AssertVecEqual(d, v1, IfNegativeThenNegOrUndefIfZero(vmin, v2), "");
350 AssertVecEqual(d, v1, IfNegativeThenNegOrUndefIfZero(vmax, v1), "");
351 AssertVecEqual(d, v2, IfNegativeThenNegOrUndefIfZero(vmax, v2), "");
352 TestMoreThan1LaneIfNegativeThenNegOrUndefIfZero(d, v1);
353 }
354 };
355 void TestAllIfNegativeThenNegOrUndefIfZero() {
356 ForSignedTypes(ForPartialVectors<TestIfNegativeThenNegOrUndefIfZero>());
357 }