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>
22#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__AVX2__) && !defined(__F16C__)
27# if defined(Q_CC_MSVC) && !defined(Q_CC_CLANG)
32#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
39#pragma qt_class(QFloat16)
40#pragma qt_no_master_include
43#ifndef QT_NO_DATASTREAM
59# define QF16_CONSTEXPR constexpr
60# define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED_LITERAL_TYPE
62# define QF16_CONSTEXPR
63# define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED
71 constexpr inline explicit Wrap(
int value) : b16(quint16(value)) {}
91 inline operator float()
const noexcept;
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; }
105 {
return qfloat16(Wrap((sign.b16 & 0x8000) | (mag.b16 & 0x7fff))); }
108 {
return x.b16 & 0x8000; }
111#ifdef __STDCPP_FLOAT16_T__
122#if QT_CONFIG(signaling_nan)
133#if QT_CONFIG(signaling_nan)
138 {
return (b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00; }
141 {
qfloat16 tmp = *
this;
++(*
this);
return tmp; }
143 {
return *
this += NearestFloat(1); }
145 {
qfloat16 tmp = *
this;
--(*
this);
return tmp; }
147 {
return *
this -= NearestFloat(1); }
158#if QFLOAT16_IS_NATIVE
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))
173 Q_CORE_EXPORT
static const quint16
basetable[];
174 Q_CORE_EXPORT
static const quint16
shifttable[];
175 Q_CORE_EXPORT
static const quint32
roundtable[];
182#if QFLOAT16_IS_NATIVE
183 return -
static_cast<NearestFloat>(a);
186 f.b16 = a.b16 ^ quint16(0x8000);
201 {
return qHash(
float(key), seed); }
204QT_WARNING_DISABLE_GCC(
"-Wfloat-conversion")
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, /)
227#if QFLOAT16_IS_NATIVE
230#undef QF16_MAKE_ARITH_OP
231#undef QF16_MAKE_ARITH_OP_FP
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); }
243#undef QF16_MAKE_ARITH_OP_INT
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)
265#if QFLOAT16_IS_NATIVE
268#undef QF16_MAKE_ORDER_OP_FP
287#ifdef QT_SUPPORTS_INT128
292#undef QF16_PARTIALLY_ORDERED
297#ifndef QT_NO_DATASTREAM
319#if defined(__cpp_lib_extended_float) && defined(__STDCPP_FLOAT16_T__) && 0
323#elif QFLOAT16_IS_NATIVE && defined(__HAVE_FLOAT16) && __HAVE_FLOAT16
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);
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);
348 float f32 =
float(f);
350 return qfloat16::NearestFloat(f32);
356{
return qRound(
static_cast<
float>(d)); }
359{
return qRound64(
static_cast<
float>(d)); }
363 qfloat16::NearestFloat f1 =
static_cast<qfloat16::NearestFloat>(p1);
364 qfloat16::NearestFloat f2 =
static_cast<qfloat16::NearestFloat>(p2);
371 return (qAbs(f1 - f2) * 102.5f <= qMin(qAbs(f1), qAbs(f2)));
375
376
379 return qAbs(f) < 0.00976f;
384 return (f.b16 &
static_cast<quint16>(0x7fff)) == 0;
388{
return int(
static_cast<qfloat16::NearestFloat>(f)); }
390#if !defined(Q_QDOC) && !QFLOAT16_IS_NATIVE
393#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
394 __m128 packsingle = _mm_set_ss(f);
396 QT_WARNING_DISABLE_GCC(
"-Wold-style-cast")
397 __m128i packhalf = _mm_cvtps_ph(packsingle, 0);
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));
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) {
413 mantissa = qMax(1U << shift, mantissa);
421 if (mantissa & (1 << shift))
427 b16 = quint16(base + (mantissa >> shift));
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)
439 memcpy(&f16, &b16,
sizeof(quint16));
442 quint32 u = mantissatable[offsettable[b16 >> 10] + (b16 & 0x3ff)]
443 + exponenttable[b16 >> 10];
445 memcpy(&f, &u,
sizeof(quint32));
452
453
472#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__) || QFLOAT16_IS_NATIVE
473 return QtPrivate::QHypotHelper<qfloat16>(x).add(y).result();
475 return qfloat16(qHypot(
float(x),
float(y)));
480template<
typename F,
typename ...Fs>
auto qHypot(F first, Fs... rest);
482template <
typename T>
typename QtPrivate::QHypotType<T, qfloat16>::type
483qHypot(T x, qfloat16 y)
489 if constexpr (requires { std::hypot(x, qfloat16::NearestFloat(y)); })
490 return qHypot(x, qfloat16::NearestFloat(y));
493 if constexpr (std::is_floating_point_v<T>)
494 return qHypot(x,
float(y));
496 return qHypot(qfloat16(x), y);
503#if defined(__cpp_lib_hypot) && __cpp_lib_hypot >= 201603L
506
507
508template <
typename Ty,
typename Tz,
509 typename std::enable_if<
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<
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<
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)); }
525inline auto qHypot(qfloat16 x, qfloat16 y, qfloat16 z)
527#if (defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)) || QFLOAT16_IS_NATIVE
528 return QtPrivate::QHypotHelper<qfloat16>(x).add(y).add(z).result();
530 return qfloat16(qHypot(
float(x),
float(y),
float(z)));
543
544
545
546
547
548
549
550
551
575#if QT_CONFIG(signaling_nan)
595#ifdef QT_SUPPORTS_STD_FORMAT
602template <
typename T,
typename CharT>
603constexpr bool FormatterDoesNotExist =
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>>
614template <
typename CharT>
615using QFloat16FormatterBaseType =
616 std::conditional_t<FormatterDoesNotExist<qfloat16::NearestFloat, CharT>,
618 qfloat16::NearestFloat>;
625template <
typename CharT>
626struct formatter<QT_PREPEND_NAMESPACE(qfloat16), CharT>
627 : std::formatter<QT_PREPEND_NAMESPACE(QtPrivate::QFloat16FormatterBaseType<CharT>), CharT>
629 template <
typename FormatContext>
630 auto format(QT_PREPEND_NAMESPACE(qfloat16) val, FormatContext &ctx)
const
632 using FloatType = QT_PREPEND_NAMESPACE(QtPrivate::QFloat16FormatterBaseType<CharT>);
633 return std::formatter<FloatType, CharT>::format(FloatType(val), ctx);
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
QF16_CONSTEXPR qfloat16 & operator--() noexcept
friend constexpr qfloat16 operator+(qfloat16 a) noexcept
constexpr bool isNormal() const noexcept
friend QF16_CONSTEXPR qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept
static constexpr qfloat16 _limit_quiet_NaN() noexcept
QF16_CONSTEXPR qfloat16 operator--(int) noexcept
friend constexpr qfloat16 operator-(qfloat16 a) noexcept
QF16_CONSTEXPR qfloat16 operator++(int) noexcept
static constexpr qfloat16 _limit_min() noexcept
bool isInf() const noexcept
friend QF16_CONSTEXPR qfloat16 operator-(qfloat16 a, qfloat16 b) noexcept
static constexpr qfloat16 _limit_infinity() noexcept
QF16_CONSTEXPR qfloat16 & operator++() noexcept
static constexpr qfloat16 _limit_epsilon() noexcept
operator float() const noexcept
static constexpr bool IsNative
friend QF16_CONSTEXPR qfloat16 operator*(qfloat16 a, qfloat16 b) noexcept
static constexpr qfloat16 _limit_denorm_min() noexcept
friend bool signbit(qfloat16 x) noexcept
bool isNaN() const noexcept
friend qfloat16 copysign(qfloat16 mag, qfloat16 sign) noexcept
friend size_t qHash(qfloat16 key, size_t seed=0) noexcept
Q_CORE_EXPORT int fpClassify() const noexcept
friend bool qIsNull(qfloat16 f) noexcept
bool isFinite() const noexcept
static constexpr qfloat16 _limit_lowest() noexcept
qfloat16 copySign(qfloat16 sign) const noexcept
static constexpr qfloat16 _limit_max() noexcept
constexpr qfloat16(T value) noexcept
friend QF16_CONSTEXPR qfloat16 operator+(qfloat16 a, qfloat16 b) noexcept
QTextStream & operator>>(QTextStream &ts, qfloat16 &f16)
static void qFloatToFloat16_fast(quint16 *, const float *, qsizetype) noexcept
static void qFloatFromFloat16_fast(float *, const quint16 *, qsizetype) noexcept
QDataStream & operator>>(QDataStream &ds, qfloat16 &f)
#define QF16_MAKE_ARITH_OP_INT(OP)
bool qIsFinite(qfloat16 f) noexcept
#define QF16_MAKE_ARITH_OP(FP)
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qsizetype length) noexcept
auto qHypot(qfloat16 x, T y)
bool qFuzzyIsNull(qfloat16 f) noexcept
bool qIsNaN(qfloat16 f) noexcept
int qFpClassify(qfloat16 f) noexcept
bool qIsInf(qfloat16 f) noexcept
Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE)
auto qHypot(qfloat16 x, qfloat16 y)
#define QF16_MAKE_ORDER_OP_FP(FP)
#define QF16_PARTIALLY_ORDERED
qint64 qRound64(qfloat16 d)
Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *, const float *, qsizetype length) noexcept
qfloat16 qSqrt(qfloat16 f)
#define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP)
int qIntCast(qfloat16 f) noexcept
auto qHypot(F first, Fs... rest)