Debian Codegraph @

libstdc++-v3/testsuite/experimental/simd/tests/integer_operators.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // expensive: * [1-9] * *
19 // timeout-factor: 2
20 #include "bits/main.h"
21
22 template <typename T, T Begin, T End, T Stride = 1, typename F>
23 void
24 for_constexpr(F&& fun)
25 {
26 if constexpr (Begin <= End)
27 {
28 fun(std::integral_constant<T, Begin>());
29 if constexpr (Begin < End)
30 {
31 for_constexpr<T, Begin + Stride, End, Stride>(static_cast<F&&>(fun));
32 }
33 }
34 }
35
36 template <typename V>
37 void
38 test()
39 {
40 using T = typename V::value_type;
41 if constexpr (std::is_integral_v<T>)
42 {
43 constexpr int nbits(sizeof(T) * __CHAR_BIT__);
44 constexpr int n_promo_bits
45 = std::max(nbits, int(sizeof(int) * __CHAR_BIT__));
46
47 // complement
48 COMPARE(~V(), V(~T()));
49 COMPARE(~V(~T()), V());
50
51 { // modulus
52 V x = make_vec<V>({3, 4}, 2);
53 COMPARE(x % x, V(0));
54 V y = x - 1;
55 COMPARE(x % y, V(1));
56 y = x + 1;
57 COMPARE(x % y, x);
58 if (std::is_signed<T>::value)
59 {
60 x = -x;
61 COMPARE(x % y, x);
62 x = -y;
63 COMPARE(x % y, V(0));
64 x = x - 1;
65 COMPARE(x % y, V(-1));
66 x %= y;
67 COMPARE(x, V(-1));
68 }
69 }
70
71 { // bit_and
72 V x = make_vec<V>({3, 4, 5}, 8);
73 COMPARE(x & x, x);
74 COMPARE(x & ~x, V());
75 COMPARE(x & V(), V());
76 COMPARE(V() & x, V());
77 V y = make_vec<V>({1, 5, 3}, 8);
78 COMPARE(x & y, make_vec<V>({1, 4, 1}, 8));
79 x &= y;
80 COMPARE(x, make_vec<V>({1, 4, 1}, 8));
81 }
82
83 { // bit_or
84 V x = make_vec<V>({3, 4, 5}, 8);
85 COMPARE(x | x, x);
86 COMPARE(x | ~x, ~V());
87 COMPARE(x | V(), x);
88 COMPARE(V() | x, x);
89 V y = make_vec<V>({1, 5, 3}, 8);
90 COMPARE(x | y, make_vec<V>({3, 5, 7}, 8));
91 x |= y;
92 COMPARE(x, make_vec<V>({3, 5, 7}, 8));
93 }
94
95 { // bit_xor
96 V x = make_vec<V>({3, 4, 5}, 8);
97 COMPARE(x ^ x, V());
98 COMPARE(x ^ ~x, ~V());
99 COMPARE(x ^ V(), x);
100 COMPARE(V() ^ x, x);
101 V y = make_vec<V>({1, 5, 3}, 8);
102 COMPARE(x ^ y, make_vec<V>({2, 1, 6}, 0));
103 x ^= y;
104 COMPARE(x, make_vec<V>({2, 1, 6}, 0));
105 }
106
107 { // bit_shift_left
108 // Note:
109 // - negative RHS or RHS >= max(#bits(T), #bits(int)) is UB
110 // - negative LHS is UB
111 // - shifting into (or over) the sign bit is UB
112 // - unsigned LHS overflow is modulo arithmetic
113 COMPARE(V() << 1, V());
114 for (int i = 0; i < nbits - 1; ++i)
115 {
116 COMPARE(V(1) << i, V(T(1) << i)) << "i: " << i;
117 }
118 for_constexpr<int, 0, n_promo_bits - 1>(
119 [](auto shift_ic) {
120 constexpr int shift = shift_ic;
121 const V seq = make_value_unknown(V([&](T i) {
122 if constexpr (std::is_signed_v<T>)
123 {
124 const T max = std::__finite_max_v<T> >> shift;
125 return max == 0 ? 1 : (std::abs(max - i) % max) + 1;
126 }
127 else
128 {
129 return ~T() - i;
130 }
131 }));
132 const V ref([&](T i) { return T(seq[i] << shift); });
133 COMPARE(seq << shift, ref) << "seq: " << seq
134 << ", shift: " << shift;
135 COMPARE(seq << make_value_unknown(shift), ref)
136 << "seq: " << seq << ", shift: " << shift;
137 });
138 {
139 V seq = make_vec<V>({0, 1}, nbits - 2);
140 seq %= nbits - 1;
141 COMPARE(make_vec<V>({0, 1}, 0) << seq,
142 V([&](auto i) { return T(T(i & 1) << seq[i]); }))
143 << "seq = " << seq;
144 COMPARE(make_vec<V>({1, 0}, 0) << seq,
145 V([&](auto i) { return T(T(~i & 1) << seq[i]); }));
146 COMPARE(V(1) << seq, V([&](auto i) { return T(T(1) << seq[i]); }));
147 }
148 if (std::is_unsigned<T>::value)
149 {
150 constexpr int shift_count = nbits - 1;
151 COMPARE(V(1) << shift_count, V(T(1) << shift_count));
152 constexpr T max = // avoid overflow warning in the last COMPARE
153 std::is_unsigned<T>::value ? std::__finite_max_v<T> : T(1);
154 COMPARE(V(max) << shift_count, V(max << shift_count))
155 << "shift_count: " << shift_count;
156 }
157 }
158
159 { // bit_shift_right
160 // Note:
161 // - negative LHS is implementation defined
162 // - negative RHS or RHS >= #bits is UB
163 // - no other UB
164 COMPARE(V(~T()) >> V(0), V(~T()));
165 COMPARE(V(~T()) >> V(make_value_unknown(0)), V(~T()));
166 for (int s = 1; s < nbits; ++s)
167 {
168 COMPARE(V(~T()) >> V(s), V(T(~T()) >> s)) << "s: " << s;
169 }
170 for (int s = 1; s < nbits; ++s)
171 {
172 COMPARE(V(~T(1)) >> V(s), V(T(~T(1)) >> s)) << "s: " << s;
173 }
174 COMPARE(V(0) >> V(1), V(0));
175 COMPARE(V(1) >> V(1), V(0));
176 COMPARE(V(2) >> V(1), V(1));
177 COMPARE(V(3) >> V(1), V(1));
178 COMPARE(V(7) >> V(2), V(1));
179 for (int j = 0; j < 100; ++j)
180 {
181 const V seq([&](auto i) -> T { return (j + i) % n_promo_bits; });
182 const V expect([&](auto i) { return seq[i] == 0 ? T(1) : T(0); });
183 COMPARE(V(1) >> seq, expect) << "\nseq = " << seq;
184 COMPARE(make_value_unknown(V(1)) >> make_value_unknown(seq), expect)
185 << "\nseq = " << seq;
186 }
187 for_constexpr<int, 0, n_promo_bits - 1>([](auto shift_ic) {
188 constexpr int shift = shift_ic;
189 const V seq = make_value_unknown(V([&](int i) {
190 using U = std::make_unsigned_t<T>;
191 return T(~U() >> (i % 32));
192 }));
193 const V ref([&](T i) { return T(seq[i] >> shift); });
194 COMPARE(seq >> shift, ref)
195 << "seq: " << seq << ", shift: " << shift;
196 COMPARE(seq >> make_value_unknown(shift), ref)
197 << "seq: " << seq << ", shift: " << shift;
198 });
199 }
200 }
201 else
202 {
203 VERIFY((is_substitution_failure<V, V, std::modulus<>>));
204 VERIFY((is_substitution_failure<V, V, std::bit_and<>>));
205 VERIFY((is_substitution_failure<V, V, std::bit_or<>>));
206 VERIFY((is_substitution_failure<V, V, std::bit_xor<>>));
207 VERIFY((is_substitution_failure<V, V, bit_shift_left>));
208 VERIFY((is_substitution_failure<V, V, bit_shift_right>));
209
210 VERIFY((is_substitution_failure<V&, V, assign_modulus>));
211 VERIFY((is_substitution_failure<V&, V, assign_bit_and>));
212 VERIFY((is_substitution_failure<V&, V, assign_bit_or>));
213 VERIFY((is_substitution_failure<V&, V, assign_bit_xor>));
214 VERIFY((is_substitution_failure<V&, V, assign_bit_shift_left>));
215 VERIFY((is_substitution_failure<V&, V, assign_bit_shift_right>));
216 }
217 }