]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/simd/tests/reductions.cc
1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
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)
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.
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/>.
18 // expensive: * [1-9] * *
19 #include "bits/main.h"
26 using T
= typename
V::value_type
;
27 COMPARE(reduce(V(1)), T(V::size()));
30 COMPARE(reduce(x
, std::multiplies
<>()), T(1));
32 COMPARE(reduce(x
, std::multiplies
<>()), T(2));
33 if constexpr (V::size() > 1)
36 COMPARE(reduce(x
, std::multiplies
<>()), T(6));
39 COMPARE(reduce(V([](int i
) { return i
& 1; })), T(V::size() / 2));
40 COMPARE(reduce(V([](int i
) { return i
% 3; })),
41 T(3 * (V::size() / 3) // 0+1+2 for every complete 3 elements in V
42 + (V::size() % 3) / 2 // 0->0, 1->0, 2->1 adjustment
44 if ((1 + V::size()) * V::size() / 2 <= std::__finite_max_v
<T
>)
46 COMPARE(reduce(V([](int i
) { return i
+ 1; })),
47 T((1 + V::size()) * V::size() / 2));
52 COMPARE(reduce(y
), T(2 * V::size()));
53 COMPARE(reduce(where(y
> 2, y
)), T(0));
54 COMPARE(reduce(where(y
== 2, y
)), T(2 * V::size()));
58 COMPARE(hmin(V(1)), T(1));
59 COMPARE(hmax(V(1)), T(1));
60 const V
z([](T i
) { return i
+ 1; });
61 COMPARE(std::experimental::reduce(z
,
68 COMPARE(std::experimental::reduce(z
,
75 COMPARE(std::experimental::reduce(where(z
> 1, z
), 117,
80 T(V::size() == 1 ? 117 : 2))
82 COMPARE(hmin(z
), T(1));
83 COMPARE(hmax(z
), T(V::size()));
86 COMPARE(hmin(where(z
> 1, z
)), T(2));
87 COMPARE(hmax(where(z
> 1, z
)), T(V::size()));
89 COMPARE(hmin(where(z
< 4, z
)), T(1));
90 COMPARE(hmax(where(z
< 4, z
)), std::min(T(V::size()), T(3)));
91 const V zz
= make_value_unknown(z
);
92 COMPARE(hmin(zz
), T(1));
93 COMPARE(hmax(zz
), T(V::size()));
96 COMPARE(hmin(where(zz
> 1, zz
)), T(2));
97 COMPARE(hmax(where(zz
> 1, zz
)), T(V::size()));
99 COMPARE(hmin(where(zz
< 4, zz
)), T(1));
100 COMPARE(hmax(where(zz
< 4, zz
)), std::min(T(V::size()), T(3)));
103 test_values
<V
>({}, {1000}, [](V x
) {
104 // avoid over-/underflow on signed integers:
105 if constexpr (std::is_signed_v
<T
> && std::is_integral_v
<T
>)
107 // The error in the following could be huge if catastrophic
108 // cancellation occurs. (e.g. `a-a+b+b` vs. `a+b+b-a`).
109 // Avoid catastrophic cancellation for floating point:
110 if constexpr (std::is_floating_point_v
<T
>)
113 for (size_t i
= 1; i
< V::size(); ++i
)
115 const T max_distance
= std::is_integral_v
<T
> ? 0 : V::size() / 2;
116 ULP_COMPARE(reduce(x
), acc
, max_distance
).on_failure("x = ", x
);