strong_order.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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___COMPARE_STRONG_ORDER
  9. #define _LIBCPP___COMPARE_STRONG_ORDER
  10. #include <__bit/bit_cast.h>
  11. #include <__compare/compare_three_way.h>
  12. #include <__compare/ordering.h>
  13. #include <__config>
  14. #include <__type_traits/conditional.h>
  15. #include <__type_traits/decay.h>
  16. #include <__utility/forward.h>
  17. #include <__utility/priority_tag.h>
  18. #include <cmath>
  19. #include <cstdint>
  20. #include <limits>
  21. #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
  22. # pragma GCC system_header
  23. #endif
  24. _LIBCPP_PUSH_MACROS
  25. #include <__undef_macros>
  26. _LIBCPP_BEGIN_NAMESPACE_STD
  27. #if _LIBCPP_STD_VER >= 20
  28. // [cmp.alg]
  29. namespace __strong_order {
  30. struct __fn {
  31. // NOLINTBEGIN(libcpp-robust-against-adl) strong_order should use ADL, but only here
  32. template<class _Tp, class _Up>
  33. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  34. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  35. __go(_Tp&& __t, _Up&& __u, __priority_tag<2>)
  36. noexcept(noexcept(strong_ordering(strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
  37. -> decltype( strong_ordering(strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
  38. { return strong_ordering(strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
  39. // NOLINTEND(libcpp-robust-against-adl)
  40. template<class _Tp, class _Up, class _Dp = decay_t<_Tp>>
  41. requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
  42. _LIBCPP_HIDE_FROM_ABI static constexpr strong_ordering
  43. __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept
  44. {
  45. if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int32_t)) {
  46. int32_t __rx = _VSTD::bit_cast<int32_t>(__t);
  47. int32_t __ry = _VSTD::bit_cast<int32_t>(__u);
  48. __rx = (__rx < 0) ? (numeric_limits<int32_t>::min() - __rx - 1) : __rx;
  49. __ry = (__ry < 0) ? (numeric_limits<int32_t>::min() - __ry - 1) : __ry;
  50. return (__rx <=> __ry);
  51. } else if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int64_t)) {
  52. int64_t __rx = _VSTD::bit_cast<int64_t>(__t);
  53. int64_t __ry = _VSTD::bit_cast<int64_t>(__u);
  54. __rx = (__rx < 0) ? (numeric_limits<int64_t>::min() - __rx - 1) : __rx;
  55. __ry = (__ry < 0) ? (numeric_limits<int64_t>::min() - __ry - 1) : __ry;
  56. return (__rx <=> __ry);
  57. } else if (__t < __u) {
  58. return strong_ordering::less;
  59. } else if (__t > __u) {
  60. return strong_ordering::greater;
  61. } else if (__t == __u) {
  62. if constexpr (numeric_limits<_Dp>::radix == 2) {
  63. return _VSTD::signbit(__u) <=> _VSTD::signbit(__t);
  64. } else {
  65. // This is bullet 3 of the IEEE754 algorithm, relevant
  66. // only for decimal floating-point;
  67. // see https://stackoverflow.com/questions/69068075/
  68. if (__t == 0 || _VSTD::isinf(__t)) {
  69. return _VSTD::signbit(__u) <=> _VSTD::signbit(__t);
  70. } else {
  71. int __texp, __uexp;
  72. (void)_VSTD::frexp(__t, &__texp);
  73. (void)_VSTD::frexp(__u, &__uexp);
  74. return (__t < 0) ? (__texp <=> __uexp) : (__uexp <=> __texp);
  75. }
  76. }
  77. } else {
  78. // They're unordered, so one of them must be a NAN.
  79. // The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN.
  80. bool __t_is_nan = _VSTD::isnan(__t);
  81. bool __u_is_nan = _VSTD::isnan(__u);
  82. bool __t_is_negative = _VSTD::signbit(__t);
  83. bool __u_is_negative = _VSTD::signbit(__u);
  84. using _IntType = conditional_t<
  85. sizeof(__t) == sizeof(int32_t), int32_t, conditional_t<
  86. sizeof(__t) == sizeof(int64_t), int64_t, void>
  87. >;
  88. if constexpr (is_same_v<_IntType, void>) {
  89. static_assert(sizeof(_Dp) == 0, "std::strong_order is unimplemented for this floating-point type");
  90. } else if (__t_is_nan && __u_is_nan) {
  91. // Order by sign bit, then by "payload bits" (we'll just use bit_cast).
  92. if (__t_is_negative != __u_is_negative) {
  93. return (__u_is_negative <=> __t_is_negative);
  94. } else {
  95. return _VSTD::bit_cast<_IntType>(__t) <=> _VSTD::bit_cast<_IntType>(__u);
  96. }
  97. } else if (__t_is_nan) {
  98. return __t_is_negative ? strong_ordering::less : strong_ordering::greater;
  99. } else {
  100. return __u_is_negative ? strong_ordering::greater : strong_ordering::less;
  101. }
  102. }
  103. }
  104. template<class _Tp, class _Up>
  105. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  106. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  107. __go(_Tp&& __t, _Up&& __u, __priority_tag<0>)
  108. noexcept(noexcept(strong_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
  109. -> decltype( strong_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
  110. { return strong_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
  111. template<class _Tp, class _Up>
  112. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
  113. noexcept(noexcept(__go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<2>())))
  114. -> decltype( __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<2>()))
  115. { return __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<2>()); }
  116. };
  117. } // namespace __strong_order
  118. inline namespace __cpo {
  119. inline constexpr auto strong_order = __strong_order::__fn{};
  120. } // namespace __cpo
  121. #endif // _LIBCPP_STD_VER >= 20
  122. _LIBCPP_END_NAMESPACE_STD
  123. _LIBCPP_POP_MACROS
  124. #endif // _LIBCPP___COMPARE_STRONG_ORDER