Coverage Report

Created: 2025-07-23 08:13

/src/qtbase/src/gui/painting/qfixed_p.h
Line
Count
Source (jump to first uncovered line)
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
// clang-format off
55
56
#include <QtGui/private/qtguiglobal_p.h>
57
#include "QtCore/qdebug.h"
58
#include "QtCore/qpoint.h"
59
#if defined(Q_OS_WIN)
60
#include <qt_windows.h> // to suppress min, max macros.
61
#endif
62
#include <QtCore/private/qnumeric_p.h>
63
#include "QtCore/qsize.h"
64
65
// clang-format on
66
67
QT_BEGIN_NAMESPACE
68
69
struct QFixed {
70
private:
71
0
    Q_DECL_CONSTEXPR QFixed(int val, int) : val(val) {} // 2nd int is just a dummy for disambiguation
72
public:
73
16.0k
    Q_DECL_CONSTEXPR QFixed() : val(0) {}
74
0
    Q_DECL_CONSTEXPR QFixed(int i) : val(i * 64) {}
75
0
    Q_DECL_CONSTEXPR QFixed(long i) : val(i * 64) {}
76
0
    QFixed &operator=(int i) { val = i * 64; return *this; }
77
0
    QFixed &operator=(long i) { val = i * 64; return *this; }
78
79
0
    Q_DECL_CONSTEXPR static QFixed fromReal(qreal r) { return fromFixed((int)(r*qreal(64))); }
80
0
    Q_DECL_CONSTEXPR static QFixed fromFixed(int fixed) { return QFixed(fixed,0); } // uses private ctor
81
82
0
    Q_DECL_CONSTEXPR inline int value() const { return val; }
83
0
    inline void setValue(int value) { val = value; }
84
85
0
    Q_DECL_CONSTEXPR inline int toInt() const { return (((val)+32) & -64)>>6; }
86
0
    Q_DECL_CONSTEXPR inline qreal toReal() const { return ((qreal)val)/(qreal)64; }
87
88
0
    Q_DECL_CONSTEXPR inline int truncate() const { return val>>6; }
89
0
    Q_DECL_CONSTEXPR inline QFixed round() const { return fromFixed(((val)+32) & -64); }
90
0
    Q_DECL_CONSTEXPR inline QFixed floor() const { return fromFixed((val) & -64); }
91
0
    Q_DECL_CONSTEXPR inline QFixed ceil() const { return fromFixed((val+63) & -64); }
92
93
0
    Q_DECL_CONSTEXPR inline QFixed operator+(int i) const { return fromFixed(val + i * 64); }
94
0
    Q_DECL_CONSTEXPR inline QFixed operator+(uint i) const { return fromFixed((val + (i<<6))); }
95
0
    Q_DECL_CONSTEXPR inline QFixed operator+(const QFixed &other) const { return fromFixed((val + other.val)); }
96
0
    inline QFixed &operator+=(int i) { val += i * 64; return *this; }
97
0
    inline QFixed &operator+=(uint i) { val += (i<<6); return *this; }
98
0
    inline QFixed &operator+=(const QFixed &other) { val += other.val; return *this; }
99
0
    Q_DECL_CONSTEXPR inline QFixed operator-(int i) const { return fromFixed(val - i * 64); }
100
0
    Q_DECL_CONSTEXPR inline QFixed operator-(uint i) const { return fromFixed((val - (i<<6))); }
101
0
    Q_DECL_CONSTEXPR inline QFixed operator-(const QFixed &other) const { return fromFixed((val - other.val)); }
102
0
    inline QFixed &operator-=(int i) { val -= i * 64; return *this; }
103
0
    inline QFixed &operator-=(uint i) { val -= (i<<6); return *this; }
104
0
    inline QFixed &operator-=(const QFixed &other) { val -= other.val; return *this; }
105
0
    Q_DECL_CONSTEXPR inline QFixed operator-() const { return fromFixed(-val); }
106
107
0
    Q_DECL_CONSTEXPR inline bool operator==(const QFixed &other) const { return val == other.val; }
108
0
    Q_DECL_CONSTEXPR inline bool operator!=(const QFixed &other) const { return val != other.val; }
109
0
    Q_DECL_CONSTEXPR inline bool operator<(const QFixed &other) const { return val < other.val; }
110
0
    Q_DECL_CONSTEXPR inline bool operator>(const QFixed &other) const { return val > other.val; }
111
0
    Q_DECL_CONSTEXPR inline bool operator<=(const QFixed &other) const { return val <= other.val; }
112
0
    Q_DECL_CONSTEXPR inline bool operator>=(const QFixed &other) const { return val >= other.val; }
113
0
    Q_DECL_CONSTEXPR inline bool operator!() const { return !val; }
114
115
0
    inline QFixed &operator/=(int x) { val /= x; return *this; }
116
0
    inline QFixed &operator/=(const QFixed &o) {
117
0
        if (o.val == 0) {
118
0
            val = 0x7FFFFFFFL;
119
0
        } else {
120
0
            bool neg = false;
121
0
            qint64 a = val;
122
0
            qint64 b = o.val;
123
0
            if (a < 0) { a = -a; neg = true; }
124
0
            if (b < 0) { b = -b; neg = !neg; }
125
126
0
            int res = (int)(((a << 6) + (b >> 1)) / b);
127
128
0
            val = (neg ? -res : res);
129
0
        }
130
0
        return *this;
131
0
    }
132
0
    Q_DECL_CONSTEXPR inline QFixed operator/(int d) const { return fromFixed(val/d); }
133
0
    inline QFixed operator/(QFixed b) const { QFixed f = *this; return (f /= b); }
134
0
    inline QFixed operator>>(int d) const { QFixed f = *this; f.val >>= d; return f; }
135
0
    inline QFixed &operator*=(int i) { val *= i; return *this; }
136
0
    inline QFixed &operator*=(uint i) { val *= i; return *this; }
137
0
    inline QFixed &operator*=(const QFixed &o) {
138
0
        bool neg = false;
139
0
        qint64 a = val;
140
0
        qint64 b = o.val;
141
0
        if (a < 0) { a = -a; neg = true; }
142
0
        if (b < 0) { b = -b; neg = !neg; }
143
144
0
        int res = (int)((a * b + 0x20L) >> 6);
145
0
        val = neg ? -res : res;
146
0
        return *this;
147
0
    }
148
0
    Q_DECL_CONSTEXPR inline QFixed operator*(int i) const { return fromFixed(val * i); }
149
0
    Q_DECL_CONSTEXPR inline QFixed operator*(uint i) const { return fromFixed(val * i); }
150
0
    inline QFixed operator*(const QFixed &o) const { QFixed f = *this; return (f *= o); }
151
152
private:
153
0
    Q_DECL_CONSTEXPR QFixed(qreal i) : val((int)(i*qreal(64))) {}
154
0
    QFixed &operator=(qreal i) { val = (int)(i*qreal(64)); return *this; }
155
0
    Q_DECL_CONSTEXPR inline QFixed operator+(qreal i) const { return fromFixed((val + (int)(i*qreal(64)))); }
156
0
    inline QFixed &operator+=(qreal i) { val += (int)(i*64); return *this; }
157
0
    Q_DECL_CONSTEXPR inline QFixed operator-(qreal i) const { return fromFixed((val - (int)(i*qreal(64)))); }
158
0
    inline QFixed &operator-=(qreal i) { val -= (int)(i*64); return *this; }
159
0
    inline QFixed &operator/=(qreal r) { val = (int)(val/r); return *this; }
160
0
    Q_DECL_CONSTEXPR inline QFixed operator/(qreal d) const { return fromFixed((int)(val/d)); }
161
0
    inline QFixed &operator*=(qreal d) { val = (int) (val*d); return *this; }
162
0
    Q_DECL_CONSTEXPR inline QFixed operator*(qreal d) const { return fromFixed((int) (val*d)); }
163
    int val;
164
};
165
Q_DECLARE_TYPEINFO(QFixed, Q_PRIMITIVE_TYPE);
166
167
0
#define QFIXED_MAX (INT_MAX/256)
168
169
0
Q_DECL_CONSTEXPR inline int qRound(const QFixed &f) { return f.toInt(); }
170
0
Q_DECL_CONSTEXPR inline int qFloor(const QFixed &f) { return f.floor().truncate(); }
171
172
0
Q_DECL_CONSTEXPR inline QFixed operator*(int i, const QFixed &d) { return d*i; }
173
0
Q_DECL_CONSTEXPR inline QFixed operator+(int i, const QFixed &d) { return d+i; }
174
0
Q_DECL_CONSTEXPR inline QFixed operator-(int i, const QFixed &d) { return -(d-i); }
175
0
Q_DECL_CONSTEXPR inline QFixed operator*(uint i, const QFixed &d) { return d*i; }
176
0
Q_DECL_CONSTEXPR inline QFixed operator+(uint i, const QFixed &d) { return d+i; }
177
0
Q_DECL_CONSTEXPR inline QFixed operator-(uint i, const QFixed &d) { return -(d-i); }
178
// Q_DECL_CONSTEXPR inline QFixed operator*(qreal d, const QFixed &d2) { return d2*d; }
179
180
0
Q_DECL_CONSTEXPR inline bool operator==(const QFixed &f, int i) { return f.value() == i * 64; }
181
0
Q_DECL_CONSTEXPR inline bool operator==(int i, const QFixed &f) { return f.value() == i * 64; }
182
0
Q_DECL_CONSTEXPR inline bool operator!=(const QFixed &f, int i) { return f.value() != i * 64; }
183
0
Q_DECL_CONSTEXPR inline bool operator!=(int i, const QFixed &f) { return f.value() != i * 64; }
184
0
Q_DECL_CONSTEXPR inline bool operator<=(const QFixed &f, int i) { return f.value() <= i * 64; }
185
0
Q_DECL_CONSTEXPR inline bool operator<=(int i, const QFixed &f) { return i * 64 <= f.value(); }
186
0
Q_DECL_CONSTEXPR inline bool operator>=(const QFixed &f, int i) { return f.value() >= i * 64; }
187
0
Q_DECL_CONSTEXPR inline bool operator>=(int i, const QFixed &f) { return i * 64 >= f.value(); }
188
0
Q_DECL_CONSTEXPR inline bool operator<(const QFixed &f, int i) { return f.value() < i * 64; }
189
0
Q_DECL_CONSTEXPR inline bool operator<(int i, const QFixed &f) { return i * 64 < f.value(); }
190
0
Q_DECL_CONSTEXPR inline bool operator>(const QFixed &f, int i) { return f.value() > i * 64; }
191
0
Q_DECL_CONSTEXPR inline bool operator>(int i, const QFixed &f) { return i * 64 > f.value(); }
192
193
inline bool qAddOverflow(QFixed v1, QFixed v2, QFixed *r)
194
0
{
195
0
    int val;
196
0
    bool result = add_overflow(v1.value(), v2.value(), &val);
197
0
    r->setValue(val);
198
0
    return result;
199
0
}
200
201
#ifndef QT_NO_DEBUG_STREAM
202
inline QDebug &operator<<(QDebug &dbg, const QFixed &f)
203
0
{ return dbg << f.toReal(); }
204
#endif
205
206
struct QFixedPoint {
207
    QFixed x;
208
    QFixed y;
209
0
    Q_DECL_CONSTEXPR inline QFixedPoint() {}
210
0
    Q_DECL_CONSTEXPR inline QFixedPoint(const QFixed &_x, const QFixed &_y) : x(_x), y(_y) {}
211
0
    Q_DECL_CONSTEXPR QPointF toPointF() const { return QPointF(x.toReal(), y.toReal()); }
212
0
    Q_DECL_CONSTEXPR static QFixedPoint fromPointF(const QPointF &p) {
213
0
        return QFixedPoint(QFixed::fromReal(p.x()), QFixed::fromReal(p.y()));
214
0
    }
215
};
216
Q_DECLARE_TYPEINFO(QFixedPoint, Q_PRIMITIVE_TYPE);
217
218
Q_DECL_CONSTEXPR inline QFixedPoint operator-(const QFixedPoint &p1, const QFixedPoint &p2)
219
0
{ return QFixedPoint(p1.x - p2.x, p1.y - p2.y); }
220
Q_DECL_CONSTEXPR inline QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2)
221
0
{ return QFixedPoint(p1.x + p2.x, p1.y + p2.y); }
222
223
struct QFixedSize {
224
    QFixed width;
225
    QFixed height;
226
0
    Q_DECL_CONSTEXPR QFixedSize() {}
227
0
    Q_DECL_CONSTEXPR QFixedSize(QFixed _width, QFixed _height) : width(_width), height(_height) {}
228
0
    Q_DECL_CONSTEXPR QSizeF toSizeF() const { return QSizeF(width.toReal(), height.toReal()); }
229
0
    Q_DECL_CONSTEXPR static QFixedSize fromSizeF(const QSizeF &s) {
230
0
        return QFixedSize(QFixed::fromReal(s.width()), QFixed::fromReal(s.height()));
231
0
    }
232
};
233
Q_DECLARE_TYPEINFO(QFixedSize, Q_PRIMITIVE_TYPE);
234
235
QT_END_NAMESPACE
236
237
#endif // QTEXTENGINE_P_H