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