summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qmath.h
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2020-08-08 12:52:36 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2020-09-13 18:26:49 +0200
commit6a37ca39ec7cfc2f4392784fd31936526d55e941 (patch)
tree240d1a2034b87a0094b0fc96a07a46e3a3b0902b /src/corelib/kernel/qmath.h
parent65afcef2173cabe297778d19dda3198595820cfa (diff)
Use universal C++20 bit operations when available
Avoids using compiler builtins, and can in future replace them. Change-Id: I3f0afe7d28b6ba05bcd1c1132b44a8db7b182d8a Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qmath.h')
-rw-r--r--src/corelib/kernel/qmath.h12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/corelib/kernel/qmath.h b/src/corelib/kernel/qmath.h
index 5f36de303c..4c5a980f3a 100644
--- a/src/corelib/kernel/qmath.h
+++ b/src/corelib/kernel/qmath.h
@@ -47,6 +47,10 @@
#include <QtCore/qglobal.h>
#include <QtCore/qalgorithms.h>
+#if __has_include(<bit>)
+#include <bit>
+#endif
+
#ifndef _USE_MATH_DEFINES
# define _USE_MATH_DEFINES
# define undef_USE_MATH_DEFINES
@@ -298,7 +302,9 @@ constexpr inline quint64 qConstexprNextPowerOfTwo(qint64 v)
constexpr inline quint32 qNextPowerOfTwo(quint32 v)
{
-#if defined(QT_HAS_BUILTIN_CLZ)
+#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
+ return std::bit_ceil(v + 1);
+#elif defined(QT_HAS_BUILTIN_CLZ)
if (v == 0)
return 1;
return 2U << (31 ^ QAlgorithmsPrivate::qt_builtin_clz(v));
@@ -309,7 +315,9 @@ constexpr inline quint32 qNextPowerOfTwo(quint32 v)
constexpr inline quint64 qNextPowerOfTwo(quint64 v)
{
-#if defined(QT_HAS_BUILTIN_CLZLL)
+#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
+ return std::bit_ceil(v + 1);
+#elif defined(QT_HAS_BUILTIN_CLZLL)
if (v == 0)
return 1;
return Q_UINT64_C(2) << (63 ^ QAlgorithmsPrivate::qt_builtin_clzll(v));