1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <algorithm>
10
11// __half_positive divides an integer number by 2 as unsigned number for known types.
12// It can be an important optimization for lower bound, for example.
13
14#include <__algorithm/half_positive.h>
15#include <cassert>
16#include <cstddef>
17#include <limits>
18
19#include "test_macros.h"
20#include "user_defined_integral.h"
21
22namespace {
23
24template <class IntType, class UnderlyingType = IntType>
25TEST_CONSTEXPR bool test(IntType max_v = IntType(std::numeric_limits<UnderlyingType>::max())) {
26 return std::__half_positive(max_v) == max_v / 2;
27}
28
29} // namespace
30
31int main(int, char**)
32{
33 {
34 assert(test<char>());
35 assert(test<int>());
36 assert(test<long>());
37 assert((test<UserDefinedIntegral<int>, int>()));
38 assert(test<std::size_t>());
39#if !defined(TEST_HAS_NO_INT128)
40 assert(test<__int128_t>());
41#endif // !defined(TEST_HAS_NO_INT128)
42 }
43
44#if TEST_STD_VER >= 11
45 {
46 static_assert(test<char>(), "");
47 static_assert(test<int>(), "");
48 static_assert(test<long>(), "");
49 static_assert(test<std::size_t>(), "");
50#if !defined(TEST_HAS_NO_INT128)
51 static_assert(test<__int128_t>(), "");
52#endif // !defined(TEST_HAS_NO_INT128)
53 }
54#endif // TEST_STD_VER >= 11
55
56 return 0;
57}
58

source code of libcxx/test/libcxx/algorithms/half_positive.pass.cpp