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