From 8c40d81d4f019b8b1ae02c154be657b949c2cf4d Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Mon, 29 Oct 2018 19:25:02 +0000 Subject: Bug 39129: Speeding up partition_point/lower_bound/upper_bound/ by using unsigned division by 2 when possible. Patch by Denis Yaroshevskiy (denis.yaroshevskij@gmail.com) The rational and measurements can be found in the bug description: https://bugs.llvm.org/show_bug.cgi?id=39129 Reviewed as https://reviews.llvm.org/D52697 llvm-svn: 345525 --- .../test/libcxx/algorithms/half_positive.pass.cpp | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 libcxx/test/libcxx/algorithms/half_positive.pass.cpp (limited to 'libcxx/test') diff --git a/libcxx/test/libcxx/algorithms/half_positive.pass.cpp b/libcxx/test/libcxx/algorithms/half_positive.pass.cpp new file mode 100644 index 00000000000..178055cbbd0 --- /dev/null +++ b/libcxx/test/libcxx/algorithms/half_positive.pass.cpp @@ -0,0 +1,59 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// + +// template _Tp __half_positive(const _Tp&); + +// __half_positive divide integer number by 2 as unsigned number +// if it's safe to do so. It can be an important optimization for lower bound, +// for example. + +#include +#include +#include +#include + +#include "test_macros.h" +#include "user_defined_integral.hpp" + +namespace { + +template +TEST_CONSTEXPR bool test(IntType max_v = IntType(std::numeric_limits::max())) { + return std::__half_positive(max_v) == max_v / 2; +} + +} // namespace + +int main() +{ + { + assert(test()); + assert(test()); + assert(test()); + assert((test, int>())); + assert(test()); +#if !defined(_LIBCPP_HAS_NO_INT128) + assert(test<__int128_t>()); +#endif // !defined(_LIBCPP_HAS_NO_INT128) + } + +#if TEST_STD_VER >= 11 + { + static_assert(test(), ""); + static_assert(test(), ""); + static_assert(test(), ""); + static_assert(test(), ""); +#if !defined(_LIBCPP_HAS_NO_INT128) + static_assert(test<__int128_t>(), ""); +#endif // !defined(_LIBCPP_HAS_NO_INT128) + } +#endif // TEST_STD_VER >= 11 +} -- cgit v1.2.3