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.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qnumeric.h"
5#include "qnumeric_p.h"
6#include <string.h>
7
9
26Q_CORE_EXPORT bool qIsInf(double d) { return qt_is_inf(d); }
27
32Q_CORE_EXPORT bool qIsNaN(double d) { return qt_is_nan(d); }
33
38Q_CORE_EXPORT bool qIsFinite(double d) { return qt_is_finite(d); }
39
45Q_CORE_EXPORT bool qIsInf(float f) { return qt_is_inf(f); }
46
51Q_CORE_EXPORT bool qIsNaN(float f) { return qt_is_nan(f); }
52
57Q_CORE_EXPORT bool qIsFinite(float f) { return qt_is_finite(f); }
58
59#if QT_CONFIG(signaling_nan)
64Q_CORE_EXPORT double qSNaN() { return qt_snan(); }
65#endif
66
72Q_CORE_EXPORT double qQNaN() { return qt_qnan(); }
73
79Q_CORE_EXPORT double qInf() { return qt_inf(); }
80
98Q_CORE_EXPORT int qFpClassify(double val) { return qt_fpclassify(val); }
99Q_CORE_EXPORT int qFpClassify(float val) { return qt_fpclassify(val); }
100
101
105static inline quint32 f2i(float f)
106{
107 quint32 i;
108 memcpy(&i, &f, sizeof(f));
109 return i;
110}
111
139Q_CORE_EXPORT quint32 qFloatDistance(float a, float b)
140{
141 static const quint32 smallestPositiveFloatAsBits = 0x00000001; // denormalized, (SMALLEST), (1.4E-45)
142 /* Assumes:
143 * IEE754 format.
144 * Integers and floats have the same endian
145 */
146 static_assert(sizeof(quint32) == sizeof(float));
148 if (a == b)
149 return 0;
150 if ((a < 0) != (b < 0)) {
151 // if they have different signs
152 if (a < 0)
153 a = -a;
154 else /*if (b < 0)*/
155 b = -b;
156 return qFloatDistance(0.0F, a) + qFloatDistance(0.0F, b);
157 }
158 if (a < 0) {
159 a = -a;
160 b = -b;
161 }
162 // at this point a and b should not be negative
163
164 // 0 is special
165 if (!a)
166 return f2i(b) - smallestPositiveFloatAsBits + 1;
167 if (!b)
168 return f2i(a) - smallestPositiveFloatAsBits + 1;
169
170 // finally do the common integer subtraction
171 return a > b ? f2i(a) - f2i(b) : f2i(b) - f2i(a);
172}
173
174
178static inline quint64 d2i(double d)
179{
180 quint64 i;
181 memcpy(&i, &d, sizeof(d));
182 return i;
183}
184
197Q_CORE_EXPORT quint64 qFloatDistance(double a, double b)
198{
199 static const quint64 smallestPositiveFloatAsBits = 0x1; // denormalized, (SMALLEST)
200 /* Assumes:
201 * IEE754 format double precision
202 * Integers and floats have the same endian
203 */
204 static_assert(sizeof(quint64) == sizeof(double));
206 if (a == b)
207 return 0;
208 if ((a < 0) != (b < 0)) {
209 // if they have different signs
210 if (a < 0)
211 a = -a;
212 else /*if (b < 0)*/
213 b = -b;
214 return qFloatDistance(0.0, a) + qFloatDistance(0.0, b);
215 }
216 if (a < 0) {
217 a = -a;
218 b = -b;
219 }
220 // at this point a and b should not be negative
221
222 // 0 is special
223 if (!a)
224 return d2i(b) - smallestPositiveFloatAsBits + 1;
225 if (!b)
226 return d2i(a) - smallestPositiveFloatAsBits + 1;
227
228 // finally do the common integer subtraction
229 return a > b ? d2i(a) - d2i(b) : d2i(b) - d2i(a);
230}
231
461namespace QtNumericTests {
462
463template <typename T> static constexpr T max = std::numeric_limits<T>::max();
464template <typename T> static constexpr T min = std::numeric_limits<T>::min();
465
466static_assert(qt_saturate<short>(max<unsigned>) == max<short>);
467static_assert(qt_saturate<int>(max<unsigned>) == max<int>);
468static_assert(qt_saturate<qint64>(max<unsigned>) == qint64(max<unsigned>));
469
470static_assert(qt_saturate<short>(max<int>) == max<short>);
471static_assert(qt_saturate<unsigned>(max<int>) == unsigned(max<int>));
472static_assert(qt_saturate<qint64>(max<int>) == qint64(max<int>));
473
474static_assert(qt_saturate<short>(max<qint64>) == max<short>);
475static_assert(qt_saturate<int>(max<qint64>) == max<int>);
476static_assert(qt_saturate<unsigned>(max<qint64>) == max<unsigned>);
477static_assert(qt_saturate<quint64>(max<qint64>) == quint64(max<qint64>));
478
479static_assert(qt_saturate<short>(max<quint64>) == max<short>);
480static_assert(qt_saturate<int>(max<quint64>) == max<int>);
481static_assert(qt_saturate<unsigned>(max<quint64>) == max<unsigned>);
482static_assert(qt_saturate<qint64>(max<quint64>) == max<qint64>);
483
484static_assert(qt_saturate<short>(min<int>) == min<short>);
485static_assert(qt_saturate<qint64>(min<int>) == qint64(min<int>));
486static_assert(qt_saturate<unsigned>(min<int>) == 0);
487static_assert(qt_saturate<quint64>(min<int>) == 0);
488
489static_assert(qt_saturate<short>(min<qint64>) == min<short>);
490static_assert(qt_saturate<int>(min<qint64>) == min<int>);
491static_assert(qt_saturate<unsigned>(min<qint64>) == 0);
492static_assert(qt_saturate<quint64>(min<qint64>) == 0);
493
494} // namespace QtNumericTests
495
Combined button and popup list for selecting options.
static constexpr T min
Definition qnumeric.cpp:464
static constexpr T max
Definition qnumeric.cpp:463
bool qIsFinite(qfloat16 f) noexcept
Definition qfloat16.h:285
bool qIsNaN(qfloat16 f) noexcept
Definition qfloat16.h:284
bool qIsInf(qfloat16 f) noexcept
Definition qfloat16.h:283
static quint64 d2i(double d)
Definition qnumeric.cpp:178
Q_CORE_EXPORT int qFpClassify(double val)
Definition qnumeric.cpp:98
static quint32 f2i(float f)
Definition qnumeric.cpp:105
Q_CORE_EXPORT quint32 qFloatDistance(float a, float b)
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf()
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qQNaN()
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
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLfloat GLfloat f
GLuint GLfloat * val
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int quint32
Definition qtypes.h:50
unsigned long long quint64
Definition qtypes.h:61
long long qint64
Definition qtypes.h:60