Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:07:23

0001 // Copyright (C) 2022 The Qt Company Ltd.
0002 // Copyright (C) 2016 by Southwest Research Institute (R)
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 
0005 #ifndef QFLOAT16_H
0006 #define QFLOAT16_H
0007 
0008 #include <QtCore/qcompare.h>
0009 #include <QtCore/qglobal.h>
0010 #include <QtCore/qhashfunctions.h>
0011 #include <QtCore/qmath.h>
0012 #include <QtCore/qnamespace.h>
0013 #include <QtCore/qtconfigmacros.h>
0014 #include <QtCore/qtypes.h>
0015 
0016 #include <limits>
0017 #include <string.h>
0018 #include <type_traits>
0019 
0020 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__AVX2__) && !defined(__F16C__)
0021 // All processors that support AVX2 do support F16C too, so we could enable the
0022 // feature unconditionally if __AVX2__ is defined. However, all currently
0023 // supported compilers except Microsoft's are able to define __F16C__ on their
0024 // own when the user enables the feature, so we'll trust them.
0025 #  if defined(Q_CC_MSVC) && !defined(Q_CC_CLANG)
0026 #    define __F16C__        1
0027 #  endif
0028 #endif
0029 
0030 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
0031 #include <immintrin.h>
0032 #endif
0033 
0034 QT_BEGIN_NAMESPACE
0035 
0036 #if 0
0037 #pragma qt_class(QFloat16)
0038 #pragma qt_no_master_include
0039 #endif
0040 
0041 #ifndef QT_NO_DATASTREAM
0042 class QDataStream;
0043 #endif
0044 class QTextStream;
0045 
0046 class qfloat16
0047 {
0048     struct Wrap
0049     {
0050         // To let our private constructor work, without other code seeing
0051         // ambiguity when constructing from int, double &c.
0052         quint16 b16;
0053         constexpr inline explicit Wrap(int value) : b16(quint16(value)) {}
0054     };
0055 
0056 #ifdef QT_SUPPORTS_INT128
0057     template <typename T>
0058     using IsIntegral = std::disjunction<std::is_integral<T>,
0059                                         std::is_same<std::remove_const_t<T>, qint128>,
0060                                         std::is_same<std::remove_const_t<T>, quint128>>;
0061 #else
0062     template <typename T>
0063     using IsIntegral = std::is_integral<T>;
0064 #endif
0065     template <typename T>
0066     using if_type_is_integral = std::enable_if_t<IsIntegral<std::remove_reference_t<T>>::value,
0067                                                  bool>;
0068 
0069 public:
0070     using NativeType = QtPrivate::NativeFloat16Type;
0071 
0072     static constexpr bool IsNative = QFLOAT16_IS_NATIVE;
0073     using NearestFloat = std::conditional_t<IsNative, NativeType, float>;
0074 
0075     constexpr inline qfloat16() noexcept : b16(0) {}
0076     explicit qfloat16(Qt::Initialization) noexcept { }
0077 
0078 #if QFLOAT16_IS_NATIVE
0079     constexpr inline qfloat16(NativeType f) : nf(f) {}
0080     constexpr operator NativeType() const noexcept { return nf; }
0081 #else
0082     inline qfloat16(float f) noexcept;
0083     inline operator float() const noexcept;
0084 #endif
0085     template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, NearestFloat>>>
0086     constexpr explicit qfloat16(T value) noexcept : qfloat16(NearestFloat(value)) {}
0087 
0088     // Support for qIs{Inf,NaN,Finite}:
0089     bool isInf() const noexcept { return (b16 & 0x7fff) == 0x7c00; }
0090     bool isNaN() const noexcept { return (b16 & 0x7fff) > 0x7c00; }
0091     bool isFinite() const noexcept { return (b16 & 0x7fff) < 0x7c00; }
0092     Q_CORE_EXPORT int fpClassify() const noexcept;
0093     // Can't specialize std::copysign() for qfloat16
0094     qfloat16 copySign(qfloat16 sign) const noexcept
0095     { return qfloat16(Wrap((sign.b16 & 0x8000) | (b16 & 0x7fff))); }
0096     // Support for std::numeric_limits<qfloat16>
0097 
0098 #ifdef __STDCPP_FLOAT16_T__
0099 private:
0100     using Bounds = std::numeric_limits<NativeType>;
0101 public:
0102     static constexpr qfloat16 _limit_epsilon()    noexcept { return Bounds::epsilon(); }
0103     static constexpr qfloat16 _limit_min()        noexcept { return Bounds::min(); }
0104     static constexpr qfloat16 _limit_denorm_min() noexcept { return Bounds::denorm_min(); }
0105     static constexpr qfloat16 _limit_max()        noexcept { return Bounds::max(); }
0106     static constexpr qfloat16 _limit_lowest()     noexcept { return Bounds::lowest(); }
0107     static constexpr qfloat16 _limit_infinity()   noexcept { return Bounds::infinity(); }
0108     static constexpr qfloat16 _limit_quiet_NaN()  noexcept { return Bounds::quiet_NaN(); }
0109 #if QT_CONFIG(signaling_nan)
0110     static constexpr qfloat16 _limit_signaling_NaN() noexcept { return Bounds::signaling_NaN(); }
0111 #endif
0112 #else
0113     static constexpr qfloat16 _limit_epsilon()    noexcept { return qfloat16(Wrap(0x1400)); }
0114     static constexpr qfloat16 _limit_min()        noexcept { return qfloat16(Wrap(0x400)); }
0115     static constexpr qfloat16 _limit_denorm_min() noexcept { return qfloat16(Wrap(1)); }
0116     static constexpr qfloat16 _limit_max()        noexcept { return qfloat16(Wrap(0x7bff)); }
0117     static constexpr qfloat16 _limit_lowest()     noexcept { return qfloat16(Wrap(0xfbff)); }
0118     static constexpr qfloat16 _limit_infinity()   noexcept { return qfloat16(Wrap(0x7c00)); }
0119     static constexpr qfloat16 _limit_quiet_NaN()  noexcept { return qfloat16(Wrap(0x7e00)); }
0120 #if QT_CONFIG(signaling_nan)
0121     static constexpr qfloat16 _limit_signaling_NaN() noexcept { return qfloat16(Wrap(0x7d00)); }
0122 #endif
0123 #endif
0124     inline constexpr bool isNormal() const noexcept
0125     { return (b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00; }
0126 private:
0127     // ABI note: Qt 6's qfloat16 began with just a quint16 member so it ended
0128     // up passed in general purpose registers in any function call taking
0129     // qfloat16 by value (it has trivial copy constructors). This means the
0130     // integer member in the anonymous union below must remain until a
0131     // binary-incompatible version of Qt. If you remove it, on platforms using
0132     // the System V ABI for C, the native type is passed in FP registers.
0133     union {
0134         quint16 b16;
0135 #if QFLOAT16_IS_NATIVE
0136         NativeType nf;
0137 #endif
0138     };
0139     constexpr inline explicit qfloat16(Wrap nibble) noexcept :
0140 #if QFLOAT16_IS_NATIVE && defined(__cpp_lib_bit_cast)
0141         nf(std::bit_cast<NativeType>(nibble.b16))
0142 #else
0143         b16(nibble.b16)
0144 #endif
0145     {}
0146 
0147     Q_CORE_EXPORT static const quint32 mantissatable[];
0148     Q_CORE_EXPORT static const quint32 exponenttable[];
0149     Q_CORE_EXPORT static const quint32 offsettable[];
0150     Q_CORE_EXPORT static const quint16 basetable[];
0151     Q_CORE_EXPORT static const quint16 shifttable[];
0152     Q_CORE_EXPORT static const quint32 roundtable[];
0153 
0154     friend bool qIsNull(qfloat16 f) noexcept;
0155 
0156     friend inline qfloat16 operator-(qfloat16 a) noexcept
0157     {
0158         qfloat16 f;
0159         f.b16 = a.b16 ^ quint16(0x8000);
0160         return f;
0161     }
0162 
0163     friend inline qfloat16 operator+(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<NearestFloat>(a) + static_cast<NearestFloat>(b)); }
0164     friend inline qfloat16 operator-(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<NearestFloat>(a) - static_cast<NearestFloat>(b)); }
0165     friend inline qfloat16 operator*(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<NearestFloat>(a) * static_cast<NearestFloat>(b)); }
0166     friend inline qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<NearestFloat>(a) / static_cast<NearestFloat>(b)); }
0167 
0168     friend size_t qHash(qfloat16 key, size_t seed = 0) noexcept
0169     { return qHash(float(key), seed); } // 6.4 algorithm, so keep using it; ### Qt 7: fix QTBUG-116077
0170 
0171 QT_WARNING_PUSH
0172 QT_WARNING_DISABLE_GCC("-Wfloat-conversion")
0173 
0174 #define QF16_MAKE_ARITH_OP_FP(FP, OP) \
0175     friend inline FP operator OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; } \
0176     friend inline FP operator OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); }
0177 #define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP) \
0178     friend inline qfloat16& operator OP_EQ(qfloat16& lhs, FP rhs) noexcept \
0179     { lhs = qfloat16(NearestFloat(static_cast<FP>(lhs) OP rhs)); return lhs; }
0180 #define QF16_MAKE_ARITH_OP(FP) \
0181     QF16_MAKE_ARITH_OP_FP(FP, +) \
0182     QF16_MAKE_ARITH_OP_FP(FP, -) \
0183     QF16_MAKE_ARITH_OP_FP(FP, *) \
0184     QF16_MAKE_ARITH_OP_FP(FP, /) \
0185     QF16_MAKE_ARITH_OP_EQ_FP(FP, +=, +) \
0186     QF16_MAKE_ARITH_OP_EQ_FP(FP, -=, -) \
0187     QF16_MAKE_ARITH_OP_EQ_FP(FP, *=, *) \
0188     QF16_MAKE_ARITH_OP_EQ_FP(FP, /=, /)
0189 
0190     QF16_MAKE_ARITH_OP(long double)
0191     QF16_MAKE_ARITH_OP(double)
0192     QF16_MAKE_ARITH_OP(float)
0193 #if QFLOAT16_IS_NATIVE
0194     QF16_MAKE_ARITH_OP(NativeType)
0195 #endif
0196 #undef QF16_MAKE_ARITH_OP
0197 #undef QF16_MAKE_ARITH_OP_FP
0198 
0199 #define QF16_MAKE_ARITH_OP_INT(OP) \
0200     friend inline double operator OP(qfloat16 lhs, int rhs) noexcept { return static_cast<double>(lhs) OP rhs; } \
0201     friend inline double operator OP(int lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<double>(rhs); }
0202 
0203     QF16_MAKE_ARITH_OP_INT(+)
0204     QF16_MAKE_ARITH_OP_INT(-)
0205     QF16_MAKE_ARITH_OP_INT(*)
0206     QF16_MAKE_ARITH_OP_INT(/)
0207 #undef QF16_MAKE_ARITH_OP_INT
0208 
0209 QT_WARNING_DISABLE_FLOAT_COMPARE
0210 
0211 #if QFLOAT16_IS_NATIVE
0212 #  define QF16_CONSTEXPR constexpr
0213 #  define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED_LITERAL_TYPE
0214 #else
0215 #  define QF16_CONSTEXPR
0216 #  define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED
0217 #endif
0218 
0219     friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, const qfloat16 &rhs) noexcept
0220     { return static_cast<NearestFloat>(lhs) == static_cast<NearestFloat>(rhs); }
0221     friend QF16_CONSTEXPR
0222     Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, const qfloat16 &rhs) noexcept
0223     { return Qt::compareThreeWay(static_cast<NearestFloat>(lhs), static_cast<NearestFloat>(rhs)); }
0224     QF16_PARTIALLY_ORDERED(qfloat16)
0225 
0226 #define QF16_MAKE_ORDER_OP_FP(FP) \
0227     friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, FP rhs) noexcept \
0228     { return static_cast<FP>(lhs) == rhs; } \
0229     friend QF16_CONSTEXPR \
0230     Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, FP rhs) noexcept \
0231     { return Qt::compareThreeWay(static_cast<FP>(lhs), rhs); } \
0232     QF16_PARTIALLY_ORDERED(qfloat16, FP)
0233 
0234     QF16_MAKE_ORDER_OP_FP(long double)
0235     QF16_MAKE_ORDER_OP_FP(double)
0236     QF16_MAKE_ORDER_OP_FP(float)
0237 #if QFLOAT16_IS_NATIVE
0238     QF16_MAKE_ORDER_OP_FP(qfloat16::NativeType)
0239 #endif
0240 #undef QF16_MAKE_ORDER_OP_FP
0241 
0242     template <typename T, if_type_is_integral<T> = true>
0243     friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, T rhs) noexcept
0244     { return static_cast<NearestFloat>(lhs) == static_cast<NearestFloat>(rhs); }
0245     template <typename T, if_type_is_integral<T> = true>
0246     friend QF16_CONSTEXPR Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, T rhs) noexcept
0247     { return Qt::compareThreeWay(static_cast<NearestFloat>(lhs), static_cast<NearestFloat>(rhs)); }
0248 
0249     QF16_PARTIALLY_ORDERED(qfloat16, qint8)
0250     QF16_PARTIALLY_ORDERED(qfloat16, quint8)
0251     QF16_PARTIALLY_ORDERED(qfloat16, qint16)
0252     QF16_PARTIALLY_ORDERED(qfloat16, quint16)
0253     QF16_PARTIALLY_ORDERED(qfloat16, qint32)
0254     QF16_PARTIALLY_ORDERED(qfloat16, quint32)
0255     QF16_PARTIALLY_ORDERED(qfloat16, long)
0256     QF16_PARTIALLY_ORDERED(qfloat16, unsigned long)
0257     QF16_PARTIALLY_ORDERED(qfloat16, qint64)
0258     QF16_PARTIALLY_ORDERED(qfloat16, quint64)
0259 #ifdef QT_SUPPORTS_INT128
0260     QF16_PARTIALLY_ORDERED(qfloat16, qint128)
0261     QF16_PARTIALLY_ORDERED(qfloat16, quint128)
0262 #endif
0263 
0264 #undef QF16_PARTIALLY_ORDERED
0265 #undef QF16_CONSTEXPR
0266 
0267 QT_WARNING_POP
0268 
0269 #ifndef QT_NO_DATASTREAM
0270     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, qfloat16 f);
0271     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &ds, qfloat16 &f);
0272 #endif
0273     friend Q_CORE_EXPORT QTextStream &operator<<(QTextStream &ts, qfloat16 f);
0274     friend Q_CORE_EXPORT QTextStream &operator>>(QTextStream &ts, qfloat16 &f);
0275 };
0276 
0277 Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE);
0278 
0279 Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *, const float *, qsizetype length) noexcept;
0280 Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length) noexcept;
0281 
0282 // Complement qnumeric.h:
0283 [[nodiscard]] inline bool qIsInf(qfloat16 f) noexcept { return f.isInf(); }
0284 [[nodiscard]] inline bool qIsNaN(qfloat16 f) noexcept { return f.isNaN(); }
0285 [[nodiscard]] inline bool qIsFinite(qfloat16 f) noexcept { return f.isFinite(); }
0286 [[nodiscard]] inline int qFpClassify(qfloat16 f) noexcept { return f.fpClassify(); }
0287 // [[nodiscard]] quint32 qFloatDistance(qfloat16 a, qfloat16 b);
0288 
0289 [[nodiscard]] inline qfloat16 qSqrt(qfloat16 f)
0290 {
0291 #if defined(__cpp_lib_extended_float) && defined(__STDCPP_FLOAT16_T__) && 0
0292     // https://wg21.link/p1467 - disabled until tested
0293     using namespace std;
0294     return sqrt(f);
0295 #elif QFLOAT16_IS_NATIVE && defined(__HAVE_FLOAT16) && __HAVE_FLOAT16
0296     // This C library (glibc) has sqrtf16().
0297     return sqrtf16(f);
0298 #else
0299     bool mathUpdatesErrno = true;
0300 #  if defined(__NO_MATH_ERRNO__) || defined(_M_FP_FAST)
0301     mathUpdatesErrno = false;
0302 #  elif defined(math_errhandling)
0303     mathUpdatesErrno = (math_errhandling & MATH_ERRNO);
0304 #  endif
0305 
0306     // We don't need to set errno to EDOM if (f >= 0 && f != -0 && !isnan(f))
0307     // (or if we don't care about errno in the first place). We can merge the
0308     // NaN check with by negating and inverting: !(0 > f), and leaving zero to
0309     // sqrtf().
0310     if (!mathUpdatesErrno || !(0 > f)) {
0311 #  if defined(__AVX512FP16__)
0312         __m128h v = _mm_set_sh(f);
0313         v = _mm_sqrt_sh(v, v);
0314         return _mm_cvtsh_h(v);
0315 #  endif
0316     }
0317 
0318     // WG14's N2601 does not provide a way to tell which types an
0319     // implementation supports, so we assume it doesn't and fall back to FP32
0320     float f32 = float(f);
0321     f32 = sqrtf(f32);
0322     return qfloat16::NearestFloat(f32);
0323 #endif
0324 }
0325 
0326 // The remainder of these utility functions complement qglobal.h
0327 [[nodiscard]] inline int qRound(qfloat16 d) noexcept
0328 { return qRound(static_cast<float>(d)); }
0329 
0330 [[nodiscard]] inline qint64 qRound64(qfloat16 d) noexcept
0331 { return qRound64(static_cast<float>(d)); }
0332 
0333 [[nodiscard]] inline bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
0334 {
0335     qfloat16::NearestFloat f1 = static_cast<qfloat16::NearestFloat>(p1);
0336     qfloat16::NearestFloat f2 = static_cast<qfloat16::NearestFloat>(p2);
0337     // The significand precision for IEEE754 half precision is
0338     // 11 bits (10 explicitly stored), or approximately 3 decimal
0339     // digits.  In selecting the fuzzy comparison factor of 102.5f
0340     // (that is, (2^10+1)/10) below, we effectively select a
0341     // window of about 1 (least significant) decimal digit about
0342     // which the two operands can vary and still return true.
0343     return (qAbs(f1 - f2) * 102.5f <= qMin(qAbs(f1), qAbs(f2)));
0344 }
0345 
0346 /*!
0347   \internal
0348 */
0349 [[nodiscard]] inline bool qFuzzyIsNull(qfloat16 f) noexcept
0350 {
0351     return qAbs(f) < 0.00976f; // 1/102.5 to 3 significant digits; see qFuzzyCompare()
0352 }
0353 
0354 [[nodiscard]] inline bool qIsNull(qfloat16 f) noexcept
0355 {
0356     return (f.b16 & static_cast<quint16>(0x7fff)) == 0;
0357 }
0358 
0359 inline int qIntCast(qfloat16 f) noexcept
0360 { return int(static_cast<qfloat16::NearestFloat>(f)); }
0361 
0362 #if !defined(Q_QDOC) && !QFLOAT16_IS_NATIVE
0363 QT_WARNING_PUSH
0364 QT_WARNING_DISABLE_CLANG("-Wc99-extensions")
0365 QT_WARNING_DISABLE_GCC("-Wold-style-cast")
0366 inline qfloat16::qfloat16(float f) noexcept
0367 {
0368 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
0369     __m128 packsingle = _mm_set_ss(f);
0370     __m128i packhalf = _mm_cvtps_ph(packsingle, 0);
0371     b16 = _mm_extract_epi16(packhalf, 0);
0372 #elif defined (__ARM_FP16_FORMAT_IEEE)
0373     __fp16 f16 = __fp16(f);
0374     memcpy(&b16, &f16, sizeof(quint16));
0375 #else
0376     quint32 u;
0377     memcpy(&u, &f, sizeof(quint32));
0378     const quint32 signAndExp = u >> 23;
0379     const quint16 base = basetable[signAndExp];
0380     const quint16 shift = shifttable[signAndExp];
0381     const quint32 round = roundtable[signAndExp];
0382     quint32 mantissa = (u & 0x007fffff);
0383     if ((signAndExp & 0xff) == 0xff) {
0384         if (mantissa) // keep nan from truncating to inf
0385             mantissa = qMax(1U << shift, mantissa);
0386     } else {
0387         // Round half to even. First round up by adding one in the most
0388         // significant bit we'll be discarding:
0389         mantissa += round;
0390         // If the last bit we'll be keeping is now set, but all later bits are
0391         // clear, we were at half and shouldn't have rounded up; decrement will
0392         // clear this last kept bit. Any later set bit hides the decrement.
0393         if (mantissa & (1 << shift))
0394             --mantissa;
0395     }
0396 
0397     // We use add as the mantissa may overflow causing
0398     // the exp part to shift exactly one value.
0399     b16 = quint16(base + (mantissa >> shift));
0400 #endif
0401 }
0402 QT_WARNING_POP
0403 
0404 inline qfloat16::operator float() const noexcept
0405 {
0406 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
0407     __m128i packhalf = _mm_cvtsi32_si128(b16);
0408     __m128 packsingle = _mm_cvtph_ps(packhalf);
0409     return _mm_cvtss_f32(packsingle);
0410 #elif defined (__ARM_FP16_FORMAT_IEEE)
0411     __fp16 f16;
0412     memcpy(&f16, &b16, sizeof(quint16));
0413     return float(f16);
0414 #else
0415     quint32 u = mantissatable[offsettable[b16 >> 10] + (b16 & 0x3ff)]
0416                 + exponenttable[b16 >> 10];
0417     float f;
0418     memcpy(&f, &u, sizeof(quint32));
0419     return f;
0420 #endif
0421 }
0422 #endif // Q_QDOC and non-native
0423 
0424 /*
0425   qHypot compatibility; see ../kernel/qmath.h
0426 */
0427 namespace QtPrivate {
0428 template <> struct QHypotType<qfloat16, qfloat16>
0429 {
0430     using type = qfloat16;
0431 };
0432 template <typename R> struct QHypotType<R, qfloat16>
0433 {
0434     using type = std::conditional_t<std::is_floating_point_v<R>, R, double>;
0435 };
0436 template <typename R> struct QHypotType<qfloat16, R> : QHypotType<R, qfloat16>
0437 {
0438 };
0439 }
0440 
0441 // Avoid passing qfloat16 to std::hypot(), while ensuring return types
0442 // consistent with the above:
0443 inline auto qHypot(qfloat16 x, qfloat16 y)
0444 {
0445 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__) || QFLOAT16_IS_NATIVE
0446     return QtPrivate::QHypotHelper<qfloat16>(x).add(y).result();
0447 #else
0448     return qfloat16(qHypot(float(x), float(y)));
0449 #endif
0450 }
0451 
0452 // in ../kernel/qmath.h
0453 template<typename F, typename ...Fs> auto qHypot(F first, Fs... rest);
0454 
0455 template <typename T> typename QtPrivate::QHypotType<T, qfloat16>::type
0456 qHypot(T x, qfloat16 y)
0457 {
0458     if constexpr (std::is_floating_point_v<T>)
0459         return qHypot(x, float(y));
0460     else
0461         return qHypot(qfloat16(x), y);
0462 }
0463 template <typename T> auto qHypot(qfloat16 x, T y)
0464 {
0465     return qHypot(y, x);
0466 }
0467 
0468 #if defined(__cpp_lib_hypot) && __cpp_lib_hypot >= 201603L // Expected to be true
0469 // If any are not qfloat16, convert each qfloat16 to float:
0470 /* (The following splits the some-but-not-all-qfloat16 cases up, using
0471    (X|Y|Z)&~(X&Y&Z) = X ? ~(Y&Z) : Y|Z = X&~(Y&Z) | ~X&Y | ~X&~Y&Z,
0472    into non-overlapping cases, to avoid ambiguity.) */
0473 template <typename Ty, typename Tz,
0474           typename std::enable_if<
0475               // Ty, Tz aren't both qfloat16:
0476               !(std::is_same_v<qfloat16, Ty> && std::is_same_v<qfloat16, Tz>), int>::type = 0>
0477 auto qHypot(qfloat16 x, Ty y, Tz z) { return qHypot(qfloat16::NearestFloat(x), y, z); }
0478 template <typename Tx, typename Tz,
0479           typename std::enable_if<
0480               // Tx isn't qfloat16:
0481               !std::is_same_v<qfloat16, Tx>, int>::type = 0>
0482 auto qHypot(Tx x, qfloat16 y, Tz z) { return qHypot(x, qfloat16::NearestFloat(y), z); }
0483 template <typename Tx, typename Ty,
0484           typename std::enable_if<
0485               // Neither Tx nor Ty is qfloat16:
0486               !std::is_same_v<qfloat16, Tx> && !std::is_same_v<qfloat16, Ty>, int>::type = 0>
0487 auto qHypot(Tx x, Ty y, qfloat16 z) { return qHypot(x, y, qfloat16::NearestFloat(z)); }
0488 
0489 // If all are qfloat16, stay with qfloat16 (albeit via float, if no native support):
0490 inline auto qHypot(qfloat16 x, qfloat16 y, qfloat16 z)
0491 {
0492 #if (defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)) || QFLOAT16_IS_NATIVE
0493     return QtPrivate::QHypotHelper<qfloat16>(x).add(y).add(z).result();
0494 #else
0495     return qfloat16(qHypot(float(x), float(y), float(z)));
0496 #endif
0497 }
0498 #endif // 3-arg std::hypot() is available
0499 
0500 QT_END_NAMESPACE
0501 
0502 namespace std {
0503 template<>
0504 class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float>
0505 {
0506 public:
0507     /*
0508       Treat quint16 b16 as if it were:
0509       uint S: 1; // b16 >> 15 (sign); can be set for zero
0510       uint E: 5; // (b16 >> 10) & 0x1f (offset exponent)
0511       uint M: 10; // b16 & 0x3ff (adjusted mantissa)
0512 
0513       for E == 0: magnitude is M / 2.^{24}
0514       for 0 < E < 31: magnitude is (1. + M / 2.^{10}) * 2.^{E - 15)
0515       for E == 31: not finite
0516      */
0517     static constexpr int digits = 11;
0518     static constexpr int min_exponent = -13;
0519     static constexpr int max_exponent = 16;
0520 
0521     static constexpr int digits10 = 3;
0522     static constexpr int max_digits10 = 5;
0523     static constexpr int min_exponent10 = -4;
0524     static constexpr int max_exponent10 = 4;
0525 
0526     static constexpr QT_PREPEND_NAMESPACE(qfloat16) epsilon()
0527     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_epsilon(); }
0528     static constexpr QT_PREPEND_NAMESPACE(qfloat16) (min)()
0529     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_min(); }
0530     static constexpr QT_PREPEND_NAMESPACE(qfloat16) denorm_min()
0531     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_denorm_min(); }
0532     static constexpr QT_PREPEND_NAMESPACE(qfloat16) (max)()
0533     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_max(); }
0534     static constexpr QT_PREPEND_NAMESPACE(qfloat16) lowest()
0535     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_lowest(); }
0536     static constexpr QT_PREPEND_NAMESPACE(qfloat16) infinity()
0537     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_infinity(); }
0538     static constexpr QT_PREPEND_NAMESPACE(qfloat16) quiet_NaN()
0539     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_quiet_NaN(); }
0540 #if QT_CONFIG(signaling_nan)
0541     static constexpr QT_PREPEND_NAMESPACE(qfloat16) signaling_NaN()
0542     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_signaling_NaN(); }
0543 #else
0544     static constexpr bool has_signaling_NaN = false;
0545 #endif
0546 };
0547 
0548 template<> class numeric_limits<const QT_PREPEND_NAMESPACE(qfloat16)>
0549     : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
0550 template<> class numeric_limits<volatile QT_PREPEND_NAMESPACE(qfloat16)>
0551     : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
0552 template<> class numeric_limits<const volatile QT_PREPEND_NAMESPACE(qfloat16)>
0553     : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
0554 
0555 // Adding overloads to std isn't allowed, so we can't extend this to support
0556 // for fpclassify(), isnormal() &c. (which, furthermore, are macros on MinGW).
0557 } // namespace std
0558 
0559 #endif // QFLOAT16_H