10#pragma qt_class(QtNumeric)
13#include <QtCore/qassert.h>
14#include <QtCore/qminmax.h>
15#include <QtCore/qtconfigmacros.h>
16#include <QtCore/qtcoreexports.h>
17#include <QtCore/qtypes.h>
21#include <QtCore/q20type_traits.h>
27# include <QtCore/qstdlibdetection.h>
28# if defined(Q_CC_GNU_ONLY) && (defined(Q_STL_LIBCPP) || Q_CC_GNU_ONLY < 1500
)
30# elif defined(Q_OS_FREEBSD) && __FreeBSD_version <= 1500000
33# include <stdckdint.h>
49#if defined(Q_CC_MSVC) && !defined(Q_NUMERIC_NO_INTRINSICS)
52# if defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_X86_64)
53# define Q_HAVE_ADDCARRY
55# if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64)
56# define Q_INTRINSIC_MUL_OVERFLOW64
57# define Q_UMULH(v1, v2) __umulh(v1, v2)
58# define Q_SMULH(v1, v2) __mulh(v1, v2)
59# pragma intrinsic(__umulh)
60# pragma intrinsic(__mulh)
68constexpr typename std::enable_if<std::is_integral<T>::value,
bool>::type
69qIsInf(T) {
return false; }
71constexpr typename std::enable_if<
std::is_integral<T>::value,
bool>::
type
74constexpr typename std::enable_if<
std::is_integral<T>::value,
bool>::
type
78Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsInf(
double d);
79Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsNaN(
double d);
80Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsFinite(
double d);
81Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
int qFpClassify(
double val);
82Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsInf(
float f);
83Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsNaN(
float f);
84Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsFinite(
float f);
85Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
int qFpClassify(
float val);
87#if QT_CONFIG(signaling_nan)
88Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qSNaN();
90Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qQNaN();
91Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qInf();
93Q_CORE_EXPORT quint32 qFloatDistance(
float a,
float b);
94Q_CORE_EXPORT quint64 qFloatDistance(
double a,
double b);
96#define Q_INFINITY (QT_PREPEND_NAMESPACE(qInf)())
97#if QT_CONFIG(signaling_nan)
98# define Q_SNAN (QT_PREPEND_NAMESPACE(qSNaN)())
100#define Q_QNAN (QT_PREPEND_NAMESPACE(qQNaN)())
109#if defined(Q_CC_GNU_ONLY)
110 || defined(Q_CC_CLANG_ONLY)
112# define Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
116# if !(QT_POINTER_SIZE == 4
&& defined(Q_CC_CLANG_ONLY) && Q_CC_CLANG_ONLY < 1400
)
117# define Q_INTRINSIC_MUL_OVERFLOW64
130 return v1 > T(v1 + v2);
174 if (v1 == 0 || v2 == 0) {
181 using U = std::make_unsigned_t<T>;
182 const U v1_abs = (v1 >= 0) ? U(v1) : (U(0) - U(v1));
183 const U v2_abs = (v2 >= 0) ? U(v2) : (U(0) - U(v2));
186 constexpr std::size_t half_width = (
sizeof(U) * 8) / 2;
187 const U half_mask = ~U(0) >> half_width;
190 const U v1_lo = v1_abs & half_mask;
191 const U v1_hi = v1_abs >> half_width;
192 const U v2_lo = v2_abs & half_mask;
193 const U v2_hi = v2_abs >> half_width;
196 const U lo_lo = v1_lo * v2_lo;
197 const U lo_hi = v1_lo * v2_hi;
198 const U hi_lo = v1_hi * v2_lo;
199 const U hi_hi = v1_hi * v2_hi;
203 const U tmp = (lo_lo >> half_width) + (hi_lo & half_mask) + lo_hi;
204 U result_hi = (hi_lo >> half_width) + (tmp >> half_width) + hi_hi;
205 U result_lo = (tmp << half_width) | (lo_lo & half_mask);
207 if constexpr (
std::is_unsigned_v<T>) {
211 return result_hi != U(0);
214 const bool isNegative = (v1 < T(0)) != (v2 < T(0));
223 result_lo = U(0) - result_lo;
228 result_hi = ~result_hi;
236 return result_hi != U(*r >>
std::numeric_limits<T>::digits);
240template <
typename T,
typename Enable =
void>
260 if constexpr (HasLargerInt<T>) {
262 using LargerInt = QIntegerForSize<
sizeof(T) * 2>;
263 using Larger =
typename std::conditional_t<std::is_signed_v<T>,
264 typename LargerInt::Signed,
typename LargerInt::Unsigned>;
265 Larger lr = Larger(v1) * Larger(v2);
267 return lr > (
std::numeric_limits<T>::max)() || lr < (
std::numeric_limits<T>::min)();
270 return qMulOverflowWideMultiplication(v1, v2, r);
280 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
281#if defined(__STDC_VERSION_STDCKDINT_H__)
282 return ckd_add(r, v1, v2);
283#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
284 return __builtin_add_overflow(v1, v2, r);
286 if (q20::is_constant_evaluated())
287 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
288# if defined(Q_HAVE_ADDCARRY)
290 if constexpr (std::is_same_v<T,
unsigned>) {
291 return _addcarry_u32(0, v1, v2, r);
292 }
else if constexpr (std::is_same_v<T, quint64>) {
293# if defined(Q_PROCESSOR_X86_64)
294 return _addcarry_u64(0, v1, v2,
reinterpret_cast<
unsigned __int64 *>(r));
297 uchar carry = _addcarry_u32(0,
unsigned(v1),
unsigned(v2), &low);
298 carry = _addcarry_u32(carry, v1 >> 32, v2 >> 32, &high);
299 *r = (quint64(high) << 32) | low;
304 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
313 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
314#if defined(__STDC_VERSION_STDCKDINT_H__)
315 return ckd_add(r, v1, v2);
316#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
317 return __builtin_add_overflow(v1, v2, r);
328 using U =
typename std::make_unsigned_t<T>;
329 *r = T(U(v1) + U(v2));
336 return ((v1 ^ *r) & (v2 ^ *r)) < 0;
345 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
346#if defined(__STDC_VERSION_STDCKDINT_H__)
347 return ckd_sub(r, v1, v2);
348#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
349 return __builtin_sub_overflow(v1, v2, r);
362 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
363#if defined(__STDC_VERSION_STDCKDINT_H__)
364 return ckd_sub(r, v1, v2);
365#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
366 return __builtin_sub_overflow(v1, v2, r);
372 using U =
typename std::make_unsigned_t<T>;
373 *r = T(U(v1) - U(v2));
375 return ((v1 ^ *r) & (~v2 ^ *r)) < 0;
384 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
385#if defined(__STDC_VERSION_STDCKDINT_H__)
386 return ckd_mul(r, v1, v2);
387#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
388# if defined(Q_INTRINSIC_MUL_OVERFLOW64
)
389 return __builtin_mul_overflow(v1, v2, r);
391 if constexpr (
sizeof(T) <= 4)
392 return __builtin_mul_overflow(v1, v2, r);
394 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
397 if (q20::is_constant_evaluated())
398 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
400# if defined(Q_INTRINSIC_MUL_OVERFLOW64)
401 if constexpr (std::is_unsigned_v<T> && (
sizeof(T) ==
sizeof(quint64))) {
405 return T(Q_UMULH(v1, v2));
406 }
else if constexpr (std::is_signed_v<T> && (
sizeof(T) ==
sizeof(qint64))) {
413 qint64 high = Q_SMULH(v1, v2);
414 *r = qint64(quint64(v1) * quint64(v2));
415 return (*r >> 63) != high;
419 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
423#undef Q_HAVE_ADDCARRY
424#undef Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
433 return qAddOverflow(v1, V2, r);
438 return qAddOverflow(v1, std::integral_constant<T, V2>{}, r);
443 return qSubOverflow(v1, V2, r);
448 return qSubOverflow(v1, std::integral_constant<T, V2>{}, r);
453 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
459 if constexpr (
sizeof(T) <=
sizeof(qregisteruint)) {
460 return qMulOverflow(v1, V2, r);
462#ifdef Q_INTRINSIC_MUL_OVERFLOW64
463 }
else if constexpr (
sizeof(T) <=
sizeof(quint64)) {
466 return qMulOverflow(v1, V2, r);
469 }
else if constexpr (V2 == 0 || V2 == 1) {
473 }
else if constexpr (V2 == -1) {
476 if (v1 < 0 && v1 == (
std::numeric_limits<T>::min)())
483 constexpr T Highest = (
std::numeric_limits<T>::max)() / V2;
484 constexpr T Lowest = (
std::numeric_limits<T>::min)() / V2;
485 if constexpr (Highest > Lowest) {
486 if (v1 > Highest || v1 < Lowest)
490 static_assert(V2 < 0);
491 if (v1 > Lowest || v1 < Highest)
502 if constexpr (V2 == 2)
503 return qAddOverflow(v1, v1, r);
504 return qMulOverflow(v1, std::integral_constant<T, V2>{}, r);
508constexpr inline T
qAbs(
const T &t)
510 if constexpr (
std::is_integral_v<T> &&
std::is_signed_v<T>)
511 Q_ASSERT(t !=
std::numeric_limits<T>::min());
512 return t >= 0 ? t : -t;
517 typename std::enable_if_t<std::is_integral_v<T>,
bool> =
true>
520 using U = std::make_unsigned_t<T>;
521 return (t >= 0) ? U(t) : U(~U(t) + U(1));
524template <
typename Result,
526 typename std::enable_if_t<std::is_integral_v<Result>,
bool> =
true,
527 typename std::enable_if_t<std::is_floating_point_v<FP>,
bool> =
true>
530#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
531 if (!q20::is_constant_evaluated())
532 Q_ASSERT(!std::isnan(value));
535 constexpr Result minimal = (
std::numeric_limits<Result>::min)();
536 constexpr Result maximal = (
std::numeric_limits<Result>::max)();
541 Q_ASSERT(value - FP(minimal) > FP(-1));
545 constexpr FP maximalPlusOne = FP(2) * (maximal / 2 + 1);
547 Q_ASSERT(value < maximalPlusOne);
551 return Result(value);
556#if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG)
559constexpr inline double qRound(
double d)
561constexpr inline float qRound(
float f)
563#elif defined(__SSE2__
) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU))
565constexpr inline double qRound(
double d)
566{
return d + __builtin_copysign(0.5, d); }
567constexpr inline float qRound(
float f)
568{
return f + __builtin_copysignf(0.5f, f); }
571{
return d >= 0.0 ?
d + 0.5 :
d - 0.5; }
573{
return d >= 0.0f ?
d + 0.5f :
d - 0.5f; }
579template <
typename FP,
580 typename std::enable_if_t<std::is_floating_point_v<FP>,
bool> =
true>
583#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
584 if (!q20::is_constant_evaluated())
585 Q_ASSERT(!qIsNaN(value));
587 constexpr FP MinBound = FP((
std::numeric_limits<
int>::min)());
588 constexpr FP MaxBound = FP((
std::numeric_limits<
int>::max)());
589 const FP beforeTruncation =
QRoundImpl::qRound(value);
590 return int(qBound(MinBound, beforeTruncation, MaxBound));
606 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(d));
611 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(f));
616constexpr inline const T &
min(
const T &a,
const T &b) {
return (a < b) ? a : b; }
621 return (qAbs(p1 - p2) * 1000000000000. <=
QtPrivate::min(qAbs(p1), qAbs(p2)));
626 return (qAbs(p1 - p2) * 100000.f <=
QtPrivate::min(qAbs(p1), qAbs(p2)));
631 return qAbs(d) <= 0.000000000001;
636 return qAbs(f) <= 0.00001f;
640QT_WARNING_DISABLE_FLOAT_COMPARE
642[[nodiscard]]
constexpr bool qIsNull(
double d)
noexcept
647[[nodiscard]]
constexpr bool qIsNull(
float f)
noexcept
Combined button and popup list for selecting options.
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