half_positive.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  21. _Integral
  22. __half_positive(_Integral __value)
  23. {
  24. return static_cast<_Integral>(static_cast<__make_unsigned_t<_Integral> >(__value) / 2);
  25. }
  26. template <typename _Tp, __enable_if_t<!is_integral<_Tp>::value, int> = 0>
  27. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  28. _Tp
  29. __half_positive(_Tp __value)
  30. {
  31. return __value / 2;
  32. }
  33. _LIBCPP_END_NAMESPACE_STD
  34. #endif // _LIBCPP___ALGORITHM_HALF_POSITIVE_H