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
qjsnumbercoercion.h
Go to the documentation of this file.
1// Copyright (C) 2020 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
4
5#ifndef QJSNUMBERCOERCION_H
6#define QJSNUMBERCOERCION_H
7
8#include <QtCore/qglobal.h>
9#include <cstring>
10
11QT_BEGIN_NAMESPACE
12
13class QJSNumberCoercion
14{
15public:
16
17 static constexpr bool isInteger(double d)
18 {
19 // Comparing d with itself checks for NaN and comparing d with the min and max values
20 // for int also covers infinities.
21 if (!equals(d, d) || d < (std::numeric_limits<int>::min)()
22 || d > (std::numeric_limits<int>::max)()) {
23 return false;
24 }
25
26 return equals(static_cast<int>(d), d);
27 }
28
29 static constexpr bool isArrayIndex(double d)
30 {
31 return d >= 0
32 && equals(d, d)
33 && d <= (std::numeric_limits<uint>::max)()
34 && equals(static_cast<uint>(d), d);
35 }
36
37 static constexpr bool isArrayIndex(qint64 i)
38 {
39 return i >= 0 && i <= (std::numeric_limits<uint>::max)();
40 }
41
42 static constexpr bool isArrayIndex(quint64 i)
43 {
44 return i <= (std::numeric_limits<uint>::max)();
45 }
46
47 static constexpr int toInteger(double d) {
48 // Check for NaN
49 if (!equals(d, d))
50 return 0;
51
52 if (d >= (std::numeric_limits<int>::min)() && d <= (std::numeric_limits<int>::max)()) {
53 const int i = static_cast<int>(d);
54 if (equals(i, d))
55 return i;
56 }
57
58 return QJSNumberCoercion(d).toInteger();
59 }
60
61 static constexpr bool equals(double lhs, double rhs)
62 {
63 QT_WARNING_PUSH
64 QT_WARNING_DISABLE_FLOAT_COMPARE
65 return lhs == rhs;
66 QT_WARNING_POP
67 }
68
69 static constexpr double roundTowards0(double d)
70 {
71 // Check for NaN
72 if (!equals(d, d))
73 return +0;
74
75 if (equals(d, 0) || std::isinf(d))
76 return d;
77
78 return d >= 0 ? std::floor(d) : std::ceil(d);
79 }
80
81private:
82 constexpr QJSNumberCoercion(double dbl)
83 {
84 // the dbl == 0 path is guaranteed constexpr. The other one may or may not be, depending
85 // on whether and how the compiler inlines the memcpy.
86 // In order to declare the ctor constexpr we need one guaranteed constexpr path.
87 if (!equals(dbl, 0))
88 memcpy(&d, &dbl, sizeof(double));
89 }
90
91 constexpr int sign() const
92 {
93 return (d >> 63) ? -1 : 1;
94 }
95
96 constexpr bool isDenormal() const
97 {
98 return static_cast<int>((d << 1) >> 53) == 0;
99 }
100
101 constexpr int exponent() const
102 {
103 return static_cast<int>((d << 1) >> 53) - 1023;
104 }
105
106 constexpr quint64 significant() const
107 {
108 quint64 m = (d << 12) >> 12;
109 if (!isDenormal())
110 m |= (static_cast<quint64>(1) << 52);
111 return m;
112 }
113
114 constexpr int toInteger()
115 {
116 int e = exponent() - 52;
117 if (e < 0) {
118 if (e <= -53)
119 return 0;
120 return sign() * static_cast<int>(significant() >> -e);
121 } else {
122 if (e > 31)
123 return 0;
124 return sign() * (static_cast<int>(significant()) << e);
125 }
126 }
127
128 quint64 d = 0;
129};
130
132
133#endif // QJSNUMBERCOERCION_H
Combined button and popup list for selecting options.