diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-02-07 11:17:11 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-02-17 17:20:34 +0000 |
commit | 65a02da1b5390e6c91a15379f6e87add37eeeeae (patch) | |
tree | 4ef80984ed9c33e3c94b59507eefd94a3ccae3f4 | |
parent | 4cdfe6f117e15a1951d8b336afa2b7ed53d44e3a (diff) |
Make large inputs to qNextPowerOfTwo give undefined output
Fixing documentation and removing tests.
[ChangeLog][Important Behavior Changes] The qNextPowerOfTwo()
functions now have preconditions.
Change-Id: If6d5e8bee66826910e89be7cac388a1f0422ebfd
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/kernel/qmath.h | 6 | ||||
-rw-r--r-- | src/corelib/kernel/qmath.qdoc | 8 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmath/tst_qmath.cpp | 11 |
3 files changed, 6 insertions, 19 deletions
diff --git a/src/corelib/kernel/qmath.h b/src/corelib/kernel/qmath.h index 2171a4ed93..ef0784a947 100644 --- a/src/corelib/kernel/qmath.h +++ b/src/corelib/kernel/qmath.h @@ -370,9 +370,8 @@ constexpr inline quint64 qConstexprNextPowerOfTwo(qint64 v) constexpr inline quint32 qNextPowerOfTwo(quint32 v) { + Q_ASSERT(static_cast<qint32>(v) >= 0); // There is a next power of two #if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L - if (static_cast<qint32>(v) < 0) - return 0; // std::bit_ceil() is undefined for values that would overflow, but we document them to be 0 return std::bit_ceil(v + 1); #elif defined(QT_HAS_BUILTIN_CLZ) if (v == 0) @@ -385,9 +384,8 @@ constexpr inline quint32 qNextPowerOfTwo(quint32 v) constexpr inline quint64 qNextPowerOfTwo(quint64 v) { + Q_ASSERT(static_cast<qint64>(v) >= 0); // There is a next power of two #if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L - if (static_cast<qint64>(v) < 0) - return 0; // std::bit_ceil() is undefined for values that would overflow, but we document them to be 0 return std::bit_ceil(v + 1); #elif defined(QT_HAS_BUILTIN_CLZLL) if (v == 0) diff --git a/src/corelib/kernel/qmath.qdoc b/src/corelib/kernel/qmath.qdoc index 4c07e40765..7a55bf96e5 100644 --- a/src/corelib/kernel/qmath.qdoc +++ b/src/corelib/kernel/qmath.qdoc @@ -322,7 +322,7 @@ \relates <QtMath> \since 5.4 - This function returns the nearest power of two greater than \a value. For 0 it returns 1, and for values larger than or equal to 2^31 it returns 0. + This function returns the nearest power of two greater than \a value. For 0 it returns 1, and for values larger than or equal to 2^31 the result is undefined. */ /*! @@ -331,7 +331,7 @@ \since 5.4 \overload - This function returns the nearest power of two greater than \a value. For negative values it returns 0. + This function returns the nearest power of two greater than \a value. For negative values the result is undefined. */ /*! @@ -339,7 +339,7 @@ \relates <QtMath> \since 5.4 - This function returns the nearest power of two greater than \a value. For 0 it returns 1, and for values larger than or equal to 2^63 it returns 0. + This function returns the nearest power of two greater than \a value. For 0 it returns 1, and for values larger than or equal to 2^63 the result is undefined. */ /*! @@ -348,5 +348,5 @@ \since 5.4 \overload - This function returns the nearest power of two greater than \a value. For negative values it returns 0. + This function returns the nearest power of two greater than \a value. For negative values the result is undefined. */ diff --git a/tests/auto/corelib/kernel/qmath/tst_qmath.cpp b/tests/auto/corelib/kernel/qmath/tst_qmath.cpp index a94b984d47..348b65e012 100644 --- a/tests/auto/corelib/kernel/qmath/tst_qmath.cpp +++ b/tests/auto/corelib/kernel/qmath/tst_qmath.cpp @@ -290,9 +290,6 @@ void tst_QMath::qNextPowerOfTwo32S_data() QTest::newRow("2^30") << (1 << 30) << (1U << 31); QTest::newRow("2^30 + 1") << (1 << 30) + 1 << (1U << 31); QTest::newRow("2^31 - 1") << 0x7FFFFFFF << (1U<<31); - QTest::newRow("-1") << -1 << 0U; - QTest::newRow("-128") << -128 << 0U; - QTest::newRow("-(2^31)") << int(0x80000000) << 0U; } void tst_QMath::qNextPowerOfTwo32S() @@ -318,8 +315,6 @@ void tst_QMath::qNextPowerOfTwo32U_data() QTest::newRow("2^30") << (1U << 30) << (1U << 31); QTest::newRow("2^30 + 1") << (1U << 30) + 1 << (1U << 31); QTest::newRow("2^31 - 1") << 2147483647U << 2147483648U; - QTest::newRow("2^31") << 2147483648U << 0U; - QTest::newRow("2^31 + 1") << 2147483649U << 0U; } void tst_QMath::qNextPowerOfTwo32U() @@ -346,10 +341,6 @@ void tst_QMath::qNextPowerOfTwo64S_data() QTest::newRow("2^31") << Q_INT64_C(2147483648) << Q_UINT64_C(0x100000000); QTest::newRow("2^31 + 1") << Q_INT64_C(2147483649) << Q_UINT64_C(0x100000000); QTest::newRow("2^63 - 1") << Q_INT64_C(0x7FFFFFFFFFFFFFFF) << Q_UINT64_C(0x8000000000000000); - QTest::newRow("-1") << Q_INT64_C(-1) << Q_UINT64_C(0); - QTest::newRow("-128") << Q_INT64_C(-128) << Q_UINT64_C(0); - QTest::newRow("-(2^31)") << -Q_INT64_C(0x80000000) << Q_UINT64_C(0); - QTest::newRow("-(2^63)") << (qint64)Q_INT64_C(0x8000000000000000) << Q_UINT64_C(0); } void tst_QMath::qNextPowerOfTwo64S() @@ -373,8 +364,6 @@ void tst_QMath::qNextPowerOfTwo64U_data() QTest::newRow("65535") << Q_UINT64_C(65535) << Q_UINT64_C(65536); QTest::newRow("65536") << Q_UINT64_C(65536) << Q_UINT64_C(131072); QTest::newRow("2^63 - 1") << Q_UINT64_C(0x7FFFFFFFFFFFFFFF) << Q_UINT64_C(0x8000000000000000); - QTest::newRow("2^63") << Q_UINT64_C(0x8000000000000000) << Q_UINT64_C(0); - QTest::newRow("2^63 + 1") << Q_UINT64_C(0x8000000000000001) << Q_UINT64_C(0); } void tst_QMath::qNextPowerOfTwo64U() |