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 // loads [simd.load]
15 // template <class U, class Flags> void copy_from(const U* mem, Flags f);
16
17 #include <experimental/simd>
18 #include <cstdint>
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_load(Args&&... args) -> decltype(
26 std::declval<ex::native_simd<T>>().copy_from(std::forward<Args>(args)...),
27 void()) = delete;
28
29 template <class T>
not_supported_load(...)30 void not_supported_load(...) {}
31
32 template <class T, class... Args>
supported_load(Args &&...args)33 auto supported_load(Args&&... args) -> decltype(
34 std::declval<ex::native_simd<T>>().copy_from(std::forward<Args>(args)...),
35 void()) {}
36
37 template <class T>
38 void supported_load(...) = delete;
39
compile_load()40 void compile_load() {
41 supported_load<int>((int*)nullptr, ex::element_aligned_tag());
42 supported_load<uint32_t>((int*)nullptr, ex::element_aligned_tag());
43 supported_load<double>((float*)nullptr, ex::element_aligned_tag());
44 supported_load<uint16_t>((unsigned int*)nullptr, ex::element_aligned_tag());
45 supported_load<uint32_t>((float*)nullptr, ex::element_aligned_tag());
46
47 not_supported_load<int>((int*)nullptr, int());
48 }
49
50 template <typename SimdType>
test_load()51 void test_load() {
52 alignas(32) int32_t buffer[] = {4, 3, 2, 1};
53 {
54 SimdType a;
55 a.copy_from(buffer, ex::element_aligned_tag());
56 assert(a[0] == 4);
57 assert(a[1] == 3);
58 assert(a[2] == 2);
59 assert(a[3] == 1);
60 }
61 {
62 SimdType a;
63 a.copy_from(buffer, ex::vector_aligned_tag());
64 assert(a[0] == 4);
65 assert(a[1] == 3);
66 assert(a[2] == 2);
67 assert(a[3] == 1);
68 }
69 {
70 SimdType a;
71 a.copy_from(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;
80 a.copy_from(buffer, ex::element_aligned);
81 assert(a[0] == 4);
82 assert(a[1] == 3);
83 assert(a[2] == 2);
84 assert(a[3] == 1);
85 }
86 {
87 SimdType a;
88 a.copy_from(buffer, ex::vector_aligned);
89 assert(a[0] == 4);
90 assert(a[1] == 3);
91 assert(a[2] == 2);
92 assert(a[3] == 1);
93 }
94 {
95 SimdType a;
96 a.copy_from(buffer, ex::overaligned<32>);
97 assert(a[0] == 4);
98 assert(a[1] == 3);
99 assert(a[2] == 2);
100 assert(a[3] == 1);
101 }
102 }
103
104 template <typename SimdType>
test_converting_load()105 void test_converting_load() {
106 float buffer[] = {1., 2., 4., 8.};
107 SimdType a;
108 a.copy_from(buffer, ex::element_aligned_tag());
109 assert(a[0] == 1);
110 assert(a[1] == 2);
111 assert(a[2] == 4);
112 assert(a[3] == 8);
113 }
114
main()115 int main() {
116 // TODO: adjust the tests when this assertion fails.
117 assert(ex::native_simd<int32_t>::size() >= 4);
118 test_load<ex::native_simd<int32_t>>();
119 test_load<ex::fixed_size_simd<int32_t, 4>>();
120 test_converting_load<ex::native_simd<int32_t>>();
121 test_converting_load<ex::fixed_size_simd<int32_t, 4>>();
122 }
123