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 // scalar access [simd.subscr]
14 // reference operator[](size_t);
15 // value_type operator[](size_t) const;
16
17 #include <experimental/simd>
18 #include <cassert>
19 #include <cstdint>
20
21 #include "test_macros.h"
22
23 namespace ex = std::experimental::parallelism_v2;
24
25 template <typename SimdType>
test_access()26 void test_access() {
27 {
28 SimdType a(42), b(4);
29 static_assert(std::is_convertible<decltype(a[0]), int8_t>::value, "");
30
31 assert(a[0] == 42);
32 assert(!a[0] == !42);
33 assert(~a[0] == ~42);
34 assert(+a[0] == +42);
35 assert(-a[0] == -42);
36 assert(a[0] + b[0] == 42 + 4);
37 assert(a[0] - b[0] == 42 - 4);
38 assert(a[0] * b[0] == 42 * 4);
39 assert(a[0] / b[0] == 42 / 4);
40 assert(a[0] % b[0] == 42 % 4);
41 assert(a[0] << b[0] == (42 << 4));
42 assert(a[0] >> b[0] == (42 >> 4));
43 assert((a[0] < b[0]) == false);
44 assert((a[0] <= b[0]) == false);
45 assert((a[0] > b[0]) == true);
46 assert((a[0] >= b[0]) == true);
47 assert((a[0] == b[0]) == false);
48 assert((a[0] != b[0]) == true);
49 assert((a[0] & b[0]) == (42 & 4));
50 assert((a[0] | b[0]) == (42 | 4));
51 assert((a[0] ^ b[0]) == (42 ^ 4));
52 assert((a[0] && b[0]) == true);
53 assert((a[0] || b[0]) == true);
54
55 {
56 auto c = a;
57 ++c[0];
58 assert(c[0] == 42 + 1);
59 assert(c[1] == 42);
60 }
61 {
62 auto c = a;
63 auto ret = c[0]++;
64 assert(ret == 42);
65 assert(c[0] == 42 + 1);
66 assert(c[1] == 42);
67 }
68 {
69 auto c = a;
70 --c[0];
71 assert(c[0] == 42 - 1);
72 assert(c[1] == 42);
73 }
74 {
75 auto c = a;
76 auto ret = c[0]--;
77 assert(ret == 42);
78 assert(c[0] == 42 - 1);
79 assert(c[1] == 42);
80 }
81
82 {
83 auto c = a;
84 c[0] += b[0];
85 assert(c[0] == 42 + 4);
86 assert(c[1] == 42);
87 }
88 {
89 auto c = a;
90 c[0] -= b[0];
91 assert(c[0] == 42 - 4);
92 assert(c[1] == 42);
93 }
94 {
95 auto c = a;
96 c[0] *= b[0];
97 assert(c[0] == 42 * 4);
98 assert(c[1] == 42);
99 }
100 {
101 auto c = a;
102 c[0] /= b[0];
103 assert(c[0] == 42 / 4);
104 assert(c[1] == 42);
105 }
106 {
107 auto c = a;
108 c[0] %= b[0];
109 assert(c[0] == 42 % 4);
110 assert(c[1] == 42);
111 }
112 {
113 auto c = a;
114 c[0] >>= b[0];
115 assert(c[0] == (42 >> 4));
116 assert(c[1] == 42);
117 }
118 {
119 auto c = a;
120 c[0] <<= b[0];
121 assert(c[0] == (42 << 4));
122 assert(c[1] == 42);
123 }
124 {
125 auto c = a;
126 c[0] &= b[0];
127 assert(c[0] == (42 & 4));
128 assert(c[1] == 42);
129 }
130 {
131 auto c = a;
132 c[0] |= b[0];
133 assert(c[0] == (42 | 4));
134 assert(c[1] == 42);
135 }
136 {
137 auto c = a;
138 c[0] ^= b[0];
139 assert(c[0] == (42 ^ 4));
140 assert(c[1] == 42);
141 }
142
143 {
144 auto c = a;
145 (void)(a[0] + (c[0] += a[0]));
146 }
147 {
148 auto c = a;
149 (void)(a[0] + (c[0] -= a[0]));
150 }
151 {
152 auto c = a;
153 (void)(a[0] + (c[0] *= a[0]));
154 }
155 {
156 auto c = a;
157 (void)(a[0] + (c[0] /= a[0]));
158 }
159 {
160 auto c = a;
161 (void)(a[0] + (c[0] %= a[0]));
162 }
163 {
164 auto c = a;
165 (void)(a[0] + (c[0] >>= b[0]));
166 }
167 {
168 auto c = a;
169 (void)(a[0] + (c[0] <<= b[0]));
170 }
171 {
172 auto c = a;
173 (void)(a[0] + (c[0] &= a[0]));
174 }
175 {
176 auto c = a;
177 (void)(a[0] + (c[0] |= a[0]));
178 }
179 {
180 auto c = a;
181 (void)(a[0] + (c[0] ^= a[0]));
182 }
183 }
184 {
185 const SimdType a(42);
186 const SimdType b(4);
187 static_assert(std::is_same<decltype(a[0]), int>::value, "");
188
189 assert(a[0] == 42);
190 assert(!a[0] == !42);
191 assert(~a[0] == ~42);
192 assert(+a[0] == +42);
193 assert(-a[0] == -42);
194 assert(a[0] + b[0] == 42 + 4);
195 assert(a[0] - b[0] == 42 - 4);
196 assert(a[0] * b[0] == 42 * 4);
197 assert(a[0] / b[0] == 42 / 4);
198 assert(a[0] % b[0] == 42 % 4);
199 assert(a[0] << b[0] == (42 << 4));
200 assert(a[0] >> b[0] == (42 >> 4));
201 assert((a[0] < b[0]) == false);
202 assert((a[0] <= b[0]) == false);
203 assert((a[0] > b[0]) == true);
204 assert((a[0] >= b[0]) == true);
205 assert((a[0] == b[0]) == false);
206 assert((a[0] != b[0]) == true);
207 assert((a[0] & b[0]) == (42 & 4));
208 assert((a[0] | b[0]) == (42 | 4));
209 assert((a[0] ^ b[0]) == (42 ^ 4));
210 assert((a[0] && b[0]) == true);
211 assert((a[0] || b[0]) == true);
212 }
213 }
214
main(int,char **)215 int main(int, char**) {
216 test_access<ex::native_simd<int>>();
217 test_access<ex::fixed_size_simd<int, 4>>();
218
219 return 0;
220 }
221