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