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 
11 // <experimental/simd>
12 //
13 // [simd.class]
14 // template <class G> explicit simd(G&& gen);
15 
16 #include <experimental/simd>
17 #include <cstdint>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 
22 namespace ex = std::experimental::parallelism_v2;
23 
24 template <class T, class... Args>
25 auto not_supported_simd128_ctor(Args&&... args) -> decltype(
26     ex::fixed_size_simd<T, 16 / sizeof(T)>(std::forward<Args>(args)...),
27     void()) = delete;
28 
29 template <class T>
not_supported_simd128_ctor(...)30 void not_supported_simd128_ctor(...) {}
31 
32 template <class T, class... Args>
supported_simd128_ctor(Args &&...args)33 auto supported_simd128_ctor(Args&&... args) -> decltype(
34     ex::fixed_size_simd<T, 16 / sizeof(T)>(std::forward<Args>(args)...),
35     void()) {}
36 
37 template <class T>
38 void supported_simd128_ctor(...) = delete;
39 
40 struct identity {
41   template <size_t value>
operator ()identity42   int operator()(std::integral_constant<size_t, value>) const {
43     return value;
44   }
45 };
46 
compile_generator()47 void compile_generator() {
48   supported_simd128_ctor<int>(identity());
49   not_supported_simd128_ctor<int>([](int i) { return float(i); });
50   not_supported_simd128_ctor<int>([](intptr_t i) { return (int*)(i); });
51   not_supported_simd128_ctor<int>([](int* i) { return i; });
52 }
53 
54 struct limited_identity {
55   template <size_t value>
56   typename std::conditional<value <= 2, int32_t, int64_t>::type
operator ()limited_identity57   operator()(std::integral_constant<size_t, value>) const {
58     return value;
59   }
60 };
61 
compile_limited_identity()62 void compile_limited_identity() {
63   supported_simd128_ctor<int64_t>(limited_identity());
64   not_supported_simd128_ctor<int32_t>(limited_identity());
65 }
66 
67 template <typename SimdType>
test_generator()68 void test_generator() {
69   {
70     SimdType a([](int i) { return i; });
71     assert(a[0] == 0);
72     assert(a[1] == 1);
73     assert(a[2] == 2);
74     assert(a[3] == 3);
75   }
76   {
77     SimdType a([](int i) { return 2 * i - 1; });
78     assert(a[0] == -1);
79     assert(a[1] == 1);
80     assert(a[2] == 3);
81     assert(a[3] == 5);
82   }
83 }
84 
main(int,char **)85 int main(int, char**) {
86   // TODO: adjust the tests when this assertion fails.
87   assert(ex::native_simd<int32_t>::size() >= 4);
88   test_generator<ex::native_simd<int32_t>>();
89   test_generator<ex::fixed_size_simd<int32_t, 4>>();
90 
91   return 0;
92 }
93