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