diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2020-03-17 09:02:03 +0100 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2020-03-17 10:01:03 +0100 |
commit | 7ef382649754c261ca9eb99dd50b67050e748efb (patch) | |
tree | 6f5c36c1816cfd313612387390d31ec97d883fb2 | |
parent | f66a5c2d61a66585eaca7d01b6749974f37b94be (diff) |
Introduce always constexpr variants of qNextPowerOfTwo
Amends e464e1eb8eb63c631fb0916c3ea4540a88d8aad3. qNextPowerOfTwo has the
same issue as qCountTrailingZeroBits.
Change-Id: Ib1905986e932ac130bce7a1d98f4f7b5ef73991f
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r-- | src/corelib/kernel/qmath.h | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/src/corelib/kernel/qmath.h b/src/corelib/kernel/qmath.h index 305e9065e9..5fe635877e 100644 --- a/src/corelib/kernel/qmath.h +++ b/src/corelib/kernel/qmath.h @@ -243,14 +243,8 @@ Q_DECL_CONSTEXPR inline double qRadiansToDegrees(double radians) return radians * (180 / M_PI); } - -Q_DECL_RELAXED_CONSTEXPR inline quint32 qNextPowerOfTwo(quint32 v) -{ -#if defined(QT_HAS_BUILTIN_CLZ) - if (v == 0) - return 1; - return 2U << (31 ^ QAlgorithmsPrivate::qt_builtin_clz(v)); -#else +namespace QtPrivate { +constexpr inline quint32 qConstexprNextPowerOfTwo(quint32 v) { v |= v >> 1; v |= v >> 2; v |= v >> 4; @@ -258,16 +252,9 @@ Q_DECL_RELAXED_CONSTEXPR inline quint32 qNextPowerOfTwo(quint32 v) v |= v >> 16; ++v; return v; -#endif } -Q_DECL_RELAXED_CONSTEXPR inline quint64 qNextPowerOfTwo(quint64 v) -{ -#if defined(QT_HAS_BUILTIN_CLZLL) - if (v == 0) - return 1; - return Q_UINT64_C(2) << (63 ^ QAlgorithmsPrivate::qt_builtin_clzll(v)); -#else +constexpr inline quint64 qConstexprNextPowerOfTwo(quint64 v) { v |= v >> 1; v |= v >> 2; v |= v >> 4; @@ -276,15 +263,45 @@ Q_DECL_RELAXED_CONSTEXPR inline quint64 qNextPowerOfTwo(quint64 v) v |= v >> 32; ++v; return v; +} + +constexpr inline quint32 qConstexprNextPowerOfTwo(qint32 v) { + return qConstexprNextPowerOfTwo(quint32(v)); +} + +constexpr inline quint32 qConstexprNextPowerOfTwo(qint64 v) { + return qConstexprNextPowerOfTwo(quint64(v)); +} +} + +constexpr inline quint32 qNextPowerOfTwo(quint32 v) +{ +#if defined(QT_HAS_BUILTIN_CLZ) + if (v == 0) + return 1; + return 2U << (31 ^ QAlgorithmsPrivate::qt_builtin_clz(v)); +#else + return QtPrivate::qConstexprNextPowerOfTwo(v); +#endif +} + +constexpr inline quint64 qNextPowerOfTwo(quint64 v) +{ +#if defined(QT_HAS_BUILTIN_CLZLL) + if (v == 0) + return 1; + return Q_UINT64_C(2) << (63 ^ QAlgorithmsPrivate::qt_builtin_clzll(v)); +#else + return QtPrivate::qConstexprNextPowerOfTwo(v); #endif } -Q_DECL_RELAXED_CONSTEXPR inline quint32 qNextPowerOfTwo(qint32 v) +constexpr inline quint32 qNextPowerOfTwo(qint32 v) { return qNextPowerOfTwo(quint32(v)); } -Q_DECL_RELAXED_CONSTEXPR inline quint64 qNextPowerOfTwo(qint64 v) +constexpr inline quint64 qNextPowerOfTwo(qint64 v) { return qNextPowerOfTwo(quint64(v)); } |