summaryrefslogtreecommitdiff
path: root/libcxx/test/libcxx-03/algorithms/half_positive.pass.cpp
diff options
context:
space:
mode:
authorMingming Liu <mingmingl@google.com>2025-09-10 15:25:31 -0700
committerGitHub <noreply@github.com>2025-09-10 15:25:31 -0700
commit1417dafa1db9cb1b2b09438aa9f53ea5ab6e36e2 (patch)
tree57f4b1f313c8cf74eed8819870f39c36ea263c68 /libcxx/test/libcxx-03/algorithms/half_positive.pass.cpp
parent898b813bc8a6d0276bf0f4769f5f2f64b34e632d (diff)
parentb8cefcb601ddaa18482555c4ff363c01a270c2fe (diff)
Merge branch 'main' into users/mingmingl-llvm/samplefdo-profile-formatusers/mingmingl-llvm/samplefdo-profile-format
Diffstat (limited to 'libcxx/test/libcxx-03/algorithms/half_positive.pass.cpp')
-rw-r--r--libcxx/test/libcxx-03/algorithms/half_positive.pass.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/libcxx/test/libcxx-03/algorithms/half_positive.pass.cpp b/libcxx/test/libcxx-03/algorithms/half_positive.pass.cpp
new file mode 100644
index 000000000000..292fcf356554
--- /dev/null
+++ b/libcxx/test/libcxx-03/algorithms/half_positive.pass.cpp
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <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 <__cxx03/__algorithm/half_positive.h>
+#include <cassert>
+#include <cstddef>
+#include <limits>
+
+#include "test_macros.h"
+#include "user_defined_integral.h"
+
+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(int, char**)
+{
+ {
+ assert(test<char>());
+ assert(test<int>());
+ assert(test<long>());
+ assert((test<UserDefinedIntegral<int>, int>()));
+ assert(test<std::size_t>());
+#if !defined(TEST_HAS_NO_INT128)
+ assert(test<__int128_t>());
+#endif // !defined(TEST_HAS_NO_INT128)
+ }
+
+#if TEST_STD_VER >= 11
+ {
+ static_assert(test<char>(), "");
+ static_assert(test<int>(), "");
+ static_assert(test<long>(), "");
+ static_assert(test<std::size_t>(), "");
+#if !defined(TEST_HAS_NO_INT128)
+ static_assert(test<__int128_t>(), "");
+#endif // !defined(TEST_HAS_NO_INT128)
+ }
+#endif // TEST_STD_VER >= 11
+
+ return 0;
+}