cuda-kat
CUDA kernel author's tools
utility.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 #ifndef CUDA_KAT_UTILITY_HPP_
9 #define CUDA_KAT_UTILITY_HPP_
10 
11 #include <kat/common.hpp>
12 
13 #include <type_traits>
14 #include <utility> // Mainly so that KAT code can our header as a drop-in for <utility> itself
15 
17 #include <kat/detail/execution_space_specifiers.hpp>
19 #include <kat/detail/integer_sequence.hpp>
20 
21 
22 namespace kat {
23 
24 #ifdef KAT_DEFINE_MOVE_AND_FORWARD
25 template<typename T>
26 constexpr KAT_FHD typename std::remove_reference<T>::type&& move(T&& v) noexcept
27 {
28  return static_cast<typename std::remove_reference<T>::type&&>(v);
29 }
30 
31 template<typename T>
32 constexpr KAT_FHD T&& forward(typename std::remove_reference<T>::type& v) noexcept
33 {
34  return static_cast<T&&>(v);
35 }
36 
37 template<typename T>
38 constexpr KAT_FHD T&& forward(typename std::remove_reference<T>::type&& v) noexcept
39 {
40  return static_cast<T&&>(v);
41 }
42 #endif
43 
44 #if __cplusplus >= 201401L
45 template <typename T, typename U = T>
46 constexpr KAT_FHD auto exchange (T& x, U&& new_value) // TODO: A noexcept clause?
47 {
48 #ifndef KAT_DEFINE_MOVE_AND_FORWARD
49  using std::move;
50  using std::forward;
51 #endif
52  auto old_value = move(x);
53  x = forward<T>(new_value);
54  return old_value;
55 }
56 #endif // __cplusplus >= 201401L
57 
72 template <typename T>
73 KAT_FHD CONSTEXPR_SINCE_CPP_14 void swap( T& a, T& b )
74  noexcept(
75  std::is_nothrow_move_constructible<T>::value &&
76  std::is_nothrow_move_assignable<T>::value
77  )
78 {
79 #ifndef KAT_DEFINE_MOVE_AND_FORWARD
80  using std::move;
81 #endif
82  T tmp ( move(a) );
83  a = move(b);
84  b = move(tmp);
85 }
86 
87 namespace detail {
88 
89 template<class T>
91 {
92  T& v_;
93 
94  KAT_FHD addr_impl_ref( T& v ): v_( v ) {}
95  KAT_FHD operator T& () const { return v_; }
96 
97 private:
98  KAT_FHD addr_impl_ref & operator=(const addr_impl_ref &);
99 };
100 
101 template<class T>
103 {
104  static KAT_FHD T* f( T& v, long ) {
105  return reinterpret_cast<T*>(
106  &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
107  }
108 
109  static KAT_FHD T* f( T* v, int ) { return v; }
110 };
111 
112 } // namespace detail
113 
121 template<class T>
122 KAT_FHD T* addressof( T& v ) {
123  // Note the complex implementation details are due to some objects
124  // overloading their & operator
126 }
127 
129 template <class T>
130 const KAT_FHD T* addressof(const T&&) = delete;
131 
132 } // namespace kat
133 
134 #endif // CUDA_KAT_UTILITY_HPP_
KAT_FHD T * addressof(T &v)
Obtains the actual address of the object or function arg, even in presence of overloaded operator&() ...
Definition: utility.hpp:122
Definition: common.hpp:16
Definition: utility.hpp:90
Basic type and macro definitions used throughout the KAT library.
Definition: utility.hpp:102