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 // // stores [simd.store]
14 // template <class U, class Flags> void copy_to(U* mem, Flags f) const;
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 <typename SimdType>
test_store()25 void test_store() {
26   SimdType a([](int i) { return 4 - i; });
27   {
28     alignas(32) int32_t buffer[4] = {0};
29     a.copy_to(buffer, ex::element_aligned_tag());
30     assert(buffer[0] == 4);
31     assert(buffer[1] == 3);
32     assert(buffer[2] == 2);
33     assert(buffer[3] == 1);
34   }
35   {
36     alignas(32) int32_t buffer[4] = {0};
37     a.copy_to(buffer, ex::vector_aligned_tag());
38     assert(buffer[0] == 4);
39     assert(buffer[1] == 3);
40     assert(buffer[2] == 2);
41     assert(buffer[3] == 1);
42   }
43   {
44     alignas(32) int32_t buffer[4] = {0};
45     a.copy_to(buffer, ex::overaligned_tag<32>());
46     assert(buffer[0] == 4);
47     assert(buffer[1] == 3);
48     assert(buffer[2] == 2);
49     assert(buffer[3] == 1);
50   }
51 
52   {
53     alignas(32) int32_t buffer[4] = {0};
54     a.copy_to(buffer, ex::element_aligned);
55     assert(buffer[0] == 4);
56     assert(buffer[1] == 3);
57     assert(buffer[2] == 2);
58     assert(buffer[3] == 1);
59   }
60   {
61     alignas(32) int32_t buffer[4] = {0};
62     a.copy_to(buffer, ex::vector_aligned);
63     assert(buffer[0] == 4);
64     assert(buffer[1] == 3);
65     assert(buffer[2] == 2);
66     assert(buffer[3] == 1);
67   }
68   {
69     alignas(32) int32_t buffer[4] = {0};
70     a.copy_to(buffer, ex::overaligned<32>);
71     assert(buffer[0] == 4);
72     assert(buffer[1] == 3);
73     assert(buffer[2] == 2);
74     assert(buffer[3] == 1);
75   }
76 }
77 
78 template <typename SimdType>
test_converting_store()79 void test_converting_store() {
80   float buffer[4] = {0.};
81   SimdType a([](int i) { return 1 << i; });
82   a.copy_to(buffer, ex::element_aligned_tag());
83   assert(buffer[0] == 1.);
84   assert(buffer[1] == 2.);
85   assert(buffer[2] == 4.);
86   assert(buffer[3] == 8.);
87 }
88 
main(int,char **)89 int main(int, char**) {
90   // TODO: adjust the tests when this assertion fails.
91   test_store<ex::native_simd<int32_t>>();
92   test_store<ex::fixed_size_simd<int32_t, 4>>();
93   test_converting_store<ex::native_simd<int32_t>>();
94   test_converting_store<ex::fixed_size_simd<int32_t, 4>>();
95 
96   return 0;
97 }
98