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>
35#if defined(Q_CC_MSVC) && !defined(Q_NUMERIC_NO_INTRINSICS)
38# if defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_X86_64)
39# define Q_HAVE_ADDCARRY
41# if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64)
42# define Q_INTRINSIC_MUL_OVERFLOW64
43# define Q_UMULH(v1, v2) __umulh(v1, v2)
44# define Q_SMULH(v1, v2) __mulh(v1, v2)
45# pragma intrinsic(__umulh)
46# pragma intrinsic(__mulh)
54constexpr typename std::enable_if<std::is_integral<T>::value,
bool>::type
55qIsInf(T) {
return false; }
57constexpr typename std::enable_if<
std::is_integral<T>::value,
bool>::
type
60constexpr typename std::enable_if<
std::is_integral<T>::value,
bool>::
type
64Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsInf(
double d);
65Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsNaN(
double d);
66Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsFinite(
double d);
67Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
int qFpClassify(
double val);
68Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsInf(
float f);
69Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsNaN(
float f);
70Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsFinite(
float f);
71Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
int qFpClassify(
float val);
73#if QT_CONFIG(signaling_nan)
74Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qSNaN();
76Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qQNaN();
77Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qInf();
79Q_CORE_EXPORT quint32 qFloatDistance(
float a,
float b);
80Q_CORE_EXPORT quint64 qFloatDistance(
double a,
double b);
82#define Q_INFINITY (QT_PREPEND_NAMESPACE(qInf)())
83#if QT_CONFIG(signaling_nan)
84# define Q_SNAN (QT_PREPEND_NAMESPACE(qSNaN)())
86#define Q_QNAN (QT_PREPEND_NAMESPACE(qQNaN)())
95#if defined(Q_CC_GNU_ONLY)
96 || defined(Q_CC_CLANG_ONLY)
98# define Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
102# if !(QT_POINTER_SIZE == 4
&& defined(Q_CC_CLANG_ONLY) && Q_CC_CLANG_ONLY < 1400
)
103# define Q_INTRINSIC_MUL_OVERFLOW64
116 return v1 > T(v1 + v2);
160 if (v1 == 0 || v2 == 0) {
167 using U = std::make_unsigned_t<T>;
168 const U v1_abs = (v1 >= 0) ? U(v1) : (U(0) - U(v1));
169 const U v2_abs = (v2 >= 0) ? U(v2) : (U(0) - U(v2));
172 constexpr std::size_t half_width = (
sizeof(U) * 8) / 2;
173 const U half_mask = ~U(0) >> half_width;
176 const U v1_lo = v1_abs & half_mask;
177 const U v1_hi = v1_abs >> half_width;
178 const U v2_lo = v2_abs & half_mask;
179 const U v2_hi = v2_abs >> half_width;
182 const U lo_lo = v1_lo * v2_lo;
183 const U lo_hi = v1_lo * v2_hi;
184 const U hi_lo = v1_hi * v2_lo;
185 const U hi_hi = v1_hi * v2_hi;
189 const U tmp = (lo_lo >> half_width) + (hi_lo & half_mask) + lo_hi;
190 U result_hi = (hi_lo >> half_width) + (tmp >> half_width) + hi_hi;
191 U result_lo = (tmp << half_width) | (lo_lo & half_mask);
193 if constexpr (
std::is_unsigned_v<T>) {
197 return result_hi != U(0);
200 const bool isNegative = (v1 < T(0)) != (v2 < T(0));
209 result_lo = U(0) - result_lo;
214 result_hi = ~result_hi;
222 return result_hi != U(*r >>
std::numeric_limits<T>::digits);
226template <
typename T,
typename Enable =
void>
246 if constexpr (HasLargerInt<T>) {
248 using LargerInt = QIntegerForSize<
sizeof(T) * 2>;
249 using Larger =
typename std::conditional_t<std::is_signed_v<T>,
250 typename LargerInt::Signed,
typename LargerInt::Unsigned>;
251 Larger lr = Larger(v1) * Larger(v2);
253 return lr > (
std::numeric_limits<T>::max)() || lr < (
std::numeric_limits<T>::min)();
256 return qMulOverflowWideMultiplication(v1, v2, r);
266 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
267#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
268 return __builtin_add_overflow(v1, v2, r);
270 if (q20::is_constant_evaluated())
271 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
272# if defined(Q_HAVE_ADDCARRY)
274 if constexpr (std::is_same_v<T,
unsigned>) {
275 return _addcarry_u32(0, v1, v2, r);
276 }
else if constexpr (std::is_same_v<T, quint64>) {
277# if defined(Q_PROCESSOR_X86_64)
278 return _addcarry_u64(0, v1, v2,
reinterpret_cast<
unsigned __int64 *>(r));
281 uchar carry = _addcarry_u32(0,
unsigned(v1),
unsigned(v2), &low);
282 carry = _addcarry_u32(carry, v1 >> 32, v2 >> 32, &high);
283 *r = (quint64(high) << 32) | low;
288 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
297 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
298#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
299 return __builtin_add_overflow(v1, v2, r);
310 using U =
typename std::make_unsigned_t<T>;
311 *r = T(U(v1) + U(v2));
318 return ((v1 ^ *r) & (v2 ^ *r)) < 0;
327 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
328#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
329 return __builtin_sub_overflow(v1, v2, r);
342 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
343#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
344 return __builtin_sub_overflow(v1, v2, r);
350 using U =
typename std::make_unsigned_t<T>;
351 *r = T(U(v1) - U(v2));
353 return ((v1 ^ *r) & (~v2 ^ *r)) < 0;
362 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
363#if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
364# if defined(Q_INTRINSIC_MUL_OVERFLOW64
)
365 return __builtin_mul_overflow(v1, v2, r);
367 if constexpr (
sizeof(T) <= 4)
368 return __builtin_mul_overflow(v1, v2, r);
370 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
373 if (q20::is_constant_evaluated())
374 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
376# if defined(Q_INTRINSIC_MUL_OVERFLOW64)
377 if constexpr (std::is_unsigned_v<T> && (
sizeof(T) ==
sizeof(quint64))) {
381 return T(Q_UMULH(v1, v2));
382 }
else if constexpr (std::is_signed_v<T> && (
sizeof(T) ==
sizeof(qint64))) {
389 qint64 high = Q_SMULH(v1, v2);
390 *r = qint64(quint64(v1) * quint64(v2));
391 return (*r >> 63) != high;
395 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
399#undef Q_HAVE_ADDCARRY
400#undef Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
409 return qAddOverflow(v1, V2, r);
414 return qAddOverflow(v1, std::integral_constant<T, V2>{}, r);
419 return qSubOverflow(v1, V2, r);
424 return qSubOverflow(v1, std::integral_constant<T, V2>{}, r);
429 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
435 if constexpr (
sizeof(T) <=
sizeof(qregisteruint)) {
436 return qMulOverflow(v1, V2, r);
438#ifdef Q_INTRINSIC_MUL_OVERFLOW64
439 }
else if constexpr (
sizeof(T) <=
sizeof(quint64)) {
442 return qMulOverflow(v1, V2, r);
445 }
else if constexpr (V2 == 0 || V2 == 1) {
449 }
else if constexpr (V2 == -1) {
452 if (v1 < 0 && v1 == (
std::numeric_limits<T>::min)())
459 constexpr T Highest = (
std::numeric_limits<T>::max)() / V2;
460 constexpr T Lowest = (
std::numeric_limits<T>::min)() / V2;
461 if constexpr (Highest > Lowest) {
462 if (v1 > Highest || v1 < Lowest)
466 static_assert(V2 < 0);
467 if (v1 > Lowest || v1 < Highest)
478 if constexpr (V2 == 2)
479 return qAddOverflow(v1, v1, r);
480 return qMulOverflow(v1, std::integral_constant<T, V2>{}, r);
484constexpr inline T
qAbs(
const T &t)
486 if constexpr (
std::is_integral_v<T> &&
std::is_signed_v<T>)
487 Q_ASSERT(t !=
std::numeric_limits<T>::min());
488 return t >= 0 ? t : -t;
493 typename std::enable_if_t<std::is_integral_v<T>,
bool> =
true>
496 using U = std::make_unsigned_t<T>;
497 return (t >= 0) ? U(t) : U(~U(t) + U(1));
500template <
typename Result,
502 typename std::enable_if_t<std::is_integral_v<Result>,
bool> =
true,
503 typename std::enable_if_t<std::is_floating_point_v<FP>,
bool> =
true>
506#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
507 if (!q20::is_constant_evaluated())
508 Q_ASSERT(!std::isnan(value));
511 constexpr Result minimal = (
std::numeric_limits<Result>::min)();
512 constexpr Result maximal = (
std::numeric_limits<Result>::max)();
517 Q_ASSERT(value - FP(minimal) > FP(-1));
521 constexpr FP maximalPlusOne = FP(2) * (maximal / 2 + 1);
523 Q_ASSERT(value < maximalPlusOne);
527 return Result(value);
532#if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG)
535constexpr inline double qRound(
double d)
537constexpr inline float qRound(
float f)
539#elif defined(__SSE2__
) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU))
541constexpr inline double qRound(
double d)
542{
return d + __builtin_copysign(0.5, d); }
543constexpr inline float qRound(
float f)
544{
return f + __builtin_copysignf(0.5f, f); }
547{
return d >= 0.0 ?
d + 0.5 :
d - 0.5; }
549{
return d >= 0.0f ?
d + 0.5f :
d - 0.5f; }
555template <
typename FP,
556 typename std::enable_if_t<std::is_floating_point_v<FP>,
bool> =
true>
559#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
560 if (!q20::is_constant_evaluated())
561 Q_ASSERT(!qIsNaN(value));
563 constexpr FP MinBound = FP((
std::numeric_limits<
int>::min)());
564 constexpr FP MaxBound = FP((
std::numeric_limits<
int>::max)());
565 const FP beforeTruncation =
QRoundImpl::qRound(value);
566 return int(qBound(MinBound, beforeTruncation, MaxBound));
582 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(d));
587 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(f));
592constexpr inline const T &
min(
const T &a,
const T &b) {
return (a < b) ? a : b; }
597 return (qAbs(p1 - p2) * 1000000000000. <=
QtPrivate::min(qAbs(p1), qAbs(p2)));
602 return (qAbs(p1 - p2) * 100000.f <=
QtPrivate::min(qAbs(p1), qAbs(p2)));
607 return qAbs(d) <= 0.000000000001;
612 return qAbs(f) <= 0.00001f;
616QT_WARNING_DISABLE_FLOAT_COMPARE
618[[nodiscard]]
constexpr bool qIsNull(
double d)
noexcept
623[[nodiscard]]
constexpr bool qIsNull(
float f)
noexcept
632
633
634
635
636
637
638
639
640template <
typename T,
typename S>
641[[nodiscard]]
constexpr bool fuzzyCompare(
const T &lhs,
const S &rhs)
noexcept
643 static_assert(
noexcept(qIsNull(lhs) && qIsNull(rhs) && qFuzzyIsNull(lhs - rhs) && qFuzzyCompare(lhs, rhs)),
644 "The operations qIsNull(), qFuzzyIsNull() and qFuzzyCompare() must be noexcept "
645 "for both argument types!");
646 return qIsNull(lhs) || qIsNull(rhs) ? qFuzzyIsNull(lhs - rhs) : qFuzzyCompare(lhs, rhs);
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 bool fuzzyCompare(const T &lhs, const S &rhs) noexcept
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)
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