forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_Swap.hpp
76 lines (64 loc) · 2.15 KB
/
Kokkos_Swap.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#ifndef KOKKOS_SWAP_HPP
#define KOKKOS_SWAP_HPP
#include <Kokkos_Macros.hpp>
#include <cstddef>
#include <type_traits>
#include <utility>
namespace Kokkos {
template <class T>
KOKKOS_FUNCTION constexpr std::enable_if_t<std::is_move_constructible_v<T> &&
std::is_move_assignable_v<T>>
kokkos_swap(T& a, T& b) noexcept(std::is_nothrow_move_constructible_v<T>&&
std::is_nothrow_move_assignable_v<T>) {
T t(std::move(a));
a = std::move(b);
b = std::move(t);
}
namespace Impl {
template <class T>
struct is_swappable {
template <class U>
static decltype(kokkos_swap(std::declval<T&>(), std::declval<T&>()))
test_swap(int);
struct Nope;
template <class U>
static Nope test_swap(long);
static constexpr bool value =
!std::is_same_v<decltype(test_swap<T>(0)), Nope>;
};
#if defined(KOKKOS_COMPILER_NVCC) && (KOKKOS_COMPILER_NVCC < 1140)
template <class T>
inline constexpr bool is_nothrow_swappable_v =
is_swappable<T>::value&& noexcept(
kokkos_swap(std::declval<std::remove_const_t<T>&>(),
std::declval<std::remove_const_t<T>&>()));
#else
template <class T>
inline constexpr bool is_nothrow_swappable_v =
noexcept(kokkos_swap(std::declval<T&>(), std::declval<T&>()));
#endif
} // namespace Impl
template <class T, std::size_t N>
KOKKOS_FUNCTION constexpr std::enable_if_t<Impl::is_swappable<T>::value>
kokkos_swap(T (&a)[N], T (&b)[N]) noexcept(Impl::is_nothrow_swappable_v<T>) {
for (std::size_t i = 0; i < N; ++i) {
kokkos_swap(a[i], b[i]);
}
}
} // namespace Kokkos
#endif