44 #include <QtCore/qglobal.h>
45 #include <QtCore/qmetatype.h>
46 #include <QtCore/qnamespace.h>
50 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__AVX2__) && !defined(__F16C__)
54 # if defined(Q_CC_INTEL) || defined(Q_CC_MSVC)
59 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
60 #include <immintrin.h>
66 #pragma qt_class(QFloat16)
67 #pragma qt_no_master_include
70 #ifndef QT_NO_DATASTREAM
84 constexpr
inline qfloat16() noexcept : b16(0) {}
87 inline operator float()
const noexcept;
90 bool isInf()
const noexcept {
return (b16 & 0x7fff) == 0x7c00; }
91 bool isNaN()
const noexcept {
return (b16 & 0x7fff) > 0x7c00; }
96 {
return qfloat16(Wrap((sign.b16 & 0x8000) | (b16 & 0x7fff))); }
105 #if QT_CONFIG(signaling_nan)
106 static constexpr
qfloat16 _limit_signaling_NaN() noexcept {
return qfloat16(Wrap(0x7d00)); }
109 {
return (b16 & 0x7c00) && (b16 & 0x7c00) != 0x7c00; }
112 constexpr
inline explicit qfloat16(Wrap nibble) noexcept : b16(nibble.b16) {}
114 Q_CORE_EXPORT
static const quint32 mantissatable[];
115 Q_CORE_EXPORT
static const quint32 exponenttable[];
116 Q_CORE_EXPORT
static const quint32 offsettable[];
117 Q_CORE_EXPORT
static const quint16 basetable[];
118 Q_CORE_EXPORT
static const quint16 shifttable[];
119 Q_CORE_EXPORT
static const quint32 roundtable[];
135 #define QF16_MAKE_ARITH_OP_FP(FP, OP) \
136 friend inline FP operator OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; } \
137 friend inline FP operator OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); }
138 #define QF16_MAKE_ARITH_OP_EQ_FP(FP, OP_EQ, OP) \
139 friend inline qfloat16& operator OP_EQ(qfloat16& lhs, FP rhs) noexcept \
140 { lhs = qfloat16(float(static_cast<FP>(lhs) OP rhs)); return lhs; }
141 #define QF16_MAKE_ARITH_OP(FP) \
142 QF16_MAKE_ARITH_OP_FP(FP, +) \
143 QF16_MAKE_ARITH_OP_FP(FP, -) \
144 QF16_MAKE_ARITH_OP_FP(FP, *) \
145 QF16_MAKE_ARITH_OP_FP(FP, /) \
146 QF16_MAKE_ARITH_OP_EQ_FP(FP, +=, +) \
147 QF16_MAKE_ARITH_OP_EQ_FP(FP, -=, -) \
148 QF16_MAKE_ARITH_OP_EQ_FP(FP, *=, *) \
149 QF16_MAKE_ARITH_OP_EQ_FP(FP, /=, /)
154 #undef QF16_MAKE_ARITH_OP
155 #undef QF16_MAKE_ARITH_OP_FP
157 #define QF16_MAKE_ARITH_OP_INT(OP) \
158 friend inline double operator OP(qfloat16 lhs, int rhs) noexcept { return static_cast<double>(lhs) OP rhs; } \
159 friend inline double operator OP(int lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<double>(rhs); }
165 #undef QF16_MAKE_ARITH_OP_INT
177 #define QF16_MAKE_BOOL_OP_FP(FP, OP) \
178 friend inline bool operator OP(qfloat16 lhs, FP rhs) noexcept { return static_cast<FP>(lhs) OP rhs; } \
179 friend inline bool operator OP(FP lhs, qfloat16 rhs) noexcept { return lhs OP static_cast<FP>(rhs); }
180 #define QF16_MAKE_BOOL_OP(FP) \
181 QF16_MAKE_BOOL_OP_FP(FP, <) \
182 QF16_MAKE_BOOL_OP_FP(FP, >) \
183 QF16_MAKE_BOOL_OP_FP(FP, >=) \
184 QF16_MAKE_BOOL_OP_FP(FP, <=) \
185 QF16_MAKE_BOOL_OP_FP(FP, ==) \
186 QF16_MAKE_BOOL_OP_FP(FP, !=)
191 #undef QF16_MAKE_BOOL_OP
192 #undef QF16_MAKE_BOOL_OP_FP
194 #define QF16_MAKE_BOOL_OP_INT(OP) \
195 friend inline bool operator OP(qfloat16 a, int b) noexcept { return static_cast<float>(a) OP static_cast<float>(b); } \
196 friend inline bool operator OP(int a, qfloat16 b) noexcept { return static_cast<float>(a) OP static_cast<float>(b); }
204 #undef QF16_MAKE_BOOL_OP_INT
208 #ifndef QT_NO_DATASTREAM
228 {
return qRound(
static_cast<float>(
d)); }
231 {
return qRound64(
static_cast<float>(
d)); }
235 float f1 =
static_cast<float>(
p1);
236 float f2 =
static_cast<float>(
p2);
243 return (
qAbs(f1 - f2) * 102.5f <= qMin(
qAbs(f1),
qAbs(f2)));
251 return qAbs(
f) < 0.00976f;
256 return (
f.b16 &
static_cast<quint16>(0x7fff)) == 0;
260 {
return int(
static_cast<float>(
f)); }
268 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
269 __m128 packsingle = _mm_set_ss(
f);
270 __m128i packhalf = _mm_cvtps_ph(packsingle, 0);
271 b16 = _mm_extract_epi16(packhalf, 0);
272 #elif defined (__ARM_FP16_FORMAT_IEEE)
273 __fp16 f16 = __fp16(
f);
274 memcpy(&b16, &f16,
sizeof(
quint16));
282 quint32 mantissa = (
u & 0x007fffff);
283 if ((signAndExp & 0xff) == 0xff) {
285 mantissa = qMax(1U <<
shift, mantissa);
289 if (mantissa & (1 <<
shift))
300 inline qfloat16::operator float()
const noexcept
302 #if defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)
303 __m128i packhalf = _mm_cvtsi32_si128(b16);
304 __m128 packsingle = _mm_cvtph_ps(packhalf);
305 return _mm_cvtss_f32(packsingle);
306 #elif defined (__ARM_FP16_FORMAT_IEEE)
308 memcpy(&f16, &b16,
sizeof(
quint16));
311 quint32 u = mantissatable[offsettable[b16 >> 10] + (b16 & 0x3ff)]
312 + exponenttable[b16 >> 10];
324 template <
typename R>
326 template <
typename R>
332 template<
typename F,
typename ...Fs>
auto qHypot(F
first, Fs... rest);
339 #if (defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)) || defined (__ARM_FP16_FORMAT_IEEE)
345 #if defined(__cpp_lib_hypot) && __cpp_lib_hypot >= 201603L
350 template <
typename Ty,
typename Tz,
351 typename std::enable_if<
353 !(std::is_same_v<qfloat16, Ty> && std::is_same_v<qfloat16, Tz>),
int>::
type = 0>
355 template <
typename Tx,
typename Tz,
356 typename std::enable_if<
358 !std::is_same_v<qfloat16, Tx>,
int>
::type = 0>
360 template <
typename Tx,
typename Ty,
361 typename std::enable_if<
363 !std::is_same_v<qfloat16, Tx> && !std::is_same_v<qfloat16, Ty>,
int>
::type = 0>
369 #if (defined(QT_COMPILER_SUPPORTS_F16C) && defined(__F16C__)) || defined (__ARM_FP16_FORMAT_IEEE)
396 static constexpr
int digits = 11;
397 static constexpr
int min_exponent = -13;
398 static constexpr
int max_exponent = 16;
400 static constexpr
int digits10 = 3;
401 static constexpr
int max_digits10 = 5;
402 static constexpr
int min_exponent10 = -4;
403 static constexpr
int max_exponent10 = 4;
419 #if QT_CONFIG(signaling_nan)
423 static constexpr
bool has_signaling_NaN =
false;
428 :
public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
430 :
public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
432 :
public numeric_limits<QT_PREPEND_NAMESPACE(qfloat16)> {};
small capitals from c petite p scientific f u
The QDataStream class provides serialization of binary data to a QIODevice.
bool qFuzzyCompare(const QMatrix4x4 &m1, const QMatrix4x4 &m2)
auto add(F first, Fs... rest) const
Provides 16-bit floating point support.
constexpr qfloat16() noexcept
Q_CORE_EXPORT void qFloatFromFloat16(float *out, const qfloat16 *in, qsizetype len) noexcept
QT_WARNING_POP friend Q_CORE_EXPORT QDataStream & operator<<(QDataStream &ds, qfloat16 f)
constexpr bool isNormal() const noexcept
static constexpr qfloat16 _limit_quiet_NaN() noexcept
friend bool operator<(qfloat16 a, qfloat16 b) noexcept
friend bool operator!=(qfloat16 a, qfloat16 b) noexcept
static constexpr qfloat16 _limit_min() noexcept
bool isInf() const noexcept
friend bool operator>=(qfloat16 a, qfloat16 b) noexcept
friend bool operator==(qfloat16 a, qfloat16 b) noexcept
friend Q_CORE_EXPORT QDataStream & operator>>(QDataStream &ds, qfloat16 &f)
QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE friend bool operator>(qfloat16 a, qfloat16 b) noexcept
static constexpr qfloat16 _limit_infinity() noexcept
friend bool qIsNull(qfloat16 f) noexcept
static constexpr qfloat16 _limit_epsilon() noexcept
static constexpr qfloat16 _limit_denorm_min() noexcept
Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *out, const float *in, qsizetype len) noexcept
bool isNaN() const noexcept
qfloat16(Qt::Initialization) noexcept
friend qfloat16 operator/(qfloat16 a, qfloat16 b) noexcept
friend bool operator<=(qfloat16 a, qfloat16 b) noexcept
friend qfloat16 operator-(qfloat16 a, qfloat16 b) noexcept
Q_CORE_EXPORT int fpClassify() const 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
qfloat16 copySign(qfloat16 sign) const noexcept
static constexpr qfloat16 _limit_max() noexcept
static constexpr QT_PREPEND_NAMESPACE(qfloat16) lowest()
static constexpr QT_PREPEND_NAMESPACE(qfloat16) epsilon()
static constexpr QT_PREPEND_NAMESPACE(qfloat16) infinity()
static constexpr QT_PREPEND_NAMESPACE(qfloat16) quiet_NaN()
static constexpr QT_PREPEND_NAMESPACE(qfloat16) denorm_min()
auto it unsigned count const
#define QT_WARNING_DISABLE_FLOAT_COMPARE
#define QT_WARNING_DISABLE_GCC(text)
#define QT_WARNING_DISABLE_CLANG(text)
EGLOutputLayerEXT EGLint EGLAttrib value
#define QF16_MAKE_ARITH_OP_INT(OP)
bool qIsFinite(qfloat16 f) noexcept
#define QF16_MAKE_ARITH_OP(FP)
#define QF16_MAKE_BOOL_OP(FP)
bool qFuzzyIsNull(qfloat16 f) noexcept
bool qIsNaN(qfloat16 f) noexcept
int qFpClassify(qfloat16 f) noexcept
#define QF16_MAKE_BOOL_OP_INT(OP)
bool qIsInf(qfloat16 f) noexcept
Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE)
bool qIsNull(qfloat16 f) noexcept
qint64 qRound64(qfloat16 d) noexcept
int qRound(qfloat16 d) noexcept
int qIntCast(qfloat16 f) noexcept
auto qHypot(F first, Fs... rest)
GLenum GLuint GLenum GLsizei length
GLboolean GLboolean GLboolean b
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat z
GLint GLint GLint GLint GLint x
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
int QT_PREPEND_NAMESPACE(QSharedMemoryPrivate)
decltype(std::hypot(R(1), 1.0f)) type
decltype(std::hypot(1.0f, R(1))) type