Program Listing for File SaturateCast.hpp
↰ Return to documentation for file (nvcv_types/include/nvcv/cuda/SaturateCast.hpp
)
/*
* SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NVCV_CUDA_SATURATE_CAST_HPP
#define NVCV_CUDA_SATURATE_CAST_HPP
#include "TypeTraits.hpp" // for Require, etc.
#include "detail/SaturateCastImpl.hpp" // for SaturateCastImpl, etc.
namespace nvcv::cuda {
template<typename T, typename U,
class = Require<(NumComponents<T> == NumComponents<U>) || (NumComponents<T> == 0 && HasTypeTraits<U>)>>
__host__ __device__ auto SaturateCast(U u)
{
using BU = BaseType<U>;
using BT = BaseType<T>;
using RT = ConvertBaseTypeTo<BT, U>;
if constexpr (std::is_same_v<U, RT>)
{
return u;
}
else
{
RT out{};
#pragma unroll
for (int e = 0; e < NumElements<RT>; ++e)
{
GetElement(out, e) = detail::SaturateCastImpl<BT, BU>(GetElement(u, e));
}
return out;
}
}
} // namespace nvcv::cuda
#endif // NVCV_CUDA_SATURATE_CAST_HPP