From 0f720fe9d2c8e8e9816fa3fa22fecd85e30addb7 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Sat, 13 Mar 2021 00:46:59 +0200 Subject: [PATCH] assume size of qreal equals size of float when QT_NO_FPU is defined Signed-off-by: Ivailo Monev --- src/core/kernel/qmath.h | 121 +++++++++++------------ src/gui/graphicsview/qgraphicsanchorlayout_p.cpp | 6 +- src/gui/painting/qbrush.cpp | 48 ++++----- src/gui/painting/qpainterpath.cpp | 2 +- src/gui/painting/qpathclipper.cpp | 5 +- src/gui/painting/qpen.cpp | 41 ++++---- src/gui/painting/qtransform.cpp | 8 +- src/test/qtestcase.h | 4 +- tests/auto/qtransform/tst_qtransform.cpp | 5 +- 9 files changed, 123 insertions(+), 117 deletions(-) diff --git a/src/core/kernel/qmath.h b/src/core/kernel/qmath.h index dbaacc34c..04a7df380 100644 --- a/src/core/kernel/qmath.h +++ b/src/core/kernel/qmath.h @@ -45,141 +45,140 @@ inline qint64 qRound64(qreal d) inline int qCeil(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return int(ceilf(float(v))); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return int(ceilf(v)); +#else return int(ceil(v)); +#endif } inline int qFloor(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return int(floorf(float(v))); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return int(floorf(v)); +#else return int(floor(v)); +#endif } inline qreal qFabs(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if(sizeof(qreal) == sizeof(float)) - return fabsf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return fabsf(v); +#else return fabs(v); +#endif } inline qreal qSin(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return sinf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return sinf(v); +#else return sin(v); +#endif } inline qreal qCos(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return cosf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return cosf(v); +#else return cos(v); +#endif } inline qreal qTan(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return tanf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return tanf(v); +#else return tan(v); +#endif } inline qreal qAcos(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return acosf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return acosf(v); +#else return acos(v); +#endif } inline qreal qAsin(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return asinf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return asinf(v); +#else return asin(v); +#endif } inline qreal qAtan(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if(sizeof(qreal) == sizeof(float)) - return atanf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return atanf(v); +#else return atan(v); +#endif } inline qreal qAtan2(qreal x, qreal y) { -#ifdef QT_USE_MATH_H_FLOATS - if(sizeof(qreal) == sizeof(float)) - return atan2f(float(x), float(y)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return atan2f(x, y); +#else return atan2(x, y); +#endif } inline qreal qSqrt(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return sqrtf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return sqrtf(v); +#else return sqrt(v); +#endif } inline qreal qLn(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return logf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return logf(v); +#else return log(v); +#endif } inline qreal qExp(qreal v) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return expf(float(v)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return expf(v); +#else return exp(v); +#endif } inline qreal qPow(qreal x, qreal y) { -#ifdef QT_USE_MATH_H_FLOATS - if (sizeof(qreal) == sizeof(float)) - return powf(float(x), float(y)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return powf(x, y); +#else return pow(x, y); +#endif } inline qreal qFmod(qreal x, qreal y) { -#ifdef QT_USE_MATH_H_FLOATS - if(sizeof(qreal) == sizeof(float)) - return fmodf(float(x), float(y)); -#endif +#if defined(QT_USE_MATH_H_FLOATS) && defined(QT_NO_FPU) + return fmodf(x, y); +#else return fmod(x, y); +#endif } QT_END_NAMESPACE - #endif // QMATH_H diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp index 4bff932e5..77f4ae21c 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp @@ -40,7 +40,11 @@ QT_BEGIN_NAMESPACE // they are actually calculated in the interval [0, 2 * limit] // To avoid numerical errors in platforms where we use single precision, // we use a tighter limit for the variables range. -const qreal g_offset = (sizeof(qreal) == sizeof(double)) ? QWIDGETSIZE_MAX : QWIDGETSIZE_MAX / 32; +#ifdef QT_NO_FPU +static const qreal g_offset = QWIDGETSIZE_MAX / 32; +#else +static const qreal g_offset = QWIDGETSIZE_MAX; +#endif QGraphicsAnchorPrivate::QGraphicsAnchorPrivate() : QObjectPrivate(), layoutPrivate(0), data(0), diff --git a/src/gui/painting/qbrush.cpp b/src/gui/painting/qbrush.cpp index 209298ad6..f42e3592a 100644 --- a/src/gui/painting/qbrush.cpp +++ b/src/gui/painting/qbrush.cpp @@ -920,19 +920,19 @@ QDataStream &operator<<(QDataStream &s, const QBrush &b) s << int(gradient->interpolationMode()); - if (sizeof(qreal) == sizeof(double)) { - s << gradient->stops(); - } else { - // ensure that we write doubles here instead of streaming the stops - // directly; otherwise, platforms that redefine qreal might generate - // data that cannot be read on other platforms. - QVector stops = gradient->stops(); - s << quint32(stops.size()); - for (int i = 0; i < stops.size(); ++i) { - const QGradientStop &stop = stops.at(i); - s << QPair(double(stop.first), stop.second); - } +#ifdef QT_NO_FPU + // ensure that we write doubles here instead of streaming the stops + // directly; otherwise, platforms that redefine qreal might generate + // data that cannot be read on other platforms. + QVector stops = gradient->stops(); + s << quint32(stops.size()); + for (int i = 0; i < stops.size(); ++i) { + const QGradientStop &stop = stops.at(i); + s << QPair(double(stop.first), stop.second); } +#else + s << gradient->stops(); +#endif if (gradient->type() == QGradient::LinearGradient) { s << static_cast(gradient)->start(); @@ -991,19 +991,19 @@ QDataStream &operator>>(QDataStream &s, QBrush &b) s >> type_as_int; imode = QGradient::InterpolationMode(type_as_int); - if (sizeof(qreal) == sizeof(double)) { - s >> stops; - } else { - quint32 numStops; - double n; - QColor c; - - s >> numStops; - for (quint32 i = 0; i < numStops; ++i) { - s >> n >> c; - stops << QPair(n, c); - } +#ifdef QT_NO_FPU + quint32 numStops; + double n; + QColor c; + + s >> numStops; + for (quint32 i = 0; i < numStops; ++i) { + s >> n >> c; + stops << QPair(n, c); } +#else + s >> stops; +#endif if (type == QGradient::LinearGradient) { QPointF p1, p2; diff --git a/src/gui/painting/qpainterpath.cpp b/src/gui/painting/qpainterpath.cpp index df0eaf693..420215d4b 100644 --- a/src/gui/painting/qpainterpath.cpp +++ b/src/gui/painting/qpainterpath.cpp @@ -2173,7 +2173,7 @@ bool QPainterPath::operator==(const QPainterPath &path) const else if (d->elements.size() != path.d_func()->elements.size()) return false; - const qreal qt_epsilon = sizeof(qreal) == sizeof(double) ? 1e-12 : qreal(1e-5); + static const qreal qt_epsilon = std::numeric_limits::epsilon();; QSizeF epsilon = boundingRect().size(); epsilon.rwidth() *= qt_epsilon; diff --git a/src/gui/painting/qpathclipper.cpp b/src/gui/painting/qpathclipper.cpp index 378402899..9e00d5aa6 100644 --- a/src/gui/painting/qpathclipper.cpp +++ b/src/gui/painting/qpathclipper.cpp @@ -46,10 +46,7 @@ QT_BEGIN_NAMESPACE static inline bool fuzzyIsNull(qreal d) { - if (sizeof(qreal) == sizeof(double)) - return qAbs(d) <= 1e-12; - else - return qAbs(d) <= 1e-5f; + return qAbs(d) <= std::numeric_limits::epsilon(); } static inline bool comparePoints(const QPointF &a, const QPointF &b) diff --git a/src/gui/painting/qpen.cpp b/src/gui/painting/qpen.cpp index a3d1b5418..b1e26b3c1 100644 --- a/src/gui/painting/qpen.cpp +++ b/src/gui/painting/qpen.cpp @@ -870,17 +870,18 @@ QDataStream &operator<<(QDataStream &s, const QPen &p) s << double(p.widthF()); s << p.brush(); s << double(p.miterLimit()); - if (sizeof(qreal) == sizeof(double)) { - s << p.dashPattern(); - } else { - // ensure that we write doubles here instead of streaming the pattern - // directly; otherwise, platforms that redefine qreal might generate - // data that cannot be read on other platforms. - QVector pattern = p.dashPattern(); - s << quint32(pattern.size()); - for (int i = 0; i < pattern.size(); ++i) - s << double(pattern.at(i)); +#ifdef QT_NO_FPU + // ensure that we write doubles here instead of streaming the pattern + // directly; otherwise, platforms that redefine qreal might generate + // data that cannot be read on other platforms. + QVector pattern = p.dashPattern(); + s << quint32(pattern.size()); + for (int i = 0; i < pattern.size(); ++i) { + s << double(pattern.at(i)); } +#else + s << p.dashPattern(); +#endif s << double(p.dashOffset()); return s; } @@ -912,17 +913,17 @@ QDataStream &operator>>(QDataStream &s, QPen &p) s >> width; s >> brush; s >> miterLimit; - if (sizeof(qreal) == sizeof(double)) { - s >> dashPattern; - } else { - quint32 numDashes; - s >> numDashes; - double dash; - for (quint32 i = 0; i < numDashes; ++i) { - s >> dash; - dashPattern << dash; - } +#ifdef QT_NO_FPU + quint32 numDashes; + s >> numDashes; + double dash; + for (quint32 i = 0; i < numDashes; ++i) { + s >> dash; + dashPattern << dash; } +#else + s >> dashPattern; +#endif s >> dashOffset; p.detach(); diff --git a/src/gui/painting/qtransform.cpp b/src/gui/painting/qtransform.cpp index e110ca136..9b5066d71 100644 --- a/src/gui/painting/qtransform.cpp +++ b/src/gui/painting/qtransform.cpp @@ -33,7 +33,11 @@ QT_BEGIN_NAMESPACE -#define Q_NEAR_CLIP (sizeof(qreal) == sizeof(double) ? 0.000001 : 0.0001) +#ifdef QT_NO_FPU +# define Q_NEAR_CLIP 0.0001 +#else +# define Q_NEAR_CLIP 0.000001 +#endif #define MAP(x, y, nx, ny) \ do { \ @@ -59,7 +63,7 @@ QT_BEGIN_NAMESPACE break; \ case TxProject: \ qreal w = (m_13 * FX_ + m_23 * FY_ + m_33); \ - if (w < qreal(Q_NEAR_CLIP)) w = qreal(Q_NEAR_CLIP); \ + if (w < Q_NEAR_CLIP) w = Q_NEAR_CLIP; \ w = 1./w; \ nx = affine._m11 * FX_ + affine._m21 * FY_ + affine._dx * w; \ ny = affine._m12 * FX_ + affine._m22 * FY_ + affine._dy * w; \ diff --git a/src/test/qtestcase.h b/src/test/qtestcase.h index 019194ccc..7c3533e2b 100644 --- a/src/test/qtestcase.h +++ b/src/test/qtestcase.h @@ -202,14 +202,14 @@ namespace QTest inline bool qCompare(qreal const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line) { - return qCompare(float(t1), float(t2), actual, expected, file, line); + return qCompare(t1, float(t2), actual, expected, file, line); } template <> inline bool qCompare(double const &t1, qreal const &t2, const char *actual, const char *expected, const char *file, int line) { - return qCompare(float(t1), float(t2), actual, expected, file, line); + return qCompare(float(t1), t2, actual, expected, file, line); } #endif diff --git a/tests/auto/qtransform/tst_qtransform.cpp b/tests/auto/qtransform/tst_qtransform.cpp index abd1c6fbf..2a4f3bae2 100644 --- a/tests/auto/qtransform/tst_qtransform.cpp +++ b/tests/auto/qtransform/tst_qtransform.cpp @@ -722,8 +722,9 @@ void tst_QTransform::inverted_data() void tst_QTransform::inverted() { - if (sizeof(qreal) != sizeof(double)) - QSKIP("precision error if qreal is not double", SkipAll); +#ifdef QT_NO_FPU + QSKIP("precision error if qreal is not double", SkipAll); +#endif QFETCH(QTransform, matrix); -- 2.11.0