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

source code of qtbase/src/corelib/global/qnumeric_p.h