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