Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfixed_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QFIXED_P_H
6#define QFIXED_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of other Qt classes. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtGui/private/qtguiglobal_p.h>
20#include "QtCore/qdebug.h"
21#include "QtCore/qpoint.h"
22#include "QtCore/qnumeric.h"
23#include "QtCore/qsize.h"
24
25QT_BEGIN_NAMESPACE
26
27struct QFixed {
28private:
29 constexpr QFixed(int val, int) : val(val) {} // 2nd int is just a dummy for disambiguation
30public:
31 constexpr QFixed() : val(0) {}
32 constexpr QFixed(int i) : val(i * 64) {}
33 constexpr QFixed(long i) : val(i * 64) {}
34 constexpr QFixed(long long i) : val(i * 64) {}
35
36 constexpr static QFixed fromReal(qreal r) { return fromFixed((int)(r*qreal(64))); }
37 constexpr static QFixed fromFixed(int fixed) { return QFixed(fixed,0); } // uses private ctor
38
39 constexpr inline int value() const { return val; }
40 inline void setValue(int value) { val = value; }
41
42 constexpr inline int toInt() const { return (((val)+32) & -64)>>6; }
43 constexpr inline qreal toReal() const { return ((qreal)val)/(qreal)64; }
44
45 constexpr inline int truncate() const { return val>>6; }
46 constexpr inline QFixed round() const { return fromFixed(((val)+32) & -64); }
47 constexpr inline QFixed floor() const { return fromFixed((val) & -64); }
48 constexpr inline QFixed ceil() const { return fromFixed((val+63) & -64); }
49
50 constexpr inline QFixed operator+(int i) const { return fromFixed(val + i * 64); }
51 constexpr inline QFixed operator+(uint i) const { return fromFixed((val + (i<<6))); }
52 constexpr inline QFixed operator+(QFixed other) const { return fromFixed((val + other.val)); }
53 inline QFixed &operator+=(int i) { val += i * 64; return *this; }
54 inline QFixed &operator+=(uint i) { val += (i<<6); return *this; }
55 inline QFixed &operator+=(QFixed other) { val += other.val; return *this; }
56 constexpr inline QFixed operator-(int i) const { return fromFixed(val - i * 64); }
57 constexpr inline QFixed operator-(uint i) const { return fromFixed((val - (i<<6))); }
58 constexpr inline QFixed operator-(QFixed other) const { return fromFixed((val - other.val)); }
59 inline QFixed &operator-=(int i) { val -= i * 64; return *this; }
60 inline QFixed &operator-=(uint i) { val -= (i<<6); return *this; }
61 inline QFixed &operator-=(QFixed other) { val -= other.val; return *this; }
62 constexpr inline QFixed operator-() const { return fromFixed(-val); }
63
64#define REL_OP(op)
65 friend constexpr bool operator op(QFixed lhs, QFixed rhs) noexcept
66 { return lhs.val op rhs.val; }
67 REL_OP(==)
68 REL_OP(!=)
69 REL_OP(< )
70 REL_OP(> )
71 REL_OP(<=)
72 REL_OP(>=)
73#undef REL_OP
74
75 constexpr inline bool operator!() const { return !val; }
76
77 inline QFixed &operator/=(int x) { val /= x; return *this; }
78 inline QFixed &operator/=(QFixed o) {
79 if (o.val == 0) {
80 val = 0x7FFFFFFFL;
81 } else {
82 bool neg = false;
83 qint64 a = val;
84 qint64 b = o.val;
85 if (a < 0) { a = -a; neg = true; }
86 if (b < 0) { b = -b; neg = !neg; }
87
88 int res = (int)(((a << 6) + (b >> 1)) / b);
89
90 val = (neg ? -res : res);
91 }
92 return *this;
93 }
94 constexpr inline QFixed operator/(int d) const { return fromFixed(val/d); }
95 inline QFixed operator/(QFixed b) const { QFixed f = *this; return (f /= b); }
96 inline QFixed operator>>(int d) const { QFixed f = *this; f.val >>= d; return f; }
97 inline QFixed &operator*=(int i) { val *= i; return *this; }
98 inline QFixed &operator*=(uint i) { val *= i; return *this; }
99 inline QFixed &operator*=(QFixed o) {
100 bool neg = false;
101 qint64 a = val;
102 qint64 b = o.val;
103 if (a < 0) { a = -a; neg = true; }
104 if (b < 0) { b = -b; neg = !neg; }
105
106 int res = (int)((a * b + 0x20L) >> 6);
107 val = neg ? -res : res;
108 return *this;
109 }
110 constexpr inline QFixed operator*(int i) const { return fromFixed(val * i); }
111 constexpr inline QFixed operator*(uint i) const { return fromFixed(val * i); }
112 inline QFixed operator*(QFixed o) const { QFixed f = *this; return (f *= o); }
113
114private:
115 constexpr QFixed(qreal i) : val((int)(i*qreal(64))) {}
116 constexpr inline QFixed operator+(qreal i) const { return fromFixed((val + (int)(i*qreal(64)))); }
117 inline QFixed &operator+=(qreal i) { val += (int)(i*64); return *this; }
118 constexpr inline QFixed operator-(qreal i) const { return fromFixed((val - (int)(i*qreal(64)))); }
119 inline QFixed &operator-=(qreal i) { val -= (int)(i*64); return *this; }
120 inline QFixed &operator/=(qreal r) { val = (int)(val/r); return *this; }
121 constexpr inline QFixed operator/(qreal d) const { return fromFixed((int)(val/d)); }
122 inline QFixed &operator*=(qreal d) { val = (int) (val*d); return *this; }
123 constexpr inline QFixed operator*(qreal d) const { return fromFixed((int) (val*d)); }
124 int val;
125};
127
128#define QFIXED_MAX (INT_MAX/256)
129
130constexpr inline int qRound(QFixed f) { return f.toInt(); }
131constexpr inline int qFloor(QFixed f) { return f.floor().truncate(); }
132
133constexpr inline QFixed operator*(int i, QFixed d) { return d*i; }
134constexpr inline QFixed operator+(int i, QFixed d) { return d+i; }
135constexpr inline QFixed operator-(int i, QFixed d) { return -(d-i); }
136constexpr inline QFixed operator*(uint i, QFixed d) { return d*i; }
137constexpr inline QFixed operator+(uint i, QFixed d) { return d+i; }
138constexpr inline QFixed operator-(uint i, QFixed d) { return -(d-i); }
139// constexpr inline QFixed operator*(qreal d, QFixed d2) { return d2*d; }
140
141inline bool qAddOverflow(QFixed v1, QFixed v2, QFixed *r)
142{
143 int val;
144 bool result = qAddOverflow(v1.value(), v2.value(), &val);
145 r->setValue(val);
146 return result;
147}
148
149inline bool qMulOverflow(QFixed v1, QFixed v2, QFixed *r)
150{
151 int val;
152 bool result = qMulOverflow(v1.value(), v2.value(), &val);
153 r->setValue(val);
154 return result;
155}
156
157#ifndef QT_NO_DEBUG_STREAM
158inline QDebug &operator<<(QDebug &dbg, QFixed f)
159{ return dbg << f.toReal(); }
160#endif
161
163 QFixed x;
164 QFixed y;
165 constexpr inline QFixedPoint() {}
166 constexpr inline QFixedPoint(QFixed _x, QFixed _y) : x(_x), y(_y) {}
167 constexpr QPointF toPointF() const { return QPointF(x.toReal(), y.toReal()); }
168 constexpr static QFixedPoint fromPointF(const QPointF &p) {
169 return QFixedPoint(QFixed::fromReal(p.x()), QFixed::fromReal(p.y()));
170 }
171 constexpr inline bool operator==(const QFixedPoint &other) const
172 {
173 return x == other.x && y == other.y;
174 }
175};
177
178constexpr inline QFixedPoint operator-(const QFixedPoint &p1, const QFixedPoint &p2)
179{ return QFixedPoint(p1.x - p2.x, p1.y - p2.y); }
180constexpr inline QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2)
181{ return QFixedPoint(p1.x + p2.x, p1.y + p2.y); }
182
184 QFixed width;
185 QFixed height;
186 constexpr QFixedSize() {}
187 constexpr QFixedSize(QFixed _width, QFixed _height) : width(_width), height(_height) {}
188 constexpr QSizeF toSizeF() const { return QSizeF(width.toReal(), height.toReal()); }
189 constexpr static QFixedSize fromSizeF(const QSizeF &s) {
190 return QFixedSize(QFixed::fromReal(s.width()), QFixed::fromReal(s.height()));
191 }
192};
194
195QT_END_NAMESPACE
196
197#endif // QTEXTENGINE_P_H
constexpr QFixed operator-(int i, QFixed d)
Definition qfixed_p.h:135
constexpr int qRound(QFixed f)
Definition qfixed_p.h:130
constexpr int qFloor(QFixed f)
Definition qfixed_p.h:131
bool qMulOverflow(QFixed v1, QFixed v2, QFixed *r)
Definition qfixed_p.h:149
constexpr QFixed operator*(int i, QFixed d)
Definition qfixed_p.h:133
bool qAddOverflow(QFixed v1, QFixed v2, QFixed *r)
Definition qfixed_p.h:141
QDebug & operator<<(QDebug &dbg, QFixed f)
Definition qfixed_p.h:158
#define REL_OP(op)
Definition qfixed_p.h:64
constexpr QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2)
Definition qfixed_p.h:180
constexpr QFixed operator+(int i, QFixed d)
Definition qfixed_p.h:134
constexpr QFixedPoint operator-(const QFixedPoint &p1, const QFixedPoint &p2)
Definition qfixed_p.h:178
Q_DECLARE_TYPEINFO(QFixedSize, Q_PRIMITIVE_TYPE)
Q_DECLARE_TYPEINFO(QFixedPoint, Q_PRIMITIVE_TYPE)
Q_DECLARE_TYPEINFO(QFixed, Q_PRIMITIVE_TYPE)
QFixed y
Definition qfixed_p.h:164
constexpr bool operator==(const QFixedPoint &other) const
Definition qfixed_p.h:171
constexpr QPointF toPointF() const
Definition qfixed_p.h:167
static constexpr QFixedPoint fromPointF(const QPointF &p)
Definition qfixed_p.h:168
constexpr QFixedPoint(QFixed _x, QFixed _y)
Definition qfixed_p.h:166
QFixed x
Definition qfixed_p.h:163
constexpr QFixedPoint()
Definition qfixed_p.h:165
QFixed height
Definition qfixed_p.h:185
static constexpr QFixedSize fromSizeF(const QSizeF &s)
Definition qfixed_p.h:189
constexpr QFixedSize(QFixed _width, QFixed _height)
Definition qfixed_p.h:187
constexpr QFixedSize()
Definition qfixed_p.h:186
constexpr QSizeF toSizeF() const
Definition qfixed_p.h:188
QFixed width
Definition qfixed_p.h:184