From e736818cefaea03e717e7417e94dc4abe074e012 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 7 Feb 2023 18:51:59 -0800 Subject: [PATCH] qalgorithms.h: fix mistake that MSVC has constexpr bitops in C++17 mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit exists in C++20 and is properly both constexpr and optimized. But in C++17 mode, we don't have constexpr bitops and instead elect to have performance at runtime instead. But somewhere along the line, either when they were added, when C++20 support was, or in any of the bugfixes for other compilers, the nesting of #ifdef got messed up and we declared that we had constexpr builtins for MSVC in C++17 too. The macro QT_HAS_CONSTEXPR_BUILTINS isn't supposed to be used by anyone else... but we ended up not being able to use it ourselves either. So I'm renaming it to a more precise label. Change-Id: I9671dee8ceb64aa9b9cafffd1741b9b4060c9753 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Edward Welbourne (cherry picked from commit e69d80e14d24001f93442ab59b8babb7e9df0092) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/tools/qalgorithms.h | 21 +++++-------------- .../kernel/qsizepolicy/tst_qsizepolicy.cpp | 2 +- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/corelib/tools/qalgorithms.h b/src/corelib/tools/qalgorithms.h index 94aa2da012e..8889effecee 100644 --- a/src/corelib/tools/qalgorithms.h +++ b/src/corelib/tools/qalgorithms.h @@ -41,19 +41,11 @@ inline void qDeleteAll(const Container &c) */ namespace QAlgorithmsPrivate { -#ifdef Q_CC_CLANG -// Clang had a bug where __builtin_ctz/clz/popcount were not marked as constexpr. -# if (defined __apple_build_version__ && __clang_major__ >= 7) || (Q_CC_CLANG >= 307) -# define QT_HAS_CONSTEXPR_BUILTINS -# endif -#elif defined(Q_CC_MSVC) && !defined(Q_PROCESSOR_ARM) -# define QT_HAS_CONSTEXPR_BUILTINS +#if defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L +// We use C++20 operations instead which ensures constexpr bit ops +# define QT_HAS_CONSTEXPR_BITOPS #elif defined(Q_CC_GNU) -# define QT_HAS_CONSTEXPR_BUILTINS -#endif - -#if defined QT_HAS_CONSTEXPR_BUILTINS -#if defined(Q_CC_GNU) || defined(Q_CC_CLANG) +# define QT_HAS_CONSTEXPR_BITOPS # define QT_HAS_BUILTIN_CTZS constexpr Q_ALWAYS_INLINE uint qt_builtin_ctzs(quint16 v) noexcept { @@ -169,9 +161,7 @@ Q_ALWAYS_INLINE uint qt_builtin_clzs(quint16 v) noexcept // architecture), but unlike the other compilers, MSVC has no option // to generate code for those processors. // So it's an acceptable compromise. -#if defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L -// We use C++20 operations instead which ensures constexpr popcount -#elif defined(__AVX__) || defined(__SSE4_2__) || defined(__POPCNT__) +#if defined(__AVX__) || defined(__SSE4_2__) || defined(__POPCNT__) #define QT_POPCOUNT_CONSTEXPR #define QT_POPCOUNT_RELAXED_CONSTEXPR #define QALGORITHMS_USE_BUILTIN_POPCOUNT @@ -200,7 +190,6 @@ Q_ALWAYS_INLINE uint qt_builtin_popcountll(quint64 v) noexcept #endif // __AVX__ || __SSE4_2__ || __POPCNT__ #endif // MSVC -#endif // QT_HAS_CONSTEXPR_BUILTINS #ifndef QT_POPCOUNT_CONSTEXPR #define QT_POPCOUNT_CONSTEXPR constexpr diff --git a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp index 5b64f122e20..f59471a6e67 100644 --- a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp +++ b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp @@ -95,7 +95,7 @@ void tst_QSizePolicy::constExpr() { // QTBUG-69983: For ControlType != QSizePolicy::DefaultType, qCountTrailingZeroBits() // is used, which MSVC 15.8.1 does not consider constexpr due to built-ins -# if defined(QT_HAS_CONSTEXPR_BUILTINS) && (!defined(Q_CC_MSVC) || _MSC_VER < 1915) +# if defined(QT_HAS_CONSTEXPR_BITOPS) constexpr auto sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, QSizePolicy::CheckBox); # else constexpr auto sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding, QSizePolicy::DefaultType);