From: <sv...@op...> - 2024-03-15 10:19:59
|
Author: manx Date: Fri Mar 15 11:19:48 2024 New Revision: 20330 URL: https://source.openmpt.org/browse/openmpt/?op=revision&rev=20330 Log: [Ref] mpt/base/numeric.hpp: Add mpt::saturate_align_up. Modified: trunk/OpenMPT/src/mpt/base/numeric.hpp Modified: trunk/OpenMPT/src/mpt/base/numeric.hpp ============================================================================== --- trunk/OpenMPT/src/mpt/base/numeric.hpp Fri Mar 15 08:50:11 2024 (r20329) +++ trunk/OpenMPT/src/mpt/base/numeric.hpp Fri Mar 15 11:19:48 2024 (r20330) @@ -80,6 +80,16 @@ return (x / target) * target; } +// rounds x up to multiples of target or saturation of T +template <typename T> +constexpr T saturate_align_up(T x, T target) { + if (x > (std::numeric_limits<T>::max() - (target - 1))) { + return std::numeric_limits<T>::max(); + } + T result = ((x + (target - 1)) / target) * target; + return result; +} + // Returns sign of a number (-1 for negative numbers, 1 for positive numbers, 0 for 0) template <class T> constexpr int signum(T value) { |