From a5c3485a583951c78332c03cc140387d3567a8a7 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 1 Jul 2019 23:00:32 +0000 Subject: Bit Operations: P0556, P0553 and P1355. Reviewed as: https://reviews.llvm.org/D51262 llvm-svn: 364862 --- .../std/numerics/bit/bit.pow.two/log2p1.pass.cpp | 177 +++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 libcxx/test/std/numerics/bit/bit.pow.two/log2p1.pass.cpp (limited to 'libcxx/test/std/numerics/bit/bit.pow.two/log2p1.pass.cpp') diff --git a/libcxx/test/std/numerics/bit/bit.pow.two/log2p1.pass.cpp b/libcxx/test/std/numerics/bit/bit.pow.two/log2p1.pass.cpp new file mode 100644 index 00000000000..39054bcf905 --- /dev/null +++ b/libcxx/test/std/numerics/bit/bit.pow.two/log2p1.pass.cpp @@ -0,0 +1,177 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// template +// constexpr T log2p1(T x) noexcept; + +// If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any fractional part discarded. + +// Remarks: This function shall not participate in overload resolution unless +// T is an unsigned integer type + +#include +#include +#include + +#include "test_macros.h" + +class A{}; +enum E1 : unsigned char { rEd }; +enum class E2 : unsigned char { red }; + +template +constexpr bool constexpr_test() +{ + return std::log2p1(T(0)) == T(0) + && std::log2p1(T(1)) == T(1) + && std::log2p1(T(2)) == T(2) + && std::log2p1(T(3)) == T(2) + && std::log2p1(T(4)) == T(3) + && std::log2p1(T(5)) == T(3) + && std::log2p1(T(6)) == T(3) + && std::log2p1(T(7)) == T(3) + && std::log2p1(T(8)) == T(4) + && std::log2p1(T(9)) == T(4) + ; +} + + +template +void runtime_test() +{ + ASSERT_SAME_TYPE(T, decltype(std::log2p1(T(0)))); + ASSERT_NOEXCEPT( std::log2p1(T(0))); + + assert( std::log2p1(T(0)) == T(0)); + assert( std::log2p1(T(1)) == T(1)); + assert( std::log2p1(T(2)) == T(2)); + assert( std::log2p1(T(3)) == T(2)); + assert( std::log2p1(T(4)) == T(3)); + assert( std::log2p1(T(5)) == T(3)); + assert( std::log2p1(T(6)) == T(3)); + assert( std::log2p1(T(7)) == T(3)); + assert( std::log2p1(T(8)) == T(4)); + assert( std::log2p1(T(9)) == T(4)); + + + assert( std::log2p1(T(121)) == T(7)); + assert( std::log2p1(T(122)) == T(7)); + assert( std::log2p1(T(123)) == T(7)); + assert( std::log2p1(T(124)) == T(7)); + assert( std::log2p1(T(125)) == T(7)); + assert( std::log2p1(T(126)) == T(7)); + assert( std::log2p1(T(127)) == T(7)); + assert( std::log2p1(T(128)) == T(8)); + assert( std::log2p1(T(129)) == T(8)); + assert( std::log2p1(T(130)) == T(8)); +} + +int main() +{ + + { + auto lambda = [](auto x) -> decltype(std::log2p1(x)) {}; + using L = decltype(lambda); + + static_assert( std::is_invocable_v, ""); + static_assert( std::is_invocable_v, ""); + static_assert( std::is_invocable_v, ""); + static_assert( std::is_invocable_v, ""); + + static_assert( std::is_invocable_v, ""); + static_assert( std::is_invocable_v, ""); + static_assert( std::is_invocable_v, ""); + static_assert( std::is_invocable_v, ""); + static_assert( std::is_invocable_v, ""); + + static_assert( std::is_invocable_v, ""); + static_assert( std::is_invocable_v, ""); + + + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + +#ifndef _LIBCPP_HAS_NO_INT128 + static_assert( std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); +#endif + + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + static_assert(!std::is_invocable_v, ""); + } + + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(), ""); + +#ifndef _LIBCPP_HAS_NO_INT128 + static_assert(constexpr_test<__uint128_t>(), ""); +#endif + + + runtime_test(); + runtime_test(); + runtime_test(); + runtime_test(); + runtime_test(); + + runtime_test(); + runtime_test(); + runtime_test(); + runtime_test(); + runtime_test(); + runtime_test(); + runtime_test(); + +#ifndef _LIBCPP_HAS_NO_INT128 + runtime_test<__uint128_t>(); + + { + __uint128_t val = 128; + val <<= 32; + assert( std::log2p1(val-1) == 39); + assert( std::log2p1(val) == 40); + assert( std::log2p1(val+1) == 40); + val <<= 2; + assert( std::log2p1(val-1) == 41); + assert( std::log2p1(val) == 42); + assert( std::log2p1(val+1) == 42); + val <<= 3; + assert( std::log2p1(val-1) == 44); + assert( std::log2p1(val) == 45); + assert( std::log2p1(val+1) == 45); + } +#endif + +} -- cgit v1.2.3