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>
This commit is contained in:
Allan Sandfeld Jensen 2022-02-07 11:17:11 +01:00
parent 4cdfe6f117
commit 65a02da1b5
3 changed files with 6 additions and 19 deletions

View File

@ -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)

View File

@ -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.
*/

View File

@ -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()