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
qnumeric.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2025 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:critical reason:data-parser
5
6#ifndef QNUMERIC_H
7#define QNUMERIC_H
8
9#if 0
10#pragma qt_class(QtNumeric)
11#endif
12
13#include <QtCore/qassert.h>
14#include <QtCore/qminmax.h>
15#include <QtCore/qtconfigmacros.h>
16#include <QtCore/qtcoreexports.h>
17#include <QtCore/qtypes.h>
18
19#include <cmath>
20#include <limits>
21#include <QtCore/q20type_traits.h>
22
23#if __has_include(<stdckdint.h>)
24// This was added by C23 and C++26, defining __STDC_VERSION_STDCKDINT_H__ when
25// usable. Its provided functions are in the global namespace even in C++ (not
26// std::) because they are sometimes macros.
27# include <QtCore/qstdlibdetection.h>
28# if defined(Q_CC_GNU_ONLY) && (defined(Q_STL_LIBCPP) || Q_CC_GNU_ONLY < 1500)
29// broken - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121811
30# elif defined(Q_OS_FREEBSD) && __FreeBSD_version <= 1500000
31// broken - https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=290299
32# else
33# include <stdckdint.h>
34# endif
35#endif
36
37// min() and max() may be #defined by windows.h if that is included before, but we need them
38// for std::numeric_limits below. You should not use the min() and max() macros, so we just #undef.
39#ifdef min
40# undef min
41# undef max
42#endif
43
44//
45// SIMDe (SIMD Everywhere) can't be used if intrin.h has been included as many definitions
46// conflict. Defining Q_NUMERIC_NO_INTRINSICS allows SIMDe users to use Qt, at the cost of
47// falling back to the prior implementations of qMulOverflow and qAddOverflow.
48//
49#if defined(Q_CC_MSVC) && !defined(Q_NUMERIC_NO_INTRINSICS)
50# include <intrin.h>
51# include <float.h>
52# if defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_X86_64)
53# define Q_HAVE_ADDCARRY
54# endif
55# if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64)
56# define Q_INTRINSIC_MUL_OVERFLOW64
57# define Q_UMULH(v1, v2) __umulh(v1, v2)
58# define Q_SMULH(v1, v2) __mulh(v1, v2)
59# pragma intrinsic(__umulh)
60# pragma intrinsic(__mulh)
61# endif
62#endif
63
64QT_BEGIN_NAMESPACE
65
66// To match std::is{inf,nan,finite} functions:
67template <typename T>
68constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
69qIsInf(T) { return false; }
70template <typename T>
71constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
72qIsNaN(T) { return false; }
73template <typename T>
74constexpr typename std::enable_if<std::is_integral<T>::value, bool>::type
75qIsFinite(T) { return true; }
76
77// Floating-point types (see qfloat16.h for its overloads).
78Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(double d);
79Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(double d);
80Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(double d);
81Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(double val);
82Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(float f);
83Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(float f);
84Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(float f);
85Q_CORE_EXPORT Q_DECL_CONST_FUNCTION int qFpClassify(float val);
86
87#if QT_CONFIG(signaling_nan)
88Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qSNaN();
89#endif
90Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN();
91Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf();
92
93Q_CORE_EXPORT quint32 qFloatDistance(float a, float b);
94Q_CORE_EXPORT quint64 qFloatDistance(double a, double b);
95
96#define Q_INFINITY (QT_PREPEND_NAMESPACE(qInf)())
97#if QT_CONFIG(signaling_nan)
98# define Q_SNAN (QT_PREPEND_NAMESPACE(qSNaN)())
99#endif
100#define Q_QNAN (QT_PREPEND_NAMESPACE(qQNaN)())
101
102// Overflow math.
103// This provides efficient implementations for int, unsigned, qsizetype and
104// size_t. Implementations for 8- and 16-bit types will work but may not be as
105// efficient. Implementations for 64-bit may be missing on 32-bit platforms.
106
107// All the GCC and Clang versions we support have constexpr
108// builtins for overflowing arithmetic.
109#if defined(Q_CC_GNU_ONLY)
110 || defined(Q_CC_CLANG_ONLY)
111 || __has_builtin(__builtin_add_overflow)
112# define Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
113// On 32-bit, Clang < 14 will fail to link if multiplying 64-bit
114// quantities (emits an unresolved call to __mulodi4), so we can't use
115// the builtin in that case.
116# if !(QT_POINTER_SIZE == 4 && defined(Q_CC_CLANG_ONLY) && Q_CC_CLANG_ONLY < 1400)
117# define Q_INTRINSIC_MUL_OVERFLOW64
118# endif
119#endif
120
121namespace QtPrivate {
122// Generic versions of (some) overflowing math functions, private API.
123template <typename T>
124constexpr inline
125typename std::enable_if_t<std::is_unsigned_v<T>, bool>
126qAddOverflowGeneric(T v1, T v2, T *r)
127{
128 // unsigned additions are well-defined
129 *r = v1 + v2;
130 return v1 > T(v1 + v2);
131}
132
133// Wide multiplication.
134// It has been isolated in its own function so that it can be tested.
135// Note that this implementation requires a T that doesn't undergo
136// promotions.
137template <typename T>
138constexpr inline
139typename std::enable_if_t<std::is_same_v<T, decltype(+T{})>, bool>
141{
142 // This is a glorified long/school-grade multiplication,
143 // that considers each input of N bits as two halves of N/2 bits:
144 //
145 // v1 = 2^(N/2) * v1_hi + v1_lo
146 // v2 = 2^(N/2) * v2_hi + v2_lo
147 //
148 // Therefore, v1*v2 = 2^N * v1_hi * v2_hi +
149 // 2^(N/2) * v1_hi * v2_lo +
150 // 2^(N/2) * v1_lo * v2_hi +
151 // * v1_lo * v2_lo
152 //
153 // Using the N bits of precision we have we can perform the hi*lo
154 // multiplications safely; that is never going to overflow.
155 //
156 // Then we can sum together these partial results:
157 //
158 // [ v1_hi | v1_lo ] *
159 // [ v2_hi | v2_lo ] =
160 // -------------------
161 // [ v1_lo * v2_lo ] +
162 // [ v1_hi * v2_lo ] + // shifted because it's * 2^(N/2)
163 // [ v2_hi * v1_lo ] + // shifted because it's * 2^(N/2)
164 // [ v1_hi * v2_hi ] = // shifted because it's * 2^N
165 // -------------------------------
166 // [ high ][ low ] // exact result (in 2^(2N) bits)
167 //
168 // ... except that this way we'll need to bring some carries, so
169 // we'll do a slightly smarter sum.
170 //
171 // We need high for detecting overflows, even if we are not returning it.
172
173 // Get multiplication by zero out of the way
174 if (v1 == 0 || v2 == 0) {
175 *r = T(0);
176 return false;
177 }
178
179 // Extract the absolute values as unsigned
180 // (will fix the sign later)
181 using U = std::make_unsigned_t<T>;
182 const U v1_abs = (v1 >= 0) ? U(v1) : (U(0) - U(v1));
183 const U v2_abs = (v2 >= 0) ? U(v2) : (U(0) - U(v2));
184
185 // Masks for N/2 bits
186 constexpr std::size_t half_width = (sizeof(U) * 8) / 2;
187 const U half_mask = ~U(0) >> half_width;
188
189 // Split in low and half quantities
190 const U v1_lo = v1_abs & half_mask;
191 const U v1_hi = v1_abs >> half_width;
192 const U v2_lo = v2_abs & half_mask;
193 const U v2_hi = v2_abs >> half_width;
194
195 // Cross-product; this will never overflow
196 const U lo_lo = v1_lo * v2_lo;
197 const U lo_hi = v1_lo * v2_hi;
198 const U hi_lo = v1_hi * v2_lo;
199 const U hi_hi = v1_hi * v2_hi;
200
201 // We could sum directly the cross-products, but then we'd have to
202 // keep track of carries. This avoids it.
203 const U tmp = (lo_lo >> half_width) + (hi_lo & half_mask) + lo_hi;
204 U result_hi = (hi_lo >> half_width) + (tmp >> half_width) + hi_hi;
205 U result_lo = (tmp << half_width) | (lo_lo & half_mask);
206
207 if constexpr (std::is_unsigned_v<T>) {
208 // If the source was unsigned, we're done; a non-zero high
209 // signals overflow.
210 *r = result_lo;
211 return result_hi != U(0);
212 } else {
213 // We need to set the correct sign back, and check for overflow.
214 const bool isNegative = (v1 < T(0)) != (v2 < T(0));
215 if (isNegative) {
216 // Result is negative; calculate two's complement of the
217 // [high, low] pair, by inverting the bits and adding 1,
218 // which is equivalent to negating it in unsigned
219 // arithmetic.
220 // This operation should be done on the pair as a whole,
221 // but we have the individual components, so start by
222 // calculating two's complement of low:
223 result_lo = U(0) - result_lo;
224
225 // If result_lo is 0, it means that the addition of 1 into
226 // it has overflown, so now we have a carry to add into the
227 // inverted high:
228 result_hi = ~result_hi;
229 if (result_lo == 0)
230 result_hi += U(1);
231 }
232
233 *r = result_lo;
234 // Overflow has happened if result_hi is not a sign extension
235 // of the sign bit of result_lo. Note the usage of T, not U.
236 return result_hi != U(*r >> std::numeric_limits<T>::digits);
237 }
238}
239
240template <typename T, typename Enable = void>
241constexpr inline bool HasLargerInt = false;
242template <typename T>
243constexpr inline bool HasLargerInt<T, std::void_t<typename QIntegerForSize<sizeof(T) * 2>::Unsigned>> = true;
244
245template <typename T>
246constexpr inline
247typename std::enable_if_t<(std::is_unsigned_v<T> || std::is_signed_v<T>), bool>
248qMulOverflowGeneric(T v1, T v2, T *r)
249{
250 // This function is a generic fallback for qMulOverflow,
251 // called either by constant or non-constant evaluation,
252 // if the compiler does not have builtins or intrinsics itself.
253 //
254 // (For instance, this is never going to be called on GCC or recent
255 // Clang, as their builtins will be used in all cases.)
256 //
257 // If a compiler does have builtins, please amend qMulOverflow
258 // directly.
259
260 if constexpr (HasLargerInt<T>) {
261 // Use the next biggest type if available
262 using LargerInt = QIntegerForSize<sizeof(T) * 2>;
263 using Larger = typename std::conditional_t<std::is_signed_v<T>,
264 typename LargerInt::Signed, typename LargerInt::Unsigned>;
265 Larger lr = Larger(v1) * Larger(v2);
266 *r = T(lr);
267 return lr > (std::numeric_limits<T>::max)() || lr < (std::numeric_limits<T>::min)();
268 } else {
269 // Otherwise fall back to a wide multiplication
270 return qMulOverflowWideMultiplication(v1, v2, r);
271 }
272}
273} // namespace QtPrivate
274
275template <typename T>
276constexpr inline
277typename std::enable_if_t<std::is_unsigned_v<T>, bool>
278qAddOverflow(T v1, T v2, T *r)
279{
280 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
281#if defined(__STDC_VERSION_STDCKDINT_H__)
282 return ckd_add(r, v1, v2);
283#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
284 return __builtin_add_overflow(v1, v2, r);
285#else
286 if (q20::is_constant_evaluated())
287 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
288# if defined(Q_HAVE_ADDCARRY)
289 // We can use intrinsics for the unsigned operations with MSVC
290 if constexpr (std::is_same_v<T, unsigned>) {
291 return _addcarry_u32(0, v1, v2, r);
292 } else if constexpr (std::is_same_v<T, quint64>) {
293# if defined(Q_PROCESSOR_X86_64)
294 return _addcarry_u64(0, v1, v2, reinterpret_cast<unsigned __int64 *>(r));
295# else
296 uint low, high;
297 uchar carry = _addcarry_u32(0, unsigned(v1), unsigned(v2), &low);
298 carry = _addcarry_u32(carry, v1 >> 32, v2 >> 32, &high);
299 *r = (quint64(high) << 32) | low;
300 return carry;
301# endif // defined(Q_PROCESSOR_X86_64)
302 }
303# endif // defined(Q_HAVE_ADDCARRY)
304 return QtPrivate::qAddOverflowGeneric(v1, v2, r);
305#endif
306}
307
308template <typename T>
309constexpr inline
310typename std::enable_if_t<std::is_signed_v<T>, bool>
311qAddOverflow(T v1, T v2, T *r)
312{
313 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
314#if defined(__STDC_VERSION_STDCKDINT_H__)
315 return ckd_add(r, v1, v2);
316#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
317 return __builtin_add_overflow(v1, v2, r);
318#else
319 // Here's how we calculate the overflow:
320 // 1) unsigned addition is well-defined, so we can always execute it
321 // 2) conversion from unsigned back to signed is implementation-
322 // defined and in the implementations we use, it's a no-op.
323 // 3) signed integer overflow happens if the sign of the two input operands
324 // is the same but the sign of the result is different. In other words,
325 // the sign of the result must be the same as the sign of either
326 // operand.
327
328 using U = typename std::make_unsigned_t<T>;
329 *r = T(U(v1) + U(v2));
330
331 // Two's complement equivalent (generates slightly shorter code):
332 // x ^ y is negative if x and y have different signs
333 // x & y is negative if x and y are negative
334 // (x ^ z) & (y ^ z) is negative if x and z have different signs
335 // AND y and z have different signs
336 return ((v1 ^ *r) & (v2 ^ *r)) < 0;
337#endif
338}
339
340template <typename T>
341constexpr inline
342typename std::enable_if_t<std::is_unsigned_v<T>, bool>
343qSubOverflow(T v1, T v2, T *r)
344{
345 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
346#if defined(__STDC_VERSION_STDCKDINT_H__)
347 return ckd_sub(r, v1, v2);
348#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
349 return __builtin_sub_overflow(v1, v2, r);
350#else
351 // unsigned subtractions are well-defined
352 *r = v1 - v2;
353 return v1 < v2;
354#endif
355}
356
357template <typename T>
358constexpr inline
359typename std::enable_if_t<std::is_signed_v<T>, bool>
360qSubOverflow(T v1, T v2, T *r)
361{
362 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
363#if defined(__STDC_VERSION_STDCKDINT_H__)
364 return ckd_sub(r, v1, v2);
365#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
366 return __builtin_sub_overflow(v1, v2, r);
367#else
368 // See above for explanation. This is the same with some signs reversed.
369 // We can't use qAddOverflow(v1, -v2, r) because it would be UB if
370 // v2 == std::numeric_limits<T>::min().
371
372 using U = typename std::make_unsigned_t<T>;
373 *r = T(U(v1) - U(v2));
374
375 return ((v1 ^ *r) & (~v2 ^ *r)) < 0;
376#endif
377}
378
379template <typename T>
380constexpr inline
381typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool>
382qMulOverflow(T v1, T v2, T *r)
383{
384 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
385#if defined(__STDC_VERSION_STDCKDINT_H__)
386 return ckd_mul(r, v1, v2);
387#elif defined(Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS)
388# if defined(Q_INTRINSIC_MUL_OVERFLOW64)
389 return __builtin_mul_overflow(v1, v2, r);
390# else
391 if constexpr (sizeof(T) <= 4)
392 return __builtin_mul_overflow(v1, v2, r);
393 else
394 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
395# endif
396#else
397 if (q20::is_constant_evaluated())
398 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
399
400# if defined(Q_INTRINSIC_MUL_OVERFLOW64)
401 if constexpr (std::is_unsigned_v<T> && (sizeof(T) == sizeof(quint64))) {
402 // T is 64 bit; either unsigned long long,
403 // or unsigned long on LP64 platforms.
404 *r = v1 * v2;
405 return T(Q_UMULH(v1, v2));
406 } else if constexpr (std::is_signed_v<T> && (sizeof(T) == sizeof(qint64))) {
407 // This is slightly more complex than the unsigned case above: the sign bit
408 // of 'low' must be replicated as the entire 'high', so the only valid
409 // values for 'high' are 0 and -1. Use unsigned multiply since it's the same
410 // as signed for the low bits and use a signed right shift to verify that
411 // 'high' is nothing but sign bits that match the sign of 'low'.
412
413 qint64 high = Q_SMULH(v1, v2);
414 *r = qint64(quint64(v1) * quint64(v2));
415 return (*r >> 63) != high;
416 }
417# endif // defined(Q_INTRINSIC_MUL_OVERFLOW64)
418
419 return QtPrivate::qMulOverflowGeneric(v1, v2, r);
420#endif
421}
422
423#undef Q_HAVE_ADDCARRY
424#undef Q_NUMERIC_USE_GCC_OVERFLOW_BUILTINS
425
426// Implementations for addition, subtraction or multiplication by a
427// compile-time constant. For addition and subtraction, we simply call the code
428// that detects overflow at runtime. For multiplication, we compare to the
429// maximum possible values before multiplying to ensure no overflow happens.
430
431template <typename T, T V2> constexpr bool qAddOverflow(T v1, std::integral_constant<T, V2>, T *r)
432{
433 return qAddOverflow(v1, V2, r);
434}
435
436template <auto V2, typename T> constexpr bool qAddOverflow(T v1, T *r)
437{
438 return qAddOverflow(v1, std::integral_constant<T, V2>{}, r);
439}
440
441template <typename T, T V2> constexpr bool qSubOverflow(T v1, std::integral_constant<T, V2>, T *r)
442{
443 return qSubOverflow(v1, V2, r);
444}
445
446template <auto V2, typename T> constexpr bool qSubOverflow(T v1, T *r)
447{
448 return qSubOverflow(v1, std::integral_constant<T, V2>{}, r);
449}
450
451template <typename T, T V2> constexpr bool qMulOverflow(T v1, std::integral_constant<T, V2>, T *r)
452{
453 static_assert(!std::is_same_v<T, char>, "Template must be an integral other than plain 'char'");
454
455 // Runtime detection for anything smaller than or equal to a register
456 // width, as most architectures' multiplication instructions actually
457 // produce a result twice as wide as the input registers, allowing us to
458 // efficiently detect the overflow.
459 if constexpr (sizeof(T) <= sizeof(qregisteruint)) {
460 return qMulOverflow(v1, V2, r);
461
462#ifdef Q_INTRINSIC_MUL_OVERFLOW64
463 } else if constexpr (sizeof(T) <= sizeof(quint64)) {
464 // If we have intrinsics detecting overflow of 64-bit multiplications,
465 // then detect overflows through them up to 64 bits.
466 return qMulOverflow(v1, V2, r);
467#endif
468
469 } else if constexpr (V2 == 0 || V2 == 1) {
470 // trivial cases (and simplify logic below due to division by zero)
471 *r = v1 * V2;
472 return false;
473 } else if constexpr (V2 == -1) {
474 // multiplication by -1 is valid *except* for signed minimum values
475 // (necessary to avoid diving min() by -1, which is an overflow)
476 if (v1 < 0 && v1 == (std::numeric_limits<T>::min)())
477 return true;
478 *r = -v1;
479 return false;
480 } else {
481 // For 64-bit multiplications on 32-bit platforms, let's instead compare v1
482 // against the bounds that would overflow.
483 constexpr T Highest = (std::numeric_limits<T>::max)() / V2;
484 constexpr T Lowest = (std::numeric_limits<T>::min)() / V2;
485 if constexpr (Highest > Lowest) {
486 if (v1 > Highest || v1 < Lowest)
487 return true;
488 } else {
489 // this can only happen if V2 < 0
490 static_assert(V2 < 0);
491 if (v1 > Lowest || v1 < Highest)
492 return true;
493 }
494
495 *r = v1 * V2;
496 return false;
497 }
498}
499
500template <auto V2, typename T> constexpr bool qMulOverflow(T v1, T *r)
501{
502 if constexpr (V2 == 2)
503 return qAddOverflow(v1, v1, r);
504 return qMulOverflow(v1, std::integral_constant<T, V2>{}, r);
505}
506
507template <typename T>
508constexpr inline T qAbs(const T &t)
509{
510 if constexpr (std::is_integral_v<T> && std::is_signed_v<T>)
511 Q_ASSERT(t != std::numeric_limits<T>::min());
512 return t >= 0 ? t : -t;
513}
514
515namespace QtPrivate {
516template <typename T,
517 typename std::enable_if_t<std::is_integral_v<T>, bool> = true>
518constexpr inline auto qUnsignedAbs(T t)
519{
520 using U = std::make_unsigned_t<T>;
521 return (t >= 0) ? U(t) : U(~U(t) + U(1));
522}
523
524template <typename Result,
525 typename FP,
526 typename std::enable_if_t<std::is_integral_v<Result>, bool> = true,
527 typename std::enable_if_t<std::is_floating_point_v<FP>, bool> = true>
528constexpr inline Result qCheckedFPConversionToInteger(FP value)
529{
530#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
531 if (!q20::is_constant_evaluated())
532 Q_ASSERT(!std::isnan(value));
533#endif
534
535 constexpr Result minimal = (std::numeric_limits<Result>::min)();
536 constexpr Result maximal = (std::numeric_limits<Result>::max)();
537
538 // We want to check that `value > minimal-1`. `minimal` is
539 // precisely representable as FP (it's -2^N), but `minimal-1`
540 // may not be. Just rearrange the terms:
541 Q_ASSERT(value - FP(minimal) > FP(-1));
542
543 // Symmetrically, `maximal` may not have a precise
544 // representation, but `maximal+1` has, so calculate that:
545 constexpr FP maximalPlusOne = FP(2) * (maximal / 2 + 1);
546 // And check that we're below that:
547 Q_ASSERT(value < maximalPlusOne);
548
549 // If both checks passed, the result of truncation is representable
550 // as `Result`:
551 return Result(value);
552}
553
554namespace QRoundImpl {
555// gcc < 10 doesn't have __has_builtin
556#if defined(Q_PROCESSOR_ARM_64) && (__has_builtin(__builtin_round) || defined(Q_CC_GNU)) && !defined(Q_CC_CLANG)
557// ARM64 has a single instruction that can do C++ rounding with conversion to integer.
558// Note current clang versions have non-constexpr __builtin_round, ### allow clang this path when they fix it.
559constexpr inline double qRound(double d)
560{ return __builtin_round(d); }
561constexpr inline float qRound(float f)
562{ return __builtin_roundf(f); }
563#elif defined(__SSE2__) && (__has_builtin(__builtin_copysign) || defined(Q_CC_GNU))
564// SSE has binary operations directly on floating point making copysign fast
565constexpr inline double qRound(double d)
566{ return d + __builtin_copysign(0.5, d); }
567constexpr inline float qRound(float f)
568{ return f + __builtin_copysignf(0.5f, f); }
569#else
570constexpr inline double qRound(double d)
571{ return d >= 0.0 ? d + 0.5 : d - 0.5; }
572constexpr inline float qRound(float d)
573{ return d >= 0.0f ? d + 0.5f : d - 0.5f; }
574#endif
575} // namespace QRoundImpl
576
577// Like qRound, but have well-defined saturating behavior.
578// NaN is not handled.
579template <typename FP,
580 typename std::enable_if_t<std::is_floating_point_v<FP>, bool> = true>
581constexpr inline int qSaturateRound(FP value)
582{
583#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED
584 if (!q20::is_constant_evaluated())
585 Q_ASSERT(!qIsNaN(value));
586#endif
587 constexpr FP MinBound = FP((std::numeric_limits<int>::min)());
588 constexpr FP MaxBound = FP((std::numeric_limits<int>::max)());
589 const FP beforeTruncation = QRoundImpl::qRound(value);
590 return int(qBound(MinBound, beforeTruncation, MaxBound));
591}
592} // namespace QtPrivate
593
594constexpr inline int qRound(double d)
595{
596 return QtPrivate::qCheckedFPConversionToInteger<int>(QtPrivate::QRoundImpl::qRound(d));
597}
598
599constexpr inline int qRound(float f)
600{
601 return QtPrivate::qCheckedFPConversionToInteger<int>(QtPrivate::QRoundImpl::qRound(f));
602}
603
604constexpr inline qint64 qRound64(double d)
605{
606 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(d));
607}
608
609constexpr inline qint64 qRound64(float f)
610{
611 return QtPrivate::qCheckedFPConversionToInteger<qint64>(QtPrivate::QRoundImpl::qRound(f));
612}
613
614namespace QtPrivate {
615template <typename T>
616constexpr inline const T &min(const T &a, const T &b) { return (a < b) ? a : b; }
617}
618
619[[nodiscard]] constexpr bool qFuzzyCompare(double p1, double p2) noexcept
620{
621 return (qAbs(p1 - p2) * 1000000000000. <= QtPrivate::min(qAbs(p1), qAbs(p2)));
622}
623
624[[nodiscard]] constexpr bool qFuzzyCompare(float p1, float p2) noexcept
625{
626 return (qAbs(p1 - p2) * 100000.f <= QtPrivate::min(qAbs(p1), qAbs(p2)));
627}
628
629[[nodiscard]] constexpr bool qFuzzyIsNull(double d) noexcept
630{
631 return qAbs(d) <= 0.000000000001;
632}
633
634[[nodiscard]] constexpr bool qFuzzyIsNull(float f) noexcept
635{
636 return qAbs(f) <= 0.00001f;
637}
638
639QT_WARNING_PUSH
640QT_WARNING_DISABLE_FLOAT_COMPARE
641
642[[nodiscard]] constexpr bool qIsNull(double d) noexcept
643{
644 return d == 0.0;
645}
646
647[[nodiscard]] constexpr bool qIsNull(float f) noexcept
648{
649 return f == 0.0f;
650}
651
653
654inline int qIntCast(double f) { return int(f); }
655inline int qIntCast(float f) { return int(f); }
656
657QT_END_NAMESPACE
658
659#endif // QNUMERIC_H
Combined button and popup list for selecting options.
constexpr const T & min(const T &a, const T &b)
Definition qnumeric.h:616
constexpr bool HasLargerInt
Definition qnumeric.h:241
constexpr int qSaturateRound(FP value)
Definition qnumeric.h:581
constexpr std::enable_if_t<(std::is_unsigned_v< T >||std::is_signed_v< T >), bool > qMulOverflowGeneric(T v1, T v2, T *r)
Definition qnumeric.h:248
constexpr auto qUnsignedAbs(T t)
Definition qnumeric.h:518
constexpr std::enable_if_t< std::is_unsigned_v< T >, bool > qAddOverflowGeneric(T v1, T v2, T *r)
Definition qnumeric.h:126
constexpr std::enable_if_t< std::is_same_v< T, decltype(+T{})>, bool > qMulOverflowWideMultiplication(T v1, T v2, T *r)
Definition qnumeric.h:140
constexpr Result qCheckedFPConversionToInteger(FP value)
Definition qnumeric.h:528
#define __has_builtin(x)
#define __has_include(x)
static quint64 d2i(double d)
Definition qnumeric.cpp:181
Q_CORE_EXPORT int qFpClassify(float val)
Definition qnumeric.cpp:102
Q_CORE_EXPORT int qFpClassify(double val)
Definition qnumeric.cpp:101
static quint32 f2i(float f)
Definition qnumeric.cpp:108
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(float f)
constexpr T qAbs(const T &t)
Definition qnumeric.h:508
constexpr std::enable_if< std::is_integral< T >::value, bool >::type qIsFinite(T)
Definition qnumeric.h:75
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsFinite(double d)
constexpr bool qSubOverflow(T v1, T *r)
Definition qnumeric.h:446
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf()
constexpr bool qMulOverflow(T v1, T *r)
Definition qnumeric.h:500
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(float f)
constexpr bool qFuzzyIsNull(double d) noexcept
Definition qnumeric.h:629
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(double d)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsInf(float f)
constexpr bool qFuzzyCompare(float p1, float p2) noexcept
Definition qnumeric.h:624
constexpr bool qSubOverflow(T v1, std::integral_constant< T, V2 >, T *r)
Definition qnumeric.h:441
constexpr bool qAddOverflow(T v1, T *r)
Definition qnumeric.h:436
constexpr std::enable_if< std::is_integral< T >::value, bool >::type qIsNaN(T)
Definition qnumeric.h:72
constexpr std::enable_if_t< std::is_unsigned_v< T >||std::is_signed_v< T >, bool > qMulOverflow(T v1, T v2, T *r)
Definition qnumeric.h:382
constexpr qint64 qRound64(double d)
Definition qnumeric.h:604
constexpr int qRound(float f)
Definition qnumeric.h:599
int qIntCast(float f)
Definition qnumeric.h:655
constexpr int qRound(double d)
Definition qnumeric.h:594
constexpr bool qIsNull(float f) noexcept
Definition qnumeric.h:647
constexpr bool qFuzzyIsNull(float f) noexcept
Definition qnumeric.h:634
constexpr std::enable_if_t< std::is_unsigned_v< T >, bool > qSubOverflow(T v1, T v2, T *r)
Definition qnumeric.h:343
constexpr bool qFuzzyCompare(double p1, double p2) noexcept
Definition qnumeric.h:619
constexpr qint64 qRound64(float f)
Definition qnumeric.h:609
constexpr bool qMulOverflow(T v1, std::integral_constant< T, V2 >, T *r)
Definition qnumeric.h:451
QT_WARNING_POP int qIntCast(double f)
Definition qnumeric.h:654
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN()
constexpr std::enable_if_t< std::is_unsigned_v< T >, bool > qAddOverflow(T v1, T v2, T *r)
Definition qnumeric.h:278
constexpr bool qAddOverflow(T v1, std::integral_constant< T, V2 >, T *r)
Definition qnumeric.h:431
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qIsNaN(double d)
QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE constexpr bool qIsNull(double d) noexcept
Definition qnumeric.h:642