QFixed{,Size}: reformulate some functions in a constexpr-friendly way
[profile/ivi/qtbase.git] / src / gui / painting / qfixed_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QFIXED_P_H
43 #define QFIXED_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists for the convenience
50 // of other Qt classes.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "QtCore/qdebug.h"
57 #include "QtCore/qpoint.h"
58 #include "QtCore/qsize.h"
59
60 QT_BEGIN_NAMESPACE
61
62 struct QFixed {
63 private:
64     QFixed(int val, int) : val(val) {} // 2nd int is just a dummy for disambiguation
65 public:
66     QFixed() : val(0) {}
67     QFixed(int i) : val(i<<6) {}
68     QFixed(long i) : val(i<<6) {}
69     QFixed &operator=(int i) { val = (i<<6); return *this; }
70     QFixed &operator=(long i) { val = (i<<6); return *this; }
71
72     static QFixed fromReal(qreal r) { return fromFixed((int)(r*qreal(64))); }
73     static QFixed fromFixed(int fixed) { return QFixed(fixed,0); } // uses private ctor
74
75     inline int value() const { return val; }
76     inline void setValue(int value) { val = value; }
77
78     inline int toInt() const { return (((val)+32) & -64)>>6; }
79     inline qreal toReal() const { return ((qreal)val)/(qreal)64; }
80
81     inline int truncate() const { return val>>6; }
82     inline QFixed round() const { return fromFixed(((val)+32) & -64); }
83     inline QFixed floor() const { return fromFixed((val) & -64); }
84     inline QFixed ceil() const { return fromFixed((val+63) & -64); }
85
86     inline QFixed operator+(int i) const { return fromFixed((val + (i<<6))); }
87     inline QFixed operator+(uint i) const { return fromFixed((val + (i<<6))); }
88     inline QFixed operator+(const QFixed &other) const { return fromFixed((val + other.val)); }
89     inline QFixed &operator+=(int i) { val += (i<<6); return *this; }
90     inline QFixed &operator+=(uint i) { val += (i<<6); return *this; }
91     inline QFixed &operator+=(const QFixed &other) { val += other.val; return *this; }
92     inline QFixed operator-(int i) const { return fromFixed((val - (i<<6))); }
93     inline QFixed operator-(uint i) const { return fromFixed((val - (i<<6))); }
94     inline QFixed operator-(const QFixed &other) const { return fromFixed((val - other.val)); }
95     inline QFixed &operator-=(int i) { val -= (i<<6); return *this; }
96     inline QFixed &operator-=(uint i) { val -= (i<<6); return *this; }
97     inline QFixed &operator-=(const QFixed &other) { val -= other.val; return *this; }
98     inline QFixed operator-() const { return fromFixed(-val); }
99
100     inline bool operator==(const QFixed &other) const { return val == other.val; }
101     inline bool operator!=(const QFixed &other) const { return val != other.val; }
102     inline bool operator<(const QFixed &other) const { return val < other.val; }
103     inline bool operator>(const QFixed &other) const { return val > other.val; }
104     inline bool operator<=(const QFixed &other) const { return val <= other.val; }
105     inline bool operator>=(const QFixed &other) const { return val >= other.val; }
106     inline bool operator!() const { return !val; }
107
108     inline QFixed &operator/=(int x) { val /= x; return *this; }
109     inline QFixed &operator/=(const QFixed &o) {
110         if (o.val == 0) {
111             val = 0x7FFFFFFFL;
112         } else {
113             bool neg = false;
114             qint64 a = val;
115             qint64 b = o.val;
116             if (a < 0) { a = -a; neg = true; }
117             if (b < 0) { b = -b; neg = !neg; }
118
119             int res = (int)(((a << 6) + (b >> 1)) / b);
120
121             val = (neg ? -res : res);
122         }
123         return *this;
124     }
125     inline QFixed operator/(int d) const { return fromFixed(val/d); }
126     inline QFixed operator/(QFixed b) const { QFixed f = *this; return (f /= b); }
127     inline QFixed operator>>(int d) const { QFixed f = *this; f.val >>= d; return f; }
128     inline QFixed &operator*=(int i) { val *= i; return *this; }
129     inline QFixed &operator*=(uint i) { val *= i; return *this; }
130     inline QFixed &operator*=(const QFixed &o) {
131         bool neg = false;
132         qint64 a = val;
133         qint64 b = o.val;
134         if (a < 0) { a = -a; neg = true; }
135         if (b < 0) { b = -b; neg = !neg; }
136
137         int res = (int)((a * b + 0x20L) >> 6);
138         val = neg ? -res : res;
139         return *this;
140     }
141     inline QFixed operator*(int i) const { return fromFixed(val * i); }
142     inline QFixed operator*(uint i) const { return fromFixed(val * i); }
143     inline QFixed operator*(const QFixed &o) const { QFixed f = *this; return (f *= o); }
144
145 private:
146     QFixed(qreal i) : val((int)(i*qreal(64))) {}
147     QFixed &operator=(qreal i) { val = (int)(i*qreal(64)); return *this; }
148     inline QFixed operator+(qreal i) const { return fromFixed((val + (int)(i*qreal(64)))); }
149     inline QFixed &operator+=(qreal i) { val += (int)(i*64); return *this; }
150     inline QFixed operator-(qreal i) const { return fromFixed((val - (int)(i*qreal(64)))); }
151     inline QFixed &operator-=(qreal i) { val -= (int)(i*64); return *this; }
152     inline QFixed &operator/=(qreal r) { val = (int)(val/r); return *this; }
153     inline QFixed operator/(qreal d) const { return fromFixed((int)(val/d)); }
154     inline QFixed &operator*=(qreal d) { val = (int) (val*d); return *this; }
155     inline QFixed operator*(qreal d) const { return fromFixed((int) (val*d)); }
156     int val;
157 };
158 Q_DECLARE_TYPEINFO(QFixed, Q_PRIMITIVE_TYPE);
159
160 #define QFIXED_MAX (INT_MAX/256)
161
162 inline int qRound(const QFixed &f) { return f.toInt(); }
163 inline int qFloor(const QFixed &f) { return f.floor().truncate(); }
164
165 inline QFixed operator*(int i, const QFixed &d) { return d*i; }
166 inline QFixed operator+(int i, const QFixed &d) { return d+i; }
167 inline QFixed operator-(int i, const QFixed &d) { return -(d-i); }
168 inline QFixed operator*(uint i, const QFixed &d) { return d*i; }
169 inline QFixed operator+(uint i, const QFixed &d) { return d+i; }
170 inline QFixed operator-(uint i, const QFixed &d) { return -(d-i); }
171 // inline QFixed operator*(qreal d, const QFixed &d2) { return d2*d; }
172
173 inline bool operator==(const QFixed &f, int i) { return f.value() == (i<<6); }
174 inline bool operator==(int i, const QFixed &f) { return f.value() == (i<<6); }
175 inline bool operator!=(const QFixed &f, int i) { return f.value() != (i<<6); }
176 inline bool operator!=(int i, const QFixed &f) { return f.value() != (i<<6); }
177 inline bool operator<=(const QFixed &f, int i) { return f.value() <= (i<<6); }
178 inline bool operator<=(int i, const QFixed &f) { return (i<<6) <= f.value(); }
179 inline bool operator>=(const QFixed &f, int i) { return f.value() >= (i<<6); }
180 inline bool operator>=(int i, const QFixed &f) { return (i<<6) >= f.value(); }
181 inline bool operator<(const QFixed &f, int i) { return f.value() < (i<<6); }
182 inline bool operator<(int i, const QFixed &f) { return (i<<6) < f.value(); }
183 inline bool operator>(const QFixed &f, int i) { return f.value() > (i<<6); }
184 inline bool operator>(int i, const QFixed &f) { return (i<<6) > f.value(); }
185
186 #ifndef QT_NO_DEBUG_STREAM
187 inline QDebug &operator<<(QDebug &dbg, const QFixed &f)
188 { return dbg << f.toReal(); }
189 #endif
190
191 struct QFixedPoint {
192     QFixed x;
193     QFixed y;
194     inline QFixedPoint() {}
195     inline QFixedPoint(const QFixed &_x, const QFixed &_y) : x(_x), y(_y) {}
196     QPointF toPointF() const { return QPointF(x.toReal(), y.toReal()); }
197     static QFixedPoint fromPointF(const QPointF &p) {
198         return QFixedPoint(QFixed::fromReal(p.x()), QFixed::fromReal(p.y()));
199     }
200 };
201 Q_DECLARE_TYPEINFO(QFixedPoint, Q_PRIMITIVE_TYPE);
202
203 inline QFixedPoint operator-(const QFixedPoint &p1, const QFixedPoint &p2)
204 { return QFixedPoint(p1.x - p2.x, p1.y - p2.y); }
205 inline QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2)
206 { return QFixedPoint(p1.x + p2.x, p1.y + p2.y); }
207
208 struct QFixedSize {
209     QFixed width;
210     QFixed height;
211     QFixedSize() {}
212     QFixedSize(QFixed _width, QFixed _height) : width(_width), height(_height) {}
213     QSizeF toSizeF() const { return QSizeF(width.toReal(), height.toReal()); }
214     static QFixedSize fromSizeF(const QSizeF &s) {
215         return QFixedSize(QFixed::fromReal(s.width()), QFixed::fromReal(s.height()));
216     }
217 };
218 Q_DECLARE_TYPEINFO(QFixedSize, Q_PRIMITIVE_TYPE);
219
220 QT_END_NAMESPACE
221
222 #endif // QTEXTENGINE_P_H