diff options
-rw-r--r-- | src/corelib/global/qfloat16.h | 155 | ||||
-rw-r--r-- | src/corelib/kernel/qmath.h | 4 | ||||
-rw-r--r-- | tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp | 4 |
3 files changed, 123 insertions, 40 deletions
diff --git a/src/corelib/global/qfloat16.h b/src/corelib/global/qfloat16.h index f7e3df63aa..10dcfe0f8d 100644 --- a/src/corelib/global/qfloat16.h +++ b/src/corelib/global/qfloat16.h @@ -11,6 +11,11 @@ #include <limits> #include <string.h> +#if defined(__STDCPP_FLOAT16_T__) && __has_include(<stdfloat>) +// P1467 implementation - https://wg21.link/p1467 +# include <stdfloat> +#endif + #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__AVX2__) && !defined(__F16C__) // All processors that support AVX2 do support F16C too, so we could enable the // feature unconditionally if __AVX2__ is defined. However, all currently @@ -45,11 +50,41 @@ class qfloat16 quint16 b16; constexpr inline explicit Wrap(int value) : b16(quint16(value)) {} }; + public: +#if defined(__STDCPP_FLOAT16_T__) +# define QFLOAT16_IS_NATIVE 1 + using NativeType = std::float16_t; +#elif defined(Q_CC_CLANG) && defined(__FLT16_MAX__) && 0 + // disabled due to https://github.com/llvm/llvm-project/issues/56963 +# define QFLOAT16_IS_NATIVE 1 + using NativeType = decltype(__FLT16_MAX__); +#elif defined(Q_CC_GNU_ONLY) && defined(__FLT16_MAX__) +# define QFLOAT16_IS_NATIVE 1 +# ifdef __ARM_FP16_FORMAT_IEEE + using NativeType = __fp16; +# else + using NativeType = _Float16; +# endif +#else +# define QFLOAT16_IS_NATIVE 0 + using NativeType = void; +#endif + 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) : f(f) {} + constexpr operator NativeType() const noexcept { return f; } +#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>>> + explicit qfloat16(T value) noexcept : qfloat16(NearestFloat(value)) {} // Support for qIs{Inf,NaN,Finite}: bool isInf() const noexcept { return (b16 & 0x7fff) == 0x7c00; } @@ -73,7 +108,18 @@ public: inline constexpr bool isNormal() const noexcept { return (b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00; } private: - quint16 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 f; +#endif + }; constexpr inline explicit qfloat16(Wrap nibble) noexcept : b16(nibble.b16) {} Q_CORE_EXPORT static const quint32 mantissatable[]; @@ -92,17 +138,17 @@ 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)); } #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 +162,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 @@ -132,12 +181,12 @@ private: 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); } + friend inline bool operator>(qfloat16 a, qfloat16 b) noexcept { return static_cast<NearestFloat>(a) > static_cast<NearestFloat>(b); } + friend inline bool operator<(qfloat16 a, qfloat16 b) noexcept { return static_cast<NearestFloat>(a) < static_cast<NearestFloat>(b); } + friend inline bool operator>=(qfloat16 a, qfloat16 b) noexcept { return static_cast<NearestFloat>(a) >= static_cast<NearestFloat>(b); } + friend inline bool operator<=(qfloat16 a, qfloat16 b) noexcept { return static_cast<NearestFloat>(a) <= static_cast<NearestFloat>(b); } + friend inline bool operator==(qfloat16 a, qfloat16 b) noexcept { return static_cast<NearestFloat>(a) == static_cast<NearestFloat>(b); } + friend inline bool operator!=(qfloat16 a, qfloat16 b) noexcept { return static_cast<NearestFloat>(a) != static_cast<NearestFloat>(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; } \ @@ -157,8 +206,8 @@ QT_WARNING_DISABLE_FLOAT_COMPARE #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); } + friend inline bool operator OP(qfloat16 a, int b) noexcept { return static_cast<NearestFloat>(a) OP static_cast<NearestFloat>(b); } \ + friend inline bool operator OP(int a, qfloat16 b) noexcept { return static_cast<NearestFloat>(a) OP static_cast<NearestFloat>(b); } QF16_MAKE_BOOL_OP_INT(>) QF16_MAKE_BOOL_OP_INT(<) @@ -188,6 +237,21 @@ 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); +#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); +} + // The remainder of these utility functions complement qglobal.h [[nodiscard]] inline int qRound(qfloat16 d) noexcept { return qRound(static_cast<float>(d)); } @@ -197,8 +261,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 +286,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 +348,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 +403,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))); diff --git a/src/corelib/kernel/qmath.h b/src/corelib/kernel/qmath.h index fd39f46c1b..b1e5b4f17e 100644 --- a/src/corelib/kernel/qmath.h +++ b/src/corelib/kernel/qmath.h @@ -113,7 +113,7 @@ class QHypotHelper public: QHypotHelper(T first) : scale(qAbs(first)), total(1) {} T result() const - { return qIsFinite(scale) ? scale > 0 ? scale * T(std::sqrt(total)) : T(0) : scale; } + { return qIsFinite(scale) ? scale > 0 ? scale * T(qSqrt(total)) : T(0) : scale; } template<typename F, typename ...Fs> auto add(F first, Fs... rest) const @@ -133,7 +133,7 @@ public: return QHypotHelper<R>(scale, total); if (val > scale) { const R ratio = scale / next; - return QHypotHelper<R>(val, total * ratio * ratio + 1); + return QHypotHelper<R>(val, total * ratio * ratio + R(1)); } const R ratio = next / scale; return QHypotHelper<R>(scale, total + ratio * ratio); diff --git a/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp b/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp index e61ceff1c0..ffd44b1ff8 100644 --- a/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp +++ b/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp @@ -301,7 +301,7 @@ void tst_qfloat16::promotionTests() QCOMPARE(sizeof(double),sizeof(qfloat16(1.f)*1)); QCOMPARE(sizeof(double),sizeof(qfloat16(1.f)/1)); - QCOMPARE(QString::number(1.f),QString::number(qfloat16(1.f))); + QCOMPARE(QString::number(1.f),QString::number(double(qfloat16(1.f)))); } void tst_qfloat16::arithOps_data() @@ -624,7 +624,7 @@ void tst_qfloat16::mantissaOverflow() float f; memcpy(&f, &in, 4); - qfloat16 f16 = f; + qfloat16 f16 = qfloat16(f); qfloat16 f16s[1]; qFloatToFloat16(f16s, &f, 1); QCOMPARE(f16, f16s[0]); |