Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-20 09:21:54

0001 // Copyright (C) 2021 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:significant reason:default
0004 
0005 #ifndef QMATH_H
0006 #define QMATH_H
0007 
0008 #if 0
0009 #pragma qt_class(QtMath)
0010 #endif
0011 
0012 #include <QtCore/qglobal.h>
0013 #include <QtCore/qalgorithms.h>
0014 #include <QtCore/qnumeric.h>
0015 
0016 #if __has_include(<bit>) && __cplusplus > 201703L
0017 #include <bit>
0018 #endif
0019 
0020 #include <cmath>
0021 
0022 QT_BEGIN_NAMESPACE
0023 
0024 #define QT_SINE_TABLE_SIZE 256
0025 
0026 extern Q_CORE_EXPORT const qreal qt_sine_table[QT_SINE_TABLE_SIZE];
0027 
0028 template <typename T> int qCeil(T v)
0029 {
0030     using std::ceil;
0031     return QtPrivate::qCheckedFPConversionToInteger<int>(ceil(v));
0032 }
0033 
0034 template <typename T> int qFloor(T v)
0035 {
0036     using std::floor;
0037     return QtPrivate::qCheckedFPConversionToInteger<int>(floor(v));
0038 }
0039 
0040 template <typename T> auto qFabs(T v)
0041 {
0042     using std::fabs;
0043     return fabs(v);
0044 }
0045 
0046 template <typename T> auto qSin(T v)
0047 {
0048     using std::sin;
0049     return sin(v);
0050 }
0051 
0052 template <typename T> auto qCos(T v)
0053 {
0054     using std::cos;
0055     return cos(v);
0056 }
0057 
0058 template <typename T> auto qTan(T v)
0059 {
0060     using std::tan;
0061     return tan(v);
0062 }
0063 
0064 template <typename T> auto qAcos(T v)
0065 {
0066     using std::acos;
0067     return acos(v);
0068 }
0069 
0070 template <typename T> auto qAsin(T v)
0071 {
0072     using std::asin;
0073     return asin(v);
0074 }
0075 
0076 template <typename T> auto qAtan(T v)
0077 {
0078     using std::atan;
0079     return atan(v);
0080 }
0081 
0082 template <typename T1, typename T2> auto qAtan2(T1 y, T2 x)
0083 {
0084     using std::atan2;
0085     return atan2(y, x);
0086 }
0087 
0088 template <typename T> auto qSqrt(T v)
0089 {
0090     using std::sqrt;
0091     return sqrt(v);
0092 }
0093 
0094 namespace QtPrivate {
0095 template <typename R, typename F> // For qfloat16 to specialize
0096 struct QHypotType { using type = decltype(std::hypot(R(1), F(1))); };
0097 
0098 // Implements hypot() without limiting number of arguments:
0099 template <typename T>
0100 class QHypotHelper
0101 {
0102     T scale, total;
0103     template <typename F> friend class QHypotHelper;
0104     QHypotHelper(T first, T prior) : scale(first), total(prior) {}
0105 public:
0106     QHypotHelper(T first) : scale(qAbs(first)), total(1) {}
0107     T result() const
0108     { return qIsFinite(scale) ? scale > 0 ? scale * T(qSqrt(total)) : T(0) : scale; }
0109 
0110     template<typename F, typename ...Fs>
0111     auto add(F first, Fs... rest) const
0112     { return add(first).add(rest...); }
0113 
0114     template<typename F, typename R = typename QHypotType<T, F>::type>
0115     QHypotHelper<R> add(F next) const
0116     {
0117         if (qIsInf(scale) || (qIsNaN(scale) && !qIsInf(next)))
0118             return QHypotHelper<R>(scale, R(1));
0119         if (qIsNaN(next))
0120             return QHypotHelper<R>(next, R(1));
0121         const R val = qAbs(next);
0122         if (!(scale > 0) || qIsInf(next))
0123             return QHypotHelper<R>(val, R(1));
0124         if (!(val > 0))
0125             return QHypotHelper<R>(scale, total);
0126         if (val > scale) {
0127             const R ratio = scale / next;
0128             return QHypotHelper<R>(val, total * ratio * ratio + R(1));
0129         }
0130         const R ratio = next / scale;
0131         return QHypotHelper<R>(scale, total + ratio * ratio);
0132     }
0133 };
0134 } // QtPrivate
0135 
0136 template<typename F, typename ...Fs>
0137 auto qHypot(F first, Fs... rest)
0138 {
0139     return QtPrivate::QHypotHelper<F>(first).add(rest...).result();
0140 }
0141 
0142 // However, where possible, use the standard library implementations:
0143 template <typename Tx, typename Ty>
0144 auto qHypot(Tx x, Ty y)
0145 {
0146     // C99 has hypot(), hence C++11 has std::hypot()
0147     using std::hypot;
0148     return hypot(x, y);
0149 }
0150 
0151 #if defined(__cpp_lib_hypot) && __cpp_lib_hypot >= 201603L // Expected to be true
0152 template <typename Tx, typename Ty, typename Tz>
0153 auto qHypot(Tx x, Ty y, Tz z)
0154 {
0155     using std::hypot;
0156     return hypot(x, y, z);
0157 }
0158 #endif // else: no need to over-ride the arbitrarily-many-arg form
0159 
0160 template <typename T> auto qLn(T v)
0161 {
0162     using std::log;
0163     return log(v);
0164 }
0165 
0166 template <typename T> auto qExp(T v)
0167 {
0168     using std::exp;
0169     return exp(v);
0170 }
0171 
0172 template <typename T1, typename T2> auto qPow(T1 x, T2 y)
0173 {
0174     using std::pow;
0175     return pow(x, y);
0176 }
0177 
0178 // TODO: use template variables (e.g. Qt::pi<type>) for these once we have C++14 support:
0179 
0180 #ifndef M_E
0181 #define M_E (2.7182818284590452354)
0182 #endif
0183 
0184 #ifndef M_LOG2E
0185 #define M_LOG2E (1.4426950408889634074)
0186 #endif
0187 
0188 #ifndef M_LOG10E
0189 #define M_LOG10E (0.43429448190325182765)
0190 #endif
0191 
0192 #ifndef M_LN2
0193 #define M_LN2 (0.69314718055994530942)
0194 #endif
0195 
0196 #ifndef M_LN10
0197 #define M_LN10 (2.30258509299404568402)
0198 #endif
0199 
0200 #ifndef M_PI
0201 #define M_PI (3.14159265358979323846)
0202 #endif
0203 
0204 #ifndef M_PI_2
0205 #define M_PI_2 (1.57079632679489661923)
0206 #endif
0207 
0208 #ifndef M_PI_4
0209 #define M_PI_4 (0.78539816339744830962)
0210 #endif
0211 
0212 #ifndef M_1_PI
0213 #define M_1_PI (0.31830988618379067154)
0214 #endif
0215 
0216 #ifndef M_2_PI
0217 #define M_2_PI (0.63661977236758134308)
0218 #endif
0219 
0220 #ifndef M_2_SQRTPI
0221 #define M_2_SQRTPI (1.12837916709551257390)
0222 #endif
0223 
0224 #ifndef M_SQRT2
0225 #define M_SQRT2 (1.41421356237309504880)
0226 #endif
0227 
0228 #ifndef M_SQRT1_2
0229 #define M_SQRT1_2 (0.70710678118654752440)
0230 #endif
0231 
0232 inline qreal qFastSin(qreal x)
0233 {
0234     int si = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
0235     qreal d = x - si * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
0236     int ci = si + QT_SINE_TABLE_SIZE / 4;
0237     si &= QT_SINE_TABLE_SIZE - 1;
0238     ci &= QT_SINE_TABLE_SIZE - 1;
0239     return qt_sine_table[si] + (qt_sine_table[ci] - 0.5 * qt_sine_table[si] * d) * d;
0240 }
0241 
0242 inline qreal qFastCos(qreal x)
0243 {
0244     int ci = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
0245     qreal d = x - ci * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
0246     int si = ci + QT_SINE_TABLE_SIZE / 4;
0247     si &= QT_SINE_TABLE_SIZE - 1;
0248     ci &= QT_SINE_TABLE_SIZE - 1;
0249     return qt_sine_table[si] - (qt_sine_table[ci] + 0.5 * qt_sine_table[si] * d) * d;
0250 }
0251 
0252 constexpr inline float qDegreesToRadians(float degrees)
0253 {
0254     return degrees * float(M_PI / 180);
0255 }
0256 
0257 constexpr inline double qDegreesToRadians(double degrees)
0258 {
0259     return degrees * (M_PI / 180);
0260 }
0261 
0262 constexpr inline long double qDegreesToRadians(long double degrees)
0263 {
0264     return degrees * (M_PI / 180);
0265 }
0266 
0267 template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
0268 constexpr inline double qDegreesToRadians(T degrees)
0269 {
0270     return qDegreesToRadians(static_cast<double>(degrees));
0271 }
0272 
0273 constexpr inline float qRadiansToDegrees(float radians)
0274 {
0275     return radians * float(180 / M_PI);
0276 }
0277 
0278 constexpr inline double qRadiansToDegrees(double radians)
0279 {
0280     return radians * (180 / M_PI);
0281 }
0282 
0283 constexpr inline long double qRadiansToDegrees(long double radians)
0284 {
0285     return radians * (180 / M_PI);
0286 }
0287 
0288 // A qRadiansToDegrees(Integral) overload isn't here; it's extremely
0289 // questionable that someone is manipulating quantities in radians
0290 // using integral datatypes...
0291 
0292 constexpr inline quint32 qNextPowerOfTwo(quint32 v)
0293 {
0294     Q_ASSERT(static_cast<qint32>(v) >= 0); // There is a next power of two
0295     return q20::bit_ceil(v + 1);
0296 }
0297 
0298 constexpr inline quint64 qNextPowerOfTwo(quint64 v)
0299 {
0300     Q_ASSERT(static_cast<qint64>(v) >= 0); // There is a next power of two
0301     return q20::bit_ceil(v + 1);
0302 }
0303 
0304 constexpr inline quint32 qNextPowerOfTwo(qint32 v)
0305 {
0306     return qNextPowerOfTwo(quint32(v));
0307 }
0308 
0309 constexpr inline quint64 qNextPowerOfTwo(qint64 v)
0310 {
0311     return qNextPowerOfTwo(quint64(v));
0312 }
0313 
0314 constexpr inline unsigned long qNextPowerOfTwo(unsigned long v)
0315 {
0316     return qNextPowerOfTwo(QIntegerForSizeof<long>::Unsigned(v));
0317 }
0318 
0319 constexpr inline unsigned long qNextPowerOfTwo(long v)
0320 {
0321     return qNextPowerOfTwo(QIntegerForSizeof<long>::Unsigned(v));
0322 }
0323 
0324 QT_END_NAMESPACE
0325 
0326 #endif // QMATH_H