half_positive.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. #ifndef _LIBCPP___ALGORITHM_HALF_POSITIVE_H
  9. #define _LIBCPP___ALGORITHM_HALF_POSITIVE_H
  10. #include <__config>
  11. #include <__type_traits/enable_if.h>
  12. #include <__type_traits/is_integral.h>
  13. #include <__type_traits/make_unsigned.h>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. // Perform division by two quickly for positive integers (llvm.org/PR39129)
  19. template <typename _Integral, __enable_if_t<is_integral<_Integral>::value, int> = 0>
  20. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Integral __half_positive(_Integral __value) {
  21. return static_cast<_Integral>(static_cast<__make_unsigned_t<_Integral> >(__value) / 2);
  22. }
  23. template <typename _Tp, __enable_if_t<!is_integral<_Tp>::value, int> = 0>
  24. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp __half_positive(_Tp __value) {
  25. return __value / 2;
  26. }
  27. _LIBCPP_END_NAMESPACE_STD
  28. #endif // _LIBCPP___ALGORITHM_HALF_POSITIVE_H