alpaka
Abstraction Library for Parallel Kernel Acceleration
ClipCast.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Benjamin Worpitz, Jan Stephan, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
8 
9 #include <algorithm>
10 #include <limits>
11 
12 namespace alpaka::core
13 {
14  //! \return The input casted and clipped to T.
15  template<typename T, typename V>
16  auto clipCast(V const& val) -> T
17  {
18  static_assert(
19  std::is_integral_v<T> && std::is_integral_v<V>,
20  "clipCast can not be called with non-integral types!");
21 
22  constexpr auto max = static_cast<V>(std::numeric_limits<alpaka::meta::LowerMax<T, V>>::max());
23  constexpr auto min = static_cast<V>(std::numeric_limits<alpaka::meta::HigherMin<T, V>>::min());
24 
25  return static_cast<T>(std::max(min, std::min(max, val)));
26  }
27 } // namespace alpaka::core
auto clipCast(V const &val) -> T
Definition: ClipCast.hpp:16
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto max(T const &max_ctx, Tx const &x, Ty const &y)
Returns the larger of two arguments. NaNs are treated as missing data (between a NaN and a numeric va...
Definition: Traits.hpp:1263
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto min(T const &min_ctx, Tx const &x, Ty const &y)
Returns the smaller of two arguments. NaNs are treated as missing data (between a NaN and a numeric v...
Definition: Traits.hpp:1280