Skip to content

Commit

Permalink
[Ref] mpt/base/numeric.hpp: Add mpt::saturate_align_up.
Browse files Browse the repository at this point in the history
git-svn-id: https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@20330 56274372-70c3-4bfc-bfc3-4c3a0b034d27
  • Loading branch information
manxorist committed Mar 15, 2024
1 parent 1241a54 commit 177eb06
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/mpt/base/numeric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ constexpr T align_down(T x, T target) {
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) {
Expand Down

0 comments on commit 177eb06

Please sign in to comment.