Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-05 09:27:31

0001 // Copyright (C) 2021 The Qt Company Ltd.
0002 // Copyright (C) 2025 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 // Qt-Security score:critical reason:data-parser
0005 
0006 #ifndef QNUMERIC_H
0007 #define QNUMERIC_H
0008 
0009 #if 0
0010 #pragma qt_class(QtNumeric)
0011 #endif
0012 
0013 #include <QtCore/qassert.h>
0014 #include <QtCore/qminmax.h>
0015 #include <QtCore/qtconfigmacros.h>
0016 #include <QtCore/qtcoreexports.h>
0017 #include <QtCore/qtypes.h>
0018 
0019 #include <cmath>
0020 #include <limits>
0021 #include <QtCore/q20type_traits.h>
0022 
0023 // min() and max() may be #defined by windows.h if that is included before, but we need them
0024 // for std::numeric_limits below. You should not use the min() and max() macros, so we just #undef.
0025 #ifdef min
0026 #  undef min
0027 #  undef max
0028 #endif
0029 
0030 //
0031 // SIMDe (SIMD Everywhere) can't be used if intrin.h has been included as many definitions
0032 // conflict.   Defining Q_NUMERIC_NO_INTRINSICS allows SIMDe users to use Qt, at the cost of
0033 // falling back to the prior implementations of qMulOverflow and qAddOverflow.
0034 //
0035 #if defined(Q_CC_MSVC) && !defined(Q_NUMERIC_NO_INTRINSICS)
0036 #  include <intrin.h>
0037 #  include <float.h>
0038 #  if defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_X86_64)
0039 #    define Q_HAVE_ADDCARRY
0040 #  endif
0041 #  if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64)
0042 #    define Q_INTRINSIC_MUL_OVERFLOW64
0043 #    define Q_UMULH(v1, v2) __umulh(v1, v2)
0044 #    define Q_SMULH(v1, v2) __mulh(v1, v2)
0045 #    pragma intrinsic(__umulh)
0046 #    pragma intrinsic(__mulh)
0047 #  endif
0048 #endif
0049 
0050 QT_BEGIN_NAMESPACE
0051 
0052 // To match std::is{inf,nan,finite} functions:
0053 template <typename T>
0054 constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
0055 qIsInf(T) { return false; }
0056 template <typename T>
0057 constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
0058 qIsNaN(T) { return false; }
0059 template <typename T>
0060 constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
0061 qIsFinite(T) { return true; }
0062 
0063 // Floating-point types (see qfloat16.h for its overloads).
0064 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(double d);
0065 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(double d);
0066 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(double d);
0067 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(double val);
0068 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(float f);
0069 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(float f);
0070 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(float f);
0071 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(float val);
0072 
0073 #if QT_CONFIG(signaling_nan)
0074 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qSNaN();
0075 #endif
0076 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN();
0077 Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf();
0078 
0079 Q_CORE_EXPORT quint32 qFloatDistance(float a, float b);
0080 Q_CORE_EXPORT quint64 qFloatDistance(double a, double b);
0081 
0082 #define Q_INFINITY (QT_PREPEND_NAMESPACE(qInf)())
0083 #if QT_CONFIG(signaling_nan)
0084 #  define Q_SNAN (QT_PREPEND_NAMESPACE(qSNaN)())
0085 #endif
0086 #define Q_QNAN (QT_PREPEND_NAMESPACE(qQNaN)())
0087 
0088 // Overflow math.
0089 // This provides efficient implementations for int, unsigned, qsizetype and
0090 // size_t. Implementations for 8- and 16-bit types will work but may not be as
0091 // efficient. Implementations for 64-bit may be missing on 32-bit platforms.
0092 
0093 // All the GCC and Clang versions we support have constexpr
0094 // builtins for overflowing arithmetic.
0095 #if defined(Q_CC_GNU_ONLY) \
0096     || defined(Q_CC_CLANG_ONLY) \
0097     || __has_builtin(__builtin_add_overflow)
0098 # define Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
0099 // On 32-bit, Clang < 14 will fail to link if multiplying 64-bit
0100 // quantities (emits an unresolved call to __mulodi4), so we can't use
0101 // the builtin in that case.
0102 # if !(QT_POINTER_SIZE == 4 && defined(Q_CC_CLANG_ONLY) && Q_CC_CLANG_ONLY < 1400)
0103 #  define Q_INTRINSIC_MUL_OVERFLOW64
0104 # endif
0105 #endif
0106 
0107 namespace QtPrivate {
0108 // Generic versions of (some) overflowing math functions, private API.
0109 template <typename T>
0110 constexpr inline
0111 typename std::enable_if_t<std::is_unsigned_v<T>, bool>
0112 qAddOverflowGeneric(T v1, T v2, T *r)
0113 {
0114     // unsigned additions are well-defined
0115     *r = v1 + v2;
0116     return v1 > T(v1 + v2);
0117 }
0118 
0119 // Wide multiplication.
0120 // It has been isolated in its own function so that it can be tested.
0121 // Note that this implementation requires a T that doesn't undergo
0122 // promotions.
0123 template <typename T>
0124 constexpr inline
0125 typename std::enable_if_t<std::is_same_v<T, decltype(+T{})>, bool>
0126 qMulOverflowWideMultiplication(T v1, T v2, T *r)
0127 {
0128     // This is a glorified long/school-grade multiplication,
0129     // that considers each input of N bits as two halves of N/2 bits:
0130     //
0131     // v1 = 2^(N/2) * v1_hi + v1_lo
0132     // v2 = 2^(N/2) * v2_hi + v2_lo
0133     //
0134     // Therefore, v1*v2 = 2^N     * v1_hi * v2_hi +
0135     //                    2^(N/2) * v1_hi * v2_lo +
0136     //                    2^(N/2) * v1_lo * v2_hi +
0137     //                            * v1_lo * v2_lo
0138     //
0139     // Using the N bits of precision we have we can perform the hi*lo
0140     // multiplications safely; that is never going to overflow.
0141     //
0142     // Then we can sum together these partial results:
0143     //
0144     //                 [ v1_hi | v1_lo ] *
0145     //                 [ v2_hi | v2_lo ] =
0146     //                 -------------------
0147     //                 [ v1_lo * v2_lo ] +
0148     //         [ v1_hi * v2_lo ]         +  // shifted because it's * 2^(N/2)
0149     //         [ v2_hi * v1_lo ]         +  // shifted because it's * 2^(N/2)
0150     // [ v1_hi * v2_hi ]                 =  // shifted because it's * 2^N
0151     // -------------------------------
0152     // [     high     ][     low       ]    // exact result (in 2^(2N) bits)
0153     //
0154     // ... except that this way we'll need to bring some carries, so
0155     // we'll do a slightly smarter sum.
0156     //
0157     // We need high for detecting overflows, even if we are not returning it.
0158 
0159     // Get multiplication by zero out of the way
0160     if (v1 == 0 || v2 == 0) {
0161         *r = T(0);
0162         return false;
0163     }
0164 
0165     // Extract the absolute values as unsigned
0166     // (will fix the sign later)
0167     using U = std::make_unsigned_t<T>;
0168     const U v1_abs = (v1 >= 0) ? U(v1) : (U(0) - U(v1));
0169     const U v2_abs = (v2 >= 0) ? U(v2) : (U(0) - U(v2));
0170 
0171     // Masks for N/2 bits
0172     constexpr std::size_t half_width = (sizeof(U) * 8) / 2;
0173     const U half_mask = ~U(0) >> half_width;
0174 
0175     // Split in low and half quantities
0176     const U v1_lo = v1_abs & half_mask;
0177     const U v1_hi = v1_abs >> half_width;
0178     const U v2_lo = v2_abs & half_mask;
0179     const U v2_hi = v2_abs >> half_width;
0180 
0181     // Cross-product; this will never overflow
0182     const U lo_lo = v1_lo * v2_lo;
0183     const U lo_hi = v1_lo * v2_hi;
0184     const U hi_lo = v1_hi * v2_lo;
0185     const U hi_hi = v1_hi * v2_hi;
0186 
0187     // We could sum directly the cross-products, but then we'd have to
0188     // keep track of carries. This avoids it.
0189     const U tmp = (lo_lo >> half_width) + (hi_lo & half_mask) + lo_hi;
0190     U result_hi = (hi_lo >> half_width) + (tmp >> half_width) + hi_hi;
0191     U result_lo = (tmp << half_width) | (lo_lo & half_mask);
0192 
0193     if constexpr (std::is_unsigned_v<T>) {
0194         // If the source was unsigned, we're done; a non-zero high
0195         // signals overflow.
0196         *r = result_lo;
0197         return result_hi != U(0);
0198     } else {
0199         // We need to set the correct sign back, and check for overflow.
0200         const bool isNegative = (v1 < T(0)) != (v2 < T(0));
0201         if (isNegative) {
0202             // Result is negative; calculate two's complement of the
0203             // [high, low] pair, by inverting the bits and adding 1,
0204             // which is equivalent to negating it in unsigned
0205             // arithmetic.
0206             // This operation should be done on the pair as a whole,
0207             // but we have the individual components, so start by
0208             // calculating two's complement of low:
0209             result_lo = U(0) - result_lo;
0210 
0211             // If result_lo is 0, it means that the addition of 1 into
0212             // it has overflown, so now we have a carry to add into the
0213             // inverted high:
0214             result_hi = ~result_hi;
0215             if (result_lo == 0)
0216                 result_hi += U(1);
0217         }
0218 
0219         *r = result_lo;
0220         // Overflow has happened if result_hi is not a sign extension
0221         // of the sign bit of result_lo. Note the usage of T, not U.
0222         return result_hi != U(*r >> std::numeric_limits<T>::digits);
0223     }
0224 }
0225 
0226 template <typename T, typename Enable = void>
0227 constexpr inline bool HasLargerInt = false;
0228 template <typename T>
0229 constexpr inline bool HasLargerInt<T, std::void_t<typename QIntegerForSize<sizeof(T) * 2>::Unsigned>> = true;
0230 
0231 template <typename T>
0232 constexpr inline
0233 typename std::enable_if_t<(std::is_unsigned_v<T> || std::is_signed_v<T>), bool>
0234 qMulOverflowGeneric(T v1, T v2, T *r)
0235 {
0236     // This function is a generic fallback for qMulOverflow,
0237     // called either by constant or non-constant evaluation,
0238     // if the compiler does not have builtins or intrinsics itself.
0239     //
0240     // (For instance, this is never going to be called on GCC or recent
0241     // Clang, as their builtins will be used in all cases.)
0242     //
0243     // If a compiler does have builtins, please amend qMulOverflow
0244     // directly.
0245 
0246     if constexpr (HasLargerInt<T>) {
0247         // Use the next biggest type if available
0248         using LargerInt = QIntegerForSize<sizeof(T) * 2>;
0249         using Larger = typename std::conditional_t<std::is_signed_v<T>,
0250                 typename LargerInt::Signed, typename LargerInt::Unsigned>;
0251         Larger lr = Larger(v1) * Larger(v2);
0252         *r = T(lr);
0253         return lr > (std::numeric_limits<T>::max)() || lr < (std::numeric_limits<T>::min)();
0254     } else {
0255         // Otherwise fall back to a wide multiplication
0256         return qMulOverflowWideMultiplication(v1, v2, r);
0257     }
0258 }
0259 } // namespace QtPrivate
0260 
0261 template <typename T>
0262 constexpr inline
0263 typename std::enable_if_t<std::is_unsigned_v<T>, bool>
0264 qAddOverflow(T v1, T v2, T *r)
0265 {
0266     static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
0267 #if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
0268     return __builtin_add_overflow(v1, v2, r);
0269 #else
0270     if (q20::is_constant_evaluated())
0271         return QtPrivate::qAddOverflowGeneric(v1, v2, r);
0272 # if defined(Q_HAVE_ADDCARRY)
0273     // We can use intrinsics for the unsigned operations with MSVC
0274     if constexpr (std::is_same_v<T, unsigned>) {
0275         return _addcarry_u32(0, v1, v2, r);
0276     } else if constexpr (std::is_same_v<T, quint64>) {
0277 #    if defined(Q_PROCESSOR_X86_64)
0278         return _addcarry_u64(0, v1, v2, reinterpret_cast<unsigned __int64 *>(r));
0279 #    else
0280         uint low, high;
0281         uchar carry = _addcarry_u32(0, unsigned(v1), unsigned(v2), &low);
0282         carry = _addcarry_u32(carry, v1 >> 32, v2 >> 32, &high);
0283         *r = (quint64(high) << 32) | low;
0284         return carry;
0285 #    endif // defined(Q_PROCESSOR_X86_64)
0286     }
0287 # endif // defined(Q_HAVE_ADDCARRY)
0288     return QtPrivate::qAddOverflowGeneric(v1, v2, r);
0289 #endif // defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
0290 }
0291 
0292 template <typename T>
0293 constexpr inline
0294 typename std::enable_if_t<std::is_signed_v<T>, bool>
0295 qAddOverflow(T v1, T v2, T *r)
0296 {
0297     static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
0298 #if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
0299     return __builtin_add_overflow(v1, v2, r);
0300 #else
0301     // Here's how we calculate the overflow:
0302     // 1) unsigned addition is well-defined, so we can always execute it
0303     // 2) conversion from unsigned back to signed is implementation-
0304     //    defined and in the implementations we use, it's a no-op.
0305     // 3) signed integer overflow happens if the sign of the two input operands
0306     //    is the same but the sign of the result is different. In other words,
0307     //    the sign of the result must be the same as the sign of either
0308     //    operand.
0309 
0310     using U = typename std::make_unsigned_t<T>;
0311     *r = T(U(v1) + U(v2));
0312 
0313     // Two's complement equivalent (generates slightly shorter code):
0314     //  x ^ y             is negative if x and y have different signs
0315     //  x & y             is negative if x and y are negative
0316     // (x ^ z) & (y ^ z)  is negative if x and z have different signs
0317     //                    AND y and z have different signs
0318     return ((v1 ^ *r) & (v2 ^ *r)) < 0;
0319 #endif // defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
0320 }
0321 
0322 template <typename T>
0323 constexpr inline
0324 typename std::enable_if_t<std::is_unsigned_v<T>, bool>
0325 qSubOverflow(T v1, T v2, T *r)
0326 {
0327     static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
0328 #if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
0329     return __builtin_sub_overflow(v1, v2, r);
0330 #else
0331     // unsigned subtractions are well-defined
0332     *r = v1 - v2;
0333     return v1 < v2;
0334 #endif
0335 }
0336 
0337 template <typename T>
0338 constexpr inline
0339 typename std::enable_if_t<std::is_signed_v<T>, bool>
0340 qSubOverflow(T v1, T v2, T *r)
0341 {
0342     static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
0343 #if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
0344     return __builtin_sub_overflow(v1, v2, r);
0345 #else
0346     // See above for explanation. This is the same with some signs reversed.
0347     // We can't use qAddOverflow(v1, -v2, r) because it would be UB if
0348     // v2 == std::numeric_limits<T>::min().
0349 
0350     using U = typename std::make_unsigned_t<T>;
0351     *r = T(U(v1) - U(v2));
0352 
0353     return ((v1 ^ *r) & (~v2 ^ *r)) < 0;
0354 #endif // defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
0355 }
0356 
0357 template <typename T>
0358 constexpr inline
0359 typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool>
0360 qMulOverflow(T v1, T v2, T *r)
0361 {
0362     static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
0363 #if defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
0364 # if defined(Q_INTRINSIC_MUL_OVERFLOW64)
0365     return __builtin_mul_overflow(v1, v2, r);
0366 # else
0367     if constexpr (sizeof(T) <= 4)
0368         return __builtin_mul_overflow(v1, v2, r);
0369     else
0370         return QtPrivate::qMulOverflowGeneric(v1, v2, r);
0371 # endif
0372 #else
0373     if (q20::is_constant_evaluated())
0374         return QtPrivate::qMulOverflowGeneric(v1, v2, r);
0375 
0376 # if defined(Q_INTRINSIC_MUL_OVERFLOW64)
0377     if constexpr (std::is_unsigned_v<T> && (sizeof(T) == sizeof(quint64))) {
0378         // T is 64 bit; either unsigned long long,
0379         // or unsigned long on LP64 platforms.
0380         *r = v1 * v2;
0381         return T(Q_UMULH(v1, v2));
0382     } else if constexpr (std::is_signed_v<T> && (sizeof(T) == sizeof(qint64))) {
0383         // This is slightly more complex than the unsigned case above: the sign bit
0384         // of 'low' must be replicated as the entire 'high', so the only valid
0385         // values for 'high' are 0 and -1. Use unsigned multiply since it's the same
0386         // as signed for the low bits and use a signed right shift to verify that
0387         // 'high' is nothing but sign bits that match the sign of 'low'.
0388 
0389         qint64 high = Q_SMULH(v1, v2);
0390         *r = qint64(quint64(v1) * quint64(v2));
0391         return (*r >> 63) != high;
0392     }
0393 # endif // defined(Q_INTRINSIC_MUL_OVERFLOW64)
0394 
0395     return QtPrivate::qMulOverflowGeneric(v1, v2, r);
0396 #endif // defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
0397 }
0398 
0399 #undef Q_HAVE_ADDCARRY
0400 #undef Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
0401 
0402 // Implementations for addition, subtraction or multiplication by a
0403 // compile-time constant. For addition and subtraction, we simply call the code
0404 // that detects overflow at runtime. For multiplication, we compare to the
0405 // maximum possible values before multiplying to ensure no overflow happens.
0406 
0407 template <typename T, T V2> constexpr bool qAddOverflow(T v1, std::integral_constant<T, V2>, T *r)
0408 {
0409     return qAddOverflow(v1, V2, r);
0410 }
0411 
0412 template <auto V2, typename T> constexpr bool qAddOverflow(T v1, T *r)
0413 {
0414     return qAddOverflow(v1, std::integral_constant<T, V2>{}, r);
0415 }
0416 
0417 template <typename T, T V2> constexpr bool qSubOverflow(T v1, std::integral_constant<T, V2>, T *r)
0418 {
0419     return qSubOverflow(v1, V2, r);
0420 }
0421 
0422 template <auto V2, typename T> constexpr bool qSubOverflow(T v1, T *r)
0423 {
0424     return qSubOverflow(v1, std::integral_constant<T, V2>{}, r);
0425 }
0426 
0427 template <typename T, T V2> constexpr bool qMulOverflow(T v1, std::integral_constant<T, V2>, T *r)
0428 {
0429     static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
0430 
0431     // Runtime detection for anything smaller than or equal to a register
0432     // width, as most architectures' multiplication instructions actually
0433     // produce a result twice as wide as the input registers, allowing us to
0434     // efficiently detect the overflow.
0435     if constexpr (sizeof(T) <= sizeof(qregisteruint)) {
0436         return qMulOverflow(v1, V2, r);
0437 
0438 #ifdef Q_INTRINSIC_MUL_OVERFLOW64
0439     } else if constexpr (sizeof(T) <= sizeof(quint64)) {
0440         // If we have intrinsics detecting overflow of 64-bit multiplications,
0441         // then detect overflows through them up to 64 bits.
0442         return qMulOverflow(v1, V2, r);
0443 #endif
0444 
0445     } else if constexpr (V2 == 0 || V2 == 1) {
0446         // trivial cases (and simplify logic below due to division by zero)
0447         *r = v1 * V2;
0448         return false;
0449     } else if constexpr (V2 == -1) {
0450         // multiplication by -1 is valid *except* for signed minimum values
0451         // (necessary to avoid diving min() by -1, which is an overflow)
0452         if (v1 < 0 && v1 == (std::numeric_limits<T>::min)())
0453             return true;
0454         *r = -v1;
0455         return false;
0456     } else {
0457         // For 64-bit multiplications on 32-bit platforms, let's instead compare v1
0458         // against the bounds that would overflow.
0459         constexpr T Highest = (std::numeric_limits<T>::max)() / V2;
0460         constexpr T Lowest = (std::numeric_limits<T>::min)() / V2;
0461         if constexpr (Highest > Lowest) {
0462             if (v1 > Highest || v1 < Lowest)
0463                 return true;
0464         } else {
0465             // this can only happen if V2 < 0
0466             static_assert(V2 < 0);
0467             if (v1 > Lowest || v1 < Highest)
0468                 return true;
0469         }
0470 
0471         *r = v1 * V2;
0472         return false;
0473     }
0474 }
0475 
0476 template <auto V2, typename T> constexpr bool qMulOverflow(T v1, T *r)
0477 {
0478     if constexpr (V2 == 2)
0479         return qAddOverflow(v1, v1, r);
0480     return qMulOverflow(v1, std::integral_constant<T, V2>{}, r);
0481 }
0482 
0483 template <typename T>
0484 constexpr inline T qAbs(const T &t)
0485 {
0486     if constexpr (std::is_integral_v<T> && std::is_signed_v<T>)
0487         Q_ASSERT(t != std::numeric_limits<T>::min());
0488     return t >= 0 ? t : -t;
0489 }
0490 
0491 namespace QtPrivate {
0492 template <typename T,
0493           typename std::enable_if_t<std::is_integral_v<T>, bool> = true>
0494 constexpr inline auto qUnsignedAbs(T t)
0495 {
0496     using U = std::make_unsigned_t<T>;
0497     return (t >= 0) ? U(t) : U(~U(t) + U(1));
0498 }
0499 
0500 template <typename Result,
0501           typename FP,
0502           typename std::enable_if_t<std::is_integral_v<Result>, bool> = true,
0503           typename std::enable_if_t<std::is_floating_point_v<FP>, bool> = true>
0504 constexpr inline Result qCheckedFPConversionToInteger(FP value)
0505 {
0506 #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
0507     if (!q20::is_constant_evaluated())
0508         Q_ASSERT(!std::isnan(value));
0509 #endif
0510 
0511     constexpr Result minimal = (std::numeric_limits<Result>::min)();
0512     constexpr Result maximal = (std::numeric_limits<Result>::max)();
0513 
0514     // We want to check that `value > minimal-1`. `minimal` is
0515     // precisely representable as FP (it's -2^N), but `minimal-1`
0516     // may not be. Just rearrange the terms:
0517     Q_ASSERT(value - FP(minimal) > FP(-1));
0518 
0519     // Symmetrically, `maximal` may not have a precise
0520     // representation, but `maximal+1` has, so calculate that:
0521     constexpr FP maximalPlusOne = FP(2) * (maximal / 2 + 1);
0522     // And check that we're below that:
0523     Q_ASSERT(value < maximalPlusOne);
0524 
0525     // If both checks passed, the result of truncation is representable
0526     // as `Result`:
0527     return Result(value);
0528 }
0529 
0530 namespace QRoundImpl {
0531 // gcc < 10 doesn't have __has_builtin
0532 #if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG)
0533 // ARM64 has a single instruction that can do C++ rounding with conversion to integer.
0534 // Note current clang versions have non-constexpr __builtin_round, ### allow clang this path when they fix it.
0535 constexpr inline double qRound(double d)
0536 { return __builtin_round(d); }
0537 constexpr inline float qRound(float f)
0538 { return __builtin_roundf(f); }
0539 #elif defined(__SSE2__) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU))
0540 // SSE has binary operations directly on floating point making copysign fast
0541 constexpr inline double qRound(double d)
0542 { return d + __builtin_copysign(0.5, d); }
0543 constexpr inline float qRound(float f)
0544 { return f + __builtin_copysignf(0.5f, f); }
0545 #else
0546 constexpr inline double qRound(double d)
0547 { return d >= 0.0 ? d + 0.5 : d - 0.5; }
0548 constexpr inline float qRound(float d)
0549 { return d >= 0.0f ? d + 0.5f : d - 0.5f; }
0550 #endif
0551 } // namespace QRoundImpl
0552 
0553 // Like qRound, but have well-defined saturating behavior.
0554 // NaN is not handled.
0555 template <typename FP,
0556           typename std::enable_if_t<std::is_floating_point_v<FP>, bool> = true>
0557 constexpr inline int qSaturateRound(FP value)
0558 {
0559 #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
0560     if (!q20::is_constant_evaluated())
0561         Q_ASSERT(!qIsNaN(value));
0562 #endif
0563     constexpr FP MinBound = FP((std::numeric_limits<int>::min)());
0564     constexpr FP MaxBound = FP((std::numeric_limits<int>::max)());
0565     const FP beforeTruncation = QRoundImpl::qRound(value);
0566     return int(qBound(MinBound, beforeTruncation, MaxBound));
0567 }
0568 } // namespace QtPrivate
0569 
0570 constexpr inline int qRound(double d)
0571 {
0572     return QtPrivate::qCheckedFPConversionToInteger<int>(QtPrivate::QRoundImpl::qRound(d));
0573 }
0574 
0575 constexpr inline int qRound(float f)
0576 {
0577     return QtPrivate::qCheckedFPConversionToInteger<int>(QtPrivate::QRoundImpl::qRound(f));
0578 }
0579 
0580 constexpr inline qint64 qRound64(double d)
0581 {
0582     return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(d));
0583 }
0584 
0585 constexpr inline qint64 qRound64(float f)
0586 {
0587     return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(f));
0588 }
0589 
0590 namespace QtPrivate {
0591 template <typename T>
0592 constexpr inline const T &min(const T &a, const T &b) { return (a < b) ? a : b; }
0593 }
0594 
0595 [[nodiscard]] constexpr bool qFuzzyCompare(double p1, double p2) noexcept
0596 {
0597     return (qAbs(p1 - p2) * 1000000000000. <= QtPrivate::min(qAbs(p1), qAbs(p2)));
0598 }
0599 
0600 [[nodiscard]] constexpr bool qFuzzyCompare(float p1, float p2) noexcept
0601 {
0602     return (qAbs(p1 - p2) * 100000.f <= QtPrivate::min(qAbs(p1), qAbs(p2)));
0603 }
0604 
0605 [[nodiscard]] constexpr bool qFuzzyIsNull(double d) noexcept
0606 {
0607     return qAbs(d) <= 0.000000000001;
0608 }
0609 
0610 [[nodiscard]] constexpr bool qFuzzyIsNull(float f) noexcept
0611 {
0612     return qAbs(f) <= 0.00001f;
0613 }
0614 
0615 QT_WARNING_PUSH
0616 QT_WARNING_DISABLE_FLOAT_COMPARE
0617 
0618 [[nodiscard]] constexpr bool qIsNull(double d) noexcept
0619 {
0620     return d == 0.0;
0621 }
0622 
0623 [[nodiscard]] constexpr bool qIsNull(float f) noexcept
0624 {
0625     return f == 0.0f;
0626 }
0627 
0628 QT_WARNING_POP
0629 
0630 namespace QtPrivate {
0631 /*
0632     A version of qFuzzyCompare that works for all values (qFuzzyCompare()
0633     requires that neither argument is numerically 0).
0634 
0635     It's private because we need a fix for the many qFuzzyCompare() uses that
0636     ignore the precondition, even for older branches.
0637 
0638     See QTBUG-142020 for discussion of a longer-term solution.
0639 */
0640 template <typename T, typename S>
0641 [[nodiscard]] constexpr bool fuzzyCompare(const T &lhs, const S &rhs) noexcept
0642 {
0643     static_assert(noexcept(qIsNull(lhs) && qIsNull(rhs) && qFuzzyIsNull(lhs - rhs) && qFuzzyCompare(lhs, rhs)),
0644                   "The operations qIsNull(), qFuzzyIsNull() and qFuzzyCompare() must be noexcept "
0645                   "for both argument types!");
0646     return qIsNull(lhs) || qIsNull(rhs) ? qFuzzyIsNull(lhs - rhs) : qFuzzyCompare(lhs, rhs);
0647 }
0648 } // namespace QtPrivate
0649 
0650 
0651 inline int qIntCast(double f) { return int(f); }
0652 inline int qIntCast(float f) { return int(f); }
0653 
0654 QT_END_NAMESPACE
0655 
0656 #endif // QNUMERIC_H