aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2018-08-30 16:07:35 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2018-08-30 16:07:35 +0100
commit2fb17d2d901d904479fa22dd01b887ff5bc48248 (patch)
tree111035d748bf025dcc5d4a417edde0802112332d /libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc
parent2ebbdb6ca3f69cdac97aeba48a7f00ea40337cd5 (diff)
downloadgcc-2fb17d2d901d904479fa22dd01b887ff5bc48248.zip
gcc-2fb17d2d901d904479fa22dd01b887ff5bc48248.tar.gz
gcc-2fb17d2d901d904479fa22dd01b887ff5bc48248.tar.bz2
Avoid undefined shifts in ceil2 operations
For values where the result cannot be represented the shift width would be equal to the width of the type, which is undefined. Perform two well-defined shifts instead of one possible undefined shift. * include/bits/hashtable_policy.h (__clp2): Fix calculation for LLP64 targets where sizeof(size_t) > sizeof(long). Avoid undefined shifts of the number of bits in the type. * include/std/bit (__ceil2): Avoid undefined shifts. * testsuite/26_numerics/bit/bit.pow.two/ceil2.cc: Test values with the most signifiant bit set. From-SVN: r263986
Diffstat (limited to 'libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc')
-rw-r--r--libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc8
1 files changed, 8 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc
index 65e1569..e41f82c 100644
--- a/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc
+++ b/libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc
@@ -55,6 +55,14 @@ test(UInt x)
static_assert( std::ceil2(UInt(3) << 64) == (UInt(4) << 64) );
}
+ constexpr UInt msb = UInt(1) << (std::numeric_limits<UInt>::digits - 1);
+ static_assert( std::ceil2( msb ) == msb );
+ // Larger values cannot be represented so the return value is unspecified,
+ // but must still be valid in constant expressions, i.e. not undefined.
+ static_assert( std::ceil2( UInt(msb + 1) ) != 77 );
+ static_assert( std::ceil2( UInt(msb + 2) ) != 77 );
+ static_assert( std::ceil2( UInt(msb + 77) ) != 77 );
+
return true;
}