1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11
12 // <experimental/simd>
13 //
14 // [simd.class]
15 // template <class U, class Flags> simd(const U* mem, Flags f);
16
17 #include <cstdint>
18 #include <experimental/simd>
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_native_simd_ctor(Args&&... args)
26 -> decltype(ex::native_simd<T>(std::forward<Args>(args)...),
27 void()) = delete;
28
29 template <class T>
not_supported_native_simd_ctor(...)30 void not_supported_native_simd_ctor(...) {}
31
32 template <class T, class... Args>
supported_native_simd_ctor(Args &&...args)33 auto supported_native_simd_ctor(Args&&... args)
34 -> decltype(ex::native_simd<T>(std::forward<Args>(args)...), void()) {}
35
36 template <class T>
37 void supported_native_simd_ctor(...) = delete;
38
compile_load_ctor()39 void compile_load_ctor() {
40 supported_native_simd_ctor<int>((int*)nullptr, ex::element_aligned_tag());
41 supported_native_simd_ctor<uint32_t>((int*)nullptr,
42 ex::element_aligned_tag());
43 supported_native_simd_ctor<double>((float*)nullptr,
44 ex::element_aligned_tag());
45 supported_native_simd_ctor<uint16_t>((unsigned int*)nullptr,
46 ex::element_aligned_tag());
47 supported_native_simd_ctor<uint32_t>((float*)nullptr,
48 ex::element_aligned_tag());
49
50 not_supported_native_simd_ctor<int>((int*)nullptr, int());
51 }
52
53 template <typename SimdType>
test_load_ctor()54 void test_load_ctor() {
55 alignas(32) int32_t buffer[] = {4, 3, 2, 1};
56 {
57 SimdType a(buffer, ex::element_aligned_tag());
58 assert(a[0] == 4);
59 assert(a[1] == 3);
60 assert(a[2] == 2);
61 assert(a[3] == 1);
62 }
63 {
64 SimdType a(buffer, ex::vector_aligned_tag());
65 assert(a[0] == 4);
66 assert(a[1] == 3);
67 assert(a[2] == 2);
68 assert(a[3] == 1);
69 }
70 {
71 SimdType a(buffer, ex::overaligned_tag<32>());
72 assert(a[0] == 4);
73 assert(a[1] == 3);
74 assert(a[2] == 2);
75 assert(a[3] == 1);
76 }
77
78 {
79 SimdType a(buffer, ex::element_aligned);
80 assert(a[0] == 4);
81 assert(a[1] == 3);
82 assert(a[2] == 2);
83 assert(a[3] == 1);
84 }
85 {
86 SimdType a(buffer, ex::vector_aligned);
87 assert(a[0] == 4);
88 assert(a[1] == 3);
89 assert(a[2] == 2);
90 assert(a[3] == 1);
91 }
92 {
93 SimdType a(buffer, ex::overaligned<32>);
94 assert(a[0] == 4);
95 assert(a[1] == 3);
96 assert(a[2] == 2);
97 assert(a[3] == 1);
98 }
99 }
100
101 template <typename SimdType>
test_converting_load_ctor()102 void test_converting_load_ctor() {
103 float buffer[] = {1., 2., 4., 8.};
104 SimdType a(buffer, ex::element_aligned_tag());
105 assert(a[0] == 1);
106 assert(a[1] == 2);
107 assert(a[2] == 4);
108 assert(a[3] == 8);
109 }
110
main()111 int main() {
112 // TODO: adjust the tests when this assertion fails.
113 assert(ex::native_simd<int32_t>::size() >= 4);
114 test_load_ctor<ex::native_simd<int32_t>>();
115 test_load_ctor<ex::fixed_size_simd<int32_t, 4>>();
116 test_converting_load_ctor<ex::native_simd<int32_t>>();
117 test_converting_load_ctor<ex::fixed_size_simd<int32_t, 4>>();
118 }
119