diff options
author | Aleksandar Sasha Babic <aleksandar.babic@nokia.com> | 2009-11-03 09:36:52 (GMT) |
---|---|---|
committer | Aleksandar Sasha Babic <aleksandar.babic@nokia.com> | 2009-11-06 16:45:51 (GMT) |
commit | 676780d515cedca85829ae962e4f501c5e5b6581 (patch) | |
tree | b2994f82bf34031ae4b23ed9eb3a078e3c2a1e39 /src/corelib | |
parent | 03b19519768b504e5c7f5fd3923a14591e58b365 (diff) | |
download | Qt-676780d515cedca85829ae962e4f501c5e5b6581.zip Qt-676780d515cedca85829ae962e4f501c5e5b6581.tar.gz Qt-676780d515cedca85829ae962e4f501c5e5b6581.tar.bz2 |
Using qreal more consistently in code (prevent misuse of double)
We want to force use of qreal where possible. This can lead to better
performance on platforms where qreal -> float (i.e. ARM). To achieve
this we:
1. changed from 'double' to 'qreal', where justified
2. using qreal() to intialize constants, where justified
3. adding helper functions that are overloaded for qreal
like qAtan2(), qAcos(), qFabs() ...
4. defining QT_USE_MATH_H_FLOATS for Symbian platform
In addtion we used opportunity to improve code with some small things
5. converting divisions to multiplications
(i.e. '/ 2.0' -> '* qreal(0.5)')
6. defining new constants (i.e. 'Q_PI / 180.0' -> 'Q_PI180')
7. declaring variables as 'const', where justified
Reviewed-by: Andreas Aardal Hanssen
Reviewed-by: Gunnar Sletta
Reviewed-by: Jan-Arve
Reviewed-by: Kim Motoyoshi Kalland
Reviewed-by: Alessandro Portale
Reviewed-by: Janne Koskinen
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/global/qglobal.h | 4 | ||||
-rw-r--r-- | src/corelib/global/qnumeric_p.h | 6 | ||||
-rw-r--r-- | src/corelib/kernel/qmath.h | 45 | ||||
-rw-r--r-- | src/corelib/tools/qdatetime.cpp | 2 | ||||
-rw-r--r-- | src/corelib/tools/qeasingcurve.cpp | 20 | ||||
-rw-r--r-- | src/corelib/tools/qline.cpp | 12 |
6 files changed, 60 insertions, 29 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 9558256..6befb33 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1081,14 +1081,14 @@ template <typename T> inline T qAbs(const T &t) { return t >= 0 ? t : -t; } inline int qRound(qreal d) -{ return d >= 0.0 ? int(d + 0.5) : int(d - int(d-1) + 0.5) + int(d-1); } +{ return d >= qreal(0.0) ? int(d + qreal(0.5)) : int(d - int(d-1) + qreal(0.5)) + int(d-1); } #if defined(QT_NO_FPU) || defined(QT_ARCH_ARM) || defined(QT_ARCH_WINDOWSCE) || defined(QT_ARCH_SYMBIAN) inline qint64 qRound64(double d) { return d >= 0.0 ? qint64(d + 0.5) : qint64(d - qreal(qint64(d-1)) + 0.5) + qint64(d-1); } #else inline qint64 qRound64(qreal d) -{ return d >= 0.0 ? qint64(d + 0.5) : qint64(d - qreal(qint64(d-1)) + 0.5) + qint64(d-1); } +{ return d >= 0.0 ? qint64(d + qreal(0.5)) : qint64(d - qreal(qint64(d-1)) + qreal(0.5)) + qint64(d-1); } #endif template <typename T> diff --git a/src/corelib/global/qnumeric_p.h b/src/corelib/global/qnumeric_p.h index c8b5735..3f7b5b5 100644 --- a/src/corelib/global/qnumeric_p.h +++ b/src/corelib/global/qnumeric_p.h @@ -57,6 +57,12 @@ QT_BEGIN_NAMESPACE +static const qreal Q_PI = qreal(3.14159265358979323846); // pi +static const qreal Q_2PI = qreal(6.28318530717958647693); // 2*pi +static const qreal Q_PI2 = qreal(1.57079632679489661923); // pi/2 +static const qreal Q_PI180 = qreal(0.01745329251994329577); // pi/180 +static const qreal Q_180PI = qreal(57.29577951308232087685); // 180/pi + #if !defined(Q_CC_MIPS) static const union { unsigned char c[8]; double d; } qt_be_inf_bytes = { { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 } }; diff --git a/src/corelib/kernel/qmath.h b/src/corelib/kernel/qmath.h index a9e4378..ef3a4b0 100644 --- a/src/corelib/kernel/qmath.h +++ b/src/corelib/kernel/qmath.h @@ -45,6 +45,7 @@ #include <math.h> #include <QtCore/qglobal.h> +#include <private/qnumeric_p.h> QT_BEGIN_HEADER @@ -106,6 +107,16 @@ inline qreal qAcos(qreal v) return acos(v); } +inline qreal qAsin(qreal v) +{ +#ifdef QT_USE_MATH_H_FLOATS + if (sizeof(qreal) == sizeof(float)) + return asinf(float(v)); + else +#endif + return asin(v); +} + inline qreal qSqrt(qreal v) { #ifdef QT_USE_MATH_H_FLOATS @@ -136,28 +147,42 @@ inline qreal qPow(qreal x, qreal y) return pow(x, y); } -#ifndef M_PI -#define M_PI (3.14159265358979323846) -#endif - inline qreal qFastSin(qreal x) { - int si = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower. - qreal d = x - si * (2.0 * M_PI / QT_SINE_TABLE_SIZE); + int si = int(x * (qreal(0.5) * QT_SINE_TABLE_SIZE / Q_PI)); // Would be more accurate with qRound, but slower. + qreal d = x - si * (qreal(2.0) * Q_PI / QT_SINE_TABLE_SIZE); int ci = si + QT_SINE_TABLE_SIZE / 4; si &= QT_SINE_TABLE_SIZE - 1; ci &= QT_SINE_TABLE_SIZE - 1; - return qt_sine_table[si] + (qt_sine_table[ci] - 0.5 * qt_sine_table[si] * d) * d; + return qt_sine_table[si] + (qt_sine_table[ci] - qreal(0.5) * qt_sine_table[si] * d) * d; } inline qreal qFastCos(qreal x) { - int ci = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower. - qreal d = x - ci * (2.0 * M_PI / QT_SINE_TABLE_SIZE); + int ci = int(x * (qreal(0.5) * QT_SINE_TABLE_SIZE / Q_PI)); // Would be more accurate with qRound, but slower. + qreal d = x - ci * (qreal(2.0) * Q_PI / QT_SINE_TABLE_SIZE); int si = ci + QT_SINE_TABLE_SIZE / 4; si &= QT_SINE_TABLE_SIZE - 1; ci &= QT_SINE_TABLE_SIZE - 1; - return qt_sine_table[si] - (qt_sine_table[ci] + 0.5 * qt_sine_table[si] * d) * d; + return qt_sine_table[si] - (qt_sine_table[ci] + qreal(0.5) * qt_sine_table[si] * d) * d; +} + +inline qreal qFabs(qreal x) +{ +#ifdef QT_USE_MATH_H_FLOATS + if(sizeof(qreal) == sizeof(float)) + return fabsf(x); +#endif + return fabs(x); +} + +inline qreal qAtan2(qreal x, qreal y) +{ +#ifdef QT_USE_MATH_H_FLOATS + if(sizeof(qreal) == sizeof(float)) + return atan2f(x, y); +#endif + return atan2(x, y); } QT_END_NAMESPACE diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index db6435e..5ae2dde 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -1915,7 +1915,7 @@ QTime QTime::fromString(const QString& s, Qt::DateFormat f) const float msec(msec_s.toFloat(&ok)); if (!ok) return QTime(); - return QTime(hour, minute, second, qMin(qRound(msec * 1000.0), 999)); + return QTime(hour, minute, second, qMin(qRound(msec * qreal(1000.0)), 999)); } } } diff --git a/src/corelib/tools/qeasingcurve.cpp b/src/corelib/tools/qeasingcurve.cpp index 3e1e1ee..a629ebc 100644 --- a/src/corelib/tools/qeasingcurve.cpp +++ b/src/corelib/tools/qeasingcurve.cpp @@ -317,7 +317,7 @@ class QEasingCurveFunction public: enum Type { In, Out, InOut, OutIn }; - QEasingCurveFunction(QEasingCurveFunction::Type type = In, qreal period = 0.3, qreal amplitude = 1.0, + QEasingCurveFunction(QEasingCurveFunction::Type type = In, qreal period = qreal(0.3), qreal amplitude = qreal(1.0), qreal overshoot = 1.70158f) : _t(type), _p(period), _a(amplitude), _o(overshoot) { } @@ -667,7 +667,7 @@ bool QEasingCurve::operator==(const QEasingCurve &other) const */ qreal QEasingCurve::amplitude() const { - return d_ptr->config ? d_ptr->config->_a : 1.0; + return d_ptr->config ? d_ptr->config->_a : qreal(1.0); } /*! @@ -691,7 +691,7 @@ void QEasingCurve::setAmplitude(qreal amplitude) */ qreal QEasingCurve::period() const { - return d_ptr->config ? d_ptr->config->_p : 0.3; + return d_ptr->config ? d_ptr->config->_p : qreal(0.3); } /*! @@ -742,9 +742,9 @@ QEasingCurve::Type QEasingCurve::type() const void QEasingCurvePrivate::setType_helper(QEasingCurve::Type newType) { - qreal amp = -1.0; - qreal period = -1.0; - qreal overshoot = -1.0; + qreal amp = qreal(-1.0); + qreal period = qreal(-1.0); + qreal overshoot = qreal(-1.0); if (config) { amp = config->_a; @@ -754,13 +754,13 @@ void QEasingCurvePrivate::setType_helper(QEasingCurve::Type newType) config = 0; } - if (isConfigFunction(newType) || (amp != -1.0) || (period != -1.0) || (overshoot != -1.0)) { + if (isConfigFunction(newType) || (amp != qreal(-1.0)) || (period != qreal(-1.0)) || (overshoot != qreal(-1.0))) { config = curveToFunctionObject(newType); - if (amp != -1.0) + if (amp != qreal(-1.0)) config->_a = amp; - if (period != -1.0) + if (period != qreal(-1.0)) config->_p = period; - if (overshoot != -1.0) + if (overshoot != qreal(-1.0)) config->_o = overshoot; func = 0; } else if (newType != QEasingCurve::Custom) { diff --git a/src/corelib/tools/qline.cpp b/src/corelib/tools/qline.cpp index d0afb7a..5b30c97 100644 --- a/src/corelib/tools/qline.cpp +++ b/src/corelib/tools/qline.cpp @@ -574,7 +574,7 @@ qreal QLineF::angle() const const qreal dx = pt2.x() - pt1.x(); const qreal dy = pt2.y() - pt1.y(); - const qreal theta = atan2(-dy, dx) * 360.0 / M_2PI; + const qreal theta = qAtan2(-dy, dx) * qreal(360.0) / Q_2PI; const qreal theta_normalized = theta < 0 ? theta + 360 : theta; @@ -598,7 +598,7 @@ qreal QLineF::angle() const */ void QLineF::setAngle(qreal angle) { - const qreal angleR = angle * M_2PI / 360.0; + const qreal angleR = angle * Q_2PI / qreal(360.0); const qreal l = length(); const qreal dx = qCos(angleR) * l; @@ -620,7 +620,7 @@ void QLineF::setAngle(qreal angle) */ QLineF QLineF::fromPolar(qreal length, qreal angle) { - const qreal angleR = angle * M_2PI / 360.0; + const qreal angleR = angle * Q_2PI / qreal(360.0); return QLineF(0, 0, qCos(angleR) * length, -qSin(angleR) * length); } @@ -639,7 +639,7 @@ QLineF QLineF::unitVector() const QLineF f(p1(), QPointF(pt1.x() + x/len, pt1.y() + y/len)); #ifndef QT_NO_DEBUG - if (qAbs(f.length() - 1) >= 0.001) + if (qAbs(f.length() - 1) >= qreal(0.001)) qWarning("QLine::unitVector: New line does not have unit length"); #endif @@ -814,8 +814,8 @@ qreal QLineF::angle(const QLineF &l) const qreal cos_line = (dx()*l.dx() + dy()*l.dy()) / (length()*l.length()); qreal rad = 0; // only accept cos_line in the range [-1,1], if it is outside, use 0 (we return 0 rather than PI for those cases) - if (cos_line >= -1.0 && cos_line <= 1.0) rad = acos( cos_line ); - return rad * 360 / M_2PI; + if (cos_line >= qreal(-1.0) && cos_line <= qreal(1.0)) rad = qAcos( cos_line ); + return rad * 360 / Q_2PI; } |