QtBase  v6.3.1
qfixed_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QFIXED_P_H
41 #define QFIXED_P_H
42 
43 //
44 // W A R N I N G
45 // -------------
46 //
47 // This file is not part of the Qt API. It exists for the convenience
48 // of other Qt classes. This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtGui/private/qtguiglobal_p.h>
55 #include "QtCore/qdebug.h"
56 #include "QtCore/qpoint.h"
57 #include "QtCore/qsize.h"
58 
60 
61 struct QFixed {
62 private:
63  constexpr QFixed(int val, int) : val(val) {} // 2nd int is just a dummy for disambiguation
64 public:
65  constexpr QFixed() : val(0) {}
66  constexpr QFixed(int i) : val(i * 64) {}
67  constexpr QFixed(long i) : val(i * 64) {}
68  QFixed &operator=(int i) { val = i * 64; return *this; }
69  QFixed &operator=(long i) { val = i * 64; return *this; }
70 
71  constexpr static QFixed fromReal(qreal r) { return fromFixed((int)(r*qreal(64))); }
72  constexpr static QFixed fromFixed(int fixed) { return QFixed(fixed,0); } // uses private ctor
73 
74  constexpr inline int value() const { return val; }
75  inline void setValue(int value) { val = value; }
76 
77  constexpr inline int toInt() const { return (((val)+32) & -64)>>6; }
78  constexpr inline qreal toReal() const { return ((qreal)val)/(qreal)64; }
79 
80  constexpr inline int truncate() const { return val>>6; }
81  constexpr inline QFixed round() const { return fromFixed(((val)+32) & -64); }
82  constexpr inline QFixed floor() const { return fromFixed((val) & -64); }
83  constexpr inline QFixed ceil() const { return fromFixed((val+63) & -64); }
84 
85  constexpr inline QFixed operator+(int i) const { return fromFixed(val + i * 64); }
86  constexpr inline QFixed operator+(uint i) const { return fromFixed((val + (i<<6))); }
87  constexpr inline QFixed operator+(const QFixed &other) const { return fromFixed((val + other.val)); }
88  inline QFixed &operator+=(int i) { val += i * 64; return *this; }
89  inline QFixed &operator+=(uint i) { val += (i<<6); return *this; }
90  inline QFixed &operator+=(const QFixed &other) { val += other.val; return *this; }
91  constexpr inline QFixed operator-(int i) const { return fromFixed(val - i * 64); }
92  constexpr inline QFixed operator-(uint i) const { return fromFixed((val - (i<<6))); }
93  constexpr inline QFixed operator-(const QFixed &other) const { return fromFixed((val - other.val)); }
94  inline QFixed &operator-=(int i) { val -= i * 64; return *this; }
95  inline QFixed &operator-=(uint i) { val -= (i<<6); return *this; }
96  inline QFixed &operator-=(const QFixed &other) { val -= other.val; return *this; }
97  constexpr inline QFixed operator-() const { return fromFixed(-val); }
98 
99  constexpr inline bool operator==(const QFixed &other) const { return val == other.val; }
100  constexpr inline bool operator!=(const QFixed &other) const { return val != other.val; }
101  constexpr inline bool operator<(const QFixed &other) const { return val < other.val; }
102  constexpr inline bool operator>(const QFixed &other) const { return val > other.val; }
103  constexpr inline bool operator<=(const QFixed &other) const { return val <= other.val; }
104  constexpr inline bool operator>=(const QFixed &other) const { return val >= other.val; }
105  constexpr inline bool operator!() const { return !val; }
106 
107  inline QFixed &operator/=(int x) { val /= x; return *this; }
108  inline QFixed &operator/=(const QFixed &o) {
109  if (o.val == 0) {
110  val = 0x7FFFFFFFL;
111  } else {
112  bool neg = false;
113  qint64 a = val;
114  qint64 b = o.val;
115  if (a < 0) { a = -a; neg = true; }
116  if (b < 0) { b = -b; neg = !neg; }
117 
118  int res = (int)(((a << 6) + (b >> 1)) / b);
119 
120  val = (neg ? -res : res);
121  }
122  return *this;
123  }
124  constexpr inline QFixed operator/(int d) const { return fromFixed(val/d); }
125  inline QFixed operator/(QFixed b) const { QFixed f = *this; return (f /= b); }
126  inline QFixed operator>>(int d) const { QFixed f = *this; f.val >>= d; return f; }
127  inline QFixed &operator*=(int i) { val *= i; return *this; }
128  inline QFixed &operator*=(uint i) { val *= i; return *this; }
129  inline QFixed &operator*=(const QFixed &o) {
130  bool neg = false;
131  qint64 a = val;
132  qint64 b = o.val;
133  if (a < 0) { a = -a; neg = true; }
134  if (b < 0) { b = -b; neg = !neg; }
135 
136  int res = (int)((a * b + 0x20L) >> 6);
137  val = neg ? -res : res;
138  return *this;
139  }
140  constexpr inline QFixed operator*(int i) const { return fromFixed(val * i); }
141  constexpr inline QFixed operator*(uint i) const { return fromFixed(val * i); }
142  inline QFixed operator*(const QFixed &o) const { QFixed f = *this; return (f *= o); }
143 
144 private:
145  constexpr QFixed(qreal i) : val((int)(i*qreal(64))) {}
146  QFixed &operator=(qreal i) { val = (int)(i*qreal(64)); return *this; }
147  constexpr inline QFixed operator+(qreal i) const { return fromFixed((val + (int)(i*qreal(64)))); }
148  inline QFixed &operator+=(qreal i) { val += (int)(i*64); return *this; }
149  constexpr inline QFixed operator-(qreal i) const { return fromFixed((val - (int)(i*qreal(64)))); }
150  inline QFixed &operator-=(qreal i) { val -= (int)(i*64); return *this; }
151  inline QFixed &operator/=(qreal r) { val = (int)(val/r); return *this; }
152  constexpr inline QFixed operator/(qreal d) const { return fromFixed((int)(val/d)); }
153  inline QFixed &operator*=(qreal d) { val = (int) (val*d); return *this; }
154  constexpr inline QFixed operator*(qreal d) const { return fromFixed((int) (val*d)); }
155  int val;
156 };
158 
159 #define QFIXED_MAX (INT_MAX/256)
160 
161 constexpr inline int qRound(const QFixed &f) { return f.toInt(); }
162 constexpr inline int qFloor(const QFixed &f) { return f.floor().truncate(); }
163 
164 constexpr inline QFixed operator*(int i, const QFixed &d) { return d*i; }
165 constexpr inline QFixed operator+(int i, const QFixed &d) { return d+i; }
166 constexpr inline QFixed operator-(int i, const QFixed &d) { return -(d-i); }
167 constexpr inline QFixed operator*(uint i, const QFixed &d) { return d*i; }
168 constexpr inline QFixed operator+(uint i, const QFixed &d) { return d+i; }
169 constexpr inline QFixed operator-(uint i, const QFixed &d) { return -(d-i); }
170 // constexpr inline QFixed operator*(qreal d, const QFixed &d2) { return d2*d; }
171 
172 constexpr inline bool operator==(const QFixed &f, int i) { return f.value() == i * 64; }
173 constexpr inline bool operator==(int i, const QFixed &f) { return f.value() == i * 64; }
174 constexpr inline bool operator!=(const QFixed &f, int i) { return f.value() != i * 64; }
175 constexpr inline bool operator!=(int i, const QFixed &f) { return f.value() != i * 64; }
176 constexpr inline bool operator<=(const QFixed &f, int i) { return f.value() <= i * 64; }
177 constexpr inline bool operator<=(int i, const QFixed &f) { return i * 64 <= f.value(); }
178 constexpr inline bool operator>=(const QFixed &f, int i) { return f.value() >= i * 64; }
179 constexpr inline bool operator>=(int i, const QFixed &f) { return i * 64 >= f.value(); }
180 constexpr inline bool operator<(const QFixed &f, int i) { return f.value() < i * 64; }
181 constexpr inline bool operator<(int i, const QFixed &f) { return i * 64 < f.value(); }
182 constexpr inline bool operator>(const QFixed &f, int i) { return f.value() > i * 64; }
183 constexpr inline bool operator>(int i, const QFixed &f) { return i * 64 > f.value(); }
184 
185 #ifndef QT_NO_DEBUG_STREAM
186 inline QDebug &operator<<(QDebug &dbg, const QFixed &f)
187 { return dbg << f.toReal(); }
188 #endif
189 
190 struct QFixedPoint {
193  constexpr inline QFixedPoint() {}
194  constexpr inline QFixedPoint(const QFixed &_x, const QFixed &_y) : x(_x), y(_y) {}
195  constexpr QPointF toPointF() const { return QPointF(x.toReal(), y.toReal()); }
196  constexpr static QFixedPoint fromPointF(const QPointF &p) {
197  return QFixedPoint(QFixed::fromReal(p.x()), QFixed::fromReal(p.y()));
198  }
199  constexpr inline bool operator==(const QFixedPoint &other) const
200  {
201  return x == other.x && y == other.y;
202  }
203 };
205 
206 constexpr inline QFixedPoint operator-(const QFixedPoint &p1, const QFixedPoint &p2)
207 { return QFixedPoint(p1.x - p2.x, p1.y - p2.y); }
208 constexpr inline QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2)
209 { return QFixedPoint(p1.x + p2.x, p1.y + p2.y); }
210 
211 struct QFixedSize {
214  constexpr QFixedSize() {}
215  constexpr QFixedSize(QFixed _width, QFixed _height) : width(_width), height(_height) {}
216  constexpr QSizeF toSizeF() const { return QSizeF(width.toReal(), height.toReal()); }
217  constexpr static QFixedSize fromSizeF(const QSizeF &s) {
218  return QFixedSize(QFixed::fromReal(s.width()), QFixed::fromReal(s.height()));
219  }
220 };
222 
224 
225 #endif // QTEXTENGINE_P_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:575
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
template< typename Enum > bool operator!=(Enum lhs, QFlags< Enum > rhs)
template< typename Enum > bool operator==(Enum lhs, QFlags< Enum > rhs)
QMargins operator*(const QMargins &margins, int factor)
Definition: qmargins.h:192
QMargins operator-(const QMargins &m1, const QMargins &m2)
Definition: qmargins.h:168
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:242
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
bool operator<=(const QUuid &lhs, const QUuid &rhs)
Definition: quuid.h:216
bool operator>=(const QUuid &lhs, const QUuid &rhs)
Definition: quuid.h:218
QPixmap p2
QPixmap p1
[0]
QTextStream & fixed(QTextStream &stream)
EGLOutputLayerEXT EGLint EGLAttrib value
constexpr int qFloor(const QFixed &f)
Definition: qfixed_p.h:162
constexpr bool operator>(const QFixed &f, int i)
Definition: qfixed_p.h:182
Q_DECLARE_TYPEINFO(QFixed, Q_PRIMITIVE_TYPE)
constexpr int qRound(const QFixed &f)
Definition: qfixed_p.h:161
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
unsigned int uint
Definition: qglobal.h:334
long long qint64
Definition: qglobal.h:298
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLboolean r
[2]
GLint GLsizei GLsizei height
GLboolean GLboolean GLboolean GLboolean a
[7]
GLfloat GLfloat f
GLint GLsizei width
GLint y
GLuint res
Definition: qopenglext.h:8867
GLuint GLfloat * val
Definition: qopenglext.h:1513
GLdouble s
[6]
Definition: qopenglext.h:235
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
@ Q_PRIMITIVE_TYPE
Definition: qtypeinfo.h:155
QSharedPointer< T > other(t)
[5]
QFixed & operator=(int i)
Definition: qfixed_p.h:68
QFixed & operator/=(const QFixed &o)
Definition: qfixed_p.h:108
constexpr QFixed operator+(const QFixed &other) const
Definition: qfixed_p.h:87
constexpr bool operator!=(const QFixed &other) const
Definition: qfixed_p.h:100
constexpr QFixed floor() const
Definition: qfixed_p.h:82
QFixed & operator+=(const QFixed &other)
Definition: qfixed_p.h:90
constexpr bool operator<(const QFixed &other) const
Definition: qfixed_p.h:101
constexpr bool operator>=(const QFixed &other) const
Definition: qfixed_p.h:104
QFixed & operator-=(uint i)
Definition: qfixed_p.h:95
constexpr int value() const
Definition: qfixed_p.h:74
constexpr QFixed operator*(uint i) const
Definition: qfixed_p.h:141
constexpr static QFixed fromReal(qreal r)
Definition: qfixed_p.h:71
QFixed operator>>(int d) const
Definition: qfixed_p.h:126
void setValue(int value)
Definition: qfixed_p.h:75
QFixed & operator/=(int x)
Definition: qfixed_p.h:107
QFixed operator*(const QFixed &o) const
Definition: qfixed_p.h:142
constexpr bool operator==(const QFixed &other) const
Definition: qfixed_p.h:99
constexpr int toInt() const
Definition: qfixed_p.h:77
constexpr QFixed operator-(int i) const
Definition: qfixed_p.h:91
constexpr bool operator<=(const QFixed &other) const
Definition: qfixed_p.h:103
QFixed & operator*=(int i)
Definition: qfixed_p.h:127
constexpr QFixed operator-(uint i) const
Definition: qfixed_p.h:92
QFixed operator/(QFixed b) const
Definition: qfixed_p.h:125
QFixed & operator+=(int i)
Definition: qfixed_p.h:88
QFixed & operator=(long i)
Definition: qfixed_p.h:69
QFixed & operator-=(int i)
Definition: qfixed_p.h:94
constexpr QFixed()
Definition: qfixed_p.h:65
constexpr QFixed operator-(const QFixed &other) const
Definition: qfixed_p.h:93
constexpr bool operator!() const
Definition: qfixed_p.h:105
constexpr QFixed round() const
Definition: qfixed_p.h:81
constexpr QFixed(int i)
Definition: qfixed_p.h:66
constexpr QFixed operator+(int i) const
Definition: qfixed_p.h:85
constexpr QFixed operator*(int i) const
Definition: qfixed_p.h:140
constexpr QFixed ceil() const
Definition: qfixed_p.h:83
constexpr qreal toReal() const
Definition: qfixed_p.h:78
QFixed & operator*=(uint i)
Definition: qfixed_p.h:128
constexpr static QFixed fromFixed(int fixed)
Definition: qfixed_p.h:72
QFixed & operator-=(const QFixed &other)
Definition: qfixed_p.h:96
constexpr QFixed operator/(int d) const
Definition: qfixed_p.h:124
constexpr QFixed operator+(uint i) const
Definition: qfixed_p.h:86
QFixed & operator*=(const QFixed &o)
Definition: qfixed_p.h:129
constexpr bool operator>(const QFixed &other) const
Definition: qfixed_p.h:102
QFixed & operator+=(uint i)
Definition: qfixed_p.h:89
constexpr QFixed(long i)
Definition: qfixed_p.h:67
constexpr int truncate() const
Definition: qfixed_p.h:80
constexpr QFixed operator-() const
Definition: qfixed_p.h:97
QFixed y
Definition: qfixed_p.h:192
constexpr bool operator==(const QFixedPoint &other) const
Definition: qfixed_p.h:199
constexpr static QFixedPoint fromPointF(const QPointF &p)
Definition: qfixed_p.h:196
constexpr QPointF toPointF() const
Definition: qfixed_p.h:195
QFixed x
Definition: qfixed_p.h:191
constexpr QFixedPoint()
Definition: qfixed_p.h:193
constexpr QFixedPoint(const QFixed &_x, const QFixed &_y)
Definition: qfixed_p.h:194
QFixed height
Definition: qfixed_p.h:213
constexpr QFixedSize(QFixed _width, QFixed _height)
Definition: qfixed_p.h:215
constexpr static QFixedSize fromSizeF(const QSizeF &s)
Definition: qfixed_p.h:217
constexpr QFixedSize()
Definition: qfixed_p.h:214
constexpr QSizeF toSizeF() const
Definition: qfixed_p.h:216
QFixed width
Definition: qfixed_p.h:212