qIsNull: redo implementation
The reason for using type-punning through an integer was to avoid compiler warnings about float comparisons. We have now a better mechanism to suppress such warnings, so there is no need to be "clever" and trigger UB because of the union usage. Drive-by change: add constexpr+noexcept because now there's no longer UB. Change-Id: I29f7514e39055658d4ef6c431daf5abfc660df16 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
1b16f79bf2
commit
fe57936d8c
@ -902,38 +902,22 @@ Q_REQUIRED_RESULT Q_DECL_CONSTEXPR static inline Q_DECL_UNUSED bool qFuzzyIsNul
|
|||||||
return qAbs(f) <= 0.00001f;
|
return qAbs(f) <= 0.00001f;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
QT_WARNING_PUSH
|
||||||
This function tests a double for a null value. It doesn't
|
QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
|
||||||
check whether the actual value is 0 or close to 0, but whether
|
QT_WARNING_DISABLE_GCC("-Wfloat-equal")
|
||||||
it is binary 0, disregarding sign.
|
|
||||||
*/
|
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR static inline Q_DECL_UNUSED bool qIsNull(double d) noexcept
|
||||||
Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool qIsNull(double d)
|
|
||||||
{
|
{
|
||||||
union U {
|
return d == 0.0;
|
||||||
double d;
|
|
||||||
quint64 u;
|
|
||||||
};
|
|
||||||
U val;
|
|
||||||
val.d = d;
|
|
||||||
return (val.u & Q_UINT64_C(0x7fffffffffffffff)) == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR static inline Q_DECL_UNUSED bool qIsNull(float f) noexcept
|
||||||
This function tests a float for a null value. It doesn't
|
|
||||||
check whether the actual value is 0 or close to 0, but whether
|
|
||||||
it is binary 0, disregarding sign.
|
|
||||||
*/
|
|
||||||
Q_REQUIRED_RESULT static inline Q_DECL_UNUSED bool qIsNull(float f)
|
|
||||||
{
|
{
|
||||||
union U {
|
return f == 0.0f;
|
||||||
float f;
|
|
||||||
quint32 u;
|
|
||||||
};
|
|
||||||
U val;
|
|
||||||
val.f = f;
|
|
||||||
return (val.u & 0x7fffffff) == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QT_WARNING_POP
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Compilers which follow outdated template instantiation rules
|
Compilers which follow outdated template instantiation rules
|
||||||
require a class to have a comparison operator to exist when
|
require a class to have a comparison operator to exist when
|
||||||
|
Loading…
x
Reference in New Issue
Block a user