Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfloat16.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2016 by Southwest Research Institute (R)
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#ifndef QFLOAT16_H
7#define QFLOAT16_H
8
9#include <QtCore/qcompare.h>
10#include <QtCore/qglobal.h>
11#include <QtCore/qhashfunctions.h>
12#include <QtCore/qmath.h>
13#include <QtCore/qnamespace.h>
14#include <QtCore/qtconfigmacros.h>
15#include <QtCore/qtformat_impl.h>
16#include <QtCore/qtypes.h>
17
18#include <limits>
19#ifdef __cpp_lib_math_constants
20#include <numbers>
21#endif
22#include <string.h>
23#include <type_traits>
24
25#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__AVX2__) && !defined(__F16C__)
26// All processors that support AVX2 do support F16C too, so we could enable the
27// feature unconditionally if __AVX2__ is defined. However, all currently
28// supported compilers except Microsoft's are able to define __F16C__ on their
29// own when the user enables the feature, so we'll trust them.
30# if defined(Q_CC_MSVC) && !defined(Q_CC_CLANG)
31# define __F16C__ 1
32# endif
33#endif
34
35#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
36#include <immintrin.h>
37#endif
38
39class tst_qfloat16;
40
41QT_BEGIN_NAMESPACE
42
43#if 0
44#pragma qt_class(QFloat16)
45#pragma qt_no_master_include
46#endif
47
48#ifndef QT_NO_DATASTREAM
49class QDataStream;
50#endif
51class QTextStream;
52
53// These macros from math.h conflict with the real functions in the std namespace:
54#ifdef copysign
55# undef copysign
56#endif
57#ifdef signbit
58# undef signbit
59#endif
60
62{
63 friend class ::tst_qfloat16;
64
65#if QFLOAT16_IS_NATIVE
66# define QF16_CONSTEXPR constexpr
67# define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED_LITERAL_TYPE
68#else
69# define QF16_CONSTEXPR
70# define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED
71#endif
72
73 struct Wrap
74 {
75 // To let our private constructor work, without other code seeing
76 // ambiguity when constructing from int, double &c.
77 quint16 b16;
78 constexpr inline explicit Wrap(int value) : b16(quint16(value)) {}
79 };
80
81 template <typename T>
83
84public:
86
87 static constexpr bool IsNative = QFLOAT16_IS_NATIVE;
89
90 explicit qfloat16(Qt::Initialization) noexcept { }
91
92#if QFLOAT16_IS_NATIVE
93 constexpr inline qfloat16() noexcept : nf(0) {}
94 constexpr inline qfloat16(NativeType f) : nf(f) {}
95 constexpr operator NativeType() const noexcept { return nf; }
96#else
97 constexpr inline qfloat16() noexcept : b16(0) {}
98 inline qfloat16(float f) noexcept;
99 inline operator float() const noexcept;
100#endif
101 template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, NearestFloat>>>
102 constexpr explicit qfloat16(T value) noexcept : qfloat16(NearestFloat(value)) {}
103
104 // Support for qIs{Inf,NaN,Finite}:
105 bool isInf() const noexcept { return (b16 & 0x7fff) == 0x7c00; }
106 bool isNaN() const noexcept { return (b16 & 0x7fff) > 0x7c00; }
107 bool isFinite() const noexcept { return (b16 & 0x7fff) < 0x7c00; }
108 Q_CORE_EXPORT int fpClassify() const noexcept;
109 // Can't specialize std::copysign() for qfloat16
110 qfloat16 copySign(qfloat16 sign) const noexcept
111 { return copysign(*this, sign); }
112 friend qfloat16 copysign(qfloat16 mag, qfloat16 sign) noexcept
113 { return qfloat16(Wrap((sign.b16 & 0x8000) | (mag.b16 & 0x7fff))); }
114 // Can't specialize std::signbit() for qfloat16
115 friend bool signbit(qfloat16 x) noexcept
116 { return x.b16 & 0x8000; }
117
118 // Support for std::numeric_limits<qfloat16>
119#ifdef __STDCPP_FLOAT16_T__
120private:
122public:
123 static constexpr qfloat16 _limit_epsilon() noexcept { return Bounds::epsilon(); }
124 static constexpr qfloat16 _limit_min() noexcept { return Bounds::min(); }
125 static constexpr qfloat16 _limit_denorm_min() noexcept { return Bounds::denorm_min(); }
126 static constexpr qfloat16 _limit_max() noexcept { return Bounds::max(); }
127 static constexpr qfloat16 _limit_lowest() noexcept { return Bounds::lowest(); }
128 static constexpr qfloat16 _limit_infinity() noexcept { return Bounds::infinity(); }
129 static constexpr qfloat16 _limit_quiet_NaN() noexcept { return Bounds::quiet_NaN(); }
130#if QT_CONFIG(signaling_nan)
131 static constexpr qfloat16 _limit_signaling_NaN() noexcept { return Bounds::signaling_NaN(); }
132#endif
133#else
134 static constexpr qfloat16 _limit_epsilon() noexcept { return qfloat16(Wrap(0x1400)); }
135 static constexpr qfloat16 _limit_min() noexcept { return qfloat16(Wrap(0x400)); }
136 static constexpr qfloat16 _limit_denorm_min() noexcept { return qfloat16(Wrap(1)); }
137 static constexpr qfloat16 _limit_max() noexcept { return qfloat16(Wrap(0x7bff)); }
138 static constexpr qfloat16 _limit_lowest() noexcept { return qfloat16(Wrap(0xfbff)); }
139 static constexpr qfloat16 _limit_infinity() noexcept { return qfloat16(Wrap(0x7c00)); }
140 static constexpr qfloat16 _limit_quiet_NaN() noexcept { return qfloat16(Wrap(0x7e00)); }
141#if QT_CONFIG(signaling_nan)
142 static constexpr qfloat16 _limit_signaling_NaN() noexcept { return qfloat16(Wrap(0x7d00)); }
143#endif
144#endif // __STDCPP_FLOAT16_T__
145 // support for C++20 <numbers>
146#define QF16_NUM(which, val)
147 static constexpr qfloat16 _num_ ## which () noexcept
148 { return qfloat16(Wrap(val)); }
149 QF16_NUM(e, 0x4170)
150 QF16_NUM(log2e, 0x3dc5)
151 QF16_NUM(log10e, 0x36f3)
152 QF16_NUM(pi, 0x4248)
153 QF16_NUM(inv_pi, 0x3518)
154 QF16_NUM(inv_sqrtpi, 0x3883)
155 QF16_NUM(ln2, 0x398c)
156 QF16_NUM(ln10, 0x409b)
157 QF16_NUM(sqrt2, 0x3da8)
158 QF16_NUM(sqrt3, 0x3eee)
159 QF16_NUM(inv_sqrt3, 0x389e)
160 QF16_NUM(egamma, 0x389e)
161 QF16_NUM(phi, 0x3e79)
162#undef QF16_NUM
163 inline constexpr bool isNormal() const noexcept
164 { return (b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00; }
165
167 { qfloat16 tmp = *this; ++(*this); return tmp; }
169 { return *this += NearestFloat(1); }
171 { qfloat16 tmp = *this; --(*this); return tmp; }
173 { return *this -= NearestFloat(1); }
174
175private:
176 // ABI note: Qt 6's qfloat16 began with just a quint16 member so it ended
177 // up passed in general purpose registers in any function call taking
178 // qfloat16 by value (it has trivial copy constructors). This means the
179 // integer member in the anonymous union below must remain until a
180 // binary-incompatible version of Qt. If you remove it, on platforms using
181 // the System V ABI for C, the native type is passed in FP registers.
182 union {
183 quint16 b16;
184#if QFLOAT16_IS_NATIVE
185 NativeType nf;
186#endif
187 };
188 constexpr inline explicit qfloat16(Wrap nibble) noexcept :
189#if QFLOAT16_IS_NATIVE && defined(__cpp_lib_bit_cast)
190 nf(std::bit_cast<NativeType>(nibble.b16))
191#else
192 b16(nibble.b16)
193#endif
194 {}
195
196 Q_CORE_EXPORT static const quint32 mantissatable[];
197 Q_CORE_EXPORT static const quint32 exponenttable[];
198 Q_CORE_EXPORT static const quint32 offsettable[];
199 Q_CORE_EXPORT static const quint16 basetable[];
200 Q_CORE_EXPORT static const quint16 shifttable[];
201 Q_CORE_EXPORT static const quint32 roundtable[];
202
203 friend bool qIsNull(qfloat16 f) noexcept;
204
205 friend constexpr inline qfloat16 operator+(qfloat16 a) noexcept { return a; }
206 friend constexpr inline qfloat16 operator-(qfloat16 a) noexcept
207 {
208#if QFLOAT16_IS_NATIVE
209 return -static_cast<NearestFloat>(a);
210#else
211 qfloat16 f;
212 f.b16 = a.b16 ^ quint16(0x8000);
213 return f;
214#endif
215 }
216
217 friend QF16_CONSTEXPR inline qfloat16 operator+(qfloat16 a, qfloat16 b) noexcept
218 { return qfloat16(static_cast<NearestFloat>(a) + static_cast<NearestFloat>(b)); }
219 friend QF16_CONSTEXPR inline qfloat16 operator-(qfloat16 a, qfloat16 b) noexcept
220 { return qfloat16(static_cast<NearestFloat>(a) - static_cast<NearestFloat>(b)); }
221 friend QF16_CONSTEXPR inline qfloat16 operator*(qfloat16 a, qfloat16 b) noexcept
222 { return qfloat16(static_cast<NearestFloat>(a) * static_cast<NearestFloat>(b)); }
223 friend QF16_CONSTEXPR inline qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept
224 { return qfloat16(static_cast<NearestFloat>(a) / static_cast<NearestFloat>(b)); }
225
226 friend size_t qHash(qfloat16 key, size_t seed = 0) noexcept
227 { return qHash(float(key), seed); } // 6.4 algorithm, so keep using it; ### Qt 7: fix QTBUG-116077
228
229QT_WARNING_PUSH
230QT_WARNING_DISABLE_GCC("-Wfloat-conversion")
231
232#define QF16_MAKE_ARITH_OP_FP(FP, OP)
233 friend QF16_CONSTEXPR inline FP operator OP(qfloat16 lhs, FP rhs) noexcept
234 { return static_cast<FP>(lhs) OP rhs; }
235 friend QF16_CONSTEXPR inline FP operator OP(FP lhs, qfloat16 rhs) noexcept
236 { return lhs OP static_cast<FP>(rhs); }
237#define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP)
238 friend QF16_CONSTEXPR inline qfloat16 &operator OP_EQ(qfloat16& lhs, FP rhs) noexcept
239 { lhs = qfloat16(NearestFloat(static_cast<FP>(lhs) OP rhs)); return lhs; }
240#define QF16_MAKE_ARITH_OP(FP)
241 QF16_MAKE_ARITH_OP_FP(FP, +)
242 QF16_MAKE_ARITH_OP_FP(FP, -)
243 QF16_MAKE_ARITH_OP_FP(FP, *)
244 QF16_MAKE_ARITH_OP_FP(FP, /)
245 QF16_MAKE_ARITH_OP_EQ_FP(FP, +=, +)
246 QF16_MAKE_ARITH_OP_EQ_FP(FP, -=, -)
247 QF16_MAKE_ARITH_OP_EQ_FP(FP, *=, *)
248 QF16_MAKE_ARITH_OP_EQ_FP(FP, /=, /)
249
250 QF16_MAKE_ARITH_OP(long double)
251 QF16_MAKE_ARITH_OP(double)
252 QF16_MAKE_ARITH_OP(float)
253#if QFLOAT16_IS_NATIVE
255#endif
256#undef QF16_MAKE_ARITH_OP
257#undef QF16_MAKE_ARITH_OP_FP
258
259#define QF16_MAKE_ARITH_OP_INT(OP)
260 friend QF16_CONSTEXPR inline double operator OP(qfloat16 lhs, int rhs) noexcept
261 { return static_cast<double>(lhs) OP rhs; }
262 friend QF16_CONSTEXPR inline double operator OP(int lhs, qfloat16 rhs) noexcept
263 { return lhs OP static_cast<double>(rhs); }
264
269#undef QF16_MAKE_ARITH_OP_INT
270
272
273 friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, const qfloat16 &rhs) noexcept
274 { return static_cast<NearestFloat>(lhs) == static_cast<NearestFloat>(rhs); }
275 friend QF16_CONSTEXPR
276 Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, const qfloat16 &rhs) noexcept
277 { return Qt::compareThreeWay(static_cast<NearestFloat>(lhs), static_cast<NearestFloat>(rhs)); }
279
280#define QF16_MAKE_ORDER_OP_FP(FP)
281 friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, FP rhs) noexcept
282 { return static_cast<FP>(lhs) == rhs; }
283 friend QF16_CONSTEXPR
284 Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, FP rhs) noexcept
285 { return Qt::compareThreeWay(static_cast<FP>(lhs), rhs); }
286 QF16_PARTIALLY_ORDERED(qfloat16, FP)
287
288 QF16_MAKE_ORDER_OP_FP(long double)
291#if QFLOAT16_IS_NATIVE
293#endif
294#undef QF16_MAKE_ORDER_OP_FP
295
296 template <typename T, if_type_is_integral<T> = true>
297 friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, T rhs) noexcept
298 { return static_cast<NearestFloat>(lhs) == static_cast<NearestFloat>(rhs); }
299 template <typename T, if_type_is_integral<T> = true>
301 { return Qt::compareThreeWay(static_cast<NearestFloat>(lhs), static_cast<NearestFloat>(rhs)); }
302
310 QF16_PARTIALLY_ORDERED(qfloat16, unsigned long)
313#ifdef QT_SUPPORTS_INT128
316#endif
317
318#undef QF16_PARTIALLY_ORDERED
319#undef QF16_CONSTEXPR
320
322
323#ifndef QT_NO_DATASTREAM
326#endif
329};
330
332
333Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *, const float *, qsizetype length) noexcept;
334Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length) noexcept;
335
336// Complement qnumeric.h:
337[[nodiscard]] inline bool qIsInf(qfloat16 f) noexcept { return f.isInf(); }
338[[nodiscard]] inline bool qIsNaN(qfloat16 f) noexcept { return f.isNaN(); }
339[[nodiscard]] inline bool qIsFinite(qfloat16 f) noexcept { return f.isFinite(); }
340[[nodiscard]] inline int qFpClassify(qfloat16 f) noexcept { return f.fpClassify(); }
341// [[nodiscard]] quint32 qFloatDistance(qfloat16 a, qfloat16 b);
342
343[[nodiscard]] inline qfloat16 qSqrt(qfloat16 f)
344{
345#if defined(__cpp_lib_extended_float) && defined(__STDCPP_FLOAT16_T__) && 0
346 // https://wg21.link/p1467 - disabled until tested
347 using namespace std;
348 return sqrt(f);
349#elif QFLOAT16_IS_NATIVE && defined(__HAVE_FLOAT16) && __HAVE_FLOAT16
350 // This C library (glibc) has sqrtf16().
351 return sqrtf16(f);
352#else
353 bool mathUpdatesErrno = true;
354# if defined(__NO_MATH_ERRNO__) || defined(_M_FP_FAST)
355 mathUpdatesErrno = false;
356# elif defined(math_errhandling)
357 mathUpdatesErrno = (math_errhandling & MATH_ERRNO);
358# endif
359
360 // We don't need to set errno to EDOM if (f >= 0 && f != -0 && !isnan(f))
361 // (or if we don't care about errno in the first place). We can merge the
362 // NaN check with by negating and inverting: !(0 > f), and leaving zero to
363 // sqrtf().
364 if (!mathUpdatesErrno || !(0 > f)) {
365# if defined(__AVX512FP16__)
366 __m128h v = _mm_set_sh(f);
367 v = _mm_sqrt_sh(v, v);
368 return _mm_cvtsh_h(v);
369# endif
370 }
371
372 // WG14's N2601 does not provide a way to tell which types an
373 // implementation supports, so we assume it doesn't and fall back to FP32
374 float f32 = float(f);
375 f32 = sqrtf(f32);
376 return qfloat16::NearestFloat(f32);
377#endif
378}
379
380// The remainder of these utility functions complement qglobal.h
381[[nodiscard]] inline int qRound(qfloat16 d)
382{ return qRound(static_cast<float>(d)); }
383
384[[nodiscard]] inline qint64 qRound64(qfloat16 d)
385{ return qRound64(static_cast<float>(d)); }
386
387[[nodiscard]] inline bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
388{
389 qfloat16::NearestFloat f1 = static_cast<qfloat16::NearestFloat>(p1);
390 qfloat16::NearestFloat f2 = static_cast<qfloat16::NearestFloat>(p2);
391 // The significand precision for IEEE754 half precision is
392 // 11 bits (10 explicitly stored), or approximately 3 decimal
393 // digits. In selecting the fuzzy comparison factor of 102.5f
394 // (that is, (2^10+1)/10) below, we effectively select a
395 // window of about 1 (least significant) decimal digit about
396 // which the two operands can vary and still return true.
397 return (qAbs(f1 - f2) * 102.5f <= qMin(qAbs(f1), qAbs(f2)));
398}
399
400/*!
401 \internal
402*/
403[[nodiscard]] inline bool qFuzzyIsNull(qfloat16 f) noexcept
404{
405 return qAbs(f) < 0.00976f; // 1/102.5 to 3 significant digits; see qFuzzyCompare()
406}
407
408[[nodiscard]] inline bool qIsNull(qfloat16 f) noexcept
409{
410 return (f.b16 & static_cast<quint16>(0x7fff)) == 0;
411}
412
413inline int qIntCast(qfloat16 f) noexcept
414{ return int(static_cast<qfloat16::NearestFloat>(f)); }
415
416#if !defined(Q_QDOC) && !QFLOAT16_IS_NATIVE
417inline qfloat16::qfloat16(float f) noexcept
418{
419#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
420 __m128 packsingle = _mm_set_ss(f);
421 QT_WARNING_PUSH
422 QT_WARNING_DISABLE_GCC("-Wold-style-cast") // _mm_cvtps_ph() may be a macro using C-style casts
423 __m128i packhalf = _mm_cvtps_ph(packsingle, 0);
424 QT_WARNING_POP
425 b16 = quint16(_mm_extract_epi16(packhalf, 0));
426#elif defined (__ARM_FP16_FORMAT_IEEE)
427 __fp16 f16 = __fp16(f);
428 memcpy(&b16, &f16, sizeof(quint16));
429#else
430 quint32 u;
431 memcpy(&u, &f, sizeof(quint32));
432 const quint32 signAndExp = u >> 23;
433 const quint16 base = basetable[signAndExp];
434 const quint16 shift = shifttable[signAndExp];
435 const quint32 round = roundtable[signAndExp];
436 quint32 mantissa = (u & 0x007fffff);
437 if ((signAndExp & 0xff) == 0xff) {
438 if (mantissa) // keep nan from truncating to inf
439 mantissa = qMax(1U << shift, mantissa);
440 } else {
441 // Round half to even. First round up by adding one in the most
442 // significant bit we'll be discarding:
443 mantissa += round;
444 // If the last bit we'll be keeping is now set, but all later bits are
445 // clear, we were at half and shouldn't have rounded up; decrement will
446 // clear this last kept bit. Any later set bit hides the decrement.
447 if (mantissa & (1 << shift))
448 --mantissa;
449 }
450
451 // We use add as the mantissa may overflow causing
452 // the exp part to shift exactly one value.
453 b16 = quint16(base + (mantissa >> shift));
454#endif
455}
456
457inline qfloat16::operator float() const noexcept
458{
459#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
460 __m128i packhalf = _mm_cvtsi32_si128(b16);
461 __m128 packsingle = _mm_cvtph_ps(packhalf);
462 return _mm_cvtss_f32(packsingle);
463#elif defined (__ARM_FP16_FORMAT_IEEE)
464 __fp16 f16;
465 memcpy(&f16, &b16, sizeof(quint16));
466 return float(f16);
467#else
468 quint32 u = mantissatable[offsettable[b16 >> 10] + (b16 & 0x3ff)]
469 + exponenttable[b16 >> 10];
470 float f;
471 memcpy(&f, &u, sizeof(quint32));
472 return f;
473#endif
474}
475#endif // Q_QDOC and non-native
476
477/*
478 qHypot compatibility; see ../kernel/qmath.h
479*/
480namespace QtPrivate {
481template <> struct QHypotType<qfloat16, qfloat16>
482{
483 using type = qfloat16;
484};
485template <typename R> struct QHypotType<R, qfloat16>
486{
488};
489template <typename R> struct QHypotType<qfloat16, R> : QHypotType<R, qfloat16>
490{
491};
492}
493
494// Avoid passing qfloat16 to std::hypot(), while ensuring return types
495// consistent with the above:
496inline auto qHypot(qfloat16 x, qfloat16 y)
497{
498#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__) || QFLOAT16_IS_NATIVE
499 return QtPrivate::QHypotHelper<qfloat16>(x).add(y).result();
500#else // NearestFloat is float:
501 return qfloat16(qHypot(float(x), float(y)));
502#endif
503}
504
505// in ../kernel/qmath.h
506template<typename F, typename ...Fs> auto qHypot(F first, Fs... rest);
507
508template <typename T> typename QtPrivate::QHypotType<T, qfloat16>::type
509qHypot(T x, qfloat16 y)
510{
511 // Two-arg qHypot() delegates to std::hypot(), which has no (T = double,
512 // NearestFloat = __fp16) overload on ARM64 Linux, leading to ambiguity in
513 // (at least) that case. Testing whether we can use it needs C++20 ...
514#ifdef __cpp_concepts
515 if constexpr (requires { std::hypot(x, qfloat16::NearestFloat(y)); })
516 return qHypot(x, qfloat16::NearestFloat(y));
517 else
518#endif
519 if constexpr (std::is_floating_point_v<T>)
520 return qHypot(x, float(y));
521 else
522 return qHypot(qfloat16(x), y);
523}
524template <typename T> auto qHypot(qfloat16 x, T y)
525{
526 return qHypot(y, x);
527}
528
529#if defined(__cpp_lib_hypot) && __cpp_lib_hypot >= 201603L // Expected to be true
530// If any are not qfloat16, convert each qfloat16 to qfloat16::NearestFloat:
531/* (The following splits the some-but-not-all-qfloat16 cases up, using
532 (X|Y|Z)&~(X&Y&Z) = X ? ~(Y&Z) : Y|Z = X&~(Y&Z) | ~X&Y | ~X&~Y&Z,
533 into non-overlapping cases, to avoid ambiguity.) */
534template <typename Ty, typename Tz,
535 typename std::enable_if<
536 // Ty, Tz aren't both qfloat16:
537 !(std::is_same_v<qfloat16, Ty> && std::is_same_v<qfloat16, Tz>), int>::type = 0>
538auto qHypot(qfloat16 x, Ty y, Tz z) { return qHypot(qfloat16::NearestFloat(x), y, z); }
539template <typename Tx, typename Tz,
540 typename std::enable_if<
541 // Tx isn't qfloat16:
542 !std::is_same_v<qfloat16, Tx>, int>::type = 0>
543auto qHypot(Tx x, qfloat16 y, Tz z) { return qHypot(x, qfloat16::NearestFloat(y), z); }
544template <typename Tx, typename Ty,
545 typename std::enable_if<
546 // Neither Tx nor Ty is qfloat16:
547 !std::is_same_v<qfloat16, Tx> && !std::is_same_v<qfloat16, Ty>, int>::type = 0>
548auto qHypot(Tx x, Ty y, qfloat16 z) { return qHypot(x, y, qfloat16::NearestFloat(z)); }
549
550// If all are qfloat16, stay with qfloat16 (albeit via float, if no native support):
551inline auto qHypot(qfloat16 x, qfloat16 y, qfloat16 z)
552{
553#if (defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)) || QFLOAT16_IS_NATIVE
554 return QtPrivate::QHypotHelper<qfloat16>(x).add(y).add(z).result();
555#else // NearestFloat is float;
556 return qfloat16(qHypot(float(x), float(y), float(z)));
557#endif
558}
559#endif // 3-arg std::hypot() is available
560
561QT_END_NAMESPACE
562
563namespace std {
564template<>
566{
567public:
568 /*
569 Treat quint16 b16 as if it were:
570 uint S: 1; // b16 >> 15 (sign); can be set for zero
571 uint E: 5; // (b16 >> 10) & 0x1f (offset exponent)
572 uint M: 10; // b16 & 0x3ff (adjusted mantissa)
573
574 for E == 0: magnitude is M / 2.^{24}
575 for 0 < E < 31: magnitude is (1. + M / 2.^{10}) * 2.^{E - 15)
576 for E == 31: not finite
577 */
578 static constexpr int digits = 11;
579 static constexpr int min_exponent = -13;
580 static constexpr int max_exponent = 16;
581
582 static constexpr int digits10 = 3;
583 static constexpr int max_digits10 = 5;
584 static constexpr int min_exponent10 = -4;
585 static constexpr int max_exponent10 = 4;
586
589 static constexpr QT_PREPEND_NAMESPACE(qfloat16) (min)()
593 static constexpr QT_PREPEND_NAMESPACE(qfloat16) (max)()
601#if QT_CONFIG(signaling_nan)
604#else
605 static constexpr bool has_signaling_NaN = false;
606#endif
607};
608
613template<> class numeric_limits<const volatile QT_PREPEND_NAMESPACE(qfloat16)>
615
616// Adding overloads to std isn't allowed, so we can't extend this to support
617// for fpclassify(), isnormal() &c. (which, furthermore, are macros on MinGW).
618} // namespace std
619
620// std::numbers support
621#ifdef __cpp_lib_math_constants
622namespace std::numbers {
623#define QF16_SPECIALIZE(which)
624 template <> inline constexpr QT_PREPEND_NAMESPACE(qfloat16)
625 which ## _v<QT_PREPEND_NAMESPACE(qfloat16)>
626 = QT_PREPEND_NAMESPACE(qfloat16)::_num_ ## which ()
627 /* end */
628QF16_SPECIALIZE(e);
629QF16_SPECIALIZE(log2e);
630QF16_SPECIALIZE(log10e);
631QF16_SPECIALIZE(pi);
632QF16_SPECIALIZE(inv_pi);
633QF16_SPECIALIZE(inv_sqrtpi);
634QF16_SPECIALIZE(ln2);
635QF16_SPECIALIZE(ln10);
636QF16_SPECIALIZE(sqrt2);
637QF16_SPECIALIZE(sqrt3);
638QF16_SPECIALIZE(inv_sqrt3);
639QF16_SPECIALIZE(egamma);
640QF16_SPECIALIZE(phi);
641#undef QF16_SPECIALIZE
642} // namespace std::numbers
643#endif // __cpp_lib_math_constants
644
645// std::format support
646#ifdef QT_SUPPORTS_STD_FORMAT
647
648QT_BEGIN_NAMESPACE
649
650namespace QtPrivate {
651
652// [format.formatter.spec] / 5
653template <typename T, typename CharT>
654constexpr bool FormatterDoesNotExist =
655 std::negation_v<
656 std::disjunction<
657 std::is_default_constructible<std::formatter<T, CharT>>,
658 std::is_copy_constructible<std::formatter<T, CharT>>,
659 std::is_move_constructible<std::formatter<T, CharT>>,
660 std::is_copy_assignable<std::formatter<T, CharT>>,
661 std::is_move_assignable<std::formatter<T, CharT>>
662 >
663 >;
664
665template <typename CharT>
666using QFloat16FormatterBaseType =
667 std::conditional_t<FormatterDoesNotExist<qfloat16::NearestFloat, CharT>,
668 float,
669 qfloat16::NearestFloat>;
670
671} // namespace QtPrivate
672
673QT_END_NAMESPACE
674
675namespace std {
676template <typename CharT>
677struct formatter<QT_PREPEND_NAMESPACE(qfloat16), CharT>
678 : std::formatter<QT_PREPEND_NAMESPACE(QtPrivate::QFloat16FormatterBaseType<CharT>), CharT>
679{
680 template <typename FormatContext>
681 auto format(QT_PREPEND_NAMESPACE(qfloat16) val, FormatContext &ctx) const
682 {
683 using FloatType = QT_PREPEND_NAMESPACE(QtPrivate::QFloat16FormatterBaseType<CharT>);
684 return std::formatter<FloatType, CharT>::format(FloatType(val), ctx);
685 }
686};
687} // namespace std
688
689#endif // QT_SUPPORTS_STD_FORMAT
690
691#endif // QFLOAT16_H
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:62
QF16_CONSTEXPR qfloat16 & operator--() noexcept
Definition qfloat16.h:172
friend constexpr qfloat16 operator+(qfloat16 a) noexcept
Definition qfloat16.h:205
constexpr bool isNormal() const noexcept
Definition qfloat16.h:163
friend QF16_CONSTEXPR qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept
Definition qfloat16.h:223
static constexpr qfloat16 _limit_quiet_NaN() noexcept
Definition qfloat16.h:140
QF16_CONSTEXPR qfloat16 operator--(int) noexcept
Definition qfloat16.h:170
friend constexpr qfloat16 operator-(qfloat16 a) noexcept
Definition qfloat16.h:206
QF16_CONSTEXPR qfloat16 operator++(int) noexcept
Definition qfloat16.h:166
static constexpr qfloat16 _limit_min() noexcept
Definition qfloat16.h:135
bool isInf() const noexcept
Definition qfloat16.h:105
friend QF16_CONSTEXPR qfloat16 operator-(qfloat16 a, qfloat16 b) noexcept
Definition qfloat16.h:219
static constexpr qfloat16 _limit_infinity() noexcept
Definition qfloat16.h:139
QF16_CONSTEXPR qfloat16 & operator++() noexcept
Definition qfloat16.h:168
static constexpr qfloat16 _limit_epsilon() noexcept
Definition qfloat16.h:134
operator float() const noexcept
Definition qfloat16.h:457
static constexpr bool IsNative
Definition qfloat16.h:87
friend QF16_CONSTEXPR qfloat16 operator*(qfloat16 a, qfloat16 b) noexcept
Definition qfloat16.h:221
static constexpr qfloat16 _limit_denorm_min() noexcept
Definition qfloat16.h:136
friend bool signbit(qfloat16 x) noexcept
Definition qfloat16.h:115
bool isNaN() const noexcept
Definition qfloat16.h:106
QT_WARNING_DISABLE_FLOAT_COMPARE friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, const qfloat16 &rhs) noexcept
Definition qfloat16.h:273
friend qfloat16 copysign(qfloat16 mag, qfloat16 sign) noexcept
Definition qfloat16.h:112
friend size_t qHash(qfloat16 key, size_t seed=0) noexcept
Definition qfloat16.h:226
Q_CORE_EXPORT int fpClassify() const noexcept
Definition qfloat16.cpp:165
friend bool qIsNull(qfloat16 f) noexcept
Definition qfloat16.h:408
bool isFinite() const noexcept
Definition qfloat16.h:107
static constexpr qfloat16 _limit_lowest() noexcept
Definition qfloat16.h:138
friend QF16_CONSTEXPR Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, const qfloat16 &rhs) noexcept
Definition qfloat16.h:276
qfloat16 copySign(qfloat16 sign) const noexcept
Definition qfloat16.h:110
static constexpr qfloat16 _limit_max() noexcept
Definition qfloat16.h:137
constexpr qfloat16(T value) noexcept
Definition qfloat16.h:102
friend QF16_CONSTEXPR qfloat16 operator+(qfloat16 a, qfloat16 b) noexcept
Definition qfloat16.h:217
QTextStream & operator>>(QTextStream &ts, qfloat16 &f16)
Definition qfloat16.cpp:475
static bool hasFastF16()
Definition qfloat16.cpp:346
static void qFloatToFloat16_fast(quint16 *, const float *, qsizetype) noexcept
Definition qfloat16.cpp:351
static void qFloatFromFloat16_fast(float *, const quint16 *, qsizetype) noexcept
Definition qfloat16.cpp:356
QDataStream & operator>>(QDataStream &ds, qfloat16 &f)
Definition qfloat16.cpp:469
#define QF16_MAKE_ARITH_OP_INT(OP)
Definition qfloat16.h:259
bool qIsFinite(qfloat16 f) noexcept
Definition qfloat16.h:339
int qRound(qfloat16 d)
Definition qfloat16.h:381
#define QF16_MAKE_ARITH_OP(FP)
Definition qfloat16.h:240
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:387
Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length) noexcept
auto qHypot(qfloat16 x, T y)
Definition qfloat16.h:524
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:403
bool qIsNaN(qfloat16 f) noexcept
Definition qfloat16.h:338
int qFpClassify(qfloat16 f) noexcept
Definition qfloat16.h:340
bool qIsInf(qfloat16 f) noexcept
Definition qfloat16.h:337
Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE)
auto qHypot(qfloat16 x, qfloat16 y)
Definition qfloat16.h:496
#define QF16_MAKE_ORDER_OP_FP(FP)
Definition qfloat16.h:280
#define QF16_CONSTEXPR
Definition qfloat16.h:69
#define QF16_PARTIALLY_ORDERED
Definition qfloat16.h:70
qint64 qRound64(qfloat16 d)
Definition qfloat16.h:384
Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *, const float *, qsizetype length) noexcept
qfloat16 qSqrt(qfloat16 f)
Definition qfloat16.h:343
#define QF16_NUM(which, val)
Definition qfloat16.h:146
#define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP)
Definition qfloat16.h:237
int qIntCast(qfloat16 f) noexcept
Definition qfloat16.h:413
auto qHypot(F first, Fs... rest)
Definition qmath.h:137