Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:31:30

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