/* 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 "mpt/base/saturate_cast.hpp" #include namespace mpt { inline namespace MPT_INLINE_NS { // 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 T saturate_round(float val) { static_assert(std::numeric_limits::is_integer); return mpt::saturate_cast(mpt::round(val)); } template inline T saturate_round(double val) { static_assert(std::numeric_limits::is_integer); return mpt::saturate_cast(mpt::round(val)); } template inline T saturate_round(long double val) { static_assert(std::numeric_limits::is_integer); return mpt::saturate_cast(mpt::round(val)); } } // namespace MPT_INLINE_NS } // namespace mpt #endif // MPT_BASE_SATURATE_ROUND_HPP