Kokkos Core Kernels Package Version of the Day
Loading...
Searching...
No Matches
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
22namespace Kokkos {
23
24template <class T>
25constexpr KOKKOS_INLINE_FUNCTION const T& clamp(const T& value, const T& lo,
26 const T& hi) {
27 KOKKOS_EXPECTS(!(hi < lo));
28 // Capturing the result of std::clamp by reference produces a dangling
29 // reference if one of the parameters is a temporary and that parameter is
30 // returned.
31 // NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter)
32 return (value < lo) ? lo : (hi < value) ? hi : value;
33}
34
35template <class T, class ComparatorType>
36constexpr KOKKOS_INLINE_FUNCTION const T& clamp(const T& value, const T& lo,
37 const T& hi,
38 ComparatorType comp) {
39 KOKKOS_EXPECTS(!comp(hi, lo));
40 // Capturing the result of std::clamp by reference produces a dangling
41 // reference if one of the parameters is a temporary and that parameter is
42 // returned.
43 // NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter)
44 return comp(value, lo) ? lo : comp(hi, value) ? hi : value;
45}
46
47} // namespace Kokkos
48
49#endif