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