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 <limits>
47 #include <string.h>
48 
49 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__AVX2__) && !defined(__F16C__)
50 // All processors that support AVX2 do support F16C too. That doesn't mean
51 // we're allowed to use the intrinsics directly, so we'll do it only for
52 // the Intel and Microsoft's compilers.
53 #  if defined(Q_CC_INTEL) || defined(Q_CC_MSVC)
54 #    define __F16C__        1
55 # endif
56 #endif
57 
58 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
59 #include <immintrin.h>
60 #endif
61 
62 QT_BEGIN_NAMESPACE
63 
64 #if 0
65 #pragma qt_class(QFloat16)
66 #pragma qt_no_master_include
67 #endif
68 
69 class qfloat16
70 {
71     struct Wrap
72     {
73         // To let our private constructor work, without other code seeing
74         // ambiguity when constructing from int, double &c.
75         quint16 b16;
WrapWrap76         constexpr inline explicit Wrap(int value) : b16(value) {}
77     };
78 public:
qfloat16()79     constexpr inline qfloat16() noexcept : b16(0) {}
80     inline qfloat16(float f) noexcept;
81     inline operator float() const noexcept;
82 
83     // Support for qIs{Inf,NaN,Finite}:
isInf()84     bool isInf() const noexcept { return (b16 & 0x7fff) == 0x7c00; }
isNaN()85     bool isNaN() const noexcept { return (b16 & 0x7fff) > 0x7c00; }
isFinite()86     bool isFinite() const noexcept { return (b16 & 0x7fff) < 0x7c00; }
87     Q_CORE_EXPORT int fpClassify() const noexcept;
88     // Can't specialize std::copysign() for qfloat16
copySign(qfloat16 sign)89     qfloat16 copySign(qfloat16 sign) const noexcept
90     { return qfloat16(Wrap((sign.b16 & 0x8000) | (b16 & 0x7fff))); }
91     // Support for std::numeric_limits<qfloat16>
_limit_epsilon()92     static constexpr qfloat16 _limit_epsilon()    noexcept { return qfloat16(Wrap(0x1400)); }
_limit_min()93     static constexpr qfloat16 _limit_min()        noexcept { return qfloat16(Wrap(0x400)); }
_limit_denorm_min()94     static constexpr qfloat16 _limit_denorm_min() noexcept { return qfloat16(Wrap(1)); }
_limit_max()95     static constexpr qfloat16 _limit_max()        noexcept { return qfloat16(Wrap(0x7bff)); }
_limit_lowest()96     static constexpr qfloat16 _limit_lowest()     noexcept { return qfloat16(Wrap(0xfbff)); }
_limit_infinity()97     static constexpr qfloat16 _limit_infinity()   noexcept { return qfloat16(Wrap(0x7c00)); }
_limit_quiet_NaN()98     static constexpr qfloat16 _limit_quiet_NaN()  noexcept { return qfloat16(Wrap(0x7e00)); }
99 #if QT_CONFIG(signaling_nan)
_limit_signaling_NaN()100     static constexpr qfloat16 _limit_signaling_NaN() noexcept { return qfloat16(Wrap(0x7d00)); }
101 #endif
isNormal()102     inline constexpr bool isNormal() const noexcept
103     { return (b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00; }
104 private:
105     quint16 b16;
qfloat16(Wrap nibble)106     constexpr inline explicit qfloat16(Wrap nibble) noexcept : b16(nibble.b16) {}
107 
108     Q_CORE_EXPORT static const quint32 mantissatable[];
109     Q_CORE_EXPORT static const quint32 exponenttable[];
110     Q_CORE_EXPORT static const quint32 offsettable[];
111     Q_CORE_EXPORT static const quint32 basetable[];
112     Q_CORE_EXPORT static const quint32 shifttable[];
113 
114     friend bool qIsNull(qfloat16 f) noexcept;
115 #if !defined(QT_NO_FLOAT16_OPERATORS)
116     friend qfloat16 operator-(qfloat16 a) noexcept;
117 #endif
118 };
119 
120 Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE);
121 
122 Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *, const float *, qsizetype length) noexcept;
123 Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length) noexcept;
124 
125 // Complement qnumeric.h:
qIsInf(qfloat16 f)126 Q_REQUIRED_RESULT inline bool qIsInf(qfloat16 f) noexcept { return f.isInf(); }
qIsNaN(qfloat16 f)127 Q_REQUIRED_RESULT inline bool qIsNaN(qfloat16 f) noexcept { return f.isNaN(); }
qIsFinite(qfloat16 f)128 Q_REQUIRED_RESULT inline bool qIsFinite(qfloat16 f) noexcept { return f.isFinite(); }
qFpClassify(qfloat16 f)129 Q_REQUIRED_RESULT inline int qFpClassify(qfloat16 f) noexcept { return f.fpClassify(); }
130 // Q_REQUIRED_RESULT quint32 qFloatDistance(qfloat16 a, qfloat16 b);
131 
132 // The remainder of these utility functions complement qglobal.h
qRound(qfloat16 d)133 Q_REQUIRED_RESULT inline int qRound(qfloat16 d) noexcept
134 { return qRound(static_cast<float>(d)); }
135 
qRound64(qfloat16 d)136 Q_REQUIRED_RESULT inline qint64 qRound64(qfloat16 d) noexcept
137 { return qRound64(static_cast<float>(d)); }
138 
qFuzzyCompare(qfloat16 p1,qfloat16 p2)139 Q_REQUIRED_RESULT inline bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
140 {
141     float f1 = static_cast<float>(p1);
142     float f2 = static_cast<float>(p2);
143     // The significand precision for IEEE754 half precision is
144     // 11 bits (10 explicitly stored), or approximately 3 decimal
145     // digits.  In selecting the fuzzy comparison factor of 102.5f
146     // (that is, (2^10+1)/10) below, we effectively select a
147     // window of about 1 (least significant) decimal digit about
148     // which the two operands can vary and still return true.
149     return (qAbs(f1 - f2) * 102.5f <= qMin(qAbs(f1), qAbs(f2)));
150 }
151 
qIsNull(qfloat16 f)152 Q_REQUIRED_RESULT inline bool qIsNull(qfloat16 f) noexcept
153 {
154     return (f.b16 & static_cast<quint16>(0x7fff)) == 0;
155 }
156 
qIntCast(qfloat16 f)157 inline int qIntCast(qfloat16 f) noexcept
158 { return int(static_cast<float>(f)); }
159 
160 #ifndef Q_QDOC
161 QT_WARNING_PUSH
162 QT_WARNING_DISABLE_CLANG("-Wc99-extensions")
163 QT_WARNING_DISABLE_GCC("-Wold-style-cast")
qfloat16(float f)164 inline qfloat16::qfloat16(float f) noexcept
165 {
166 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
167     __m128 packsingle = _mm_set_ss(f);
168     __m128i packhalf = _mm_cvtps_ph(packsingle, 0);
169     b16 = _mm_extract_epi16(packhalf, 0);
170 #elif defined (__ARM_FP16_FORMAT_IEEE)
171     __fp16 f16 = __fp16(f);
172     memcpy(&b16, &f16, sizeof(quint16));
173 #else
174     quint32 u;
175     memcpy(&u, &f, sizeof(quint32));
176     b16 = basetable[(u >> 23) & 0x1ff]
177           + ((u & 0x007fffff) >> shifttable[(u >> 23) & 0x1ff]);
178 #endif
179 }
180 QT_WARNING_POP
181 
182 inline qfloat16::operator float() const noexcept
183 {
184 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
185     __m128i packhalf = _mm_cvtsi32_si128(b16);
186     __m128 packsingle = _mm_cvtph_ps(packhalf);
187     return _mm_cvtss_f32(packsingle);
188 #elif defined (__ARM_FP16_FORMAT_IEEE)
189     __fp16 f16;
190     memcpy(&f16, &b16, sizeof(quint16));
191     return float(f16);
192 #else
193     quint32 u = mantissatable[offsettable[b16 >> 10] + (b16 & 0x3ff)]
194                 + exponenttable[b16 >> 10];
195     float f;
196     memcpy(&f, &u, sizeof(quint32));
197     return f;
198 #endif
199 }
200 #endif
201 
202 #if !defined(QT_NO_FLOAT16_OPERATORS)
203 inline qfloat16 operator-(qfloat16 a) noexcept
204 {
205     qfloat16 f;
206     f.b16 = a.b16 ^ quint16(0x8000);
207     return f;
208 }
209 
210 inline qfloat16 operator+(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) + static_cast<float>(b)); }
211 inline qfloat16 operator-(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) - static_cast<float>(b)); }
212 inline qfloat16 operator*(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) * static_cast<float>(b)); }
213 inline qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept { return qfloat16(static_cast<float>(a) / static_cast<float>(b)); }
214 
215 #define QF16_MAKE_ARITH_OP_FP(FP, OP) \
216     inline FP operator OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; } \
217     inline FP operator OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); }
218 #define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP) \
219     inline qfloat16& operator OP_EQ(qfloat16& lhs, FP rhs) noexcept \
220     { lhs = qfloat16(float(static_cast<FP>(lhs) OP rhs)); return lhs; }
221 #define QF16_MAKE_ARITH_OP(FP) \
222     QF16_MAKE_ARITH_OP_FP(FP, +) \
223     QF16_MAKE_ARITH_OP_FP(FP, -) \
224     QF16_MAKE_ARITH_OP_FP(FP, *) \
225     QF16_MAKE_ARITH_OP_FP(FP, /) \
226     QF16_MAKE_ARITH_OP_EQ_FP(FP, +=, +) \
227     QF16_MAKE_ARITH_OP_EQ_FP(FP, -=, -) \
228     QF16_MAKE_ARITH_OP_EQ_FP(FP, *=, *) \
229     QF16_MAKE_ARITH_OP_EQ_FP(FP, /=, /)
230 QF16_MAKE_ARITH_OP(long double)
QF16_MAKE_ARITH_OP(double)231 QF16_MAKE_ARITH_OP(double)
232 QF16_MAKE_ARITH_OP(float)
233 #undef QF16_MAKE_ARITH_OP
234 #undef QF16_MAKE_ARITH_OP_FP
235 
236 #define QF16_MAKE_ARITH_OP_INT(OP) \
237     inline double operator OP(qfloat16 lhs, int rhs) noexcept { return static_cast<double>(lhs) OP rhs; } \
238     inline double operator OP(int lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<double>(rhs); }
239 QF16_MAKE_ARITH_OP_INT(+)
240 QF16_MAKE_ARITH_OP_INT(-)
241 QF16_MAKE_ARITH_OP_INT(*)
242 QF16_MAKE_ARITH_OP_INT(/)
243 #undef QF16_MAKE_ARITH_OP_INT
244 
245 QT_WARNING_PUSH
246 QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
247 QT_WARNING_DISABLE_GCC("-Wfloat-equal")
248 QT_WARNING_DISABLE_INTEL(1572)
249 
250 inline bool operator>(qfloat16 a, qfloat16 b)  noexcept { return static_cast<float>(a) >  static_cast<float>(b); }
251 inline bool operator<(qfloat16 a, qfloat16 b)  noexcept { return static_cast<float>(a) <  static_cast<float>(b); }
252 inline bool operator>=(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) >= static_cast<float>(b); }
253 inline bool operator<=(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) <= static_cast<float>(b); }
254 inline bool operator==(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) == static_cast<float>(b); }
255 inline bool operator!=(qfloat16 a, qfloat16 b) noexcept { return static_cast<float>(a) != static_cast<float>(b); }
256 
257 #define QF16_MAKE_BOOL_OP_FP(FP, OP) \
258     inline bool operator OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; } \
259     inline bool operator OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); }
260 #define QF16_MAKE_BOOL_OP(FP) \
261     QF16_MAKE_BOOL_OP_FP(FP, <) \
262     QF16_MAKE_BOOL_OP_FP(FP, >) \
263     QF16_MAKE_BOOL_OP_FP(FP, >=) \
264     QF16_MAKE_BOOL_OP_FP(FP, <=) \
265     QF16_MAKE_BOOL_OP_FP(FP, ==) \
266     QF16_MAKE_BOOL_OP_FP(FP, !=)
267 QF16_MAKE_BOOL_OP(long double)
QF16_MAKE_BOOL_OP(double)268 QF16_MAKE_BOOL_OP(double)
269 QF16_MAKE_BOOL_OP(float)
270 #undef QF16_MAKE_BOOL_OP
271 #undef QF16_MAKE_BOOL_OP_FP
272 
273 #define QF16_MAKE_BOOL_OP_INT(OP) \
274     inline bool operator OP(qfloat16 a, int b) noexcept { return static_cast<float>(a) OP b; } \
275     inline bool operator OP(int a, qfloat16 b) noexcept { return a OP static_cast<float>(b); }
276 QF16_MAKE_BOOL_OP_INT(>)
277 QF16_MAKE_BOOL_OP_INT(<)
278 QF16_MAKE_BOOL_OP_INT(>=)
279 QF16_MAKE_BOOL_OP_INT(<=)
280 QF16_MAKE_BOOL_OP_INT(==)
281 QF16_MAKE_BOOL_OP_INT(!=)
282 #undef QF16_MAKE_BOOL_OP_INT
283 
284 QT_WARNING_POP
285 #endif // QT_NO_FLOAT16_OPERATORS
286 
287 /*!
288   \internal
289 */
290 Q_REQUIRED_RESULT inline bool qFuzzyIsNull(qfloat16 f) noexcept
291 {
292     return qAbs(static_cast<float>(f)) <= 0.001f;
293 }
294 
295 QT_END_NAMESPACE
296 
Q_DECLARE_METATYPE(qfloat16)297 Q_DECLARE_METATYPE(qfloat16)
298 
299 namespace std {
300 template<>
301 class numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> : public numeric_limits<float>
302 {
303 public:
304     /*
305       Treat quint16 b16 as if it were:
306       uint S: 1; // b16 >> 15 (sign); can be set for zero
307       uint E: 5; // (b16 >> 10) & 0x1f (offset exponent)
308       uint M: 10; // b16 & 0x3ff (adjusted mantissa)
309 
310       for E == 0: magnitude is M / 2.^{24}
311       for 0 < E < 31: magnitude is (1. + M / 2.^{10}) * 2.^{E - 15)
312       for E == 31: not finite
313      */
314     static constexpr int digits = 11;
315     static constexpr int min_exponent = -13;
316     static constexpr int max_exponent = 16;
317 
318     static constexpr int digits10 = 3;
319     static constexpr int max_digits10 = 5;
320     static constexpr int min_exponent10 = -4;
321     static constexpr int max_exponent10 = 4;
322 
323     static constexpr QT_PREPEND_NAMESPACE(qfloat16) epsilon()
324     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_epsilon(); }
325     static constexpr QT_PREPEND_NAMESPACE(qfloat16) (min)()
326     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_min(); }
327     static constexpr QT_PREPEND_NAMESPACE(qfloat16) denorm_min()
328     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_denorm_min(); }
329     static constexpr QT_PREPEND_NAMESPACE(qfloat16) (max)()
330     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_max(); }
331     static constexpr QT_PREPEND_NAMESPACE(qfloat16) lowest()
332     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_lowest(); }
333     static constexpr QT_PREPEND_NAMESPACE(qfloat16) infinity()
334     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_infinity(); }
335     static constexpr QT_PREPEND_NAMESPACE(qfloat16) quiet_NaN()
336     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_quiet_NaN(); }
337 #if QT_CONFIG(signaling_nan)
338     static constexpr QT_PREPEND_NAMESPACE(qfloat16) signaling_NaN()
339     { return QT_PREPEND_NAMESPACE(qfloat16)::_limit_signaling_NaN(); }
340 #else
341     static constexpr bool has_signaling_NaN = false;
342 #endif
343 };
344 
345 template<> class numeric_limits<const QT_PREPEND_NAMESPACE(qfloat16)>
346     : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
347 template<> class numeric_limits<volatile QT_PREPEND_NAMESPACE(qfloat16)>
348     : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
349 template<> class numeric_limits<const volatile QT_PREPEND_NAMESPACE(qfloat16)>
350     : public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
351 
352 // Adding overloads to std isn't allowed, so we can't extend this to support
353 // for fpclassify(), isnormal() &c. (which, furthermore, are macros on MinGW).
354 } // namespace std
355 
356 #endif // QFLOAT16_H
357