From 7a90ed1238e543f1db1c4e5be5b53f3f07ec679e Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 29 Jul 2024 23:54:50 +0200 Subject: [PATCH] qt_saturate: use saturate_cast if available Just wrap the existing implementation. Perhaps it should be renamed and moved into q26numeric.h, but that's left for later. Change-Id: I0d53da8876029e89c3cd55cecff90bbe984b695b Reviewed-by: Thiago Macieira --- src/corelib/global/qnumeric_p.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/corelib/global/qnumeric_p.h b/src/corelib/global/qnumeric_p.h index d40e6b964b0..1b88c28974b 100644 --- a/src/corelib/global/qnumeric_p.h +++ b/src/corelib/global/qnumeric_p.h @@ -23,6 +23,10 @@ #include #include +#ifdef __cpp_lib_saturation_arithmetic +# include // for saturate_cast +#endif + #ifndef __has_extension # define __has_extension(X) 0 #endif @@ -441,6 +445,9 @@ template bool mul_overflow(T v1, T *r) template static constexpr auto qt_saturate(From x) { +#ifdef __cpp_lib_saturation_arithmetic + return std::saturate_cast(x); +#else static_assert(std::is_integral_v); static_assert(std::is_integral_v); @@ -464,6 +471,7 @@ static constexpr auto qt_saturate(From x) using ToU = std::make_unsigned_t; return FromU(x) > ToU(Hi) ? Hi : To(x); // assumes Hi >= 0 } +#endif // __cpp_lib_saturation_arithmetic } QT_END_NAMESPACE