9#pragma qt_class(QtNumeric)
12#include <QtCore/qassert.h>
13#include <QtCore/qtconfigmacros.h>
14#include <QtCore/qtcoreexports.h>
15#include <QtCore/qtypes.h>
19#include <QtCore/q20type_traits.h>
33#if defined(Q_CC_MSVC) && !defined(Q_NUMERIC_NO_INTRINSICS)
36# if defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_X86_64)
37# define Q_HAVE_ADDCARRY
39# if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64)
40# define Q_INTRINSIC_MUL_OVERFLOW64
41# define Q_UMULH(v1, v2) __umulh(v1, v2)
42# define Q_SMULH(v1, v2) __mulh(v1, v2)
43# pragma intrinsic(__umulh)
44# pragma intrinsic(__mulh)
52constexpr typename std::enable_if<std::is_integral<T>::value,
bool>::type
53qIsInf(T) {
return false; }
55constexpr typename std::enable_if<
std::is_integral<T>::value,
bool>::
type
58constexpr typename std::enable_if<
std::is_integral<T>::value,
bool>::
type
62Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsInf(
double d);
63Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsNaN(
double d);
64Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsFinite(
double d);
65Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
int qFpClassify(
double val);
66Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsInf(
float f);
67Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsNaN(
float f);
68Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsFinite(
float f);
69Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
int qFpClassify(
float val);
71#if QT_CONFIG(signaling_nan)
72Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qSNaN();
74Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qQNaN();
75Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qInf();
77Q_CORE_EXPORT quint32 qFloatDistance(
float a,
float b);
78Q_CORE_EXPORT quint64 qFloatDistance(
double a,
double b);
80#define Q_INFINITY (QT_PREPEND_NAMESPACE(qInf)())
81#if QT_CONFIG(signaling_nan)
82# define Q_SNAN (QT_PREPEND_NAMESPACE(qSNaN)())
84#define Q_QNAN (QT_PREPEND_NAMESPACE(qQNaN)())
93#if defined(Q_CC_GNU_ONLY)
94 || defined(Q_CC_CLANG_ONLY)
96# define Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
100# if !(QT_POINTER_SIZE == 4
&& defined(Q_CC_CLANG_ONLY) && Q_CC_CLANG_ONLY < 1400
)
101# define Q_INTRINSIC_MUL_OVERFLOW64
114 return v1 > T(v1 + v2);
158 if (v1 == 0 || v2 == 0) {
165 using U = std::make_unsigned_t<T>;
166 const U v1_abs = (v1 >= 0) ? U(v1) : (U(0) - U(v1));
167 const U v2_abs = (v2 >= 0) ? U(v2) : (U(0) - U(v2));
170 constexpr std::size_t half_width = (
sizeof(U) * 8) / 2;
171 const U half_mask = ~U(0) >> half_width;
174 const U v1_lo = v1_abs & half_mask;
175 const U v1_hi = v1_abs >> half_width;
176 const U v2_lo = v2_abs & half_mask;
177 const U v2_hi = v2_abs >> half_width;
180 const U lo_lo = v1_lo * v2_lo;
181 const U lo_hi = v1_lo * v2_hi;
182 const U hi_lo = v1_hi * v2_lo;
183 const U hi_hi = v1_hi * v2_hi;
187 const U tmp = (lo_lo >> half_width) + (hi_lo & half_mask) + lo_hi;
188 U result_hi = (hi_lo >> half_width) + (tmp >> half_width) + hi_hi;
189 U result_lo = (tmp << half_width) | (lo_lo & half_mask);
191 if constexpr (
std::is_unsigned_v<T>) {
195 return result_hi != U(0);
198 const bool isNegative = (v1 < T(0)) != (v2 < T(0));
207 result_lo = U(0) - result_lo;
212 result_hi = ~result_hi;
220 return result_hi != U(*r >>
std::numeric_limits<T>::digits);
224template <
typename T,
typename Enable =
void>
244 if constexpr (HasLargerInt<T>) {
246 using LargerInt = QIntegerForSize<
sizeof(T) * 2>;
247 using Larger =
typename std::conditional_t<std::is_signed_v<T>,
248 typename LargerInt::Signed,
typename LargerInt::Unsigned>;
249 Larger lr = Larger(v1) * Larger(v2);
251 return lr > (
std::numeric_limits<T>::max)() || lr < (
std::numeric_limits<T>::min)();
254 return qMulOverflowWideMultiplication(v1, v2, r);
264#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
265 return __builtin_add_overflow(v1, v2, r);
267 if (q20::is_constant_evaluated())
268 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
269# if defined(Q_HAVE_ADDCARRY)
271 if constexpr (std::is_same_v<T,
unsigned>) {
272 return _addcarry_u32(0, v1, v2, r);
273 }
else if constexpr (std::is_same_v<T, quint64>) {
274# if defined(Q_PROCESSOR_X86_64)
275 return _addcarry_u64(0, v1, v2,
reinterpret_cast<
unsigned __int64 *>(r));
278 uchar carry = _addcarry_u32(0,
unsigned(v1),
unsigned(v2), &low);
279 carry = _addcarry_u32(carry, v1 >> 32, v2 >> 32, &high);
280 *r = (quint64(high) << 32) | low;
285 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
294#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
295 return __builtin_add_overflow(v1, v2, r);
306 using U =
typename std::make_unsigned_t<T>;
307 *r = T(U(v1) + U(v2));
314 return ((v1 ^ *r) & (v2 ^ *r)) < 0;
323#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
324 return __builtin_sub_overflow(v1, v2, r);
337#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
338 return __builtin_sub_overflow(v1, v2, r);
344 using U =
typename std::make_unsigned_t<T>;
345 *r = T(U(v1) - U(v2));
347 return ((v1 ^ *r) & (~v2 ^ *r)) < 0;
356#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
357# if defined(Q_INTRINSIC_MUL_OVERFLOW64
)
358 return __builtin_mul_overflow(v1, v2, r);
360 if constexpr (
sizeof(T) <= 4)
361 return __builtin_mul_overflow(v1, v2, r);
363 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
366 if (q20::is_constant_evaluated())
367 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
369# if defined(Q_INTRINSIC_MUL_OVERFLOW64)
370 if constexpr (std::is_unsigned_v<T> && (
sizeof(T) ==
sizeof(quint64))) {
374 return T(Q_UMULH(v1, v2));
375 }
else if constexpr (std::is_signed_v<T> && (
sizeof(T) ==
sizeof(qint64))) {
382 qint64 high = Q_SMULH(v1, v2);
383 *r = qint64(quint64(v1) * quint64(v2));
384 return (*r >> 63) != high;
388 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
392#undef Q_HAVE_ADDCARRY
393#undef Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
402 return qAddOverflow(v1, V2, r);
407 return qAddOverflow(v1, std::integral_constant<T, V2>{}, r);
412 return qSubOverflow(v1, V2, r);
417 return qSubOverflow(v1, std::integral_constant<T, V2>{}, r);
426 if constexpr (
sizeof(T) <=
sizeof(qregisteruint)) {
427 return qMulOverflow(v1, V2, r);
429#ifdef Q_INTRINSIC_MUL_OVERFLOW64
430 }
else if constexpr (
sizeof(T) <=
sizeof(quint64)) {
433 return qMulOverflow(v1, V2, r);
436 }
else if constexpr (V2 == 0 || V2 == 1) {
440 }
else if constexpr (V2 == -1) {
443 if (v1 < 0 && v1 == (
std::numeric_limits<T>::min)())
450 constexpr T Highest = (
std::numeric_limits<T>::max)() / V2;
451 constexpr T Lowest = (
std::numeric_limits<T>::min)() / V2;
452 if constexpr (Highest > Lowest) {
453 if (v1 > Highest || v1 < Lowest)
457 static_assert(V2 < 0);
458 if (v1 > Lowest || v1 < Highest)
469 if constexpr (V2 == 2)
470 return qAddOverflow(v1, v1, r);
471 return qMulOverflow(v1, std::integral_constant<T, V2>{}, r);
475constexpr inline T
qAbs(
const T &t)
477 if constexpr (
std::is_integral_v<T> &&
std::is_signed_v<T>)
478 Q_ASSERT(t !=
std::numeric_limits<T>::min());
479 return t >= 0 ? t : -t;
484 typename std::enable_if_t<std::is_integral_v<T>,
bool> =
true>
487 using U = std::make_unsigned_t<T>;
488 return (t >= 0) ? U(t) : U(~U(t) + U(1));
491template <
typename Result,
493 typename std::enable_if_t<std::is_integral_v<Result>,
bool> =
true,
494 typename std::enable_if_t<std::is_floating_point_v<FP>,
bool> =
true>
498#if !defined(__cpp_lib_constexpr_cmath) && !defined(Q_CC_GNU_ONLY)
499# ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
500 if (!q20::is_constant_evaluated())
506 const FP truncatedValue =
std::trunc(value);
507 Q_ASSERT(truncatedValue >= FP((
std::numeric_limits<Result>::min)()));
508 Q_ASSERT(truncatedValue <= FP((
std::numeric_limits<Result>::max)()));
510 return Result(value);
515#if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG)
518constexpr inline double qRound(
double d)
520constexpr inline float qRound(
float f)
522#elif defined(__SSE2__
) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU))
524constexpr inline double qRound(
double d)
525{
return d + __builtin_copysign(0.5, d); }
526constexpr inline float qRound(
float f)
527{
return f + __builtin_copysignf(0.5f, f); }
530{
return d >= 0.0 ?
d + 0.5 :
d - 0.5; }
532{
return d >= 0.0f ?
d + 0.5f :
d - 0.5f; }
538template <
typename FP,
539 typename std::enable_if_t<std::is_floating_point_v<FP>,
bool> =
true>
542#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
543 if (!q20::is_constant_evaluated())
544 Q_ASSERT(!qIsNaN(value));
546 constexpr FP MinBound = FP((
std::numeric_limits<
int>::min)());
547 constexpr FP MaxBound = FP((
std::numeric_limits<
int>::max)());
548 const FP beforeTruncation =
QRoundImpl::qRound(value);
549 return int(qBound(MinBound, beforeTruncation, MaxBound));
565 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(d));
570 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(f));
575constexpr inline const T &
min(
const T &a,
const T &b) {
return (a < b) ? a : b; }
580 return (qAbs(p1 - p2) * 1000000000000. <=
QtPrivate::min(qAbs(p1), qAbs(p2)));
585 return (qAbs(p1 - p2) * 100000.f <=
QtPrivate::min(qAbs(p1), qAbs(p2)));
590 return qAbs(d) <= 0.000000000001;
595 return qAbs(f) <= 0.00001f;
599QT_WARNING_DISABLE_FLOAT_COMPARE
601[[nodiscard]]
constexpr bool qIsNull(
double d)
noexcept
606[[nodiscard]]
constexpr bool qIsNull(
float f)
noexcept
constexpr const T & min(const T &a, const T &b)
constexpr bool HasLargerInt
constexpr int qSaturateRound(FP value)
constexpr std::enable_if_t<(std::is_unsigned_v< T >||std::is_signed_v< T >), bool > qMulOverflowGeneric(T v1, T v2, T *r)
constexpr auto qUnsignedAbs(T t)
constexpr std::enable_if_t< std::is_unsigned_v< T >, bool > qAddOverflowGeneric(T v1, T v2, T *r)
constexpr std::enable_if_t< std::is_same_v< T, decltype(+T{})>, bool > qMulOverflowWideMultiplication(T v1, T v2, T *r)
constexpr Result qCheckedFPConversionToInteger(FP value)
static quint64 d2i(double d)
Q_CORE_EXPORT int qFpClassify(float val)
Q_CORE_EXPORT int qFpClassify(double val)
static quint32 f2i(float f)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(float f)
constexpr T qAbs(const T &t)
constexpr std::enable_if< std::is_integral< T >::value, bool >::type qIsFinite(T)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(double d)
constexpr bool qSubOverflow(T v1, T *r)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf()
constexpr bool qMulOverflow(T v1, T *r)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(float f)
constexpr bool qFuzzyIsNull(double d) noexcept
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(double d)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(float f)
constexpr bool qFuzzyCompare(float p1, float p2) noexcept
constexpr bool qSubOverflow(T v1, std::integral_constant< T, V2 >, T *r)
constexpr bool qAddOverflow(T v1, T *r)
constexpr std::enable_if< std::is_integral< T >::value, bool >::type qIsNaN(T)
constexpr std::enable_if_t< std::is_unsigned_v< T >||std::is_signed_v< T >, bool > qMulOverflow(T v1, T v2, T *r)
constexpr qint64 qRound64(double d)
constexpr int qRound(float f)
constexpr int qRound(double d)
constexpr bool qIsNull(float f) noexcept
constexpr bool qFuzzyIsNull(float f) noexcept
constexpr std::enable_if_t< std::is_unsigned_v< T >, bool > qSubOverflow(T v1, T v2, T *r)
constexpr bool qFuzzyCompare(double p1, double p2) noexcept
constexpr qint64 qRound64(float f)
constexpr bool qMulOverflow(T v1, std::integral_constant< T, V2 >, T *r)
QT_WARNING_POP int qIntCast(double f)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN()
constexpr std::enable_if_t< std::is_unsigned_v< T >, bool > qAddOverflow(T v1, T v2, T *r)
constexpr bool qAddOverflow(T v1, std::integral_constant< T, V2 >, T *r)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(double d)
QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE constexpr bool qIsNull(double d) noexcept