xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/half_positive.h (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
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 
26 template <typename _Integral>
27 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
28 typename 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 
38 template <typename _Tp>
39 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
40 typename 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