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