Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qnumeric_p.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// Copyright (C) 2021 Intel Corporation.
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_P_H
6#define QNUMERIC_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include "QtCore/private/qglobal_p.h"
20#include "QtCore/qnumeric.h"
21#include "QtCore/qsimd.h"
22#include <cmath>
23#include <limits>
24#include <type_traits>
25
26#ifndef __has_extension
27# define __has_extension(X) 0
28#endif
29
30#if !defined(Q_CC_MSVC) && defined(Q_OS_QNX)
31# include <math.h>
32# ifdef isnan
33# define QT_MATH_H_DEFINES_MACROS
35namespace qnumeric_std_wrapper {
36// the 'using namespace std' below is cases where the stdlib already put the math.h functions in the std namespace and undefined the macros.
37Q_DECL_CONST_FUNCTION static inline bool math_h_isnan(double d) { using namespace std; return isnan(d); }
38Q_DECL_CONST_FUNCTION static inline bool math_h_isinf(double d) { using namespace std; return isinf(d); }
39Q_DECL_CONST_FUNCTION static inline bool math_h_isfinite(double d) { using namespace std; return isfinite(d); }
40Q_DECL_CONST_FUNCTION static inline int math_h_fpclassify(double d) { using namespace std; return fpclassify(d); }
41Q_DECL_CONST_FUNCTION static inline bool math_h_isnan(float f) { using namespace std; return isnan(f); }
42Q_DECL_CONST_FUNCTION static inline bool math_h_isinf(float f) { using namespace std; return isinf(f); }
43Q_DECL_CONST_FUNCTION static inline bool math_h_isfinite(float f) { using namespace std; return isfinite(f); }
44Q_DECL_CONST_FUNCTION static inline int math_h_fpclassify(float f) { using namespace std; return fpclassify(f); }
45}
47// These macros from math.h conflict with the real functions in the std namespace.
48# undef signbit
49# undef isnan
50# undef isinf
51# undef isfinite
52# undef fpclassify
53# endif // defined(isnan)
54#endif
55
57
58class qfloat16;
59
61#if defined(QT_MATH_H_DEFINES_MACROS)
62# undef QT_MATH_H_DEFINES_MACROS
63Q_DECL_CONST_FUNCTION static inline bool isnan(double d) { return math_h_isnan(d); }
64Q_DECL_CONST_FUNCTION static inline bool isinf(double d) { return math_h_isinf(d); }
65Q_DECL_CONST_FUNCTION static inline bool isfinite(double d) { return math_h_isfinite(d); }
66Q_DECL_CONST_FUNCTION static inline int fpclassify(double d) { return math_h_fpclassify(d); }
67Q_DECL_CONST_FUNCTION static inline bool isnan(float f) { return math_h_isnan(f); }
68Q_DECL_CONST_FUNCTION static inline bool isinf(float f) { return math_h_isinf(f); }
69Q_DECL_CONST_FUNCTION static inline bool isfinite(float f) { return math_h_isfinite(f); }
70Q_DECL_CONST_FUNCTION static inline int fpclassify(float f) { return math_h_fpclassify(f); }
71#else
72Q_DECL_CONST_FUNCTION static inline bool isnan(double d) { return std::isnan(d); }
73Q_DECL_CONST_FUNCTION static inline bool isinf(double d) { return std::isinf(d); }
74Q_DECL_CONST_FUNCTION static inline bool isfinite(double d) { return std::isfinite(d); }
75Q_DECL_CONST_FUNCTION static inline int fpclassify(double d) { return std::fpclassify(d); }
76Q_DECL_CONST_FUNCTION static inline bool isnan(float f) { return std::isnan(f); }
77Q_DECL_CONST_FUNCTION static inline bool isinf(float f) { return std::isinf(f); }
78Q_DECL_CONST_FUNCTION static inline bool isfinite(float f) { return std::isfinite(f); }
79Q_DECL_CONST_FUNCTION static inline int fpclassify(float f) { return std::fpclassify(f); }
80#endif
81}
82
83constexpr Q_DECL_CONST_FUNCTION static inline double qt_inf() noexcept
84{
85 static_assert(std::numeric_limits<double>::has_infinity,
86 "platform has no definition for infinity for type double");
87 return std::numeric_limits<double>::infinity();
88}
89
90#if QT_CONFIG(signaling_nan)
91constexpr Q_DECL_CONST_FUNCTION static inline double qt_snan() noexcept
92{
93 static_assert(std::numeric_limits<double>::has_signaling_NaN,
94 "platform has no definition for signaling NaN for type double");
95 return std::numeric_limits<double>::signaling_NaN();
96}
97#endif
98
99// Quiet NaN
100constexpr Q_DECL_CONST_FUNCTION static inline double qt_qnan() noexcept
101{
102 static_assert(std::numeric_limits<double>::has_quiet_NaN,
103 "platform has no definition for quiet NaN for type double");
104 return std::numeric_limits<double>::quiet_NaN();
105}
106
107Q_DECL_CONST_FUNCTION static inline bool qt_is_inf(double d)
108{
110}
111
112Q_DECL_CONST_FUNCTION static inline bool qt_is_nan(double d)
113{
115}
116
117Q_DECL_CONST_FUNCTION static inline bool qt_is_finite(double d)
118{
120}
121
122Q_DECL_CONST_FUNCTION static inline int qt_fpclassify(double d)
123{
125}
126
127Q_DECL_CONST_FUNCTION static inline bool qt_is_inf(float f)
128{
130}
131
132Q_DECL_CONST_FUNCTION static inline bool qt_is_nan(float f)
133{
135}
136
137Q_DECL_CONST_FUNCTION static inline bool qt_is_finite(float f)
138{
140}
141
142Q_DECL_CONST_FUNCTION static inline int qt_fpclassify(float f)
143{
145}
146
147#ifndef Q_QDOC
148namespace {
161template <typename T> static inline std::enable_if_t<std::is_integral_v<T>, bool>
162convertDoubleTo(double v, T *value, bool allow_precision_upgrade = true)
163{
164 static_assert(std::is_integral_v<T>);
165 constexpr bool TypeIsLarger = std::numeric_limits<T>::digits > std::numeric_limits<double>::digits;
166
167 if constexpr (TypeIsLarger) {
168 using S = std::make_signed_t<T>;
169 constexpr S max_mantissa = S(1) << std::numeric_limits<double>::digits;
170 // T has more bits than double's mantissa, so don't allow "upgrading"
171 // to T (makes it look like the number had more precision than really
172 // was transmitted)
173 if (!allow_precision_upgrade && !(v <= double(max_mantissa) && v >= double(-max_mantissa - 1)))
174 return false;
175 }
176
177 constexpr T Tmin = (std::numeric_limits<T>::min)();
178 constexpr T Tmax = (std::numeric_limits<T>::max)();
179
180 // The [conv.fpint] (7.10 Floating-integral conversions) section of the C++
181 // standard says only exact conversions are guaranteed. Converting
182 // integrals to floating-point with loss of precision has implementation-
183 // defined behavior whether the next higher or next lower is returned;
184 // converting FP to integral is UB if it can't be represented.
185 //
186 // That means we can't write UINT64_MAX+1. Writing ldexp(1, 64) would be
187 // correct, but Clang, ICC and MSVC don't realize that it's a constant and
188 // the math call stays in the compiled code.
189
190#if defined(Q_PROCESSOR_X86_64) && defined(__SSE2__)
191 // Of course, UB doesn't apply if we use intrinsics, in which case we are
192 // allowed to dpeend on exactly the processor's behavior. This
193 // implementation uses the truncating conversions from Scalar Double to
194 // integral types (CVTTSD2SI and VCVTTSD2USI), which is documented to
195 // return the "indefinite integer value" if the range of the target type is
196 // exceeded. (only implemented for x86-64 to avoid having to deal with the
197 // non-existence of the 64-bit intrinsics on i386)
198
199 if (std::numeric_limits<T>::is_signed) {
200 __m128d mv = _mm_set_sd(v);
201# ifdef __AVX512F__
202 // use explicit round control and suppress exceptions
203 if (sizeof(T) > 4)
204 *value = T(_mm_cvtt_roundsd_i64(mv, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
205 else
206 *value = _mm_cvtt_roundsd_i32(mv, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
207# else
208 *value = sizeof(T) > 4 ? T(_mm_cvttsd_si64(mv)) : _mm_cvttsd_si32(mv);
209# endif
210
211 // if *value is the "indefinite integer value", check if the original
212 // variable \a v is the same value (Tmin is an exact representation)
213 if (*value == Tmin && !_mm_ucomieq_sd(mv, _mm_set_sd(Tmin))) {
214 // v != Tmin, so it was out of range
215 if (v > 0)
216 *value = Tmax;
217 return false;
218 }
219
220 // convert the integer back to double and compare for equality with v,
221 // to determine if we've lost any precision
222 __m128d mi = _mm_setzero_pd();
223 mi = sizeof(T) > 4 ? _mm_cvtsi64_sd(mv, *value) : _mm_cvtsi32_sd(mv, *value);
224 return _mm_ucomieq_sd(mv, mi);
225 }
226
227# ifdef __AVX512F__
228 if (!std::numeric_limits<T>::is_signed) {
229 // Same thing as above, but this function operates on absolute values
230 // and the "indefinite integer value" for the 64-bit unsigned
231 // conversion (Tmax) is not representable in double, so it can never be
232 // the result of an in-range conversion. This is implemented for AVX512
233 // and later because of the unsigned conversion instruction. Converting
234 // to unsigned without losing an extra bit of precision prior to AVX512
235 // is left to the compiler below.
236
237 v = fabs(v);
238 __m128d mv = _mm_set_sd(v);
239
240 // use explicit round control and suppress exceptions
241 if (sizeof(T) > 4)
242 *value = T(_mm_cvtt_roundsd_u64(mv, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
243 else
244 *value = _mm_cvtt_roundsd_u32(mv, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
245
246 if (*value == Tmax) {
247 // no double can have an exact value of quint64(-1), but they can
248 // quint32(-1), so we need to compare for that
249 if (TypeIsLarger || _mm_ucomieq_sd(mv, _mm_set_sd(Tmax)))
250 return false;
251 }
252
253 // return true if it was an exact conversion
254 __m128d mi = _mm_setzero_pd();
255 mi = sizeof(T) > 4 ? _mm_cvtu64_sd(mv, *value) : _mm_cvtu32_sd(mv, *value);
256 return _mm_ucomieq_sd(mv, mi);
257 }
258# endif
259#endif
260
261 double supremum;
262 if (std::numeric_limits<T>::is_signed) {
263 supremum = -1.0 * Tmin; // -1 * (-2^63) = 2^63, exact (for T = qint64)
264 *value = Tmin;
265 if (v < Tmin)
266 return false;
267 } else {
268 using ST = typename std::make_signed<T>::type;
269 supremum = -2.0 * (std::numeric_limits<ST>::min)(); // -2 * (-2^63) = 2^64, exact (for T = quint64)
270 v = fabs(v);
271 }
272
273 *value = Tmax;
274 if (v >= supremum)
275 return false;
276
277 // Now we can convert, these two conversions cannot be UB
278 *value = T(v);
279
282
283 return *value == v;
284
286}
287
288template <typename T> static
289std::enable_if_t<std::is_floating_point_v<T> || std::is_same_v<T, qfloat16>, bool>
290convertDoubleTo(double v, T *value, bool allow_precision_upgrade = true)
291{
292 Q_UNUSED(allow_precision_upgrade);
293 constexpr T Huge = std::numeric_limits<T>::infinity();
294
295 if constexpr (std::numeric_limits<double>::max_exponent <=
296 std::numeric_limits<T>::max_exponent) {
297 // no UB can happen
298 *value = T(v);
299 return true;
300 }
301
302#if defined(__SSE2__) && (defined(Q_CC_GNU) || __has_extension(gnu_asm))
303 // The x86 CVTSD2SH instruction from SSE2 does what we want:
304 // - converts out-of-range doubles to ±infinity and sets #O
305 // - converts underflows to zero and sets #U
306 // We need to clear any previously-stored exceptions from it before the
307 // operation (3-cycle cost) and obtain the new state afterwards (1 cycle).
308
309 unsigned csr = _MM_MASK_MASK; // clear stored exception indicators
310 auto sse_check_result = [&](auto result) {
311 if ((csr & (_MM_EXCEPT_UNDERFLOW | _MM_EXCEPT_OVERFLOW)) == 0)
312 return true;
313 if (csr & _MM_EXCEPT_OVERFLOW)
314 return false;
315
316 // According to IEEE 754[1], #U is also set when the result is tiny and
317 // inexact, but still non-zero, so detect that (this won't generate
318 // good code for types without hardware support).
319 // [1] https://en.wikipedia.org/wiki/Floating-point_arithmetic#Exception_handling
320 return result != 0;
321 };
322
323 // Written directly in assembly because both Clang and GCC have been
324 // observed to reorder the STMXCSR instruction above the conversion
325 // operation. MSVC generates horrid code when using the intrinsics anyway,
326 // so it's not a loss.
327 // See https://github.com/llvm/llvm-project/issues/83661.
328 if constexpr (std::is_same_v<T, float>) {
329# ifdef __AVX__
330 asm ("vldmxcsr %[csr]\n\t"
331 "vcvtsd2ss %[in], %[in], %[out]\n\t"
332 "vstmxcsr %[csr]"
333 : [csr] "+m" (csr), [out] "=v" (*value) : [in] "v" (v));
334# else
335 asm ("ldmxcsr %[csr]\n\t"
336 "cvtsd2ss %[in], %[out]\n\t"
337 "stmxcsr %[csr]"
338 : [csr] "+m" (csr), [out] "=v" (*value) : [in] "v" (v));
339# endif
340 return sse_check_result(*value);
341 }
342
343# if defined(__F16C__) || defined(__AVX512FP16__)
344 if constexpr (sizeof(T) == 2 && std::numeric_limits<T>::max_exponent == 16) {
345 // qfloat16 or std::float16_t, but not std::bfloat16_t or std::bfloat8_t
346 auto doConvert = [&](auto *out) {
347 asm ("vldmxcsr %[csr]\n\t"
348# ifdef __AVX512FP16__
349 // AVX512FP16 & AVX10 have an instruction for this
350 "vcvtsd2sh %[in], %[in], %[out]\n\t"
351# else
352 "vcvtsd2ss %[in], %[in], %[out]\n\t" // sets DEST[MAXVL-1:128] := 0
353 "vcvtps2ph %[rc], %[out], %[out]\n\t"
354# endif
355 "vstmxcsr %[csr]"
356 : [csr] "+m" (csr), [out] "=v" (*out)
357 : [in] "v" (v), [rc] "i" (_MM_FROUND_CUR_DIRECTION)
358 );
359 return sse_check_result(out);
360 };
361
362 if constexpr (std::is_same_v<T, qfloat16> && !std::is_void_v<typename T::NativeType>) {
363 typename T::NativeType tmp;
364 bool b = doConvert(&tmp);
365 *value = tmp;
366 return b;
367 } else {
368# ifndef Q_CC_CLANG
369 // Clang can only implement this if it has a native FP16 type
370 return doConvert(value);
371# endif
372 }
373 }
374# endif
375#endif // __SSE2__ && inline assembly
376
377 if (!qt_is_finite(v) && std::numeric_limits<T>::has_infinity) {
378 // infinity (or NaN)
379 *value = T(v);
380 return true;
381 }
382
383 // Check for in-range value to ensure the conversion is not UB (see the
384 // comment above for Standard language).
385 if (std::fabs(v) > (std::numeric_limits<T>::max)()) {
386 *value = v < 0 ? -Huge : Huge;
387 return false;
388 }
389
390 *value = T(v);
391 if (v != 0 && *value == 0) {
392 // Underflow through loss of precision
393 return false;
394 }
395 return true;
396}
397
398template <typename T> inline bool add_overflow(T v1, T v2, T *r) { return qAddOverflow(v1, v2, r); }
399template <typename T> inline bool sub_overflow(T v1, T v2, T *r) { return qSubOverflow(v1, v2, r); }
400template <typename T> inline bool mul_overflow(T v1, T v2, T *r) { return qMulOverflow(v1, v2, r); }
401
402template <typename T, T V2> bool add_overflow(T v1, std::integral_constant<T, V2>, T *r)
403{
404 return qAddOverflow<T, V2>(v1, std::integral_constant<T, V2>{}, r);
405}
406
407template <auto V2, typename T> bool add_overflow(T v1, T *r)
408{
409 return qAddOverflow<V2, T>(v1, r);
410}
411
412template <typename T, T V2> bool sub_overflow(T v1, std::integral_constant<T, V2>, T *r)
413{
414 return qSubOverflow<T, V2>(v1, std::integral_constant<T, V2>{}, r);
415}
416
417template <auto V2, typename T> bool sub_overflow(T v1, T *r)
418{
419 return qSubOverflow<V2, T>(v1, r);
420}
421
422template <typename T, T V2> bool mul_overflow(T v1, std::integral_constant<T, V2>, T *r)
423{
424 return qMulOverflow<T, V2>(v1, std::integral_constant<T, V2>{}, r);
425}
426
427template <auto V2, typename T> bool mul_overflow(T v1, T *r)
428{
429 return qMulOverflow<V2, T>(v1, r);
430}
431}
432#endif // Q_QDOC
433
434/*
435 Safely narrows \a x to \c{To}. Let \c L be
436 \c{std::numeric_limit<To>::min()} and \c H be \c{std::numeric_limit<To>::max()}.
437
438 If \a x is less than L, returns L. If \a x is greater than H,
439 returns H. Otherwise, returns \c{To(x)}.
440*/
441template <typename To, typename From>
442static constexpr auto qt_saturate(From x)
443{
444 static_assert(std::is_integral_v<To>);
445 static_assert(std::is_integral_v<From>);
446
447 [[maybe_unused]]
448 constexpr auto Lo = (std::numeric_limits<To>::min)();
449 constexpr auto Hi = (std::numeric_limits<To>::max)();
450
451 if constexpr (std::is_signed_v<From> == std::is_signed_v<To>) {
452 // same signedness, we can accept regular integer conversion rules
453 return x < Lo ? Lo :
454 x > Hi ? Hi :
455 /*else*/ To(x);
456 } else {
457 if constexpr (std::is_signed_v<From>) { // ie. !is_signed_v<To>
458 if (x < From{0})
459 return To{0};
460 }
461
462 // from here on, x >= 0
463 using FromU = std::make_unsigned_t<From>;
464 using ToU = std::make_unsigned_t<To>;
465 return FromU(x) > ToU(Hi) ? Hi : To(x); // assumes Hi >= 0
466 }
467}
468
470
471#endif // QNUMERIC_P_H
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:47
Combined button and popup list for selecting options.
static Q_DECL_CONST_FUNCTION bool isinf(double d)
Definition qnumeric_p.h:73
static Q_DECL_CONST_FUNCTION int fpclassify(double d)
Definition qnumeric_p.h:75
static Q_DECL_CONST_FUNCTION bool isfinite(double d)
Definition qnumeric_p.h:74
static Q_DECL_CONST_FUNCTION bool isnan(double d)
Definition qnumeric_p.h:72
#define Q_DECL_CONST_FUNCTION
#define QT_WARNING_POP
#define QT_WARNING_DISABLE_FLOAT_COMPARE
#define QT_WARNING_PUSH
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
std::enable_if_t< std::is_unsigned_v< T >, bool > qAddOverflow(T v1, T v2, T *r)
Definition qnumeric.h:113
std::enable_if_t< std::is_unsigned_v< T >, bool > qSubOverflow(T v1, T v2, T *r)
Definition qnumeric.h:153
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:182
static constexpr auto qt_saturate(From x)
Definition qnumeric_p.h:442
static Q_DECL_CONST_FUNCTION bool qt_is_nan(double d)
Definition qnumeric_p.h:112
constexpr static Q_DECL_CONST_FUNCTION double qt_qnan() noexcept
Definition qnumeric_p.h:100
constexpr static Q_DECL_CONST_FUNCTION double qt_inf() noexcept
Definition qnumeric_p.h:83
static Q_DECL_CONST_FUNCTION int qt_fpclassify(double d)
Definition qnumeric_p.h:122
static Q_DECL_CONST_FUNCTION bool qt_is_inf(double d)
Definition qnumeric_p.h:107
static Q_DECL_CONST_FUNCTION bool qt_is_finite(double d)
Definition qnumeric_p.h:117
GLint GLfloat GLfloat GLfloat v2
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLboolean r
[2]
GLfloat GLfloat f
GLint GLfloat GLfloat v1
GLuint in
GLuint64EXT * result
[6]
#define Q_UNUSED(x)
QTextStream out(stdout)
[7]