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/qtformat_impl.h>
15#include <QtCore/qtypes.h>
21#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__AVX2__) && !defined(__F16C__)
26# if defined(Q_CC_MSVC) && !defined(Q_CC_CLANG)
31#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
38#pragma qt_class(QFloat16)
39#pragma qt_no_master_include
42#ifndef QT_NO_DATASTREAM
54 constexpr inline explicit Wrap(
int value) : b16(quint16(value)) {}
74 inline operator float()
const noexcept;
80 bool isInf()
const noexcept {
return (b16 & 0x7fff) == 0x7c00; }
81 bool isNaN()
const noexcept {
return (b16 & 0x7fff) > 0x7c00; }
82 bool isFinite()
const noexcept {
return (b16 & 0x7fff) < 0x7c00; }
86 {
return qfloat16(Wrap((sign.b16 & 0x8000) | (b16 & 0x7fff))); }
89#ifdef __STDCPP_FLOAT16_T__
100#if QT_CONFIG(signaling_nan)
111#if QT_CONFIG(signaling_nan)
116 {
return (b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00; }
126#if QFLOAT16_IS_NATIVE
130 constexpr inline explicit qfloat16(Wrap nibble)
noexcept :
131#if QFLOAT16_IS_NATIVE && defined(__cpp_lib_bit_cast)
132 nf(std::bit_cast<NativeType>(nibble.b16))
141 Q_CORE_EXPORT
static const quint16
basetable[];
142 Q_CORE_EXPORT
static const quint16
shifttable[];
143 Q_CORE_EXPORT
static const quint32
roundtable[];
150 f.b16 = a.b16 ^ quint16(0x8000);
160 {
return qHash(
float(key), seed); }
163QT_WARNING_DISABLE_GCC(
"-Wfloat-conversion")
165#define QF16_MAKE_ARITH_OP_FP(FP, OP)
166 friend inline FP operator
OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; }
167 friend inline FP operator
OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); }
168#define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP)
169 friend inline qfloat16& operator
OP_EQ(qfloat16& lhs, FP rhs) noexcept
170 { lhs = qfloat16(NearestFloat(static_cast<FP>(lhs) OP rhs)); return lhs; }
171#define QF16_MAKE_ARITH_OP(FP)
172 QF16_MAKE_ARITH_OP_FP(FP, +)
173 QF16_MAKE_ARITH_OP_FP(FP, -)
174 QF16_MAKE_ARITH_OP_FP(FP, *)
175 QF16_MAKE_ARITH_OP_FP(FP, /)
184#if QFLOAT16_IS_NATIVE
187#undef QF16_MAKE_ARITH_OP
188#undef QF16_MAKE_ARITH_OP_FP
190#define QF16_MAKE_ARITH_OP_INT(OP)
191 friend inline double operator
OP(qfloat16 lhs, int rhs) noexcept { return static_cast<double>(lhs) OP rhs; }
192 friend inline double operator
OP(int lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<double>(rhs); }
198#undef QF16_MAKE_ARITH_OP_INT
202#if QFLOAT16_IS_NATIVE
203# define QF16_CONSTEXPR constexpr
204# define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED_LITERAL_TYPE
206# define QF16_CONSTEXPR
207# define QF16_PARTIALLY_ORDERED Q_DECLARE_PARTIALLY_ORDERED
217#define QF16_MAKE_ORDER_OP_FP(FP)
218 friend QF16_CONSTEXPR bool comparesEqual(const qfloat16 &lhs, FP rhs) noexcept
219 { return static_cast<FP>(lhs) == rhs; }
220 friend QF16_CONSTEXPR
221 Qt::partial_ordering compareThreeWay(const qfloat16 &lhs, FP rhs) noexcept
222 { return Qt::compareThreeWay(static_cast<FP>(lhs), rhs); }
223 QF16_PARTIALLY_ORDERED(qfloat16, FP)
228#if QFLOAT16_IS_NATIVE
231#undef QF16_MAKE_ORDER_OP_FP
250#ifdef QT_SUPPORTS_INT128
255#undef QF16_PARTIALLY_ORDERED
260#ifndef QT_NO_DATASTREAM
282#if defined(__cpp_lib_extended_float) && defined(__STDCPP_FLOAT16_T__) && 0
286#elif QFLOAT16_IS_NATIVE && defined(__HAVE_FLOAT16) && __HAVE_FLOAT16
290 bool mathUpdatesErrno =
true;
291# if defined(__NO_MATH_ERRNO__) || defined(_M_FP_FAST)
292 mathUpdatesErrno =
false;
293# elif defined(math_errhandling)
294 mathUpdatesErrno = (math_errhandling & MATH_ERRNO);
301 if (!mathUpdatesErrno || !(0 > f)) {
302# if defined(__AVX512FP16__)
303 __m128h v = _mm_set_sh(f);
304 v = _mm_sqrt_sh(v, v);
305 return _mm_cvtsh_h(v);
311 float f32 =
float(f);
313 return qfloat16::NearestFloat(f32);
319{
return qRound(
static_cast<
float>(d)); }
322{
return qRound64(
static_cast<
float>(d)); }
326 qfloat16::NearestFloat f1 =
static_cast<qfloat16::NearestFloat>(p1);
327 qfloat16::NearestFloat f2 =
static_cast<qfloat16::NearestFloat>(p2);
334 return (qAbs(f1 - f2) * 102.5f <= qMin(qAbs(f1), qAbs(f2)));
338
339
342 return qAbs(f) < 0.00976f;
347 return (f.b16 &
static_cast<quint16>(0x7fff)) == 0;
351{
return int(
static_cast<qfloat16::NearestFloat>(f)); }
353#if !defined(Q_QDOC) && !QFLOAT16_IS_NATIVE
356QT_WARNING_DISABLE_GCC(
"-Wold-style-cast")
357inline qfloat16::qfloat16(
float f)
noexcept
359#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
360 __m128 packsingle = _mm_set_ss(f);
361 __m128i packhalf = _mm_cvtps_ph(packsingle, 0);
362 b16 = _mm_extract_epi16(packhalf, 0);
363#elif defined (__ARM_FP16_FORMAT_IEEE)
364 __fp16 f16 =
__fp16(f);
365 memcpy(&b16, &f16,
sizeof(quint16));
368 memcpy(&u, &f,
sizeof(quint32));
369 const quint32 signAndExp = u >> 23;
370 const quint16 base = basetable[signAndExp];
371 const quint16 shift = shifttable[signAndExp];
372 const quint32 round = roundtable[signAndExp];
373 quint32 mantissa = (u & 0x007fffff);
374 if ((signAndExp & 0xff) == 0xff) {
376 mantissa = qMax(1U << shift, mantissa);
384 if (mantissa & (1 << shift))
390 b16 = quint16(base + (mantissa >> shift));
395inline qfloat16::operator
float()
const noexcept
397#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
398 __m128i packhalf = _mm_cvtsi32_si128(b16);
399 __m128 packsingle = _mm_cvtph_ps(packhalf);
400 return _mm_cvtss_f32(packsingle);
401#elif defined (__ARM_FP16_FORMAT_IEEE)
403 memcpy(&f16, &b16,
sizeof(quint16));
406 quint32 u = mantissatable[offsettable[b16 >> 10] + (b16 & 0x3ff)]
407 + exponenttable[b16 >> 10];
409 memcpy(&f, &u,
sizeof(quint32));
416
417
419template <>
struct QHypotType<qfloat16, qfloat16>
436#if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__) || QFLOAT16_IS_NATIVE
437 return QtPrivate::QHypotHelper<qfloat16>(x).add(y).result();
439 return qfloat16(qHypot(
float(x),
float(y)));
444template<
typename F,
typename ...Fs>
auto qHypot(F first, Fs... rest);
446template <
typename T>
typename QtPrivate::QHypotType<T, qfloat16>::type
447qHypot(T x, qfloat16 y)
449 if constexpr (std::is_floating_point_v<T>)
450 return qHypot(x,
float(y));
452 return qHypot(qfloat16(x), y);
459#if defined(__cpp_lib_hypot) && __cpp_lib_hypot >= 201603L
462
463
464template <
typename Ty,
typename Tz,
465 typename std::enable_if<
467 !(std::is_same_v<qfloat16, Ty> && std::is_same_v<qfloat16, Tz>),
int>::type = 0>
468auto qHypot(qfloat16 x, Ty y, Tz z) {
return qHypot(qfloat16::NearestFloat(x), y, z); }
469template <
typename Tx,
typename Tz,
470 typename std::enable_if<
472 !std::is_same_v<qfloat16, Tx>,
int>::type = 0>
473auto qHypot(Tx x, qfloat16 y, Tz z) {
return qHypot(x, qfloat16::NearestFloat(y), z); }
474template <
typename Tx,
typename Ty,
475 typename std::enable_if<
477 !std::is_same_v<qfloat16, Tx> && !std::is_same_v<qfloat16, Ty>,
int>::type = 0>
478auto qHypot(Tx x, Ty y, qfloat16 z) {
return qHypot(x, y, qfloat16::NearestFloat(z)); }
481inline auto qHypot(qfloat16 x, qfloat16 y, qfloat16 z)
483#if (defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)) || QFLOAT16_IS_NATIVE
484 return QtPrivate::QHypotHelper<qfloat16>(x).add(y).add(z).result();
486 return qfloat16(qHypot(
float(x),
float(y),
float(z)));
499
500
501
502
503
504
505
506
507
531#if QT_CONFIG(signaling_nan)
551#ifdef QT_SUPPORTS_STD_FORMAT
558template <
typename T,
typename CharT>
559constexpr bool FormatterDoesNotExist =
562 std::is_default_constructible<std::formatter<T, CharT>>,
563 std::is_copy_constructible<std::formatter<T, CharT>>,
564 std::is_move_constructible<std::formatter<T, CharT>>,
565 std::is_copy_assignable<std::formatter<T, CharT>>,
566 std::is_move_assignable<std::formatter<T, CharT>>
570template <
typename CharT>
571using QFloat16FormatterBaseType =
572 std::conditional_t<FormatterDoesNotExist<qfloat16::NearestFloat, CharT>,
574 qfloat16::NearestFloat>;
581template <
typename CharT>
582struct formatter<QT_PREPEND_NAMESPACE(qfloat16), CharT>
583 : std::formatter<QT_PREPEND_NAMESPACE(QtPrivate::QFloat16FormatterBaseType<CharT>), CharT>
585 template <
typename FormatContext>
586 auto format(QT_PREPEND_NAMESPACE(qfloat16) val, FormatContext &ctx)
const
588 using FloatType = QT_PREPEND_NAMESPACE(QtPrivate::QFloat16FormatterBaseType<CharT>);
589 return std::formatter<FloatType, CharT>::format(FloatType(val), ctx);
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
constexpr qfloat16() noexcept
constexpr bool isNormal() const noexcept
static constexpr qfloat16 _limit_quiet_NaN() noexcept
static constexpr qfloat16 _limit_min() noexcept
bool isInf() const noexcept
static constexpr qfloat16 _limit_infinity() noexcept
static constexpr qfloat16 _limit_epsilon() noexcept
static constexpr bool IsNative
static constexpr qfloat16 _limit_denorm_min() noexcept
bool isNaN() const noexcept
friend qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept
friend qfloat16 operator-(qfloat16 a, qfloat16 b) 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
friend qfloat16 operator+(qfloat16 a, qfloat16 b) noexcept
friend qfloat16 operator*(qfloat16 a, qfloat16 b) noexcept
friend qfloat16 operator-(qfloat16 a) noexcept
operator float() const noexcept
qfloat16 copySign(qfloat16 sign) const noexcept
static constexpr qfloat16 _limit_max() noexcept
constexpr qfloat16(T value) 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
Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *, const float *, qsizetype length) noexcept
qfloat16 qSqrt(qfloat16 f)
qint64 qRound64(qfloat16 d) noexcept
#define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP)
int qRound(qfloat16 d) noexcept
int qIntCast(qfloat16 f) noexcept
auto qHypot(F first, Fs... rest)