half_positive.h revision 1.1.1.2
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#ifndef _LIBCPP___ALGORITHM_HALF_POSITIVE_H
10#define _LIBCPP___ALGORITHM_HALF_POSITIVE_H
11
12#include <__config>
13#include <type_traits>
14
15#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16#  pragma GCC system_header
17#endif
18
19_LIBCPP_BEGIN_NAMESPACE_STD
20
21// Perform division by two quickly for positive integers (llvm.org/PR39129)
22
23template <typename _Integral>
24_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
25typename enable_if
26<
27    is_integral<_Integral>::value,
28    _Integral
29>::type
30__half_positive(_Integral __value)
31{
32    return static_cast<_Integral>(static_cast<__make_unsigned_t<_Integral> >(__value) / 2);
33}
34
35template <typename _Tp>
36_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
37typename enable_if
38<
39    !is_integral<_Tp>::value,
40    _Tp
41>::type
42__half_positive(_Tp __value)
43{
44    return __value / 2;
45}
46
47_LIBCPP_END_NAMESPACE_STD
48
49#endif // _LIBCPP___ALGORITHM_HALF_POSITIVE_H
50