/src/qtbase/src/gui/painting/qfixed_p.h
Line | Count | Source |
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 | | |
25 | | QT_BEGIN_NAMESPACE |
26 | | |
27 | | struct QFixed { |
28 | | private: |
29 | 242M | constexpr QFixed(int val, int) : val(val) {} // 2nd int is just a dummy for disambiguation |
30 | | public: |
31 | 89.5M | constexpr QFixed() : val(0) {} |
32 | 167M | constexpr QFixed(int i) : val(i * 64) {} |
33 | 0 | constexpr QFixed(long i) : val(i * 64) {} |
34 | 0 | constexpr QFixed(long long i) : val(i * 64) {} |
35 | | |
36 | 25.5M | constexpr static QFixed fromReal(qreal r) { return fromFixed((int)(r*qreal(64))); } |
37 | 242M | constexpr static QFixed fromFixed(int fixed) { return QFixed(fixed,0); } // uses private ctor |
38 | | |
39 | 20.1M | constexpr inline int value() const { return val; } |
40 | 2.07M | inline void setValue(int value) { val = value; } |
41 | | |
42 | 1.86M | constexpr inline int toInt() const { return (((val)+32) & -64)>>6; } |
43 | 23.2M | constexpr inline qreal toReal() const { return ((qreal)val)/(qreal)64; } |
44 | | |
45 | 1.92M | constexpr inline int truncate() const { return val>>6; } |
46 | 13.5M | constexpr inline QFixed round() const { return fromFixed(((val)+32) & -64); } |
47 | 1.07M | constexpr inline QFixed floor() const { return fromFixed((val) & -64); } |
48 | 4.60M | constexpr inline QFixed ceil() const { return fromFixed((val+63) & -64); } |
49 | | |
50 | 0 | constexpr inline QFixed operator+(int i) const { return fromFixed(val + i * 64); } |
51 | 0 | constexpr inline QFixed operator+(uint i) const { return fromFixed((val + (i<<6))); } |
52 | 99.6M | constexpr inline QFixed operator+(QFixed other) const { return fromFixed((val + other.val)); } |
53 | 0 | inline QFixed &operator+=(int i) { val += i * 64; return *this; } |
54 | 0 | inline QFixed &operator+=(uint i) { val += (i<<6); return *this; } |
55 | 91.1M | inline QFixed &operator+=(QFixed other) { val += other.val; return *this; } |
56 | 0 | constexpr inline QFixed operator-(int i) const { return fromFixed(val - i * 64); } |
57 | 0 | constexpr inline QFixed operator-(uint i) const { return fromFixed((val - (i<<6))); } |
58 | 56.7M | constexpr inline QFixed operator-(QFixed other) const { return fromFixed((val - other.val)); } |
59 | 0 | inline QFixed &operator-=(int i) { val -= i * 64; return *this; } |
60 | 0 | inline QFixed &operator-=(uint i) { val -= (i<<6); return *this; } |
61 | 2.73M | inline QFixed &operator-=(QFixed other) { val -= other.val; return *this; } |
62 | 0 | 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 | 206M | { return lhs.val op rhs.val; }operator==(QFixed, QFixed) Line | Count | Source | 66 | 18.3M | { return lhs.val op rhs.val; } |
operator!=(QFixed, QFixed) Line | Count | Source | 66 | 13.7M | { return lhs.val op rhs.val; } |
operator<(QFixed, QFixed) Line | Count | Source | 66 | 104M | { return lhs.val op rhs.val; } |
operator>(QFixed, QFixed) Line | Count | Source | 66 | 32.8M | { return lhs.val op rhs.val; } |
operator<=(QFixed, QFixed) Line | Count | Source | 66 | 2.33M | { return lhs.val op rhs.val; } |
operator>=(QFixed, QFixed) Line | Count | Source | 66 | 34.4M | { 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 | 0 | constexpr inline bool operator!() const { return !val; } |
76 | | |
77 | 0 | inline QFixed &operator/=(int x) { val /= x; return *this; } |
78 | 1.42M | inline QFixed &operator/=(QFixed o) { |
79 | 1.42M | if (o.val == 0) { |
80 | 0 | val = 0x7FFFFFFFL; |
81 | 1.42M | } else { |
82 | 1.42M | bool neg = false; |
83 | 1.42M | qint64 a = val; |
84 | 1.42M | qint64 b = o.val; |
85 | 1.42M | if (a < 0) { a = -a; neg = true; } |
86 | 1.42M | if (b < 0) { b = -b; neg = !neg; } |
87 | | |
88 | 1.42M | int res = (int)(((a << 6) + (b >> 1)) / b); |
89 | | |
90 | 1.42M | val = (neg ? -res : res); |
91 | 1.42M | } |
92 | 1.42M | return *this; |
93 | 1.42M | } |
94 | 0 | constexpr inline QFixed operator/(int d) const { return fromFixed(val/d); } |
95 | 1.42M | inline QFixed operator/(QFixed b) const { QFixed f = *this; return (f /= b); } |
96 | 0 | inline QFixed operator>>(int d) const { QFixed f = *this; f.val >>= d; return f; } |
97 | 0 | inline QFixed &operator*=(int i) { val *= i; return *this; } |
98 | 0 | inline QFixed &operator*=(uint i) { val *= i; return *this; } |
99 | 696k | inline QFixed &operator*=(QFixed o) { |
100 | 696k | bool neg = false; |
101 | 696k | qint64 a = val; |
102 | 696k | qint64 b = o.val; |
103 | 696k | if (a < 0) { a = -a; neg = true; } |
104 | 696k | if (b < 0) { b = -b; neg = !neg; } |
105 | | |
106 | 696k | int res = (int)((a * b + 0x20L) >> 6); |
107 | 696k | val = neg ? -res : res; |
108 | 696k | return *this; |
109 | 696k | } |
110 | 14.8M | constexpr inline QFixed operator*(int i) const { return fromFixed(val * i); } |
111 | 0 | constexpr inline QFixed operator*(uint i) const { return fromFixed(val * i); } |
112 | 0 | inline QFixed operator*(QFixed o) const { QFixed f = *this; return (f *= o); } |
113 | | |
114 | | private: |
115 | 0 | constexpr QFixed(qreal i) : val((int)(i*qreal(64))) {} |
116 | 0 | constexpr inline QFixed operator+(qreal i) const { return fromFixed((val + (int)(i*qreal(64)))); } |
117 | 0 | inline QFixed &operator+=(qreal i) { val += (int)(i*64); return *this; } |
118 | 0 | constexpr inline QFixed operator-(qreal i) const { return fromFixed((val - (int)(i*qreal(64)))); } |
119 | 0 | inline QFixed &operator-=(qreal i) { val -= (int)(i*64); return *this; } |
120 | 0 | inline QFixed &operator/=(qreal r) { val = (int)(val/r); return *this; } |
121 | 0 | constexpr inline QFixed operator/(qreal d) const { return fromFixed((int)(val/d)); } |
122 | 0 | inline QFixed &operator*=(qreal d) { val = (int) (val*d); return *this; } |
123 | 0 | constexpr inline QFixed operator*(qreal d) const { return fromFixed((int) (val*d)); } |
124 | | int val; |
125 | | }; |
126 | | Q_DECLARE_TYPEINFO(QFixed, Q_PRIMITIVE_TYPE); |
127 | | |
128 | 31.1M | #define QFIXED_MAX (INT_MAX/256) |
129 | | |
130 | 1.80M | constexpr inline int qRound(QFixed f) { return f.toInt(); } |
131 | 1.07M | constexpr inline int qFloor(QFixed f) { return f.floor().truncate(); } |
132 | | |
133 | 3.43M | constexpr inline QFixed operator*(int i, QFixed d) { return d*i; } |
134 | 0 | constexpr inline QFixed operator+(int i, QFixed d) { return d+i; } |
135 | 0 | constexpr inline QFixed operator-(int i, QFixed d) { return -(d-i); } |
136 | 0 | constexpr inline QFixed operator*(uint i, QFixed d) { return d*i; } |
137 | 0 | constexpr inline QFixed operator+(uint i, QFixed d) { return d+i; } |
138 | 0 | constexpr inline QFixed operator-(uint i, QFixed d) { return -(d-i); } |
139 | | // constexpr inline QFixed operator*(qreal d, QFixed d2) { return d2*d; } |
140 | | |
141 | | inline bool qAddOverflow(QFixed v1, QFixed v2, QFixed *r) |
142 | 2.07M | { |
143 | 2.07M | int val; |
144 | 2.07M | bool result = qAddOverflow(v1.value(), v2.value(), &val); |
145 | 2.07M | r->setValue(val); |
146 | 2.07M | return result; |
147 | 2.07M | } |
148 | | |
149 | | inline bool qMulOverflow(QFixed v1, QFixed v2, QFixed *r) |
150 | 0 | { |
151 | 0 | int val; |
152 | 0 | bool result = qMulOverflow(v1.value(), v2.value(), &val); |
153 | 0 | r->setValue(val); |
154 | 0 | return result; |
155 | 0 | } |
156 | | |
157 | | #ifndef QT_NO_DEBUG_STREAM |
158 | | inline QDebug &operator<<(QDebug &dbg, QFixed f) |
159 | 0 | { return dbg << f.toReal(); } |
160 | | #endif |
161 | | |
162 | | struct QFixedPoint { |
163 | | QFixed x; |
164 | | QFixed y; |
165 | 2.17M | constexpr inline QFixedPoint() {} |
166 | 0 | constexpr inline QFixedPoint(QFixed _x, QFixed _y) : x(_x), y(_y) {} |
167 | 37.2k | constexpr QPointF toPointF() const { return QPointF(x.toReal(), y.toReal()); } |
168 | 0 | constexpr static QFixedPoint fromPointF(const QPointF &p) { |
169 | 0 | return QFixedPoint(QFixed::fromReal(p.x()), QFixed::fromReal(p.y())); |
170 | 0 | } |
171 | | constexpr inline bool operator==(const QFixedPoint &other) const |
172 | 2.12M | { |
173 | 2.12M | return x == other.x && y == other.y; |
174 | 2.12M | } |
175 | | }; |
176 | | Q_DECLARE_TYPEINFO(QFixedPoint, Q_PRIMITIVE_TYPE); |
177 | | |
178 | | constexpr inline QFixedPoint operator-(const QFixedPoint &p1, const QFixedPoint &p2) |
179 | 0 | { return QFixedPoint(p1.x - p2.x, p1.y - p2.y); } |
180 | | constexpr inline QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2) |
181 | 0 | { return QFixedPoint(p1.x + p2.x, p1.y + p2.y); } |
182 | | |
183 | | struct QFixedSize { |
184 | | QFixed width; |
185 | | QFixed height; |
186 | 18.6k | constexpr QFixedSize() {} |
187 | 0 | constexpr QFixedSize(QFixed _width, QFixed _height) : width(_width), height(_height) {} |
188 | 75.3k | constexpr QSizeF toSizeF() const { return QSizeF(width.toReal(), height.toReal()); } |
189 | 0 | constexpr static QFixedSize fromSizeF(const QSizeF &s) { |
190 | 0 | return QFixedSize(QFixed::fromReal(s.width()), QFixed::fromReal(s.height())); |
191 | 0 | } |
192 | | }; |
193 | | Q_DECLARE_TYPEINFO(QFixedSize, Q_PRIMITIVE_TYPE); |
194 | | |
195 | | QT_END_NAMESPACE |
196 | | |
197 | | #endif // QTEXTENGINE_P_H |