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
)
31# include <stdckdint.h>
47#if defined(Q_CC_MSVC) && !defined(Q_NUMERIC_NO_INTRINSICS)
50# if defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_X86_64)
51# define Q_HAVE_ADDCARRY
53# if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64)
54# define Q_INTRINSIC_MUL_OVERFLOW64
55# define Q_UMULH(v1, v2) __umulh(v1, v2)
56# define Q_SMULH(v1, v2) __mulh(v1, v2)
57# pragma intrinsic(__umulh)
58# pragma intrinsic(__mulh)
66constexpr typename std::enable_if<std::is_integral<T>::value,
bool>::type
67qIsInf(T) {
return false; }
69constexpr typename std::enable_if<
std::is_integral<T>::value,
bool>::
type
72constexpr typename std::enable_if<
std::is_integral<T>::value,
bool>::
type
76Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsInf(
double d);
77Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsNaN(
double d);
78Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsFinite(
double d);
79Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
int qFpClassify(
double val);
80Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsInf(
float f);
81Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsNaN(
float f);
82Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
bool qIsFinite(
float f);
83Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
int qFpClassify(
float val);
85#if QT_CONFIG(signaling_nan)
86Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qSNaN();
88Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qQNaN();
89Q_CORE_EXPORT Q_DECL_CONST_FUNCTION
double qInf();
91Q_CORE_EXPORT quint32 qFloatDistance(
float a,
float b);
92Q_CORE_EXPORT quint64 qFloatDistance(
double a,
double b);
94#define Q_INFINITY (QT_PREPEND_NAMESPACE(qInf)())
95#if QT_CONFIG(signaling_nan)
96# define Q_SNAN (QT_PREPEND_NAMESPACE(qSNaN)())
98#define Q_QNAN (QT_PREPEND_NAMESPACE(qQNaN)())
107#if defined(Q_CC_GNU_ONLY)
108 || defined(Q_CC_CLANG_ONLY)
110# define Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
114# if !(QT_POINTER_SIZE == 4
&& defined(Q_CC_CLANG_ONLY) && Q_CC_CLANG_ONLY < 1400
)
115# define Q_INTRINSIC_MUL_OVERFLOW64
128 return v1 > T(v1 + v2);
172 if (v1 == 0 || v2 == 0) {
179 using U = std::make_unsigned_t<T>;
180 const U v1_abs = (v1 >= 0) ? U(v1) : (U(0) - U(v1));
181 const U v2_abs = (v2 >= 0) ? U(v2) : (U(0) - U(v2));
184 constexpr std::size_t half_width = (
sizeof(U) * 8) / 2;
185 const U half_mask = ~U(0) >> half_width;
188 const U v1_lo = v1_abs & half_mask;
189 const U v1_hi = v1_abs >> half_width;
190 const U v2_lo = v2_abs & half_mask;
191 const U v2_hi = v2_abs >> half_width;
194 const U lo_lo = v1_lo * v2_lo;
195 const U lo_hi = v1_lo * v2_hi;
196 const U hi_lo = v1_hi * v2_lo;
197 const U hi_hi = v1_hi * v2_hi;
201 const U tmp = (lo_lo >> half_width) + (hi_lo & half_mask) + lo_hi;
202 U result_hi = (hi_lo >> half_width) + (tmp >> half_width) + hi_hi;
203 U result_lo = (tmp << half_width) | (lo_lo & half_mask);
205 if constexpr (
std::is_unsigned_v<T>) {
209 return result_hi != U(0);
212 const bool isNegative = (v1 < T(0)) != (v2 < T(0));
221 result_lo = U(0) - result_lo;
226 result_hi = ~result_hi;
234 return result_hi != U(*r >>
std::numeric_limits<T>::digits);
238template <
typename T,
typename Enable =
void>
258 if constexpr (HasLargerInt<T>) {
260 using LargerInt = QIntegerForSize<
sizeof(T) * 2>;
261 using Larger =
typename std::conditional_t<std::is_signed_v<T>,
262 typename LargerInt::Signed,
typename LargerInt::Unsigned>;
263 Larger lr = Larger(v1) * Larger(v2);
265 return lr > (
std::numeric_limits<T>::max)() || lr < (
std::numeric_limits<T>::min)();
268 return qMulOverflowWideMultiplication(v1, v2, r);
278 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
279#if defined(__STDC_VERSION_STDCKDINT_H__)
280 return ckd_add(r, v1, v2);
281#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
282 return __builtin_add_overflow(v1, v2, r);
284 if (q20::is_constant_evaluated())
285 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
286# if defined(Q_HAVE_ADDCARRY)
288 if constexpr (std::is_same_v<T,
unsigned>) {
289 return _addcarry_u32(0, v1, v2, r);
290 }
else if constexpr (std::is_same_v<T, quint64>) {
291# if defined(Q_PROCESSOR_X86_64)
292 return _addcarry_u64(0, v1, v2,
reinterpret_cast<
unsigned __int64 *>(r));
295 uchar carry = _addcarry_u32(0,
unsigned(v1),
unsigned(v2), &low);
296 carry = _addcarry_u32(carry, v1 >> 32, v2 >> 32, &high);
297 *r = (quint64(high) << 32) | low;
302 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
311 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
312#if defined(__STDC_VERSION_STDCKDINT_H__)
313 return ckd_add(r, v1, v2);
314#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
315 return __builtin_add_overflow(v1, v2, r);
326 using U =
typename std::make_unsigned_t<T>;
327 *r = T(U(v1) + U(v2));
334 return ((v1 ^ *r) & (v2 ^ *r)) < 0;
343 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
344#if defined(__STDC_VERSION_STDCKDINT_H__)
345 return ckd_sub(r, v1, v2);
346#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
347 return __builtin_sub_overflow(v1, v2, r);
360 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
361#if defined(__STDC_VERSION_STDCKDINT_H__)
362 return ckd_sub(r, v1, v2);
363#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
364 return __builtin_sub_overflow(v1, v2, r);
370 using U =
typename std::make_unsigned_t<T>;
371 *r = T(U(v1) - U(v2));
373 return ((v1 ^ *r) & (~v2 ^ *r)) < 0;
382 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
383#if defined(__STDC_VERSION_STDCKDINT_H__)
384 return ckd_mul(r, v1, v2);
385#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
)
386# if defined(Q_INTRINSIC_MUL_OVERFLOW64
)
387 return __builtin_mul_overflow(v1, v2, r);
389 if constexpr (
sizeof(T) <= 4)
390 return __builtin_mul_overflow(v1, v2, r);
392 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
395 if (q20::is_constant_evaluated())
396 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
398# if defined(Q_INTRINSIC_MUL_OVERFLOW64)
399 if constexpr (std::is_unsigned_v<T> && (
sizeof(T) ==
sizeof(quint64))) {
403 return T(Q_UMULH(v1, v2));
404 }
else if constexpr (std::is_signed_v<T> && (
sizeof(T) ==
sizeof(qint64))) {
411 qint64 high = Q_SMULH(v1, v2);
412 *r = qint64(quint64(v1) * quint64(v2));
413 return (*r >> 63) != high;
417 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
421#undef Q_HAVE_ADDCARRY
422#undef Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
431 return qAddOverflow(v1, V2, r);
436 return qAddOverflow(v1, std::integral_constant<T, V2>{}, r);
441 return qSubOverflow(v1, V2, r);
446 return qSubOverflow(v1, std::integral_constant<T, V2>{}, r);
451 static_assert(!
std::is_same_v<T,
char>,
"Template must be an integral other than plain 'char'");
457 if constexpr (
sizeof(T) <=
sizeof(qregisteruint)) {
458 return qMulOverflow(v1, V2, r);
460#ifdef Q_INTRINSIC_MUL_OVERFLOW64
461 }
else if constexpr (
sizeof(T) <=
sizeof(quint64)) {
464 return qMulOverflow(v1, V2, r);
467 }
else if constexpr (V2 == 0 || V2 == 1) {
471 }
else if constexpr (V2 == -1) {
474 if (v1 < 0 && v1 == (
std::numeric_limits<T>::min)())
481 constexpr T Highest = (
std::numeric_limits<T>::max)() / V2;
482 constexpr T Lowest = (
std::numeric_limits<T>::min)() / V2;
483 if constexpr (Highest > Lowest) {
484 if (v1 > Highest || v1 < Lowest)
488 static_assert(V2 < 0);
489 if (v1 > Lowest || v1 < Highest)
500 if constexpr (V2 == 2)
501 return qAddOverflow(v1, v1, r);
502 return qMulOverflow(v1, std::integral_constant<T, V2>{}, r);
506constexpr inline T
qAbs(
const T &t)
508 if constexpr (
std::is_integral_v<T> &&
std::is_signed_v<T>)
509 Q_ASSERT(t !=
std::numeric_limits<T>::min());
510 return t >= 0 ? t : -t;
515 typename std::enable_if_t<std::is_integral_v<T>,
bool> =
true>
518 using U = std::make_unsigned_t<T>;
519 return (t >= 0) ? U(t) : U(~U(t) + U(1));
522template <
typename Result,
524 typename std::enable_if_t<std::is_integral_v<Result>,
bool> =
true,
525 typename std::enable_if_t<std::is_floating_point_v<FP>,
bool> =
true>
528#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
529 if (!q20::is_constant_evaluated())
530 Q_ASSERT(!std::isnan(value));
533 constexpr Result minimal = (
std::numeric_limits<Result>::min)();
534 constexpr Result maximal = (
std::numeric_limits<Result>::max)();
539 Q_ASSERT(value - FP(minimal) > FP(-1));
543 constexpr FP maximalPlusOne = FP(2) * (maximal / 2 + 1);
545 Q_ASSERT(value < maximalPlusOne);
549 return Result(value);
554#if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG)
557constexpr inline double qRound(
double d)
559constexpr inline float qRound(
float f)
561#elif defined(__SSE2__
) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU))
563constexpr inline double qRound(
double d)
564{
return d + __builtin_copysign(0.5, d); }
565constexpr inline float qRound(
float f)
566{
return f + __builtin_copysignf(0.5f, f); }
569{
return d >= 0.0 ?
d + 0.5 :
d - 0.5; }
571{
return d >= 0.0f ?
d + 0.5f :
d - 0.5f; }
577template <
typename FP,
578 typename std::enable_if_t<std::is_floating_point_v<FP>,
bool> =
true>
581#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
582 if (!q20::is_constant_evaluated())
583 Q_ASSERT(!qIsNaN(value));
585 constexpr FP MinBound = FP((
std::numeric_limits<
int>::min)());
586 constexpr FP MaxBound = FP((
std::numeric_limits<
int>::max)());
587 const FP beforeTruncation =
QRoundImpl::qRound(value);
588 return int(qBound(MinBound, beforeTruncation, MaxBound));
604 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(d));
609 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(f));
614constexpr inline const T &
min(
const T &a,
const T &b) {
return (a < b) ? a : b; }
619 return (qAbs(p1 - p2) * 1000000000000. <=
QtPrivate::min(qAbs(p1), qAbs(p2)));
624 return (qAbs(p1 - p2) * 100000.f <=
QtPrivate::min(qAbs(p1), qAbs(p2)));
629 return qAbs(d) <= 0.000000000001;
634 return qAbs(f) <= 0.00001f;
638QT_WARNING_DISABLE_FLOAT_COMPARE
640[[nodiscard]]
constexpr bool qIsNull(
double d)
noexcept
645[[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