Line data Source code
1 : ///////////////////////////////////////////////////////////////////////////
2 : // Inastemp - Berenger Bramas MPCDF - 2016
3 : // Under MIT Licence, please you must read the LICENCE file.
4 : ///////////////////////////////////////////////////////////////////////////
5 :
6 : #include "InastempGlobal.h"
7 :
8 : #include "SSE42/InaVecSSE42Double.hpp"
9 : #include "SSE42/InaVecSSE42Float.hpp"
10 :
11 : #include "UTester.hpp"
12 :
13 : #include <cmath>
14 : #include <cstring>
15 :
16 : template < class VecType >
17 8 : class TestAll : public UTester< TestAll< VecType > > {
18 : using Parent = UTester< TestAll< VecType > >;
19 :
20 : using RealType = typename VecType::RealType;
21 :
22 2 : void TestBasic() {
23 : RealType inputVal[VecType::GetVecLength()];
24 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
25 6 : inputVal[idx] = RealType(idx);
26 : }
27 2 : const VecType inputVec(inputVal);
28 :
29 : {
30 6 : const VecType res0 = VecType::If(inputVec.isZeroMask()).Then([&]() {
31 : return RealType(1);
32 : });
33 :
34 : RealType outputVal[VecType::GetVecLength()];
35 2 : res0.storeInArray(outputVal);
36 :
37 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
38 6 : if (inputVal[idx] == 0) {
39 12 : UASSERTEEQUAL(outputVal[idx], RealType(1));
40 : } else {
41 24 : UASSERTEEQUAL(outputVal[idx], RealType(0));
42 : }
43 : }
44 : }
45 : {
46 8 : const VecType res0 = VecType::If(inputVec.isZeroMask())
47 : .Then([&]() {
48 : return RealType(-1);
49 : })
50 4 : .Else([&]() {
51 : return RealType(99);
52 : });
53 :
54 : RealType outputVal[VecType::GetVecLength()];
55 2 : res0.storeInArray(outputVal);
56 :
57 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
58 6 : if (inputVal[idx] == 0) {
59 12 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
60 : } else {
61 24 : UASSERTEEQUAL(outputVal[idx], RealType(99));
62 : }
63 : }
64 : }
65 : {
66 8 : const VecType res0 = VecType::If(inputVec.isZeroMask())
67 : .Then([&]() {
68 : return RealType(1);
69 : })
70 8 : .ElseIf(VecType::IsLowerOrEqualMask(1, inputVec))
71 2 : .Then([&]() {
72 : return RealType(2);
73 : })
74 4 : .Else([&]() {
75 : return RealType(3);
76 : });
77 :
78 : RealType outputVal[VecType::GetVecLength()];
79 2 : res0.storeInArray(outputVal);
80 :
81 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
82 6 : if (inputVal[idx] == 0) {
83 12 : UASSERTEEQUAL(outputVal[idx], RealType(1));
84 4 : } else if (1 <= inputVal[idx]) {
85 24 : UASSERTEEQUAL(outputVal[idx], RealType(2));
86 : } else {
87 0 : UASSERTEEQUAL(outputVal[idx], RealType(3));
88 : }
89 : }
90 : }
91 : {
92 8 : const VecType res0 = VecType::If(inputVec.isNotZeroMask())
93 : .Then([&]() {
94 : return RealType(1);
95 : })
96 8 : .ElseIf(VecType::IsGreaterOrEqualMask(1, inputVec))
97 2 : .Then([&]() {
98 : return RealType(2);
99 : })
100 4 : .Else([&]() {
101 : return RealType(3);
102 : });
103 :
104 : RealType outputVal[VecType::GetVecLength()];
105 2 : res0.storeInArray(outputVal);
106 :
107 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
108 6 : if (inputVal[idx] != 0) {
109 24 : UASSERTEEQUAL(outputVal[idx], RealType(1));
110 2 : } else if (1 >= inputVal[idx]) {
111 12 : UASSERTEEQUAL(outputVal[idx], RealType(2));
112 : } else {
113 0 : UASSERTEEQUAL(outputVal[idx], RealType(3));
114 : }
115 : }
116 : }
117 : {
118 10 : const VecType res0 = VecType::If(VecType::IsEqualMask(100,
119 : inputVec))
120 : .Then([&]() {
121 : return RealType(1);
122 : })
123 8 : .ElseIf(VecType::IsGreaterOrEqualMask(1, inputVec))
124 2 : .Then([&]() {
125 : return RealType(2);
126 : })
127 4 : .Else([&]() {
128 : return RealType(3);
129 : });
130 :
131 : RealType outputVal[VecType::GetVecLength()];
132 2 : res0.storeInArray(outputVal);
133 :
134 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
135 6 : if (inputVal[idx] == 100) {
136 0 : UASSERTEEQUAL(outputVal[idx], RealType(1));
137 6 : } else if (1 >= inputVal[idx]) {
138 24 : UASSERTEEQUAL(outputVal[idx], RealType(2));
139 : } else {
140 12 : UASSERTEEQUAL(outputVal[idx], RealType(3));
141 : }
142 : }
143 : }
144 :
145 : {
146 10 : const VecType res0 = VecType::IfElse(inputVec.isZeroMask(),
147 : -1,
148 : 99);
149 :
150 : RealType outputVal[VecType::GetVecLength()];
151 2 : res0.storeInArray(outputVal);
152 :
153 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
154 6 : if (inputVal[idx] == 0) {
155 12 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
156 : } else {
157 24 : UASSERTEEQUAL(outputVal[idx], RealType(99));
158 : }
159 : }
160 : }
161 : {
162 10 : const VecType res0 = VecType::IfElse(inputVec.isNotZeroMask(),
163 : -1,
164 : 99);
165 :
166 : RealType outputVal[VecType::GetVecLength()];
167 2 : res0.storeInArray(outputVal);
168 :
169 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
170 6 : if (inputVal[idx] != 0) {
171 24 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
172 : } else {
173 12 : UASSERTEEQUAL(outputVal[idx], RealType(99));
174 : }
175 : }
176 : }
177 :
178 : {
179 8 : const VecType res0 = VecType::IfTrue(inputVec.isZeroMask(),
180 : -1);
181 :
182 : RealType outputVal[VecType::GetVecLength()];
183 2 : res0.storeInArray(outputVal);
184 :
185 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
186 6 : if (inputVal[idx] == 0) {
187 12 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
188 : } else {
189 24 : UASSERTEEQUAL(outputVal[idx], RealType(0));
190 : }
191 : }
192 : }
193 : {
194 8 : const VecType res0 = VecType::IfTrue(inputVec.isNotZeroMask(),
195 : -1);
196 :
197 : RealType outputVal[VecType::GetVecLength()];
198 2 : res0.storeInArray(outputVal);
199 :
200 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
201 6 : if (inputVal[idx] != 0) {
202 24 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
203 : } else {
204 12 : UASSERTEEQUAL(outputVal[idx], RealType(0));
205 : }
206 : }
207 : }
208 2 : }
209 :
210 :
211 2 : void TestOperator() {
212 : RealType inputVal[VecType::GetVecLength()];
213 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
214 6 : inputVal[idx] = RealType(idx);
215 : }
216 2 : const VecType inputVec(inputVal);
217 :
218 : {
219 8 : const VecType res0 = VecType::If(inputVec == 0).Then([&]() {
220 : return RealType(1);
221 : });
222 :
223 : RealType outputVal[VecType::GetVecLength()];
224 2 : res0.storeInArray(outputVal);
225 :
226 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
227 6 : if (inputVal[idx] == 0) {
228 12 : UASSERTEEQUAL(outputVal[idx], RealType(1));
229 : } else {
230 24 : UASSERTEEQUAL(outputVal[idx], RealType(0));
231 : }
232 : }
233 : }
234 : {
235 10 : const VecType res0 = VecType::If(inputVec == 0)
236 : .Then([&]() {
237 : return RealType(-1);
238 : })
239 4 : .Else([&]() {
240 : return RealType(99);
241 : });
242 :
243 : RealType outputVal[VecType::GetVecLength()];
244 2 : res0.storeInArray(outputVal);
245 :
246 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
247 6 : if (inputVal[idx] == 0) {
248 12 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
249 : } else {
250 24 : UASSERTEEQUAL(outputVal[idx], RealType(99));
251 : }
252 : }
253 : }
254 : {
255 10 : const VecType res0 = VecType::If(inputVec == 0)
256 : .Then([&]() {
257 : return RealType(1);
258 : })
259 8 : .ElseIf(inputVec >= 1)
260 2 : .Then([&]() {
261 : return RealType(2);
262 : })
263 4 : .Else([&]() {
264 : return RealType(3);
265 : });
266 :
267 : RealType outputVal[VecType::GetVecLength()];
268 2 : res0.storeInArray(outputVal);
269 :
270 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
271 6 : if (inputVal[idx] == 0) {
272 12 : UASSERTEEQUAL(outputVal[idx], RealType(1));
273 4 : } else if (1 <= inputVal[idx]) {
274 24 : UASSERTEEQUAL(outputVal[idx], RealType(2));
275 : } else {
276 0 : UASSERTEEQUAL(outputVal[idx], RealType(3));
277 : }
278 : }
279 : }
280 : {
281 10 : const VecType res0 = VecType::If(inputVec != 0)
282 : .Then([&]() {
283 : return RealType(1);
284 : })
285 8 : .ElseIf(inputVec <= 1)
286 2 : .Then([&]() {
287 : return RealType(2);
288 : })
289 4 : .Else([&]() {
290 : return RealType(3);
291 : });
292 :
293 : RealType outputVal[VecType::GetVecLength()];
294 2 : res0.storeInArray(outputVal);
295 :
296 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
297 6 : if (inputVal[idx] != 0) {
298 24 : UASSERTEEQUAL(outputVal[idx], RealType(1));
299 2 : } else if (1 >= inputVal[idx]) {
300 12 : UASSERTEEQUAL(outputVal[idx], RealType(2));
301 : } else {
302 0 : UASSERTEEQUAL(outputVal[idx], RealType(3));
303 : }
304 : }
305 : }
306 : {
307 10 : const VecType res0 = VecType::If(inputVec == 100)
308 : .Then([&]() {
309 : return RealType(1);
310 : })
311 8 : .ElseIf(inputVec <= 1)
312 2 : .Then([&]() {
313 : return RealType(2);
314 : })
315 4 : .Else([&]() {
316 : return RealType(3);
317 : });
318 :
319 : RealType outputVal[VecType::GetVecLength()];
320 2 : res0.storeInArray(outputVal);
321 :
322 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
323 6 : if (inputVal[idx] == 100) {
324 0 : UASSERTEEQUAL(outputVal[idx], RealType(1));
325 6 : } else if (1 >= inputVal[idx]) {
326 24 : UASSERTEEQUAL(outputVal[idx], RealType(2));
327 : } else {
328 12 : UASSERTEEQUAL(outputVal[idx], RealType(3));
329 : }
330 : }
331 : }
332 :
333 : {
334 12 : const VecType res0 = VecType::IfElse(inputVec == 0, -1, 99);
335 :
336 : RealType outputVal[VecType::GetVecLength()];
337 2 : res0.storeInArray(outputVal);
338 :
339 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
340 6 : if (inputVal[idx] == 0) {
341 12 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
342 : } else {
343 24 : UASSERTEEQUAL(outputVal[idx], RealType(99));
344 : }
345 : }
346 : }
347 : {
348 12 : const VecType res0 = VecType::IfElse(inputVec != 0, -1, 99);
349 :
350 : RealType outputVal[VecType::GetVecLength()];
351 2 : res0.storeInArray(outputVal);
352 :
353 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
354 6 : if (inputVal[idx] != 0) {
355 24 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
356 : } else {
357 12 : UASSERTEEQUAL(outputVal[idx], RealType(99));
358 : }
359 : }
360 : }
361 :
362 : {
363 10 : const VecType res0 = VecType::IfTrue(inputVec == 0, -1);
364 :
365 : RealType outputVal[VecType::GetVecLength()];
366 2 : res0.storeInArray(outputVal);
367 :
368 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
369 6 : if (inputVal[idx] == 0) {
370 12 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
371 : } else {
372 24 : UASSERTEEQUAL(outputVal[idx], RealType(0));
373 : }
374 : }
375 : }
376 : {
377 10 : const VecType res0 = VecType::IfTrue(inputVec != 0, -1);
378 :
379 : RealType outputVal[VecType::GetVecLength()];
380 2 : res0.storeInArray(outputVal);
381 :
382 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
383 6 : if (inputVal[idx] != 0) {
384 24 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
385 : } else {
386 12 : UASSERTEEQUAL(outputVal[idx], RealType(0));
387 : }
388 : }
389 : }
390 2 : }
391 :
392 :
393 2 : void TestOperatorNoRet() {
394 : RealType inputVal[VecType::GetVecLength()];
395 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
396 6 : inputVal[idx] = RealType(idx);
397 : }
398 2 : const VecType inputVec(inputVal);
399 :
400 : {
401 10 : const VecType res0 = VecType::If(inputVec == 0).Then(1);
402 :
403 : RealType outputVal[VecType::GetVecLength()];
404 2 : res0.storeInArray(outputVal);
405 :
406 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
407 6 : if (inputVal[idx] == 0) {
408 12 : UASSERTEEQUAL(outputVal[idx], RealType(1));
409 : } else {
410 24 : UASSERTEEQUAL(outputVal[idx], RealType(0));
411 : }
412 : }
413 : }
414 : {
415 14 : const VecType res0 = VecType::If(inputVec == 0).Then(-1).Else(99);
416 :
417 : RealType outputVal[VecType::GetVecLength()];
418 2 : res0.storeInArray(outputVal);
419 :
420 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
421 6 : if (inputVal[idx] == 0) {
422 12 : UASSERTEEQUAL(outputVal[idx], RealType(-1));
423 : } else {
424 24 : UASSERTEEQUAL(outputVal[idx], RealType(99));
425 : }
426 : }
427 : }
428 : {
429 24 : const VecType res0 = VecType::If(inputVec == 0).Then(1).ElseIf(inputVec >= 1).Then(2).Else(3);
430 :
431 : RealType outputVal[VecType::GetVecLength()];
432 2 : res0.storeInArray(outputVal);
433 :
434 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
435 6 : if (inputVal[idx] == 0) {
436 12 : UASSERTEEQUAL(outputVal[idx], RealType(1));
437 4 : } else if (1 <= inputVal[idx]) {
438 24 : UASSERTEEQUAL(outputVal[idx], RealType(2));
439 : } else {
440 0 : UASSERTEEQUAL(outputVal[idx], RealType(3));
441 : }
442 : }
443 : }
444 : {
445 24 : const VecType res0 = VecType::If(inputVec != 0).Then(1).ElseIf(inputVec <= 1).Then(2).Else(3);
446 :
447 : RealType outputVal[VecType::GetVecLength()];
448 2 : res0.storeInArray(outputVal);
449 :
450 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
451 6 : if (inputVal[idx] != 0) {
452 24 : UASSERTEEQUAL(outputVal[idx], RealType(1));
453 2 : } else if (1 >= inputVal[idx]) {
454 12 : UASSERTEEQUAL(outputVal[idx], RealType(2));
455 : } else {
456 0 : UASSERTEEQUAL(outputVal[idx], RealType(3));
457 : }
458 : }
459 : }
460 : {
461 24 : const VecType res0 = VecType::If(inputVec == 100).Then(1).ElseIf(inputVec <= 1).Then(2).Else(3);
462 :
463 : RealType outputVal[VecType::GetVecLength()];
464 2 : res0.storeInArray(outputVal);
465 :
466 8 : for (size_t idx = 0; idx < size_t(VecType::GetVecLength()); ++idx) {
467 6 : if (inputVal[idx] == 100) {
468 0 : UASSERTEEQUAL(outputVal[idx], RealType(1));
469 6 : } else if (1 >= inputVal[idx]) {
470 24 : UASSERTEEQUAL(outputVal[idx], RealType(2));
471 : } else {
472 12 : UASSERTEEQUAL(outputVal[idx], RealType(3));
473 : }
474 : }
475 : }
476 : {
477 22 : UASSERTETRUE((inputVec == 100.) == (inputVec == 100.));
478 22 : UASSERTETRUE((inputVec != 100.) != (inputVec == 100.));
479 22 : UASSERTETRUE((inputVec != 100.) == (inputVec != 100.));
480 20 : UASSERTETRUE((VecType(0.) == VecType(1.)).isAllTrue() == false);
481 20 : UASSERTETRUE((VecType(0.) == VecType(1.)).isAllFalse() == true);
482 20 : UASSERTETRUE((VecType(0.) == VecType(0.)).isAllFalse() == false);
483 20 : UASSERTETRUE((VecType(0.) == VecType(0.)).isAllTrue() == true);
484 :
485 : RealType values10[VecType::GetVecLength()];
486 2 : values10[0] = 1;
487 6 : for (size_t idx = 1; idx < size_t(VecType::GetVecLength()); ++idx) {
488 4 : values10[idx] = 0;
489 : }
490 2 : VecType vecValues10(values10);
491 :
492 22 : UASSERTETRUE((vecValues10 == 100.) == (vecValues10 == 100.));
493 22 : UASSERTETRUE((vecValues10 != 100.) != (vecValues10 == 100.));
494 22 : UASSERTETRUE((vecValues10 != 100.) == (vecValues10 != 100.));
495 18 : UASSERTETRUE((vecValues10 == VecType(1.)).isAllTrue() == false || VecType::GetVecLength() == 1);
496 18 : UASSERTETRUE((vecValues10 == VecType(1.)).isAllFalse() == false);
497 18 : UASSERTETRUE((vecValues10 == VecType(0.)).isAllFalse() == false || VecType::GetVecLength() == 1);
498 18 : UASSERTETRUE((vecValues10 == VecType(0.)).isAllTrue() == false);
499 :
500 16 : UASSERTETRUE((vecValues10 == vecValues10).isAllTrue() == true);
501 16 : UASSERTETRUE((vecValues10 != vecValues10).isAllFalse() == true);
502 : }
503 2 : }
504 :
505 2 : void SetTests() {
506 6 : Parent::AddTest(&TestAll::TestBasic, "Basic ifelse tests for vec type");
507 6 : Parent::AddTest(&TestAll::TestOperator, "Basic ifelse tests for vec type but with overloaded operators");
508 6 : Parent::AddTest(&TestAll::TestOperatorNoRet, "Basic ifelse tests for vec type but with overloaded operators"
509 : "and no return in statement");
510 2 : }
511 : };
512 :
513 :
514 1 : int main() {
515 : // clang-format off
516 2 : TestAll< InaVecSSE42<double> > testerDouble;
517 2 : TestAll< InaVecSSE42<float> > testerSingle;
518 : // clang-format on
519 1 : return testerDouble.Run() + testerSingle.Run();
520 2 : }
|