Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Clamp.hpp
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 
17 #ifndef KOKKOS_CLAMP_HPP
18 #define KOKKOS_CLAMP_HPP
19 
20 #include <Kokkos_Macros.hpp>
21 
22 namespace Kokkos {
23 
24 template <class T>
25 constexpr KOKKOS_INLINE_FUNCTION const T& clamp(const T& value, const T& lo,
26  const T& hi) {
27  KOKKOS_EXPECTS(!(hi < lo));
28  return (value < lo) ? lo : (hi < value) ? hi : value;
29 }
30 
31 template <class T, class ComparatorType>
32 constexpr KOKKOS_INLINE_FUNCTION const T& clamp(const T& value, const T& lo,
33  const T& hi,
34  ComparatorType comp) {
35  KOKKOS_EXPECTS(!comp(hi, lo));
36  return comp(value, lo) ? lo : comp(hi, value) ? hi : value;
37 }
38 
39 } // namespace Kokkos
40 
41 #endif