| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_BASE_NUMERIC_HPP |
| 4 |
#define MPT_BASE_NUMERIC_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect_compiler.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
|
| 11 |
#include "mpt/base/bit.hpp" |
| 12 |
#include "mpt/base/saturate_cast.hpp" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <limits> |
| 16 |
#include <type_traits> |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace mpt { |
| 21 |
inline namespace MPT_INLINE_NS { |
| 22 |
|
| 23 |
|
| 24 |
template <typename Tmod, Tmod m> |
| 25 |
struct ModIfNotZeroImpl { |
| 26 |
template <typename Tval> |
| 27 |
constexpr Tval mod(Tval x) { |
| 28 |
static_assert(std::numeric_limits<Tmod>::is_integer); |
| 29 |
static_assert(!std::numeric_limits<Tmod>::is_signed); |
| 30 |
static_assert(std::numeric_limits<Tval>::is_integer); |
| 31 |
static_assert(!std::numeric_limits<Tval>::is_signed); |
| 32 |
return static_cast<Tval>(x % m); |
| 33 |
} |
| 34 |
}; |
| 35 |
template <> |
| 36 |
struct ModIfNotZeroImpl<uint8, 0> { |
| 37 |
template <typename Tval> |
| 38 |
constexpr Tval mod(Tval x) { |
| 39 |
return x; |
| 40 |
} |
| 41 |
}; |
| 42 |
template <> |
| 43 |
struct ModIfNotZeroImpl<uint16, 0> { |
| 44 |
template <typename Tval> |
| 45 |
constexpr Tval mod(Tval x) { |
| 46 |
return x; |
| 47 |
} |
| 48 |
}; |
| 49 |
template <> |
| 50 |
struct ModIfNotZeroImpl<uint32, 0> { |
| 51 |
template <typename Tval> |
| 52 |
constexpr Tval mod(Tval x) { |
| 53 |
return x; |
| 54 |
} |
| 55 |
}; |
| 56 |
template <> |
| 57 |
struct ModIfNotZeroImpl<uint64, 0> { |
| 58 |
template <typename Tval> |
| 59 |
constexpr Tval mod(Tval x) { |
| 60 |
return x; |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
// Returns x % m if m != 0, x otherwise. |
| 65 |
// i.e. "return (m == 0) ? x : (x % m);", but without causing a warning with stupid older compilers |
| 66 |
template <typename Tmod, Tmod m, typename Tval> |
| 67 |
constexpr Tval modulo_if_not_zero(Tval x) { |
| 68 |
return ModIfNotZeroImpl<Tmod, m>().mod(x); |
| 69 |
} |
| 70 |
|
| 71 |
// rounds x up to multiples of target |
| 72 |
template <typename T> |
| 73 |
constexpr T align_up(T x, T target) { |
| 74 |
return ((x + (target - 1)) / target) * target; |
| 75 |
} |
| 76 |
|
| 77 |
// rounds x down to multiples of target |
| 78 |
template <typename T> |
| 79 |
constexpr T align_down(T x, T target) { |
| 80 |
return (x / target) * target; |
| 81 |
} |
| 82 |
|
| 83 |
// rounds x up to multiples of target or saturation of T |
| 84 |
template <typename T> |
| 85 |
constexpr T saturate_align_up(T x, T target) { |
| 86 |
if (x > (std::numeric_limits<T>::max() - (target - 1))) { |
| 87 |
return std::numeric_limits<T>::max(); |
| 88 |
} |
| 89 |
return ((x + (target - 1)) / target) * target; |
| 90 |
} |
| 91 |
|
| 92 |
// Returns sign of a number (-1 for negative numbers, 1 for positive numbers, 0 for 0) |
| 93 |
template <class T> |
| 94 |
constexpr int signum(T value) { |
| 95 |
return (value > T(0)) - (value < T(0)); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
} // namespace MPT_INLINE_NS |
| 100 |
} // namespace mpt |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#endif // MPT_BASE_ALGORITHM_HPP |