xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/half_positive.h (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
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 
23 template <typename _Integral>
24 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
25 typename 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<typename make_unsigned<_Integral>::type>(__value) / 2);
33 }
34 
35 template <typename _Tp>
36 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
37 typename 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