summaryrefslogtreecommitdiff
path: root/libcxx/test
diff options
context:
space:
mode:
authorLouis Dionne <ldionne@apple.com>2018-12-17 16:04:39 +0000
committerLouis Dionne <ldionne@apple.com>2018-12-17 16:04:39 +0000
commit6920f81f3f8e2f6895a2543895f493e5d14f71d3 (patch)
treec0a75a0150d58fd41280bc6d2d569e86834cd3a9 /libcxx/test
parent3313b6f4b8687d4a2f98ec4b08f4d1a80e4b23a5 (diff)
[libcxx] Speeding up partition_point/lower_bound/upper_bound
This is a re-application of r345525, which had been reverted by fear of a regression. Reviewed as https://reviews.llvm.org/D53994. Thanks to Denis Yaroshevskiy for the patch.
Diffstat (limited to 'libcxx/test')
-rw-r--r--libcxx/test/libcxx/algorithms/half_positive.pass.cpp56
1 files changed, 56 insertions, 0 deletions
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..7dcfc2c92c9
--- /dev/null
+++ b/libcxx/test/libcxx/algorithms/half_positive.pass.cpp
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// __half_positive divides an integer number by 2 as unsigned number for known types.
+// It can be an important optimization for lower bound, for example.
+
+#include <algorithm>
+#include <cassert>
+#include <limits>
+#include <type_traits>
+
+#include "test_macros.h"
+#include "user_defined_integral.hpp"
+
+namespace {
+
+template <class IntType, class UnderlyingType = IntType>
+TEST_CONSTEXPR bool test(IntType max_v = IntType(std::numeric_limits<UnderlyingType>::max())) {
+ return std::__half_positive(max_v) == max_v / 2;
+}
+
+} // namespace
+
+int main()
+{
+ {
+ assert(test<char>());
+ assert(test<int>());
+ assert(test<long>());
+ assert((test<UserDefinedIntegral<int>, int>()));
+ assert(test<size_t>());
+#if !defined(_LIBCPP_HAS_NO_INT128)
+ assert(test<__int128_t>());
+#endif // !defined(_LIBCPP_HAS_NO_INT128)
+ }
+
+#if TEST_STD_VER >= 11
+ {
+ static_assert(test<char>(), "");
+ static_assert(test<int>(), "");
+ static_assert(test<long>(), "");
+ static_assert(test<size_t>(), "");
+#if !defined(_LIBCPP_HAS_NO_INT128)
+ static_assert(test<__int128_t>(), "");
+#endif // !defined(_LIBCPP_HAS_NO_INT128)
+ }
+#endif // TEST_STD_VER >= 11
+}