1010#include " mpt/base/math.hpp"
1111
1212#include < limits>
13+ #include < type_traits>
14+
15+ #include < cmath>
1316
1417
1518
@@ -18,23 +21,13 @@ inline namespace MPT_INLINE_NS {
1821
1922
2023
21- template <typename Tdst>
22- constexpr Tdst saturate_trunc (double src) {
23- if (src >= static_cast <double >(std::numeric_limits<Tdst>::max ())) {
24- return std::numeric_limits<Tdst>::max ();
25- }
26- if (src <= static_cast <double >(std::numeric_limits<Tdst>::min ())) {
27- return std::numeric_limits<Tdst>::min ();
28- }
29- return static_cast <Tdst>(src);
30- }
31-
32- template <typename Tdst>
33- constexpr Tdst saturate_trunc (float src) {
34- if (src >= static_cast <float >(std::numeric_limits<Tdst>::max ())) {
24+ template <typename Tdst, typename Tsrc>
25+ constexpr Tdst saturate_trunc (Tsrc src) {
26+ static_assert (std::is_floating_point<Tsrc>::value);
27+ if (src >= static_cast <Tsrc>(std::numeric_limits<Tdst>::max ())) {
3528 return std::numeric_limits<Tdst>::max ();
3629 }
37- if (src <= static_cast <float >(std::numeric_limits<Tdst>::min ())) {
30+ if (src <= static_cast <Tsrc >(std::numeric_limits<Tdst>::min ())) {
3831 return std::numeric_limits<Tdst>::min ();
3932 }
4033 return static_cast <Tdst>(src);
@@ -44,22 +37,27 @@ constexpr Tdst saturate_trunc(float src) {
4437// Rounds given double value to nearest integer value of type T.
4538// Out-of-range values are saturated to the specified integer type's limits.
4639
47- template <typename T>
48- inline T saturate_round (float val) {
49- static_assert (std::numeric_limits<T>::is_integer);
50- return mpt::saturate_trunc<T>(mpt::round (val));
40+ template <typename Tdst, typename Tsrc>
41+ inline Tdst saturate_round (Tsrc val) {
42+ static_assert (std::is_floating_point<Tsrc>::value);
43+ static_assert (std::numeric_limits<Tdst>::is_integer);
44+ return mpt::saturate_trunc<Tdst>(mpt::round (val));
5145}
5246
53- template <typename T>
54- inline T saturate_round (double val) {
55- static_assert (std::numeric_limits<T>::is_integer);
56- return mpt::saturate_trunc<T>(mpt::round (val));
47+
48+ template <typename Tdst, typename Tsrc>
49+ inline Tdst saturate_ceil (Tsrc val) {
50+ static_assert (std::is_floating_point<Tsrc>::value);
51+ static_assert (std::numeric_limits<Tdst>::is_integer);
52+ return mpt::saturate_trunc<Tdst>(std::ceil (val));
5753}
5854
59- template <typename T>
60- inline T saturate_round (long double val) {
61- static_assert (std::numeric_limits<T>::is_integer);
62- return mpt::saturate_trunc<T>(mpt::round (val));
55+
56+ template <typename Tdst, typename Tsrc>
57+ inline Tdst saturate_floor (Tsrc val) {
58+ static_assert (std::is_floating_point<Tsrc>::value);
59+ static_assert (std::numeric_limits<Tdst>::is_integer);
60+ return mpt::saturate_trunc<Tdst>(std::floor (val));
6361}
6462
6563
0 commit comments