1// Copyright (C) 2021 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#ifndef QMATH_H
5#define QMATH_H
6
7#if 0
8#pragma qt_class(QtMath)
9#endif
10
11#include <QtCore/qglobal.h>
12#include <QtCore/qalgorithms.h>
13#include <QtCore/qnumeric.h>
14
15#if __has_include(<bit>) && __cplusplus > 201703L
16#include <bit>
17#endif
18
19#include <cmath>
20
21QT_BEGIN_NAMESPACE
22
23#define QT_SINE_TABLE_SIZE 256
24
25extern Q_CORE_EXPORT const qreal qt_sine_table[QT_SINE_TABLE_SIZE];
26
27template <typename T> int qCeil(T v)
28{
29 using std::ceil;
30 return QtPrivate::qCheckedFPConversionToInteger<int>(ceil(v));
31}
32
33template <typename T> int qFloor(T v)
34{
35 using std::floor;
36 return QtPrivate::qCheckedFPConversionToInteger<int>(floor(v));
37}
38
39template <typename T> auto qFabs(T v)
40{
41 using std::fabs;
42 return fabs(v);
43}
44
45template <typename T> auto qSin(T v)
46{
47 using std::sin;
48 return sin(v);
49}
50
51template <typename T> auto qCos(T v)
52{
53 using std::cos;
54 return cos(v);
55}
56
57template <typename T> auto qTan(T v)
58{
59 using std::tan;
60 return tan(v);
61}
62
63template <typename T> auto qAcos(T v)
64{
65 using std::acos;
66 return acos(v);
67}
68
69template <typename T> auto qAsin(T v)
70{
71 using std::asin;
72 return asin(v);
73}
74
75template <typename T> auto qAtan(T v)
76{
77 using std::atan;
78 return atan(v);
79}
80
81template <typename T1, typename T2> auto qAtan2(T1 y, T2 x)
82{
83 using std::atan2;
84 return atan2(y, x);
85}
86
87template <typename T> auto qSqrt(T v)
88{
89 using std::sqrt;
90 return sqrt(v);
91}
92
93namespace QtPrivate {
94template <typename R, typename F> // For qfloat16 to specialize
95struct QHypotType { using type = decltype(std::hypot(R(1), F(1))); };
96
97// Implements hypot() without limiting number of arguments:
98template <typename T>
99class QHypotHelper
100{
101 T scale, total;
102 template <typename F> friend class QHypotHelper;
103 QHypotHelper(T first, T prior) : scale(first), total(prior) {}
104public:
105 QHypotHelper(T first) : scale(qAbs(first)), total(1) {}
106 T result() const
107 { return qIsFinite(scale) ? scale > 0 ? scale * T(qSqrt(total)) : T(0) : scale; }
108
109 template<typename F, typename ...Fs>
110 auto add(F first, Fs... rest) const
111 { return add(first).add(rest...); }
112
113 template<typename F, typename R = typename QHypotType<T, F>::type>
114 QHypotHelper<R> add(F next) const
115 {
116 if (qIsInf(scale) || (qIsNaN(scale) && !qIsInf(next)))
117 return QHypotHelper<R>(scale, R(1));
118 if (qIsNaN(next))
119 return QHypotHelper<R>(next, R(1));
120 const R val = qAbs(next);
121 if (!(scale > 0) || qIsInf(next))
122 return QHypotHelper<R>(val, R(1));
123 if (!(val > 0))
124 return QHypotHelper<R>(scale, total);
125 if (val > scale) {
126 const R ratio = scale / next;
127 return QHypotHelper<R>(val, total * ratio * ratio + R(1));
128 }
129 const R ratio = next / scale;
130 return QHypotHelper<R>(scale, total + ratio * ratio);
131 }
132};
133} // QtPrivate
134
135template<typename F, typename ...Fs>
136auto qHypot(F first, Fs... rest)
137{
138 return QtPrivate::QHypotHelper<F>(first).add(rest...).result();
139}
140
141// However, where possible, use the standard library implementations:
142template <typename Tx, typename Ty>
143auto qHypot(Tx x, Ty y)
144{
145 // C99 has hypot(), hence C++11 has std::hypot()
146 using std::hypot;
147 return hypot(x, y);
148}
149
150#if defined(__cpp_lib_hypot) && __cpp_lib_hypot >= 201603L // Expected to be true
151template <typename Tx, typename Ty, typename Tz>
152auto qHypot(Tx x, Ty y, Tz z)
153{
154 using std::hypot;
155 return hypot(x, y, z);
156}
157#endif // else: no need to over-ride the arbitrarily-many-arg form
158
159template <typename T> auto qLn(T v)
160{
161 using std::log;
162 return log(v);
163}
164
165template <typename T> auto qExp(T v)
166{
167 using std::exp;
168 return exp(v);
169}
170
171template <typename T1, typename T2> auto qPow(T1 x, T2 y)
172{
173 using std::pow;
174 return pow(x, y);
175}
176
177// TODO: use template variables (e.g. Qt::pi<type>) for these once we have C++14 support:
178
179#ifndef M_E
180#define M_E (2.7182818284590452354)
181#endif
182
183#ifndef M_LOG2E
184#define M_LOG2E (1.4426950408889634074)
185#endif
186
187#ifndef M_LOG10E
188#define M_LOG10E (0.43429448190325182765)
189#endif
190
191#ifndef M_LN2
192#define M_LN2 (0.69314718055994530942)
193#endif
194
195#ifndef M_LN10
196#define M_LN10 (2.30258509299404568402)
197#endif
198
199#ifndef M_PI
200#define M_PI (3.14159265358979323846)
201#endif
202
203#ifndef M_PI_2
204#define M_PI_2 (1.57079632679489661923)
205#endif
206
207#ifndef M_PI_4
208#define M_PI_4 (0.78539816339744830962)
209#endif
210
211#ifndef M_1_PI
212#define M_1_PI (0.31830988618379067154)
213#endif
214
215#ifndef M_2_PI
216#define M_2_PI (0.63661977236758134308)
217#endif
218
219#ifndef M_2_SQRTPI
220#define M_2_SQRTPI (1.12837916709551257390)
221#endif
222
223#ifndef M_SQRT2
224#define M_SQRT2 (1.41421356237309504880)
225#endif
226
227#ifndef M_SQRT1_2
228#define M_SQRT1_2 (0.70710678118654752440)
229#endif
230
231inline qreal qFastSin(qreal x)
232{
233 int si = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
234 qreal d = x - si * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
235 int ci = si + QT_SINE_TABLE_SIZE / 4;
236 si &= QT_SINE_TABLE_SIZE - 1;
237 ci &= QT_SINE_TABLE_SIZE - 1;
238 return qt_sine_table[si] + (qt_sine_table[ci] - 0.5 * qt_sine_table[si] * d) * d;
239}
240
241inline qreal qFastCos(qreal x)
242{
243 int ci = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
244 qreal d = x - ci * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
245 int si = ci + QT_SINE_TABLE_SIZE / 4;
246 si &= QT_SINE_TABLE_SIZE - 1;
247 ci &= QT_SINE_TABLE_SIZE - 1;
248 return qt_sine_table[si] - (qt_sine_table[ci] + 0.5 * qt_sine_table[si] * d) * d;
249}
250
251constexpr inline float qDegreesToRadians(float degrees)
252{
253 return degrees * float(M_PI / 180);
254}
255
256constexpr inline double qDegreesToRadians(double degrees)
257{
258 return degrees * (M_PI / 180);
259}
260
261constexpr inline long double qDegreesToRadians(long double degrees)
262{
263 return degrees * (M_PI / 180);
264}
265
266template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
267constexpr inline double qDegreesToRadians(T degrees)
268{
269 return qDegreesToRadians(degrees: static_cast<double>(degrees));
270}
271
272constexpr inline float qRadiansToDegrees(float radians)
273{
274 return radians * float(180 / M_PI);
275}
276
277constexpr inline double qRadiansToDegrees(double radians)
278{
279 return radians * (180 / M_PI);
280}
281
282constexpr inline long double qRadiansToDegrees(long double radians)
283{
284 return radians * (180 / M_PI);
285}
286
287// A qRadiansToDegrees(Integral) overload isn't here; it's extremely
288// questionable that someone is manipulating quantities in radians
289// using integral datatypes...
290
291namespace QtPrivate {
292constexpr inline quint32 qConstexprNextPowerOfTwo(quint32 v)
293{
294 v |= v >> 1;
295 v |= v >> 2;
296 v |= v >> 4;
297 v |= v >> 8;
298 v |= v >> 16;
299 ++v;
300 return v;
301}
302
303constexpr inline quint64 qConstexprNextPowerOfTwo(quint64 v)
304{
305 v |= v >> 1;
306 v |= v >> 2;
307 v |= v >> 4;
308 v |= v >> 8;
309 v |= v >> 16;
310 v |= v >> 32;
311 ++v;
312 return v;
313}
314
315constexpr inline quint32 qConstexprNextPowerOfTwo(qint32 v)
316{
317 return qConstexprNextPowerOfTwo(v: quint32(v));
318}
319
320constexpr inline quint64 qConstexprNextPowerOfTwo(qint64 v)
321{
322 return qConstexprNextPowerOfTwo(v: quint64(v));
323}
324} // namespace QtPrivate
325
326constexpr inline quint32 qNextPowerOfTwo(quint32 v)
327{
328 Q_ASSERT(static_cast<qint32>(v) >= 0); // There is a next power of two
329#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
330 return std::bit_ceil(v + 1);
331#elif defined(QT_HAS_BUILTIN_CLZ)
332 if (v == 0)
333 return 1;
334 return 2U << (31 ^ QAlgorithmsPrivate::qt_builtin_clz(v));
335#else
336 return QtPrivate::qConstexprNextPowerOfTwo(v);
337#endif
338}
339
340constexpr inline quint64 qNextPowerOfTwo(quint64 v)
341{
342 Q_ASSERT(static_cast<qint64>(v) >= 0); // There is a next power of two
343#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
344 return std::bit_ceil(v + 1);
345#elif defined(QT_HAS_BUILTIN_CLZLL)
346 if (v == 0)
347 return 1;
348 return Q_UINT64_C(2) << (63 ^ QAlgorithmsPrivate::qt_builtin_clzll(v));
349#else
350 return QtPrivate::qConstexprNextPowerOfTwo(v);
351#endif
352}
353
354constexpr inline quint32 qNextPowerOfTwo(qint32 v)
355{
356 return qNextPowerOfTwo(v: quint32(v));
357}
358
359constexpr inline quint64 qNextPowerOfTwo(qint64 v)
360{
361 return qNextPowerOfTwo(v: quint64(v));
362}
363
364constexpr inline unsigned long qNextPowerOfTwo(unsigned long v)
365{
366 return qNextPowerOfTwo(v: QIntegerForSizeof<long>::Unsigned(v));
367}
368
369constexpr inline unsigned long qNextPowerOfTwo(long v)
370{
371 return qNextPowerOfTwo(v: QIntegerForSizeof<long>::Unsigned(v));
372}
373
374QT_END_NAMESPACE
375
376#endif // QMATH_H
377

source code of qtbase/src/corelib/kernel/qmath.h