xref: /llvm-project/libcxx/test/std/experimental/simd/simd.reference/reference_swap.pass.cpp (revision 6ec1ddfd72656cbf8e4185239175e52d50e0f4a3)
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 // Note: To ensure the swap functions can be called directly in the std::experimental namespace,
14 // the implementation approach might not fully align with the specification.
15 //
16 // [simd.reference]
17 // friend void swap(reference&& a, reference&& b) noexcept;
18 // friend void swap(value_type& a, reference&& b) noexcept;
19 // friend void swap(reference&& a, value_type& b) noexcept;
20 
21 #include "../test_utils.h"
22 #include <experimental/simd>
23 
24 namespace ex = std::experimental::parallelism_v2;
25 
26 template <class T, std::size_t>
27 struct CheckSimdRefSwap {
28   template <class SimdAbi>
operator ()CheckSimdRefSwap29   void operator()() {
30     ex::simd<T, SimdAbi> origin_simd_1(1);
31     ex::simd<T, SimdAbi> origin_simd_2(2);
32     T value = 3;
33 
34     static_assert(noexcept(ex::swap(std::move(origin_simd_1[0]), std::move(origin_simd_2[0]))));
35     ex::swap(std::move(origin_simd_1[0]), std::move(origin_simd_2[0]));
36     assert((origin_simd_1[0] == 2) && (origin_simd_2[0] == 1));
37 
38     static_assert(noexcept(ex::swap(std::move(origin_simd_1[0]), value)));
39     ex::swap(std::move(origin_simd_1[0]), value);
40     assert((origin_simd_1[0] == 3) && (value == 2));
41 
42     static_assert(noexcept(ex::swap(value, std::move(origin_simd_2[0]))));
43     ex::swap(value, std::move(origin_simd_2[0]));
44     assert((value == 1) && (origin_simd_2[0] == 2));
45   }
46 };
47 
48 template <class T, std::size_t>
49 struct CheckMaskRefSwap {
50   template <class SimdAbi>
operator ()CheckMaskRefSwap51   void operator()() {
52     ex::simd_mask<T, SimdAbi> origin_mask_1(true);
53     ex::simd_mask<T, SimdAbi> origin_mask_2(false);
54     bool value = true;
55 
56     static_assert(noexcept(ex::swap(std::move(origin_mask_1[0]), std::move(origin_mask_2[0]))));
57     ex::swap(std::move(origin_mask_1[0]), std::move(origin_mask_2[0]));
58     assert((origin_mask_1[0] == false) && (origin_mask_2[0] == true));
59 
60     static_assert(noexcept(ex::swap(std::move(origin_mask_1[0]), value)));
61     ex::swap(std::move(origin_mask_1[0]), value);
62     assert((origin_mask_1[0] == true) && (value == false));
63 
64     static_assert(noexcept(ex::swap(value, std::move(origin_mask_2[0]))));
65     ex::swap(value, std::move(origin_mask_2[0]));
66     assert((value == true) && (origin_mask_2[0] == false));
67   }
68 };
69 
main(int,char **)70 int main(int, char**) {
71   test_all_simd_abi<CheckSimdRefSwap>();
72   test_all_simd_abi<CheckMaskRefSwap>();
73   return 0;
74 }
75