Debian Codegraph @

libcxx/test/std/experimental/simd/simd.class/simd_ctor_broadcast.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03, c++11, c++14
10 // XFAIL: target=powerpc{{.*}}le-unknown-linux-gnu
11
12 // <experimental/simd>
13 //
14 // [simd.class]
15 // template<class U> simd(U&& value) noexcept;
16
17 #include "../test_utils.h"
18
19 namespace ex = std::experimental::parallelism_v2;
20
21 template <class T, class SimdAbi, std::size_t array_size>
22 struct BroadCastHelper {
23 const std::array<T, array_size>& expected_value;
24
25 BroadCastHelper(const std::array<T, array_size>& value) : expected_value(value) {}
26
27 template <class U>
28 void operator()() const {
29 if constexpr (is_non_narrowing_convertible_v<U, T>) {
30 ex::simd<T, SimdAbi> simd_broadcast_from_vectorizable_type(static_cast<U>(3));
31 assert_simd_values_equal<array_size>(simd_broadcast_from_vectorizable_type, expected_value);
32 }
33 }
34 };
35
36 template <class T, std::size_t>
37 struct CheckSimdBroadcastCtorFromVectorizedType {
38 template <class SimdAbi>
39 void operator()() {
40 constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
41 std::array<T, array_size> expected_value;
42 std::fill(expected_value.begin(), expected_value.end(), 3);
43
44 types::for_each(simd_test_types(), BroadCastHelper<T, SimdAbi, array_size>(expected_value));
45 }
46 };
47
48 template <class T>
49 class implicit_type {
50 T val;
51
52 public:
53 implicit_type(T v) : val(v) {}
54 operator T() const { return val; }
55 };
56
57 template <class T, std::size_t>
58 struct CheckSimdBroadcastCtor {
59 template <class SimdAbi>
60 void operator()() {
61 constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
62 std::array<T, array_size> expected_value;
63 std::fill(expected_value.begin(), expected_value.end(), 3);
64
65 implicit_type<T> implicit_convert_to_3(3);
66 ex::simd<T, SimdAbi> simd_broadcast_from_implicit_type(std::move(implicit_convert_to_3));
67 assert_simd_values_equal<array_size>(simd_broadcast_from_implicit_type, expected_value);
68
69 ex::simd<T, SimdAbi> simd_broadcast_from_int(3);
70 assert_simd_values_equal<array_size>(simd_broadcast_from_int, expected_value);
71
72 if constexpr (std::is_unsigned_v<T>) {
73 ex::simd<T, SimdAbi> simd_broadcast_from_uint(3u);
74 assert_simd_values_equal<array_size>(simd_broadcast_from_uint, expected_value);
75 }
76 }
77 };
78
79 template <class T>
80 class no_implicit_type {
81 T val;
82
83 public:
84 no_implicit_type(T v) : val(v) {}
85 };
86
87 template <class U, class T, class SimdAbi = ex::simd_abi::compatible<T>, class = void>
88 struct has_broadcast_ctor : std::false_type {};
89
90 template <class U, class T, class SimdAbi>
91 struct has_broadcast_ctor<U, T, SimdAbi, std::void_t<decltype(ex::simd<T, SimdAbi>(std::declval<U>()))>>
92 : std::true_type {};
93
94 template <class T, class SimdAbi>
95 struct CheckBroadcastCtorTraitsHelper {
96 template <class U>
97 void operator()() const {
98 if constexpr (std::is_same_v<U, int>)
99 static_assert(has_broadcast_ctor<U, T, SimdAbi>::value);
100 else if constexpr (std::is_same_v<U, unsigned int> && std::is_unsigned_v<T>)
101 static_assert(has_broadcast_ctor<U, T, SimdAbi>::value);
102 else if constexpr (is_non_narrowing_convertible_v<U, T>)
103 static_assert(has_broadcast_ctor<U, T, SimdAbi>::value);
104 else
105 static_assert(!has_broadcast_ctor<U, T, SimdAbi>::value);
106 }
107 };
108
109 template <class T, std::size_t>
110 struct CheckBroadcastCtorTraits {
111 template <class SimdAbi>
112 void operator()() {
113 types::for_each(simd_test_types(), CheckBroadcastCtorTraitsHelper<T, SimdAbi>());
114
115 static_assert(!has_broadcast_ctor<no_implicit_type<T>, T, SimdAbi>::value);
116 static_assert(has_broadcast_ctor<implicit_type<T>, T, SimdAbi>::value);
117 }
118 };
119
120 int main(int, char**) {
121 test_all_simd_abi<CheckSimdBroadcastCtorFromVectorizedType>();
122 test_all_simd_abi<CheckSimdBroadcastCtor>();
123 test_all_simd_abi<CheckBroadcastCtorTraits>();
124 return 0;
125 }