Debian Codegraph @

libstdc++-v3/testsuite/experimental/simd/tests/where.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 #include "bits/main.h"
20
21 template <class V>
22 struct Convertible
23 {
24 operator V() const { return V(4); }
25 };
26
27 template <class M, class T>
28 constexpr bool
29 where_is_ill_formed_impl(M, const T&, float)
30 {
31 return true;
32 }
33
34 template <class M, class T>
35 constexpr auto
36 where_is_ill_formed_impl(M m, const T& v, int)
37 -> std::conditional_t<true, bool, decltype(std::experimental::where(m, v))>
38 {
39 return false;
40 }
41
42 template <class M, class T>
43 constexpr bool
44 where_is_ill_formed(M m, const T& v)
45 {
46 return where_is_ill_formed_impl(m, v, int());
47 }
48
49 template <typename T>
50 void
51 where_fundamental()
52 {
53 using std::experimental::where;
54 T x = T();
55 where(true, x) = x + 1;
56 COMPARE(x, T(1));
57 where(false, x) = x - 1;
58 COMPARE(x, T(1));
59 where(true, x) += T(1);
60 COMPARE(x, T(2));
61 }
62
63 template <typename V>
64 void
65 test()
66 {
67 using M = typename V::mask_type;
68 using T = typename V::value_type;
69 where_fundamental<T>();
70 VERIFY(!(sfinae_is_callable<V>(
71 [](auto x) -> decltype(where(true, x))* { return nullptr; })));
72
73 const V indexes([](int i) { return i + 1; });
74 const M alternating_mask = make_mask<M>({true, false});
75 V x = 0;
76 where(alternating_mask, x) = indexes;
77 COMPARE(alternating_mask, x == indexes);
78
79 where(!alternating_mask, x) = T(2);
80 COMPARE(!alternating_mask, x == T(2)) << x;
81
82 where(!alternating_mask, x) = Convertible<V>();
83 COMPARE(!alternating_mask, x == T(4));
84
85 x = 0;
86 COMPARE(x, T(0));
87 where(alternating_mask, x) += indexes;
88 COMPARE(alternating_mask, x == indexes);
89
90 x = 10;
91 COMPARE(x, T(10));
92 where(!alternating_mask, x) += T(1);
93 COMPARE(!alternating_mask, x == T(11));
94 where(alternating_mask, x) -= Convertible<V>();
95 COMPARE(alternating_mask, x == T(6));
96 constexpr bool fast_math =
97 #ifdef __FAST_MATH__
98 true;
99 #else
100 false;
101 #endif
102 if constexpr (fast_math && std::is_floating_point_v<T>)
103 where(alternating_mask, x) *= T(.5);
104 else
105 where(alternating_mask, x) /= T(2);
106 COMPARE(alternating_mask, x == T(3)) << "\nx = " << x;
107 where(alternating_mask, x) *= T(3);
108 COMPARE(alternating_mask, x == T(9));
109 COMPARE(!alternating_mask, x == T(11));
110
111 x = 10;
112 where(alternating_mask, x)++;
113 COMPARE(alternating_mask, x == T(11));
114 ++where(alternating_mask, x);
115 COMPARE(alternating_mask, x == T(12));
116 where(alternating_mask, x)--;
117 COMPARE(alternating_mask, x == T(11));
118 --where(alternating_mask, x);
119 --where(alternating_mask, x);
120 COMPARE(alternating_mask, x == T(9));
121 COMPARE(alternating_mask, -where(alternating_mask, x) == T(-T(9)));
122
123 const auto y = x;
124 VERIFY(where_is_ill_formed(true, y));
125 VERIFY(where_is_ill_formed(true, x));
126 VERIFY(where_is_ill_formed(true, V(x)));
127
128 M test = alternating_mask;
129 where(alternating_mask, test) = M(true);
130 COMPARE(test, alternating_mask);
131 where(alternating_mask, test) = M(false);
132 COMPARE(test, M(false));
133 where(alternating_mask, test) = M(true);
134 COMPARE(test, alternating_mask);
135 }