1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Copyright (C) 2016 by Southwest Research Institute (R)
5** Contact: http://www.qt-project.org/legal
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QFLOAT16_H
42#define QFLOAT16_H
43
44#include <QtCore/qglobal.h>
45#include <QtCore/qmetatype.h>
46#include <string.h>
47
48#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__AVX2__) && !defined(__F16C__)
49// All processors that support AVX2 do support F16C too. That doesn't mean
50// we're allowed to use the intrinsics directly, so we'll do it only for
51// the Intel and Microsoft's compilers.
52# if defined(Q_CC_INTEL) || defined(Q_CC_MSVC)
53# define __F16C__ 1
54# endif
55#endif
56
57#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
58#include <immintrin.h>
59#endif
60
61QT_BEGIN_NAMESPACE
62
63#if 0
64#pragma qt_class(QFloat16)
65#pragma qt_no_master_include
66#endif
67
68class qfloat16
69{
70 struct Wrap
71 {
72 // To let our private constructor work, without other code seeing
73 // ambiguity when constructing from int, double &c.
74 quint16 b16;
75 constexpr inline explicit Wrap(int value) : b16(value) {}
76 };
77public:
78 constexpr inline qfloat16() noexcept : b16(0) {}
79 inline qfloat16(float f) noexcept;
80 inline operator float() const noexcept;
81
82 // Support for qIs{Inf,NaN,Finite}:
83 bool isInf() const noexcept { return ((b16 >> 8) & 0x7e) == 0x7c; }
84 bool isNaN() const noexcept { return ((b16 >> 8) & 0x7e) == 0x7e; }
85 bool isFinite() const noexcept { return ((b16 >> 8) & 0x7c) != 0x7c; }
86 Q_CORE_EXPORT int fpClassify() const noexcept;
87 // Support for std::numeric_limits<qfloat16>
88 static constexpr qfloat16 _limit_epsilon() noexcept { return qfloat16(Wrap(0x1400)); }
89 static constexpr qfloat16 _limit_min() noexcept { return qfloat16(Wrap(0x400)); }
90 static constexpr qfloat16 _limit_denorm_min() noexcept { return qfloat16(Wrap(1)); }
91 static constexpr qfloat16 _limit_max() noexcept { return qfloat16(Wrap(0x7bff)); }
92 static constexpr qfloat16 _limit_lowest() noexcept { return qfloat16(Wrap(0xfbff)); }
93 static constexpr qfloat16 _limit_infinity() noexcept { return qfloat16(Wrap(0x7c00)); }
94 static constexpr qfloat16 _limit_quiet_NaN() noexcept { return qfloat16(Wrap(0x7e00)); }
95 // Signalling NaN is 0x7f00
96 inline constexpr bool isNormal() const noexcept
97 { return b16 == 0 || ((b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00); }
98private:
99 quint16 b16;
100 constexpr inline explicit qfloat16(Wrap nibble) noexcept : b16(nibble.b16) {}
101
102 Q_CORE_EXPORT static const quint32 mantissatable[];
103 Q_CORE_EXPORT static const quint32 exponenttable[];
104 Q_CORE_EXPORT static const quint32 offsettable[];
105 Q_CORE_EXPORT static const quint32 basetable[];
106 Q_CORE_EXPORT static const quint32 shifttable[];
107
108 friend bool qIsNull(qfloat16 f) noexcept;
109#if !defined(QT_NO_FLOAT16_OPERATORS)
110 friend qfloat16 operator-(qfloat16 a) noexcept;
111#endif
112};
113
114Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE);
115
116Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *, const float *, qsizetype length) noexcept;
117Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length) noexcept;
118
119// Complement qnumeric.h:
120Q_REQUIRED_RESULT inline bool qIsInf(qfloat16 f) noexcept { return f.isInf(); }
121Q_REQUIRED_RESULT inline bool qIsNaN(qfloat16 f) noexcept { return f.isNaN(); }
122Q_REQUIRED_RESULT inline bool qIsFinite(qfloat16 f) noexcept { return f.isFinite(); }
123Q_REQUIRED_RESULT inline int qFpClassify(qfloat16 f) noexcept { return f.fpClassify(); }
124// Q_REQUIRED_RESULT quint32 qFloatDistance(qfloat16 a, qfloat16 b);
125
126// The remainder of these utility functions complement qglobal.h
127Q_REQUIRED_RESULT inline int qRound(qfloat16 d) noexcept
128{ return qRound(static_cast<float>(d)); }
129
130Q_REQUIRED_RESULT inline qint64 qRound64(qfloat16 d) noexcept
131{ return qRound64(static_cast<float>(d)); }
132
133Q_REQUIRED_RESULT inline bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
134{
135 float f1 = static_cast<float>(p1);
136 float f2 = static_cast<float>(p2);
137 // The significand precision for IEEE754 half precision is
138 // 11 bits (10 explicitly stored), or approximately 3 decimal
139 // digits. In selecting the fuzzy comparison factor of 102.5f
140 // (that is, (2^10+1)/10) below, we effectively select a
141 // window of about 1 (least significant) decimal digit about
142 // which the two operands can vary and still return true.
143 return (qAbs(f1 - f2) * 102.5f <= qMin(qAbs(f1), qAbs(f2)));
144}
145
146Q_REQUIRED_RESULT inline bool qIsNull(qfloat16 f) noexcept
147{
148 return (f.b16 & static_cast<quint16>(0x7fff)) == 0;
149}
150
151inline int qIntCast(qfloat16 f) noexcept
152{ return int(static_cast<float>(f)); }
153
154#ifndef Q_QDOC
155QT_WARNING_PUSH
156QT_WARNING_DISABLE_CLANG("-Wc99-extensions")
157QT_WARNING_DISABLE_GCC("-Wold-style-cast")
158inline qfloat16::qfloat16(float f) noexcept
159{
160#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
161 __m128 packsingle = _mm_set_ss(f);
162 __m128i packhalf = _mm_cvtps_ph(packsingle, 0);
163 b16 = _mm_extract_epi16(packhalf, 0);
164#elif defined (__ARM_FP16_FORMAT_IEEE)
165 __fp16 f16 = __fp16(f);
166 memcpy(&b16, &f16, sizeof(quint16));
167#else
168 quint32 u;
169 memcpy(&u, &f, sizeof(quint32));
170 b16 = basetable[(u >> 23) & 0x1ff]
171 + ((u & 0x007fffff) >> shifttable[(u >> 23) & 0x1ff]);
172#endif
173}
174QT_WARNING_POP
175
176inline qfloat16::operator float() const noexcept
177{
178#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
179 __m128i packhalf = _mm_cvtsi32_si128(b16);
180 __m128 packsingle = _mm_cvtph_ps(packhalf);
181 return _mm_cvtss_f32(packsingle);
182#elif defined (__ARM_FP16_FORMAT_IEEE)
183 __fp16 f16;
184 memcpy(&f16, &b16, sizeof(quint16));
185 return float(f16);
186#else
187 quint32 u = mantissatable[offsettable[b16 >> 10] + (b16 & 0x3ff)]
188 + exponenttable[b16 >> 10];
189 float f;
190 memcpy(&f, &u, sizeof(quint32));
191 return f;
192#endif
193}
194#endif
195
196#if !defined(QT_NO_FLOAT16_OPERATORS)
197inline qfloat16 operator-(qfloat16 a) noexcept
198{
199 qfloat16 f;
200 f.b16 = a.b16 ^ quint16(0x8000);
201 return f;
202}
203
204inline qfloat16 operator+(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) + static_cast<float>(b)); }
205inline qfloat16 operator-(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) - static_cast<float>(b)); }
206inline qfloat16 operator*(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) * static_cast<float>(b)); }
207inline qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) / static_cast<float>(b)); }
208
209#define QF16_MAKE_ARITH_OP_FP(FP, OP) \
210 inline FP operator OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; } \
211 inline FP operator OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); }
212#define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP) \
213 inline qfloat16& operator OP_EQ(qfloat16& lhs, FP rhs) noexcept \
214 { lhs = qfloat16(float(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, /=, /)
224QF16_MAKE_ARITH_OP(long double)
225QF16_MAKE_ARITH_OP(double)
226QF16_MAKE_ARITH_OP(float)
227#undef QF16_MAKE_ARITH_OP
228#undef QF16_MAKE_ARITH_OP_FP
229
230#define QF16_MAKE_ARITH_OP_INT(OP) \
231 inline double operator OP(qfloat16 lhs, int rhs) noexcept { return static_cast<double>(lhs) OP rhs; } \
232 inline double operator OP(int lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<double>(rhs); }
233QF16_MAKE_ARITH_OP_INT(+)
234QF16_MAKE_ARITH_OP_INT(-)
235QF16_MAKE_ARITH_OP_INT(*)
236QF16_MAKE_ARITH_OP_INT(/)
237#undef QF16_MAKE_ARITH_OP_INT
238
239QT_WARNING_PUSH
240QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
241QT_WARNING_DISABLE_GCC("-Wfloat-equal")
242
243inline bool operator>(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) > static_cast<float>(b); }
244inline bool operator<(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) < static_cast<float>(b); }
245inline bool operator>=(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) >= static_cast<float>(b); }
246inline bool operator<=(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) <= static_cast<float>(b); }
247inline bool operator==(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) == static_cast<float>(b); }
248inline bool operator!=(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) != static_cast<float>(b); }
249
250#define QF16_MAKE_BOOL_OP_FP(FP, OP) \
251 inline bool operator OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; } \
252 inline bool operator OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); }
253#define QF16_MAKE_BOOL_OP(FP) \
254 QF16_MAKE_BOOL_OP_FP(FP, <) \
255 QF16_MAKE_BOOL_OP_FP(FP, >) \
256 QF16_MAKE_BOOL_OP_FP(FP, >=) \
257 QF16_MAKE_BOOL_OP_FP(FP, <=) \
258 QF16_MAKE_BOOL_OP_FP(FP, ==) \
259 QF16_MAKE_BOOL_OP_FP(FP, !=)
260QF16_MAKE_BOOL_OP(long double)
261QF16_MAKE_BOOL_OP(double)
262QF16_MAKE_BOOL_OP(float)
263#undef QF16_MAKE_BOOL_OP
264#undef QF16_MAKE_BOOL_OP_FP
265
266#define QF16_MAKE_BOOL_OP_INT(OP) \
267 inline bool operator OP(qfloat16 a, int b) noexcept { return static_cast<float>(a) OP b; } \
268 inline bool operator OP(int a, qfloat16 b) noexcept { return a OP static_cast<float>(b); }
269QF16_MAKE_BOOL_OP_INT(>)
270QF16_MAKE_BOOL_OP_INT(<)
271QF16_MAKE_BOOL_OP_INT(>=)
272QF16_MAKE_BOOL_OP_INT(<=)
273QF16_MAKE_BOOL_OP_INT(==)
274QF16_MAKE_BOOL_OP_INT(!=)
275#undef QF16_MAKE_BOOL_OP_INT
276
277QT_WARNING_POP
278#endif // QT_NO_FLOAT16_OPERATORS
279
280/*!
281 \internal
282*/
283Q_REQUIRED_RESULT inline bool qFuzzyIsNull(qfloat16 f) noexcept
284{
285 return qAbs(static_cast<float>(f)) <= 0.001f;
286}
287
288QT_END_NAMESPACE
289
290Q_DECLARE_METATYPE(qfloat16)
291
292namespace std {
293template<>
294class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float>
295{
296public:
297 /*
298 Treat quint16 b16 as if it were:
299 uint S: 1; // b16 >> 15 (sign)
300 uint E: 5; // (b16 >> 10) & 0x1f (offset exponent)
301 uint M: 10; // b16 & 0x3ff (adjusted mantissa)
302
303 for E == 0: magnitude is M / 2.^{24}
304 for 0 < E < 31: magnitude is (1. + M / 2.^{10}) * 2.^{E - 15)
305 for E == 31: not finite
306 */
307 static constexpr int digits = 11;
308 static constexpr int min_exponent = -13;
309 static constexpr int max_exponent = 16;
310
311 static constexpr int digits10 = 3;
312 static constexpr int max_digits10 = 5;
313 static constexpr int min_exponent10 = -4;
314 static constexpr int max_exponent10 = 4;
315
316 static constexpr QT_PREPEND_NAMESPACE(qfloat16) epsilon()
317 { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_epsilon(); }
318 static constexpr QT_PREPEND_NAMESPACE(qfloat16) (min)()
319 { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_min(); }
320 static constexpr QT_PREPEND_NAMESPACE(qfloat16) denorm_min()
321 { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_denorm_min(); }
322 static constexpr QT_PREPEND_NAMESPACE(qfloat16) (max)()
323 { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_max(); }
324 static constexpr QT_PREPEND_NAMESPACE(qfloat16) lowest()
325 { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_lowest(); }
326 static constexpr QT_PREPEND_NAMESPACE(qfloat16) infinity()
327 { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_infinity(); }
328 static constexpr QT_PREPEND_NAMESPACE(qfloat16) quiet_NaN()
329 { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_quiet_NaN(); }
330};
331
332template<> class numeric_limits<const QT_PREPEND_NAMESPACE(qfloat16)>
333 : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
334template<> class numeric_limits<volatile QT_PREPEND_NAMESPACE(qfloat16)>
335 : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
336template<> class numeric_limits<const volatile QT_PREPEND_NAMESPACE(qfloat16)>
337 : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
338
339// Adding overloads to std isn't allowed, so we can't extend this to support
340// for fpclassify(), isnormal() &c. (which, furthermore, are macros on MinGW).
341} // namespace std
342
343#endif // QFLOAT16_H
344