diff options
Diffstat (limited to 'src/corelib/global/qfloat16.h')
-rw-r--r-- | src/corelib/global/qfloat16.h | 288 |
1 files changed, 220 insertions, 68 deletions
diff --git a/src/corelib/global/qfloat16.h b/src/corelib/global/qfloat16.h index fc0d9e2702..30dd9a60af 100644 --- a/src/corelib/global/qfloat16.h +++ b/src/corelib/global/qfloat16.h @@ -5,11 +5,17 @@ #ifndef QFLOAT16_H #define QFLOAT16_H +#include <QtCore/qcompare.h> #include <QtCore/qglobal.h> -#include <QtCore/qmetatype.h> +#include <QtCore/qhashfunctions.h> +#include <QtCore/qmath.h> #include <QtCore/qnamespace.h> +#include <QtCore/qtconfigmacros.h> +#include <QtCore/qtypes.h> + #include <limits> #include <string.h> +#include <type_traits> #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__AVX2__) && !defined(__F16C__) // All processors that support AVX2 do support F16C too, so we could enable the @@ -35,6 +41,7 @@ QT_BEGIN_NAMESPACE #ifndef QT_NO_DATASTREAM class QDataStream; #endif +class QTextStream; class qfloat16 { @@ -45,11 +52,38 @@ class qfloat16 quint16 b16; constexpr inline explicit Wrap(int value) : b16(quint16(value)) {} }; + +#ifdef QT_SUPPORTS_INT128 + template <typename T> + using IsIntegral = std::disjunction<std::is_integral<T>, + std::is_same<std::remove_const_t<T>, qint128>, + std::is_same<std::remove_const_t<T>, quint128>>; +#else + template <typename T> + using IsIntegral = std::is_integral<T>; +#endif + template <typename T> + using if_type_is_integral = std::enable_if_t<IsIntegral<std::remove_reference_t<T>>::value, + bool>; + public: + using NativeType = QtPrivate::NativeFloat16Type; + + static constexpr bool IsNative = QFLOAT16_IS_NATIVE; + using NearestFloat = std::conditional_t<IsNative, NativeType, float>; + constexpr inline qfloat16() noexcept : b16(0) {} explicit qfloat16(Qt::Initialization) noexcept { } + +#if QFLOAT16_IS_NATIVE + constexpr inline qfloat16(NativeType f) : nf(f) {} + constexpr operator NativeType() const noexcept { return nf; } +#else inline qfloat16(float f) noexcept; inline operator float() const noexcept; +#endif + template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, NearestFloat>>> + constexpr explicit qfloat16(T value) noexcept : qfloat16(NearestFloat(value)) {} // Support for qIs{Inf,NaN,Finite}: bool isInf() const noexcept { return (b16 & 0x7fff) == 0x7c00; } @@ -60,6 +94,22 @@ public: qfloat16 copySign(qfloat16 sign) const noexcept { return qfloat16(Wrap((sign.b16 & 0x8000) | (b16 & 0x7fff))); } // Support for std::numeric_limits<qfloat16> + +#ifdef __STDCPP_FLOAT16_T__ +private: + using Bounds = std::numeric_limits<NativeType>; +public: + static constexpr qfloat16 _limit_epsilon() noexcept { return Bounds::epsilon(); } + static constexpr qfloat16 _limit_min() noexcept { return Bounds::min(); } + static constexpr qfloat16 _limit_denorm_min() noexcept { return Bounds::denorm_min(); } + static constexpr qfloat16 _limit_max() noexcept { return Bounds::max(); } + static constexpr qfloat16 _limit_lowest() noexcept { return Bounds::lowest(); } + static constexpr qfloat16 _limit_infinity() noexcept { return Bounds::infinity(); } + static constexpr qfloat16 _limit_quiet_NaN() noexcept { return Bounds::quiet_NaN(); } +#if QT_CONFIG(signaling_nan) + static constexpr qfloat16 _limit_signaling_NaN() noexcept { return Bounds::signaling_NaN(); } +#endif +#else static constexpr qfloat16 _limit_epsilon() noexcept { return qfloat16(Wrap(0x1400)); } static constexpr qfloat16 _limit_min() noexcept { return qfloat16(Wrap(0x400)); } static constexpr qfloat16 _limit_denorm_min() noexcept { return qfloat16(Wrap(1)); } @@ -70,11 +120,29 @@ public: #if QT_CONFIG(signaling_nan) static constexpr qfloat16 _limit_signaling_NaN() noexcept { return qfloat16(Wrap(0x7d00)); } #endif +#endif inline constexpr bool isNormal() const noexcept { return (b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00; } private: - quint16 b16; - constexpr inline explicit qfloat16(Wrap nibble) noexcept : b16(nibble.b16) {} + // ABI note: Qt 6's qfloat16 began with just a quint16 member so it ended + // up passed in general purpose registers in any function call taking + // qfloat16 by value (it has trivial copy constructors). This means the + // integer member in the anonymous union below must remain until a + // binary-incompatible version of Qt. If you remove it, on platforms using + // the System V ABI for C, the native type is passed in FP registers. + union { + quint16 b16; +#if QFLOAT16_IS_NATIVE + NativeType nf; +#endif + }; + constexpr inline explicit qfloat16(Wrap nibble) noexcept : +#if QFLOAT16_IS_NATIVE && defined(__cpp_lib_bit_cast) + nf(std::bit_cast<NativeType>(nibble.b16)) +#else + b16(nibble.b16) +#endif + {} Q_CORE_EXPORT static const quint32 mantissatable[]; Q_CORE_EXPORT static const quint32 exponenttable[]; @@ -92,17 +160,23 @@ private: return f; } - friend inline qfloat16 operator+(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) + static_cast<float>(b)); } - friend inline qfloat16 operator-(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) - static_cast<float>(b)); } - friend inline qfloat16 operator*(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) * static_cast<float>(b)); } - friend inline qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) / static_cast<float>(b)); } + friend inline qfloat16 operator+(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<NearestFloat>(a) + static_cast<NearestFloat>(b)); } + friend inline qfloat16 operator-(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<NearestFloat>(a) - static_cast<NearestFloat>(b)); } + friend inline qfloat16 operator*(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<NearestFloat>(a) * static_cast<NearestFloat>(b)); } + friend inline qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<NearestFloat>(a) / static_cast<NearestFloat>(b)); } + + friend size_t qHash(qfloat16 key, size_t seed = 0) noexcept + { return qHash(float(key), seed); } // 6.4 algorithm, so keep using it; ### Qt 7: fix QTBUG-116077 + +QT_WARNING_PUSH +QT_WARNING_DISABLE_GCC("-Wfloat-conversion") #define QF16_MAKE_ARITH_OP_FP(FP, OP) \ friend inline FP operator OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; } \ friend inline FP operator OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); } #define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP) \ friend inline qfloat16& operator OP_EQ(qfloat16& lhs, FP rhs) noexcept \ - { lhs = qfloat16(float(static_cast<FP>(lhs) OP rhs)); return lhs; } + { lhs = qfloat16(NearestFloat(static_cast<FP>(lhs) OP rhs)); return lhs; } #define QF16_MAKE_ARITH_OP(FP) \ QF16_MAKE_ARITH_OP_FP(FP, +) \ QF16_MAKE_ARITH_OP_FP(FP, -) \ @@ -116,6 +190,9 @@ private: QF16_MAKE_ARITH_OP(long double) QF16_MAKE_ARITH_OP(double) QF16_MAKE_ARITH_OP(float) +#if QFLOAT16_IS_NATIVE + QF16_MAKE_ARITH_OP(NativeType) +#endif #undef QF16_MAKE_ARITH_OP #undef QF16_MAKE_ARITH_OP_FP @@ -129,44 +206,63 @@ private: QF16_MAKE_ARITH_OP_INT(/) #undef QF16_MAKE_ARITH_OP_INT -QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE - friend inline bool operator>(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) > static_cast<float>(b); } - friend inline bool operator<(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) < static_cast<float>(b); } - friend inline bool operator>=(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) >= static_cast<float>(b); } - friend inline bool operator<=(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) <= static_cast<float>(b); } - friend inline bool operator==(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) == static_cast<float>(b); } - friend inline bool operator!=(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) != static_cast<float>(b); } - -#define QF16_MAKE_BOOL_OP_FP(FP, OP) \ - friend inline bool operator OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; } \ - friend inline bool operator OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); } -#define QF16_MAKE_BOOL_OP(FP) \ - QF16_MAKE_BOOL_OP_FP(FP, <) \ - QF16_MAKE_BOOL_OP_FP(FP, >) \ - QF16_MAKE_BOOL_OP_FP(FP, >=) \ - QF16_MAKE_BOOL_OP_FP(FP, <=) \ - QF16_MAKE_BOOL_OP_FP(FP, ==) \ - QF16_MAKE_BOOL_OP_FP(FP, !=) - - QF16_MAKE_BOOL_OP(long double) - QF16_MAKE_BOOL_OP(double) - QF16_MAKE_BOOL_OP(float) -#undef QF16_MAKE_BOOL_OP -#undef QF16_MAKE_BOOL_OP_FP - -#define QF16_MAKE_BOOL_OP_INT(OP) \ - friend inline bool operator OP(qfloat16 a, int b) noexcept { return static_cast<float>(a) OP static_cast<float>(b); } \ - friend inline bool operator OP(int a, qfloat16 b) noexcept { return static_cast<float>(a) OP static_cast<float>(b); } - - QF16_MAKE_BOOL_OP_INT(>) - QF16_MAKE_BOOL_OP_INT(<) - QF16_MAKE_BOOL_OP_INT(>=) - QF16_MAKE_BOOL_OP_INT(<=) - QF16_MAKE_BOOL_OP_INT(==) - QF16_MAKE_BOOL_OP_INT(!=) -#undef QF16_MAKE_BOOL_OP_INT +#if QFLOAT16_IS_NATIVE +# define QF16_CONSTEXPR constexpr +# define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED_LITERAL_TYPE +#else +# define QF16_CONSTEXPR +# define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED +#endif + + friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, const qfloat16 &rhs) noexcept + { return static_cast<NearestFloat>(lhs) == static_cast<NearestFloat>(rhs); } + friend QF16_CONSTEXPR + Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, const qfloat16 &rhs) noexcept + { return Qt::compareThreeWay(static_cast<NearestFloat>(lhs), static_cast<NearestFloat>(rhs)); } + QF16_PARTIALLY_ORDERED(qfloat16) + +#define QF16_MAKE_ORDER_OP_FP(FP) \ + friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, FP rhs) noexcept \ + { return static_cast<FP>(lhs) == rhs; } \ + friend QF16_CONSTEXPR \ + Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, FP rhs) noexcept \ + { return Qt::compareThreeWay(static_cast<FP>(lhs), rhs); } \ + QF16_PARTIALLY_ORDERED(qfloat16, FP) + + QF16_MAKE_ORDER_OP_FP(long double) + QF16_MAKE_ORDER_OP_FP(double) + QF16_MAKE_ORDER_OP_FP(float) +#if QFLOAT16_IS_NATIVE + QF16_MAKE_ORDER_OP_FP(qfloat16::NativeType) +#endif +#undef QF16_MAKE_ORDER_OP_FP + + template <typename T, if_type_is_integral<T> = true> + friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, T rhs) noexcept + { return static_cast<NearestFloat>(lhs) == static_cast<NearestFloat>(rhs); } + template <typename T, if_type_is_integral<T> = true> + friend QF16_CONSTEXPR Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, T rhs) noexcept + { return Qt::compareThreeWay(static_cast<NearestFloat>(lhs), static_cast<NearestFloat>(rhs)); } + + QF16_PARTIALLY_ORDERED(qfloat16, qint8) + QF16_PARTIALLY_ORDERED(qfloat16, quint8) + QF16_PARTIALLY_ORDERED(qfloat16, qint16) + QF16_PARTIALLY_ORDERED(qfloat16, quint16) + QF16_PARTIALLY_ORDERED(qfloat16, qint32) + QF16_PARTIALLY_ORDERED(qfloat16, quint32) + QF16_PARTIALLY_ORDERED(qfloat16, long) + QF16_PARTIALLY_ORDERED(qfloat16, unsigned long) + QF16_PARTIALLY_ORDERED(qfloat16, qint64) + QF16_PARTIALLY_ORDERED(qfloat16, quint64) +#ifdef QT_SUPPORTS_INT128 + QF16_PARTIALLY_ORDERED(qfloat16, qint128) + QF16_PARTIALLY_ORDERED(qfloat16, quint128) +#endif + +#undef QF16_PARTIALLY_ORDERED +#undef QF16_CONSTEXPR QT_WARNING_POP @@ -174,6 +270,8 @@ QT_WARNING_POP friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, qfloat16 f); friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &ds, qfloat16 &f); #endif + friend Q_CORE_EXPORT QTextStream &operator<<(QTextStream &ts, qfloat16 f); + friend Q_CORE_EXPORT QTextStream &operator>>(QTextStream &ts, qfloat16 &f); }; Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE); @@ -188,6 +286,43 @@ Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length [[nodiscard]] inline int qFpClassify(qfloat16 f) noexcept { return f.fpClassify(); } // [[nodiscard]] quint32 qFloatDistance(qfloat16 a, qfloat16 b); +[[nodiscard]] inline qfloat16 qSqrt(qfloat16 f) +{ +#if defined(__cpp_lib_extended_float) && defined(__STDCPP_FLOAT16_T__) && 0 + // https://wg21.link/p1467 - disabled until tested + using namespace std; + return sqrt(f); +#elif QFLOAT16_IS_NATIVE && defined(__HAVE_FLOAT16) && __HAVE_FLOAT16 + // This C library (glibc) has sqrtf16(). + return sqrtf16(f); +#else + bool mathUpdatesErrno = true; +# if defined(__NO_MATH_ERRNO__) || defined(_M_FP_FAST) + mathUpdatesErrno = false; +# elif defined(math_errhandling) + mathUpdatesErrno = (math_errhandling & MATH_ERRNO); +# endif + + // We don't need to set errno to EDOM if (f >= 0 && f != -0 && !isnan(f)) + // (or if we don't care about errno in the first place). We can merge the + // NaN check with by negating and inverting: !(0 > f), and leaving zero to + // sqrtf(). + if (!mathUpdatesErrno || !(0 > f)) { +# if defined(__AVX512FP16__) + __m128h v = _mm_set_sh(f); + v = _mm_sqrt_sh(v, v); + return _mm_cvtsh_h(v); +# endif + } + + // WG14's N2601 does not provide a way to tell which types an + // implementation supports, so we assume it doesn't and fall back to FP32 + float f32 = float(f); + f32 = sqrtf(f32); + return qfloat16::NearestFloat(f32); +#endif +} + // The remainder of these utility functions complement qglobal.h [[nodiscard]] inline int qRound(qfloat16 d) noexcept { return qRound(static_cast<float>(d)); } @@ -197,8 +332,8 @@ Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length [[nodiscard]] inline bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept { - float f1 = static_cast<float>(p1); - float f2 = static_cast<float>(p2); + qfloat16::NearestFloat f1 = static_cast<qfloat16::NearestFloat>(p1); + qfloat16::NearestFloat f2 = static_cast<qfloat16::NearestFloat>(p2); // The significand precision for IEEE754 half precision is // 11 bits (10 explicitly stored), or approximately 3 decimal // digits. In selecting the fuzzy comparison factor of 102.5f @@ -222,9 +357,9 @@ Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length } inline int qIntCast(qfloat16 f) noexcept -{ return int(static_cast<float>(f)); } +{ return int(static_cast<qfloat16::NearestFloat>(f)); } -#ifndef Q_QDOC +#if !defined(Q_QDOC) && !QFLOAT16_IS_NATIVE QT_WARNING_PUSH QT_WARNING_DISABLE_CLANG("-Wc99-extensions") QT_WARNING_DISABLE_GCC("-Wold-style-cast") @@ -284,33 +419,52 @@ inline qfloat16::operator float() const noexcept return f; #endif } -#endif +#endif // Q_QDOC and non-native /* qHypot compatibility; see ../kernel/qmath.h */ namespace QtPrivate { -template <typename R> -struct QHypotType<R, qfloat16> { using type = decltype(std::hypot(R(1), 1.0f)); }; -template <typename R> -struct QHypotType<qfloat16, R> { using type = decltype(std::hypot(1.0f, R(1))); }; -template <> struct QHypotType<qfloat16, qfloat16> { using type = qfloat16; }; +template <> struct QHypotType<qfloat16, qfloat16> +{ + using type = qfloat16; +}; +template <typename R> struct QHypotType<R, qfloat16> +{ + using type = std::conditional_t<std::is_floating_point_v<R>, R, double>; +}; +template <typename R> struct QHypotType<qfloat16, R> : QHypotType<R, qfloat16> +{ +}; } + // Avoid passing qfloat16 to std::hypot(), while ensuring return types // consistent with the above: -template<typename F, typename ...Fs> auto qHypot(F first, Fs... rest); -template <typename T, typename std::enable_if<!std::is_same<qfloat16, T>::value, int>::type = 0> -auto qHypot(T x, qfloat16 y) { return qHypot(x, float(y)); } -template <typename T, typename std::enable_if<!std::is_same<qfloat16, T>::value, int>::type = 0> -auto qHypot(qfloat16 x, T y) { return qHypot(float(x), y); } -template <> inline auto qHypot(qfloat16 x, qfloat16 y) +inline auto qHypot(qfloat16 x, qfloat16 y) { -#if (defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)) || defined (__ARM_FP16_FORMAT_IEEE) +#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__) || QFLOAT16_IS_NATIVE return QtPrivate::QHypotHelper<qfloat16>(x).add(y).result(); #else return qfloat16(qHypot(float(x), float(y))); #endif } + +// in ../kernel/qmath.h +template<typename F, typename ...Fs> auto qHypot(F first, Fs... rest); + +template <typename T> typename QtPrivate::QHypotType<T, qfloat16>::type +qHypot(T x, qfloat16 y) +{ + if constexpr (std::is_floating_point_v<T>) + return qHypot(x, float(y)); + else + return qHypot(qfloat16(x), y); +} +template <typename T> auto qHypot(qfloat16 x, T y) +{ + return qHypot(y, x); +} + #if defined(__cpp_lib_hypot) && __cpp_lib_hypot >= 201603L // Expected to be true // If any are not qfloat16, convert each qfloat16 to float: /* (The following splits the some-but-not-all-qfloat16 cases up, using @@ -320,22 +474,22 @@ template <typename Ty, typename Tz, typename std::enable_if< // Ty, Tz aren't both qfloat16: !(std::is_same_v<qfloat16, Ty> && std::is_same_v<qfloat16, Tz>), int>::type = 0> -auto qHypot(qfloat16 x, Ty y, Tz z) { return qHypot(float(x), y, z); } +auto qHypot(qfloat16 x, Ty y, Tz z) { return qHypot(qfloat16::NearestFloat(x), y, z); } template <typename Tx, typename Tz, typename std::enable_if< // Tx isn't qfloat16: !std::is_same_v<qfloat16, Tx>, int>::type = 0> -auto qHypot(Tx x, qfloat16 y, Tz z) { return qHypot(x, float(y), z); } +auto qHypot(Tx x, qfloat16 y, Tz z) { return qHypot(x, qfloat16::NearestFloat(y), z); } template <typename Tx, typename Ty, typename std::enable_if< // Neither Tx nor Ty is qfloat16: !std::is_same_v<qfloat16, Tx> && !std::is_same_v<qfloat16, Ty>, int>::type = 0> -auto qHypot(Tx x, Ty y, qfloat16 z) { return qHypot(x, y, float(z)); } +auto qHypot(Tx x, Ty y, qfloat16 z) { return qHypot(x, y, qfloat16::NearestFloat(z)); } + // If all are qfloat16, stay with qfloat16 (albeit via float, if no native support): -template <> inline auto qHypot(qfloat16 x, qfloat16 y, qfloat16 z) { -#if (defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)) || defined (__ARM_FP16_FORMAT_IEEE) +#if (defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)) || QFLOAT16_IS_NATIVE return QtPrivate::QHypotHelper<qfloat16>(x).add(y).add(z).result(); #else return qfloat16(qHypot(float(x), float(y), float(z))); @@ -345,8 +499,6 @@ inline auto qHypot(qfloat16 x, qfloat16 y, qfloat16 z) QT_END_NAMESPACE -QT_DECL_METATYPE_EXTERN(qfloat16, Q_CORE_EXPORT) - namespace std { template<> class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float> |