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