Path: blob/main/contrib/llvm-project/libcxx/include/__compare/strong_order.h
35262 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___COMPARE_STRONG_ORDER9#define _LIBCPP___COMPARE_STRONG_ORDER1011#include <__bit/bit_cast.h>12#include <__compare/compare_three_way.h>13#include <__compare/ordering.h>14#include <__config>15#include <__math/exponential_functions.h>16#include <__math/traits.h>17#include <__type_traits/conditional.h>18#include <__type_traits/decay.h>19#include <__type_traits/is_floating_point.h>20#include <__type_traits/is_same.h>21#include <__utility/forward.h>22#include <__utility/priority_tag.h>23#include <cstdint>24#include <limits>2526#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER27# pragma GCC system_header28#endif2930_LIBCPP_PUSH_MACROS31#include <__undef_macros>3233_LIBCPP_BEGIN_NAMESPACE_STD3435#if _LIBCPP_STD_VER >= 203637// [cmp.alg]38namespace __strong_order {39void strong_order() = delete;4041struct __fn {42// NOLINTBEGIN(libcpp-robust-against-adl) strong_order should use ADL, but only here43template <class _Tp, class _Up>44requires is_same_v<decay_t<_Tp>, decay_t<_Up>>45_LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept(46noexcept(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))47-> decltype(strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {48return strong_ordering(strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));49}50// NOLINTEND(libcpp-robust-against-adl)5152template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>53requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>54_LIBCPP_HIDE_FROM_ABI static constexpr strong_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept {55if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int32_t)) {56int32_t __rx = std::bit_cast<int32_t>(__t);57int32_t __ry = std::bit_cast<int32_t>(__u);58__rx = (__rx < 0) ? (numeric_limits<int32_t>::min() - __rx - 1) : __rx;59__ry = (__ry < 0) ? (numeric_limits<int32_t>::min() - __ry - 1) : __ry;60return (__rx <=> __ry);61} else if constexpr (numeric_limits<_Dp>::is_iec559 && sizeof(_Dp) == sizeof(int64_t)) {62int64_t __rx = std::bit_cast<int64_t>(__t);63int64_t __ry = std::bit_cast<int64_t>(__u);64__rx = (__rx < 0) ? (numeric_limits<int64_t>::min() - __rx - 1) : __rx;65__ry = (__ry < 0) ? (numeric_limits<int64_t>::min() - __ry - 1) : __ry;66return (__rx <=> __ry);67} else if (__t < __u) {68return strong_ordering::less;69} else if (__t > __u) {70return strong_ordering::greater;71} else if (__t == __u) {72if constexpr (numeric_limits<_Dp>::radix == 2) {73return __math::signbit(__u) <=> __math::signbit(__t);74} else {75// This is bullet 3 of the IEEE754 algorithm, relevant76// only for decimal floating-point;77// see https://stackoverflow.com/questions/69068075/78if (__t == 0 || __math::isinf(__t)) {79return __math::signbit(__u) <=> __math::signbit(__t);80} else {81int __texp, __uexp;82(void)__math::frexp(__t, &__texp);83(void)__math::frexp(__u, &__uexp);84return (__t < 0) ? (__texp <=> __uexp) : (__uexp <=> __texp);85}86}87} else {88// They're unordered, so one of them must be a NAN.89// The order is -QNAN, -SNAN, numbers, +SNAN, +QNAN.90bool __t_is_nan = __math::isnan(__t);91bool __u_is_nan = __math::isnan(__u);92bool __t_is_negative = __math::signbit(__t);93bool __u_is_negative = __math::signbit(__u);94using _IntType =95conditional_t< sizeof(__t) == sizeof(int32_t),96int32_t,97conditional_t< sizeof(__t) == sizeof(int64_t), int64_t, void> >;98if constexpr (is_same_v<_IntType, void>) {99static_assert(sizeof(_Dp) == 0, "std::strong_order is unimplemented for this floating-point type");100} else if (__t_is_nan && __u_is_nan) {101// Order by sign bit, then by "payload bits" (we'll just use bit_cast).102if (__t_is_negative != __u_is_negative) {103return (__u_is_negative <=> __t_is_negative);104} else {105return std::bit_cast<_IntType>(__t) <=> std::bit_cast<_IntType>(__u);106}107} else if (__t_is_nan) {108return __t_is_negative ? strong_ordering::less : strong_ordering::greater;109} else {110return __u_is_negative ? strong_ordering::greater : strong_ordering::less;111}112}113}114115template <class _Tp, class _Up>116requires is_same_v<decay_t<_Tp>, decay_t<_Up>>117_LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept(118noexcept(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))119-> decltype(strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {120return strong_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)));121}122123template <class _Tp, class _Up>124_LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const125noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())))126-> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>())) {127return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<2>());128}129};130} // namespace __strong_order131132inline namespace __cpo {133inline constexpr auto strong_order = __strong_order::__fn{};134} // namespace __cpo135136#endif // _LIBCPP_STD_VER >= 20137138_LIBCPP_END_NAMESPACE_STD139140_LIBCPP_POP_MACROS141142#endif // _LIBCPP___COMPARE_STRONG_ORDER143144145