midpoint.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___NUMERIC_MIDPOINT_H
  10. #define _LIBCPP___NUMERIC_MIDPOINT_H
  11. #include <__config>
  12. #include <__type_traits/enable_if.h>
  13. #include <__type_traits/is_floating_point.h>
  14. #include <__type_traits/is_integral.h>
  15. #include <__type_traits/is_null_pointer.h>
  16. #include <__type_traits/is_object.h>
  17. #include <__type_traits/is_pointer.h>
  18. #include <__type_traits/is_same.h>
  19. #include <__type_traits/is_void.h>
  20. #include <__type_traits/make_unsigned.h>
  21. #include <__type_traits/remove_pointer.h>
  22. #include <cstddef>
  23. #include <limits>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_PUSH_MACROS
  28. #include <__undef_macros>
  29. _LIBCPP_BEGIN_NAMESPACE_STD
  30. #if _LIBCPP_STD_VER > 17
  31. template <class _Tp>
  32. _LIBCPP_INLINE_VISIBILITY constexpr
  33. enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
  34. midpoint(_Tp __a, _Tp __b) noexcept
  35. _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
  36. {
  37. using _Up = make_unsigned_t<_Tp>;
  38. constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
  39. _Up __diff = _Up(__b) - _Up(__a);
  40. _Up __sign_bit = __b < __a;
  41. _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
  42. return __a + __half_diff;
  43. }
  44. template <class _TPtr>
  45. _LIBCPP_INLINE_VISIBILITY constexpr
  46. enable_if_t<is_pointer_v<_TPtr>
  47. && is_object_v<remove_pointer_t<_TPtr>>
  48. && ! is_void_v<remove_pointer_t<_TPtr>>
  49. && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
  50. midpoint(_TPtr __a, _TPtr __b) noexcept
  51. {
  52. return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a);
  53. }
  54. template <typename _Tp>
  55. _LIBCPP_HIDE_FROM_ABI constexpr int __sign(_Tp __val) {
  56. return (_Tp(0) < __val) - (__val < _Tp(0));
  57. }
  58. template <typename _Fp>
  59. _LIBCPP_HIDE_FROM_ABI constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; }
  60. template <class _Fp>
  61. _LIBCPP_INLINE_VISIBILITY constexpr
  62. enable_if_t<is_floating_point_v<_Fp>, _Fp>
  63. midpoint(_Fp __a, _Fp __b) noexcept
  64. {
  65. constexpr _Fp __lo = numeric_limits<_Fp>::min()*2;
  66. constexpr _Fp __hi = numeric_limits<_Fp>::max()/2;
  67. return std::__fp_abs(__a) <= __hi && std::__fp_abs(__b) <= __hi ? // typical case: overflow is impossible
  68. (__a + __b)/2 : // always correctly rounded
  69. std::__fp_abs(__a) < __lo ? __a + __b/2 : // not safe to halve a
  70. std::__fp_abs(__b) < __lo ? __a/2 + __b : // not safe to halve b
  71. __a/2 + __b/2; // otherwise correctly rounded
  72. }
  73. #endif // _LIBCPP_STD_VER > 17
  74. _LIBCPP_END_NAMESPACE_STD
  75. _LIBCPP_POP_MACROS
  76. #endif // _LIBCPP___NUMERIC_MIDPOINT_H