/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ #ifndef MPT_BASE_SATURATE_ROUND_HPP #define MPT_BASE_SATURATE_ROUND_HPP #include "mpt/base/namespace.hpp" #include "mpt/base/math.hpp" #include #include #include namespace mpt { inline namespace MPT_INLINE_NS { template constexpr Tdst saturate_trunc(Tsrc src) { static_assert(std::is_floating_point::value); if (src >= static_cast(std::numeric_limits::max())) { return std::numeric_limits::max(); } if (src <= static_cast(std::numeric_limits::min())) { return std::numeric_limits::min(); } return static_cast(src); } // Rounds given double value to nearest integer value of type T. // Out-of-range values are saturated to the specified integer type's limits. template inline Tdst saturate_round(Tsrc val) { static_assert(std::is_floating_point::value); static_assert(std::numeric_limits::is_integer); return mpt::saturate_trunc(mpt::round(val)); } template inline Tdst saturate_ceil(Tsrc val) { static_assert(std::is_floating_point::value); static_assert(std::numeric_limits::is_integer); return mpt::saturate_trunc(std::ceil(val)); } template inline Tdst saturate_floor(Tsrc val) { static_assert(std::is_floating_point::value); static_assert(std::numeric_limits::is_integer); return mpt::saturate_trunc(std::floor(val)); } } // namespace MPT_INLINE_NS } // namespace mpt #endif // MPT_BASE_SATURATE_ROUND_HPP