half_positive.h revision 1.1.1.1
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_PUSH_MACROS
20#include <__undef_macros>
21
22_LIBCPP_BEGIN_NAMESPACE_STD
23
24// Perform division by two quickly for positive integers (llvm.org/PR39129)
25
26template <typename _Integral>
27_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
28typename enable_if
29<
30    is_integral<_Integral>::value,
31    _Integral
32>::type
33__half_positive(_Integral __value)
34{
35    return static_cast<_Integral>(static_cast<typename make_unsigned<_Integral>::type>(__value) / 2);
36}
37
38template <typename _Tp>
39_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
40typename enable_if
41<
42    !is_integral<_Tp>::value,
43    _Tp
44>::type
45__half_positive(_Tp __value)
46{
47    return __value / 2;
48}
49
50_LIBCPP_END_NAMESPACE_STD
51
52_LIBCPP_POP_MACROS
53
54#endif // _LIBCPP___ALGORITHM_HALF_POSITIVE_H
55