File indexing completed on 2025-12-15 10:24:05
0001
0002
0003 #ifndef Q26NUMERIC_H
0004 #define Q26NUMERIC_H
0005
0006 #include <QtCore/qglobal.h>
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 #include <numeric>
0025 #include <limits>
0026 #include <type_traits>
0027
0028 QT_BEGIN_NAMESPACE
0029
0030 namespace q26 {
0031
0032
0033 #ifdef __cpp_lib_saturation_arithmetic
0034 using std::saturate_cast;
0035 #else
0036 template <typename To, typename From>
0037 constexpr auto saturate_cast(From x)
0038 {
0039 static_assert(std::is_integral_v<To>);
0040 static_assert(std::is_integral_v<From>);
0041
0042 [[maybe_unused]]
0043 constexpr auto Lo = (std::numeric_limits<To>::min)();
0044 constexpr auto Hi = (std::numeric_limits<To>::max)();
0045
0046 if constexpr (std::is_signed_v<From> == std::is_signed_v<To>) {
0047
0048 return x < Lo ? Lo :
0049 x > Hi ? Hi :
0050 To(x);
0051 } else {
0052 if constexpr (std::is_signed_v<From>) {
0053 if (x < From{0})
0054 return To{0};
0055 }
0056
0057
0058 using FromU = std::make_unsigned_t<From>;
0059 using ToU = std::make_unsigned_t<To>;
0060 return FromU(x) > ToU(Hi) ? Hi : To(x);
0061 }
0062 }
0063 #endif
0064
0065 }
0066
0067 QT_END_NAMESPACE
0068
0069 #endif