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