/*************************************************************************** * tests/common/test_log2.cpp * * Part of the STXXL. See http://stxxl.sourceforge.net * * Copyright (C) 2008 Andreas Beckmann * Copyright (C) 2013 Timo Bingmann * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) **************************************************************************/ #include #include #include #include #include #include #include using stxxl::LOG2; using stxxl::unsigned_type; template void log_i(unsigned_type floorv, unsigned_type ceilv) { std::cout << i << "\t" << (i < 1000000 ? "\t" : "") << stxxl::LOG2_floor::value << "\t" << stxxl::LOG2::floor << "\t" << stxxl::LOG2::ceil << std::endl; std::cout << "\t\t" << stxxl::ilog2_floor(i) << "\t" << stxxl::ilog2_floor(i) << "\t" << stxxl::ilog2_ceil(i) << std::endl; STXXL_CHECK(stxxl::LOG2_floor::value == stxxl::ilog2_floor(i)); STXXL_CHECK(stxxl::LOG2::floor == stxxl::ilog2_floor(i)); STXXL_CHECK(stxxl::LOG2::ceil == stxxl::ilog2_ceil(i)); if (i <= 1) { STXXL_CHECK(stxxl::LOG2_floor::value == 0); STXXL_CHECK(stxxl::LOG2::floor == 0); STXXL_CHECK(stxxl::LOG2::ceil == 0); } else if (i == 2) { STXXL_CHECK(stxxl::LOG2_floor::value == 1); STXXL_CHECK(stxxl::LOG2::floor == 1); STXXL_CHECK(stxxl::LOG2::ceil == 1); } else { STXXL_CHECK(stxxl::LOG2_floor::value == floorv); STXXL_CHECK(stxxl::LOG2::floor == floorv); STXXL_CHECK(stxxl::LOG2::ceil == ceilv); #if 0 // not many compiler have log2l() if (i <= ((stxxl::uint64)(1) << 59)) // does not work for higher powers { STXXL_CHECK(stxxl::LOG2_floor::value == (unsigned_type)floorl(log2l(i))); STXXL_CHECK(stxxl::LOG2::floor == (unsigned_type)floorl(log2l(i))); STXXL_CHECK(stxxl::LOG2::ceil == (unsigned_type)ceill(log2l(i))); } #endif } std::cout << "\n"; } template void log_ipm1(unsigned_type p) { log_i(p - 1, p); log_i(p, p); log_i(p, p + 1); std::cout << std::endl; } int main() { std::cout << "i\t\tLOG2\tLOG2\tLOG2" << std::endl; std::cout << "\t\t\tfloor\tceil" << std::endl; log_ipm1<1 << 0>(0); log_ipm1<1 << 1>(1); log_ipm1<1 << 2>(2); log_ipm1<1 << 3>(3); log_ipm1<1 << 4>(4); log_ipm1<1 << 5>(5); log_ipm1<1 << 6>(6); log_ipm1<1 << 7>(7); log_ipm1<1 << 8>(8); log_ipm1<1 << 9>(9); log_ipm1<1 << 10>(10); log_ipm1<1 << 11>(11); log_ipm1<1 << 12>(12); log_ipm1<1 << 16>(16); log_ipm1<1 << 24>(24); log_ipm1<1 << 30>(30); log_ipm1<1UL << 31>(31); #if __WORDSIZE == 64 log_ipm1<1UL << 32>(32); log_ipm1<1UL << 33>(33); log_ipm1<1UL << 48>(48); log_ipm1<1UL << 50>(50); log_ipm1<1UL << 55>(55); log_ipm1<1UL << 63>(63); #endif }