1 | // Copyright (C) 2021 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 QNUMERIC_H |
5 | #define QNUMERIC_H |
6 | |
7 | #if 0 |
8 | #pragma qt_class(QtNumeric) |
9 | #endif |
10 | |
11 | #include <QtCore/qtconfigmacros.h> |
12 | #include <QtCore/qtcoreexports.h> |
13 | #include <QtCore/qtypes.h> |
14 | |
15 | #include <cmath> |
16 | #include <limits> |
17 | #include <type_traits> |
18 | |
19 | // min() and max() may be #defined by windows.h if that is included before, but we need them |
20 | // for std::numeric_limits below. You should not use the min() and max() macros, so we just #undef. |
21 | #ifdef min |
22 | # undef min |
23 | # undef max |
24 | #endif |
25 | |
26 | #if defined(Q_CC_MSVC) |
27 | # include <intrin.h> |
28 | # include <float.h> |
29 | # if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64) |
30 | # define Q_INTRINSIC_MUL_OVERFLOW64 |
31 | # define Q_UMULH(v1, v2) __umulh(v1, v2); |
32 | # define Q_SMULH(v1, v2) __mulh(v1, v2); |
33 | # pragma intrinsic(__umulh) |
34 | # pragma intrinsic(__mulh) |
35 | # endif |
36 | #endif |
37 | |
38 | # if defined(Q_OS_INTEGRITY) && defined(Q_PROCESSOR_ARM_64) |
39 | # include <arm64_ghs.h> |
40 | # define Q_INTRINSIC_MUL_OVERFLOW64 |
41 | # define Q_UMULH(v1, v2) __MULUH64(v1, v2); |
42 | # define Q_SMULH(v1, v2) __MULSH64(v1, v2); |
43 | #endif |
44 | |
45 | QT_BEGIN_NAMESPACE |
46 | |
47 | // To match std::is{inf,nan,finite} functions: |
48 | template <typename T> |
49 | constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type |
50 | qIsInf(T) { return false; } |
51 | template <typename T> |
52 | constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type |
53 | qIsNaN(T) { return false; } |
54 | template <typename T> |
55 | constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type |
56 | qIsFinite(T) { return true; } |
57 | |
58 | // Floating-point types (see qfloat16.h for its overloads). |
59 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(double d); |
60 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(double d); |
61 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(double d); |
62 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(double val); |
63 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(float f); |
64 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(float f); |
65 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(float f); |
66 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(float val); |
67 | |
68 | #if QT_CONFIG(signaling_nan) |
69 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qSNaN(); |
70 | #endif |
71 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN(); |
72 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf(); |
73 | |
74 | Q_CORE_EXPORT quint32 qFloatDistance(float a, float b); |
75 | Q_CORE_EXPORT quint64 qFloatDistance(double a, double b); |
76 | |
77 | #define Q_INFINITY (QT_PREPEND_NAMESPACE(qInf)()) |
78 | #if QT_CONFIG(signaling_nan) |
79 | # define Q_SNAN (QT_PREPEND_NAMESPACE(qSNaN)()) |
80 | #endif |
81 | #define Q_QNAN (QT_PREPEND_NAMESPACE(qQNaN)()) |
82 | |
83 | // Overflow math. |
84 | // This provides efficient implementations for int, unsigned, qsizetype and |
85 | // size_t. Implementations for 8- and 16-bit types will work but may not be as |
86 | // efficient. Implementations for 64-bit may be missing on 32-bit platforms. |
87 | |
88 | #if (Q_CC_GNU >= 500 || __has_builtin(__builtin_add_overflow)) \ |
89 | && !(QT_POINTER_SIZE == 4 && defined(Q_CC_CLANG)) |
90 | // GCC 5 and Clang 3.8 have builtins to detect overflows |
91 | // 32 bit Clang has the builtins but tries to link a library which hasn't |
92 | #define Q_INTRINSIC_MUL_OVERFLOW64 |
93 | |
94 | template <typename T> inline |
95 | typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> |
96 | qAddOverflow(T v1, T v2, T *r) |
97 | { return __builtin_add_overflow(v1, v2, r); } |
98 | |
99 | template <typename T> inline |
100 | typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> |
101 | qSubOverflow(T v1, T v2, T *r) |
102 | { return __builtin_sub_overflow(v1, v2, r); } |
103 | |
104 | template <typename T> inline |
105 | typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> |
106 | qMulOverflow(T v1, T v2, T *r) |
107 | { return __builtin_mul_overflow(v1, v2, r); } |
108 | |
109 | #else |
110 | // Generic implementations |
111 | |
112 | template <typename T> inline typename std::enable_if_t<std::is_unsigned_v<T>, bool> |
113 | qAddOverflow(T v1, T v2, T *r) |
114 | { |
115 | // unsigned additions are well-defined |
116 | *r = v1 + v2; |
117 | return v1 > T(v1 + v2); |
118 | } |
119 | |
120 | template <typename T> inline typename std::enable_if_t<std::is_signed_v<T>, bool> |
121 | qAddOverflow(T v1, T v2, T *r) |
122 | { |
123 | // Here's how we calculate the overflow: |
124 | // 1) unsigned addition is well-defined, so we can always execute it |
125 | // 2) conversion from unsigned back to signed is implementation- |
126 | // defined and in the implementations we use, it's a no-op. |
127 | // 3) signed integer overflow happens if the sign of the two input operands |
128 | // is the same but the sign of the result is different. In other words, |
129 | // the sign of the result must be the same as the sign of either |
130 | // operand. |
131 | |
132 | using U = typename std::make_unsigned_t<T>; |
133 | *r = T(U(v1) + U(v2)); |
134 | |
135 | // If int is two's complement, assume all integer types are too. |
136 | if (std::is_same_v<int32_t, int>) { |
137 | // Two's complement equivalent (generates slightly shorter code): |
138 | // x ^ y is negative if x and y have different signs |
139 | // x & y is negative if x and y are negative |
140 | // (x ^ z) & (y ^ z) is negative if x and z have different signs |
141 | // AND y and z have different signs |
142 | return ((v1 ^ *r) & (v2 ^ *r)) < 0; |
143 | } |
144 | |
145 | bool s1 = (v1 < 0); |
146 | bool s2 = (v2 < 0); |
147 | bool sr = (*r < 0); |
148 | return s1 != sr && s2 != sr; |
149 | // also: return s1 == s2 && s1 != sr; |
150 | } |
151 | |
152 | template <typename T> inline typename std::enable_if_t<std::is_unsigned_v<T>, bool> |
153 | qSubOverflow(T v1, T v2, T *r) |
154 | { |
155 | // unsigned subtractions are well-defined |
156 | *r = v1 - v2; |
157 | return v1 < v2; |
158 | } |
159 | |
160 | template <typename T> inline typename std::enable_if_t<std::is_signed_v<T>, bool> |
161 | qSubOverflow(T v1, T v2, T *r) |
162 | { |
163 | // See above for explanation. This is the same with some signs reversed. |
164 | // We can't use qAddOverflow(v1, -v2, r) because it would be UB if |
165 | // v2 == std::numeric_limits<T>::min(). |
166 | |
167 | using U = typename std::make_unsigned_t<T>; |
168 | *r = T(U(v1) - U(v2)); |
169 | |
170 | if (std::is_same_v<int32_t, int>) |
171 | return ((v1 ^ *r) & (~v2 ^ *r)) < 0; |
172 | |
173 | bool s1 = (v1 < 0); |
174 | bool s2 = !(v2 < 0); |
175 | bool sr = (*r < 0); |
176 | return s1 != sr && s2 != sr; |
177 | // also: return s1 == s2 && s1 != sr; |
178 | } |
179 | |
180 | template <typename T> inline |
181 | typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> |
182 | qMulOverflow(T v1, T v2, T *r) |
183 | { |
184 | // use the next biggest type |
185 | // Note: for 64-bit systems where __int128 isn't supported, this will cause an error. |
186 | using LargerInt = QIntegerForSize<sizeof(T) * 2>; |
187 | using Larger = typename std::conditional_t<std::is_signed_v<T>, |
188 | typename LargerInt::Signed, typename LargerInt::Unsigned>; |
189 | Larger lr = Larger(v1) * Larger(v2); |
190 | *r = T(lr); |
191 | return lr > (std::numeric_limits<T>::max)() || lr < (std::numeric_limits<T>::min)(); |
192 | } |
193 | |
194 | # if defined(Q_INTRINSIC_MUL_OVERFLOW64) |
195 | template <> inline bool qMulOverflow(quint64 v1, quint64 v2, quint64 *r) |
196 | { |
197 | *r = v1 * v2; |
198 | return Q_UMULH(v1, v2); |
199 | } |
200 | template <> inline bool qMulOverflow(qint64 v1, qint64 v2, qint64 *r) |
201 | { |
202 | // This is slightly more complex than the unsigned case above: the sign bit |
203 | // of 'low' must be replicated as the entire 'high', so the only valid |
204 | // values for 'high' are 0 and -1. Use unsigned multiply since it's the same |
205 | // as signed for the low bits and use a signed right shift to verify that |
206 | // 'high' is nothing but sign bits that match the sign of 'low'. |
207 | |
208 | qint64 high = Q_SMULH(v1, v2); |
209 | *r = qint64(quint64(v1) * quint64(v2)); |
210 | return (*r >> 63) != high; |
211 | } |
212 | |
213 | # if defined(Q_OS_INTEGRITY) && defined(Q_PROCESSOR_ARM_64) |
214 | template <> inline bool qMulOverflow(uint64_t v1, uint64_t v2, uint64_t *r) |
215 | { |
216 | return qMulOverflow<quint64>(v1,v2,reinterpret_cast<quint64*>(r)); |
217 | } |
218 | |
219 | template <> inline bool qMulOverflow(int64_t v1, int64_t v2, int64_t *r) |
220 | { |
221 | return qMulOverflow<qint64>(v1,v2,reinterpret_cast<qint64*>(r)); |
222 | } |
223 | # endif // OS_INTEGRITY ARM64 |
224 | # endif // Q_INTRINSIC_MUL_OVERFLOW64 |
225 | |
226 | # if defined(Q_CC_MSVC) && defined(Q_PROCESSOR_X86) |
227 | // We can use intrinsics for the unsigned operations with MSVC |
228 | template <> inline bool qAddOverflow(unsigned v1, unsigned v2, unsigned *r) |
229 | { return _addcarry_u32(0, v1, v2, r); } |
230 | |
231 | // 32-bit qMulOverflow is fine with the generic code above |
232 | |
233 | template <> inline bool qAddOverflow(quint64 v1, quint64 v2, quint64 *r) |
234 | { |
235 | # if defined(Q_PROCESSOR_X86_64) |
236 | return _addcarry_u64(0, v1, v2, reinterpret_cast<unsigned __int64 *>(r)); |
237 | # else |
238 | uint low, high; |
239 | uchar carry = _addcarry_u32(0, unsigned(v1), unsigned(v2), &low); |
240 | carry = _addcarry_u32(carry, v1 >> 32, v2 >> 32, &high); |
241 | *r = (quint64(high) << 32) | low; |
242 | return carry; |
243 | # endif // !x86-64 |
244 | } |
245 | # endif // MSVC X86 |
246 | #endif // !GCC |
247 | |
248 | // Implementations for addition, subtraction or multiplication by a |
249 | // compile-time constant. For addition and subtraction, we simply call the code |
250 | // that detects overflow at runtime. For multiplication, we compare to the |
251 | // maximum possible values before multiplying to ensure no overflow happens. |
252 | |
253 | template <typename T, T V2> bool qAddOverflow(T v1, std::integral_constant<T, V2>, T *r) |
254 | { |
255 | return qAddOverflow(v1, V2, r); |
256 | } |
257 | |
258 | template <auto V2, typename T> bool qAddOverflow(T v1, T *r) |
259 | { |
260 | return qAddOverflow(v1, std::integral_constant<T, V2>{}, r); |
261 | } |
262 | |
263 | template <typename T, T V2> bool qSubOverflow(T v1, std::integral_constant<T, V2>, T *r) |
264 | { |
265 | return qSubOverflow(v1, V2, r); |
266 | } |
267 | |
268 | template <auto V2, typename T> bool qSubOverflow(T v1, T *r) |
269 | { |
270 | return qSubOverflow(v1, std::integral_constant<T, V2>{}, r); |
271 | } |
272 | |
273 | template <typename T, T V2> bool qMulOverflow(T v1, std::integral_constant<T, V2>, T *r) |
274 | { |
275 | // Runtime detection for anything smaller than or equal to a register |
276 | // width, as most architectures' multiplication instructions actually |
277 | // produce a result twice as wide as the input registers, allowing us to |
278 | // efficiently detect the overflow. |
279 | if constexpr (sizeof(T) <= sizeof(qregisteruint)) { |
280 | return qMulOverflow(v1, V2, r); |
281 | |
282 | #ifdef Q_INTRINSIC_MUL_OVERFLOW64 |
283 | } else if constexpr (sizeof(T) <= sizeof(quint64)) { |
284 | // If we have intrinsics detecting overflow of 64-bit multiplications, |
285 | // then detect overflows through them up to 64 bits. |
286 | return qMulOverflow(v1, V2, r); |
287 | #endif |
288 | |
289 | } else if constexpr (V2 == 0 || V2 == 1) { |
290 | // trivial cases (and simplify logic below due to division by zero) |
291 | *r = v1 * V2; |
292 | return false; |
293 | } else if constexpr (V2 == -1) { |
294 | // multiplication by -1 is valid *except* for signed minimum values |
295 | // (necessary to avoid diving min() by -1, which is an overflow) |
296 | if (v1 < 0 && v1 == (std::numeric_limits<T>::min)()) |
297 | return true; |
298 | *r = -v1; |
299 | return false; |
300 | } else { |
301 | // For 64-bit multiplications on 32-bit platforms, let's instead compare v1 |
302 | // against the bounds that would overflow. |
303 | constexpr T Highest = (std::numeric_limits<T>::max)() / V2; |
304 | constexpr T Lowest = (std::numeric_limits<T>::min)() / V2; |
305 | if constexpr (Highest > Lowest) { |
306 | if (v1 > Highest || v1 < Lowest) |
307 | return true; |
308 | } else { |
309 | // this can only happen if V2 < 0 |
310 | static_assert(V2 < 0); |
311 | if (v1 > Lowest || v1 < Highest) |
312 | return true; |
313 | } |
314 | |
315 | *r = v1 * V2; |
316 | return false; |
317 | } |
318 | } |
319 | |
320 | template <auto V2, typename T> bool qMulOverflow(T v1, T *r) |
321 | { |
322 | if constexpr (V2 == 2) |
323 | return qAddOverflow(v1, v1, r); |
324 | return qMulOverflow(v1, std::integral_constant<T, V2>{}, r); |
325 | } |
326 | |
327 | template <typename T> |
328 | constexpr inline T qAbs(const T &t) { return t >= 0 ? t : -t; } |
329 | |
330 | // gcc < 10 doesn't have __has_builtin |
331 | #if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG) |
332 | // ARM64 has a single instruction that can do C++ rounding with conversion to integer. |
333 | // Note current clang versions have non-constexpr __builtin_round, ### allow clang this path when they fix it. |
334 | constexpr inline int qRound(double d) |
335 | { return int(__builtin_round(d)); } |
336 | constexpr inline int qRound(float f) |
337 | { return int(__builtin_roundf(f)); } |
338 | constexpr inline qint64 qRound64(double d) |
339 | { return qint64(__builtin_round(d)); } |
340 | constexpr inline qint64 qRound64(float f) |
341 | { return qint64(__builtin_roundf(f)); } |
342 | #elif defined(__SSE2__) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU)) |
343 | // SSE has binary operations directly on floating point making copysign fast |
344 | constexpr inline int qRound(double d) |
345 | { return int(d + __builtin_copysign(0.5, d)); } |
346 | constexpr inline int qRound(float f) |
347 | { return int(f + __builtin_copysignf(0.5f, f)); } |
348 | constexpr inline qint64 qRound64(double d) |
349 | { return qint64(d + __builtin_copysign(0.5, d)); } |
350 | constexpr inline qint64 qRound64(float f) |
351 | { return qint64(f + __builtin_copysignf(0.5f, f)); } |
352 | #else |
353 | constexpr inline int qRound(double d) |
354 | { return d >= 0.0 ? int(d + 0.5) : int(d - 0.5); } |
355 | constexpr inline int qRound(float d) |
356 | { return d >= 0.0f ? int(d + 0.5f) : int(d - 0.5f); } |
357 | |
358 | constexpr inline qint64 qRound64(double d) |
359 | { return d >= 0.0 ? qint64(d + 0.5) : qint64(d - 0.5); } |
360 | constexpr inline qint64 qRound64(float d) |
361 | { return d >= 0.0f ? qint64(d + 0.5f) : qint64(d - 0.5f); } |
362 | #endif |
363 | |
364 | namespace QtPrivate { |
365 | template <typename T> |
366 | constexpr inline const T &min(const T &a, const T &b) { return (a < b) ? a : b; } |
367 | } |
368 | |
369 | [[nodiscard]] constexpr bool qFuzzyCompare(double p1, double p2) |
370 | { |
371 | return (qAbs(t: p1 - p2) * 1000000000000. <= QtPrivate::min(a: qAbs(t: p1), b: qAbs(t: p2))); |
372 | } |
373 | |
374 | [[nodiscard]] constexpr bool qFuzzyCompare(float p1, float p2) |
375 | { |
376 | return (qAbs(t: p1 - p2) * 100000.f <= QtPrivate::min(a: qAbs(t: p1), b: qAbs(t: p2))); |
377 | } |
378 | |
379 | [[nodiscard]] constexpr bool qFuzzyIsNull(double d) |
380 | { |
381 | return qAbs(t: d) <= 0.000000000001; |
382 | } |
383 | |
384 | [[nodiscard]] constexpr bool qFuzzyIsNull(float f) |
385 | { |
386 | return qAbs(t: f) <= 0.00001f; |
387 | } |
388 | |
389 | QT_WARNING_PUSH |
390 | QT_WARNING_DISABLE_FLOAT_COMPARE |
391 | |
392 | [[nodiscard]] constexpr bool qIsNull(double d) noexcept |
393 | { |
394 | return d == 0.0; |
395 | } |
396 | |
397 | [[nodiscard]] constexpr bool qIsNull(float f) noexcept |
398 | { |
399 | return f == 0.0f; |
400 | } |
401 | |
402 | QT_WARNING_POP |
403 | |
404 | inline int qIntCast(double f) { return int(f); } |
405 | inline int qIntCast(float f) { return int(f); } |
406 | |
407 | QT_END_NAMESPACE |
408 | |
409 | #endif // QNUMERIC_H |
410 | |