xref: /openbsd-src/gnu/llvm/libcxx/include/__algorithm/half_positive.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
176d0caaeSpatrick //===----------------------------------------------------------------------===//
276d0caaeSpatrick //
376d0caaeSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
476d0caaeSpatrick // See https://llvm.org/LICENSE.txt for license information.
576d0caaeSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
676d0caaeSpatrick //
776d0caaeSpatrick //===----------------------------------------------------------------------===//
876d0caaeSpatrick 
976d0caaeSpatrick #ifndef _LIBCPP___ALGORITHM_HALF_POSITIVE_H
1076d0caaeSpatrick #define _LIBCPP___ALGORITHM_HALF_POSITIVE_H
1176d0caaeSpatrick 
1276d0caaeSpatrick #include <__config>
1376d0caaeSpatrick #include <type_traits>
1476d0caaeSpatrick 
1576d0caaeSpatrick #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1676d0caaeSpatrick #  pragma GCC system_header
1776d0caaeSpatrick #endif
1876d0caaeSpatrick 
1976d0caaeSpatrick _LIBCPP_BEGIN_NAMESPACE_STD
2076d0caaeSpatrick 
2176d0caaeSpatrick // Perform division by two quickly for positive integers (llvm.org/PR39129)
2276d0caaeSpatrick 
2376d0caaeSpatrick template <typename _Integral>
2476d0caaeSpatrick _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2576d0caaeSpatrick typename enable_if
2676d0caaeSpatrick <
2776d0caaeSpatrick     is_integral<_Integral>::value,
2876d0caaeSpatrick     _Integral
2976d0caaeSpatrick >::type
__half_positive(_Integral __value)3076d0caaeSpatrick __half_positive(_Integral __value)
3176d0caaeSpatrick {
32*4bdff4beSrobert     return static_cast<_Integral>(static_cast<__make_unsigned_t<_Integral> >(__value) / 2);
3376d0caaeSpatrick }
3476d0caaeSpatrick 
3576d0caaeSpatrick template <typename _Tp>
3676d0caaeSpatrick _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
3776d0caaeSpatrick typename enable_if
3876d0caaeSpatrick <
3976d0caaeSpatrick     !is_integral<_Tp>::value,
4076d0caaeSpatrick     _Tp
4176d0caaeSpatrick >::type
__half_positive(_Tp __value)4276d0caaeSpatrick __half_positive(_Tp __value)
4376d0caaeSpatrick {
4476d0caaeSpatrick     return __value / 2;
4576d0caaeSpatrick }
4676d0caaeSpatrick 
4776d0caaeSpatrick _LIBCPP_END_NAMESPACE_STD
4876d0caaeSpatrick 
4976d0caaeSpatrick #endif // _LIBCPP___ALGORITHM_HALF_POSITIVE_H
50