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