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