1// -*- C++ -*- operator<=> three-way comparison support.
3// Copyright (C) 2019-2025 Free Software Foundation, Inc.
5// This file is part of GCC.
7// GCC is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 3, or (at your option)
12// GCC is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
27 * This is a Standard C++ Library header.
34#pragma GCC system_header
37#define __glibcxx_want_three_way_comparison
38#define __glibcxx_want_type_order
39#include <bits/version.h>
41#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
45#pragma GCC diagnostic push
46#pragma GCC diagnostic ignored "-Wpedantic" // __int128
47#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
49namespace std _GLIBCXX_VISIBILITY(default)
51 // [cmp.categories], comparison category types
55 using type = signed char;
57 enum class _Ord : type
59 equivalent = 0, less = -1, greater = 1,
60 // value remains unchanged when negated
61 unordered = -__SCHAR_MAX__ - 1
64 template<typename _Ordering>
65 [[__gnu__::__always_inline__]]
67 __ord(_Ordering __o) noexcept
68 { return _Ord(__o._M_value); }
70 template<typename _Ordering>
71 [[__gnu__::__always_inline__]]
73 __make(_Ord __o) noexcept
74 { return _Ordering(__o); }
78 consteval __literal_zero(__literal_zero*) noexcept { }
82 class partial_ordering
84 // less=0xff, equiv=0x00, greater=0x01, unordered=0x80
85 __cmp_cat::type _M_value;
88 partial_ordering(__cmp_cat::_Ord __v) noexcept
89 : _M_value(__cmp_cat::type(__v))
92 [[__gnu__::__always_inline__]]
93 constexpr __cmp_cat::type
96 // leaves _Ord::unordered unchanged
97 return static_cast<__cmp_cat::type>(-_M_value);
100 friend constexpr __cmp_cat::_Ord
101 __cmp_cat::__ord<partial_ordering>(partial_ordering) noexcept;
102 friend constexpr partial_ordering
103 __cmp_cat::__make<partial_ordering>(__cmp_cat::_Ord) noexcept;
107 static const partial_ordering less;
108 static const partial_ordering equivalent;
109 static const partial_ordering greater;
110 static const partial_ordering unordered;
114 friend constexpr bool
115 operator==(partial_ordering __v, __cmp_cat::__literal_zero) noexcept
116 { return __v._M_value == 0; }
119 friend constexpr bool
120 operator==(partial_ordering, partial_ordering) noexcept = default;
123 friend constexpr bool
124 operator< (partial_ordering __v, __cmp_cat::__literal_zero) noexcept
125 { return __v._M_value == -1; }
128 friend constexpr bool
129 operator> (partial_ordering __v, __cmp_cat::__literal_zero) noexcept
130 { return __v._M_value == 1; }
133 friend constexpr bool
134 operator<=(partial_ordering __v, __cmp_cat::__literal_zero) noexcept
135 { return __v._M_reverse() >= 0; }
138 friend constexpr bool
139 operator>=(partial_ordering __v, __cmp_cat::__literal_zero) noexcept
140 { return __v._M_value >= 0; }
143 friend constexpr bool
144 operator< (__cmp_cat::__literal_zero, partial_ordering __v) noexcept
145 { return __v._M_value == 1; }
148 friend constexpr bool
149 operator> (__cmp_cat::__literal_zero, partial_ordering __v) noexcept
150 { return __v._M_value == -1; }
153 friend constexpr bool
154 operator<=(__cmp_cat::__literal_zero, partial_ordering __v) noexcept
155 { return 0 <= __v._M_value; }
158 friend constexpr bool
159 operator>=(__cmp_cat::__literal_zero, partial_ordering __v) noexcept
160 { return 0 <= __v._M_reverse(); }
163 friend constexpr partial_ordering
164 operator<=>(partial_ordering __v, __cmp_cat::__literal_zero) noexcept
168 friend constexpr partial_ordering
169 operator<=>(__cmp_cat::__literal_zero, partial_ordering __v) noexcept
170 { return partial_ordering(__cmp_cat::_Ord(__v._M_reverse())); }
173 // valid values' definitions
174 inline constexpr partial_ordering
175 partial_ordering::less(__cmp_cat::_Ord::less);
177 inline constexpr partial_ordering
178 partial_ordering::equivalent(__cmp_cat::_Ord::equivalent);
180 inline constexpr partial_ordering
181 partial_ordering::greater(__cmp_cat::_Ord::greater);
183 inline constexpr partial_ordering
184 partial_ordering::unordered(__cmp_cat::_Ord::unordered);
188 __cmp_cat::type _M_value;
191 weak_ordering(__cmp_cat::_Ord __v) noexcept : _M_value(__cmp_cat::type(__v))
194 friend constexpr __cmp_cat::_Ord
195 __cmp_cat::__ord<weak_ordering>(weak_ordering) noexcept;
196 friend constexpr weak_ordering
197 __cmp_cat::__make<weak_ordering>(__cmp_cat::_Ord) noexcept;
201 static const weak_ordering less;
202 static const weak_ordering equivalent;
203 static const weak_ordering greater;
206 constexpr operator partial_ordering() const noexcept
207 { return __cmp_cat::__make<partial_ordering>(__cmp_cat::_Ord(_M_value)); }
211 friend constexpr bool
212 operator==(weak_ordering __v, __cmp_cat::__literal_zero) noexcept
213 { return __v._M_value == 0; }
216 friend constexpr bool
217 operator==(weak_ordering, weak_ordering) noexcept = default;
220 friend constexpr bool
221 operator< (weak_ordering __v, __cmp_cat::__literal_zero) noexcept
222 { return __v._M_value < 0; }
225 friend constexpr bool
226 operator> (weak_ordering __v, __cmp_cat::__literal_zero) noexcept
227 { return __v._M_value > 0; }
230 friend constexpr bool
231 operator<=(weak_ordering __v, __cmp_cat::__literal_zero) noexcept
232 { return __v._M_value <= 0; }
235 friend constexpr bool
236 operator>=(weak_ordering __v, __cmp_cat::__literal_zero) noexcept
237 { return __v._M_value >= 0; }
240 friend constexpr bool
241 operator< (__cmp_cat::__literal_zero, weak_ordering __v) noexcept
242 { return 0 < __v._M_value; }
245 friend constexpr bool
246 operator> (__cmp_cat::__literal_zero, weak_ordering __v) noexcept
247 { return 0 > __v._M_value; }
250 friend constexpr bool
251 operator<=(__cmp_cat::__literal_zero, weak_ordering __v) noexcept
252 { return 0 <= __v._M_value; }
255 friend constexpr bool
256 operator>=(__cmp_cat::__literal_zero, weak_ordering __v) noexcept
257 { return 0 >= __v._M_value; }
260 friend constexpr weak_ordering
261 operator<=>(weak_ordering __v, __cmp_cat::__literal_zero) noexcept
265 friend constexpr weak_ordering
266 operator<=>(__cmp_cat::__literal_zero, weak_ordering __v) noexcept
267 { return weak_ordering(__cmp_cat::_Ord(-__v._M_value)); }
270 // valid values' definitions
271 inline constexpr weak_ordering
272 weak_ordering::less(__cmp_cat::_Ord::less);
274 inline constexpr weak_ordering
275 weak_ordering::equivalent(__cmp_cat::_Ord::equivalent);
277 inline constexpr weak_ordering
278 weak_ordering::greater(__cmp_cat::_Ord::greater);
280 class strong_ordering
282 __cmp_cat::type _M_value;
285 strong_ordering(__cmp_cat::_Ord __v) noexcept
286 : _M_value(__cmp_cat::type(__v))
289 friend constexpr __cmp_cat::_Ord
290 __cmp_cat::__ord<strong_ordering>(strong_ordering) noexcept;
291 friend constexpr strong_ordering
292 __cmp_cat::__make<strong_ordering>(__cmp_cat::_Ord) noexcept;
296 static const strong_ordering less;
297 static const strong_ordering equal;
298 static const strong_ordering equivalent;
299 static const strong_ordering greater;
302 constexpr operator partial_ordering() const noexcept
303 { return __cmp_cat::__make<partial_ordering>(__cmp_cat::_Ord(_M_value)); }
306 constexpr operator weak_ordering() const noexcept
307 { return __cmp_cat::__make<weak_ordering>(__cmp_cat::_Ord(_M_value)); }
311 friend constexpr bool
312 operator==(strong_ordering __v, __cmp_cat::__literal_zero) noexcept
313 { return __v._M_value == 0; }
316 friend constexpr bool
317 operator==(strong_ordering, strong_ordering) noexcept = default;
320 friend constexpr bool
321 operator< (strong_ordering __v, __cmp_cat::__literal_zero) noexcept
322 { return __v._M_value < 0; }
325 friend constexpr bool
326 operator> (strong_ordering __v, __cmp_cat::__literal_zero) noexcept
327 { return __v._M_value > 0; }
330 friend constexpr bool
331 operator<=(strong_ordering __v, __cmp_cat::__literal_zero) noexcept
332 { return __v._M_value <= 0; }
335 friend constexpr bool
336 operator>=(strong_ordering __v, __cmp_cat::__literal_zero) noexcept
337 { return __v._M_value >= 0; }
340 friend constexpr bool
341 operator< (__cmp_cat::__literal_zero, strong_ordering __v) noexcept
342 { return 0 < __v._M_value; }
345 friend constexpr bool
346 operator> (__cmp_cat::__literal_zero, strong_ordering __v) noexcept
347 { return 0 > __v._M_value; }
350 friend constexpr bool
351 operator<=(__cmp_cat::__literal_zero, strong_ordering __v) noexcept
352 { return 0 <= __v._M_value; }
355 friend constexpr bool
356 operator>=(__cmp_cat::__literal_zero, strong_ordering __v) noexcept
357 { return 0 >= __v._M_value; }
360 friend constexpr strong_ordering
361 operator<=>(strong_ordering __v, __cmp_cat::__literal_zero) noexcept
365 friend constexpr strong_ordering
366 operator<=>(__cmp_cat::__literal_zero, strong_ordering __v) noexcept
367 { return strong_ordering(__cmp_cat::_Ord(-__v._M_value)); }
370 // valid values' definitions
371 inline constexpr strong_ordering
372 strong_ordering::less(__cmp_cat::_Ord::less);
374 inline constexpr strong_ordering
375 strong_ordering::equal(__cmp_cat::_Ord::equivalent);
377 inline constexpr strong_ordering
378 strong_ordering::equivalent(__cmp_cat::_Ord::equivalent);
380 inline constexpr strong_ordering
381 strong_ordering::greater(__cmp_cat::_Ord::greater);
384 // named comparison functions
387 is_eq(partial_ordering __cmp) noexcept
388 { return __cmp == 0; }
392 is_neq(partial_ordering __cmp) noexcept
393 { return __cmp != 0; }
397 is_lt (partial_ordering __cmp) noexcept
398 { return __cmp < 0; }
402 is_lteq(partial_ordering __cmp) noexcept
403 { return __cmp <= 0; }
407 is_gt (partial_ordering __cmp) noexcept
408 { return __cmp > 0; }
412 is_gteq(partial_ordering __cmp) noexcept
413 { return __cmp >= 0; }
417 template<typename _Tp>
418 inline constexpr unsigned __cmp_cat_id = 1;
420 inline constexpr unsigned __cmp_cat_id<partial_ordering> = 2;
422 inline constexpr unsigned __cmp_cat_id<weak_ordering> = 4;
424 inline constexpr unsigned __cmp_cat_id<strong_ordering> = 8;
426 template<typename... _Ts>
427 constexpr auto __common_cmp_cat()
429 constexpr unsigned __cats = (__cmp_cat_id<_Ts> | ...);
430 // If any Ti is not a comparison category type, U is void.
431 if constexpr (__cats & 1)
433 // Otherwise, if at least one Ti is std::partial_ordering,
434 // U is std::partial_ordering.
435 else if constexpr (bool(__cats & __cmp_cat_id<partial_ordering>))
436 return partial_ordering::equivalent;
437 // Otherwise, if at least one Ti is std::weak_ordering,
438 // U is std::weak_ordering.
439 else if constexpr (bool(__cats & __cmp_cat_id<weak_ordering>))
440 return weak_ordering::equivalent;
441 // Otherwise, U is std::strong_ordering.
443 return strong_ordering::equivalent;
445 } // namespace __detail
447 // [cmp.common], common comparison category type
448 template<typename... _Ts>
449 struct common_comparison_category
451 using type = decltype(__detail::__common_cmp_cat<_Ts...>());
454 // Partial specializations for one and zero argument cases.
456 template<typename _Tp>
457 struct common_comparison_category<_Tp>
458 { using type = void; };
461 struct common_comparison_category<partial_ordering>
462 { using type = partial_ordering; };
465 struct common_comparison_category<weak_ordering>
466 { using type = weak_ordering; };
469 struct common_comparison_category<strong_ordering>
470 { using type = strong_ordering; };
473 struct common_comparison_category<>
474 { using type = strong_ordering; };
476 template<typename... _Ts>
477 using common_comparison_category_t
478 = typename common_comparison_category<_Ts...>::type;
480#if __cpp_lib_three_way_comparison >= 201907L
481 // C++ >= 20 && impl_3way_comparison >= 201907 && lib_concepts
484 template<typename _Tp, typename _Cat>
485 concept __compares_as
486 = same_as<common_comparison_category_t<_Tp, _Cat>, _Cat>;
487 } // namespace __detail
489 // [cmp.concept], concept three_way_comparable
490 template<typename _Tp, typename _Cat = partial_ordering>
491 concept three_way_comparable
492 = __detail::__weakly_eq_cmp_with<_Tp, _Tp>
493 && __detail::__partially_ordered_with<_Tp, _Tp>
494 && requires(const remove_reference_t<_Tp>& __a,
495 const remove_reference_t<_Tp>& __b)
497 { __a <=> __b } -> __detail::__compares_as<_Cat>;
500 template<typename _Tp, typename _Up, typename _Cat = partial_ordering>
501 concept three_way_comparable_with
502 = three_way_comparable<_Tp, _Cat>
503 && three_way_comparable<_Up, _Cat>
504 && common_reference_with<const remove_reference_t<_Tp>&,
505 const remove_reference_t<_Up>&>
506 && three_way_comparable<
507 common_reference_t<const remove_reference_t<_Tp>&,
508 const remove_reference_t<_Up>&>, _Cat>
509 && __detail::__weakly_eq_cmp_with<_Tp, _Up>
510 && __detail::__partially_ordered_with<_Tp, _Up>
511 && requires(const remove_reference_t<_Tp>& __t,
512 const remove_reference_t<_Up>& __u)
514 { __t <=> __u } -> __detail::__compares_as<_Cat>;
515 { __u <=> __t } -> __detail::__compares_as<_Cat>;
520 template<typename _Tp, typename _Up>
521 using __cmp3way_res_t
522 = decltype(std::declval<_Tp>() <=> std::declval<_Up>());
524 // Implementation of std::compare_three_way_result.
525 // It is undefined for a program to add specializations of
526 // std::compare_three_way_result, so the std::compare_three_way_result_t
527 // alias ignores std::compare_three_way_result and uses
528 // __detail::__cmp3way_res_impl directly instead.
529 template<typename _Tp, typename _Up>
530 struct __cmp3way_res_impl
533 template<typename _Tp, typename _Up>
534 requires requires { typename __cmp3way_res_t<__cref<_Tp>, __cref<_Up>>; }
535 struct __cmp3way_res_impl<_Tp, _Up>
537 using type = __cmp3way_res_t<__cref<_Tp>, __cref<_Up>>;
539 } // namespace __detail
541 /// [cmp.result], result of three-way comparison
542 template<typename _Tp, typename _Up = _Tp>
543 struct compare_three_way_result
544 : __detail::__cmp3way_res_impl<_Tp, _Up>
547 /// [cmp.result], result of three-way comparison
548 template<typename _Tp, typename _Up = _Tp>
549 using compare_three_way_result_t
550 = typename __detail::__cmp3way_res_impl<_Tp, _Up>::type;
554 // BUILTIN-PTR-THREE-WAY(T, U)
555 // This determines whether t <=> u results in a call to a built-in
556 // operator<=> comparing pointers. It doesn't work for function pointers
558 template<typename _Tp, typename _Up>
559 concept __3way_builtin_ptr_cmp
560 = requires(_Tp&& __t, _Up&& __u)
561 { static_cast<_Tp&&>(__t) <=> static_cast<_Up&&>(__u); }
562 && convertible_to<_Tp, const volatile void*>
563 && convertible_to<_Up, const volatile void*>
564 && ! requires(_Tp&& __t, _Up&& __u)
565 { operator<=>(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)); }
566 && ! requires(_Tp&& __t, _Up&& __u)
567 { static_cast<_Tp&&>(__t).operator<=>(static_cast<_Up&&>(__u)); };
568 } // namespace __detail
570 // _GLIBCXX_RESOLVE_LIB_DEFECTS
571 // 3530 BUILTIN-PTR-MEOW should not opt the type out of syntactic checks
573 // [cmp.object], typename compare_three_way
574 struct compare_three_way
576 template<typename _Tp, typename _Up>
577 requires three_way_comparable_with<_Tp, _Up>
579 operator() [[nodiscard]] (_Tp&& __t, _Up&& __u) const
580 noexcept(noexcept(std::declval<_Tp>() <=> std::declval<_Up>()))
582 if constexpr (__detail::__3way_builtin_ptr_cmp<_Tp, _Up>)
584 auto __pt = static_cast<const volatile void*>(__t);
585 auto __pu = static_cast<const volatile void*>(__u);
586 if (std::__is_constant_evaluated())
587 return __pt <=> __pu;
588 auto __it = reinterpret_cast<__UINTPTR_TYPE__>(__pt);
589 auto __iu = reinterpret_cast<__UINTPTR_TYPE__>(__pu);
590 return __it <=> __iu;
593 return static_cast<_Tp&&>(__t) <=> static_cast<_Up&&>(__u);
596 using is_transparent = void;
599 /// @cond undocumented
600 // Namespace for helpers for the <compare> customization points.
603 template<floating_point _Tp>
604 constexpr weak_ordering
605 __fp_weak_ordering(_Tp __e, _Tp __f)
607 auto __po = __cmp_cat::__ord(__e <=> __f);
608 if (__po != __cmp_cat::_Ord::unordered)
609 return __cmp_cat::__make<weak_ordering>(__po);
610 else // unordered, at least one argument is NaN
612 // return -1 for negative nan, +1 for positive nan, 0 otherwise.
613 auto __isnan_sign = [](_Tp __fp) -> int {
614 return __builtin_isnan(__fp)
615 ? __builtin_signbit(__fp) ? -1 : 1
618 return __isnan_sign(__e) <=> __isnan_sign(__f);
622 void strong_order() = delete;
624 template<typename _Tp, typename _Up>
625 concept __adl_strong = requires(_Tp&& __t, _Up&& __u)
627 strong_ordering(strong_order(static_cast<_Tp&&>(__t),
628 static_cast<_Up&&>(__u)));
631 void weak_order() = delete;
633 template<typename _Tp, typename _Up>
634 concept __adl_weak = requires(_Tp&& __t, _Up&& __u)
636 weak_ordering(weak_order(static_cast<_Tp&&>(__t),
637 static_cast<_Up&&>(__u)));
640 void partial_order() = delete;
642 template<typename _Tp, typename _Up>
643 concept __adl_partial = requires(_Tp&& __t, _Up&& __u)
645 partial_ordering(partial_order(static_cast<_Tp&&>(__t),
646 static_cast<_Up&&>(__u)));
649 template<typename _Ord, typename _Tp, typename _Up>
650 concept __cmp3way = requires(_Tp&& __t, _Up&& __u, compare_three_way __c)
652 _Ord(__c(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)));
655 template<typename _Tp, typename _Up>
656 concept __strongly_ordered
657 = __adl_strong<_Tp, _Up>
658 || floating_point<remove_reference_t<_Tp>>
659 || __cmp3way<strong_ordering, _Tp, _Up>;
661 template<typename _Tp, typename _Up>
662 concept __decayed_same_as = same_as<decay_t<_Tp>, decay_t<_Up>>;
666 template<typename _Tp, typename _Up>
667 static constexpr bool
670 if constexpr (floating_point<decay_t<_Tp>>)
672 else if constexpr (__adl_strong<_Tp, _Up>)
673 return noexcept(strong_ordering(strong_order(std::declval<_Tp>(),
674 std::declval<_Up>())));
675 else if constexpr (__cmp3way<strong_ordering, _Tp, _Up>)
676 return noexcept(compare_three_way()(std::declval<_Tp>(),
677 std::declval<_Up>()));
680 friend class _Weak_order;
681 friend class _Strong_fallback;
683 // Names for the supported floating-point representations.
686 _Binary16, _Binary32, _Binary64, _Binary128, // IEEE
687 _X86_80bit, // x86 80-bit extended precision
688 _M68k_80bit, // m68k 80-bit extended precision
689 _Dbldbl, // IBM 128-bit double-double
690 _Bfloat16, // std::bfloat16_t
693#ifndef __cpp_using_enum
694 // XXX Remove these once 'using enum' support is ubiquitous.
695 static constexpr _Fp_fmt _Binary16 = _Fp_fmt::_Binary16;
696 static constexpr _Fp_fmt _Binary32 = _Fp_fmt::_Binary32;
697 static constexpr _Fp_fmt _Binary64 = _Fp_fmt::_Binary64;
698 static constexpr _Fp_fmt _Binary128 = _Fp_fmt::_Binary128;
699 static constexpr _Fp_fmt _X86_80bit = _Fp_fmt::_X86_80bit;
700 static constexpr _Fp_fmt _M68k_80bit = _Fp_fmt::_M68k_80bit;
701 static constexpr _Fp_fmt _Dbldbl = _Fp_fmt::_Dbldbl;
702 static constexpr _Fp_fmt _Bfloat16 = _Fp_fmt::_Bfloat16;
705 // Identify the format used by a floating-point type.
706 template<typename _Tp>
707 static consteval _Fp_fmt
710#ifdef __cpp_using_enum
714 // Identify these formats first, then assume anything else is IEEE.
715 // N.B. ARM __fp16 alternative format can be handled as binary16.
717#ifdef __LONG_DOUBLE_IBM128__
718 if constexpr (__is_same(_Tp, long double))
720#elif defined __LONG_DOUBLE_IEEE128__ && defined __SIZEOF_IBM128__
721 if constexpr (__is_same(_Tp, __ibm128))
725#if __LDBL_MANT_DIG__ == 64
726 if constexpr (__is_same(_Tp, long double))
727 return __LDBL_MIN_EXP__ == -16381 ? _X86_80bit : _M68k_80bit;
729#ifdef __SIZEOF_FLOAT80__
730 if constexpr (__is_same(_Tp, __float80))
733#ifdef __STDCPP_BFLOAT16_T__
734 if constexpr (__is_same(_Tp, decltype(0.0bf16)))
738 constexpr int __width = sizeof(_Tp) * __CHAR_BIT__;
740 if constexpr (__width == 16) // IEEE binary16 (or ARM fp16).
742 else if constexpr (__width == 32) // IEEE binary32
744 else if constexpr (__width == 64) // IEEE binary64
746 else if constexpr (__width == 128) // IEEE binary128
750 // So we don't need to include <stdint.h> and pollute the namespace.
751 using int64_t = __INT64_TYPE__;
752 using int32_t = __INT32_TYPE__;
753 using int16_t = __INT16_TYPE__;
754 using uint64_t = __UINT64_TYPE__;
755 using uint16_t = __UINT16_TYPE__;
757 // Used to unpack floating-point types that do not fit into an integer.
758 template<typename _Tp>
761#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
770 _Int(_Tp __hi, uint64_t __lo) noexcept : _M_hi(__hi)
774 _Int(uint64_t __lo) noexcept : _M_hi(0)
777 constexpr bool operator==(const _Int&) const = default;
779#if defined __hppa__ || (defined __mips__ && !defined __mips_nan2008)
781 operator<<(int __n) const noexcept
783 // XXX this assumes n >= 64, which is true for the use below.
784 return _Int(static_cast<_Tp>(_M_lo << (__n - 64)), 0);
789 operator^=(const _Int& __rhs) noexcept
791 _M_hi ^= __rhs._M_hi;
792 _M_lo ^= __rhs._M_lo;
796 constexpr strong_ordering
797 operator<=>(const _Int& __rhs) const noexcept
799 strong_ordering __cmp = _M_hi <=> __rhs._M_hi;
800 if (__cmp != strong_ordering::equal)
802 return _M_lo <=> __rhs._M_lo;
806 template<typename _Tp>
808 _S_compl(_Tp __t) noexcept
810 constexpr int __width = sizeof(_Tp) * __CHAR_BIT__;
811 // Sign extend to get all ones or all zeros.
812 make_unsigned_t<_Tp> __sign = __t >> (__width - 1);
813 // If the sign bit was set, this flips all bits below it.
814 // This converts ones' complement to two's complement.
815 return __t ^ (__sign >> 1);
818 // As above but works on both parts of _Int<T>.
819 template<typename _Tp>
820 static constexpr _Int<_Tp>
821 _S_compl(_Int<_Tp> __t) noexcept
823 constexpr int __width = sizeof(_Tp) * __CHAR_BIT__;
824 make_unsigned_t<_Tp> __sign = __t._M_hi >> (__width - 1);
825 __t._M_hi ^= (__sign >> 1 );
826 uint64_t __sign64 = (_Tp)__sign;
827 __t._M_lo ^= __sign64;
831 // Bit-cast a floating-point value to an unsigned integer.
832 template<typename _Tp>
833 constexpr static auto
834 _S_fp_bits(_Tp __val) noexcept
836 if constexpr (sizeof(_Tp) == sizeof(int64_t))
837 return __builtin_bit_cast(int64_t, __val);
838 else if constexpr (sizeof(_Tp) == sizeof(int32_t))
839 return __builtin_bit_cast(int32_t, __val);
840 else if constexpr (sizeof(_Tp) == sizeof(int16_t))
841 return __builtin_bit_cast(int16_t, __val);
844#ifdef __cpp_using_enum
847 constexpr auto __fmt = _S_fp_fmt<_Tp>();
848 if constexpr (__fmt == _X86_80bit)
850 if constexpr (sizeof(_Tp) == 3 * sizeof(int32_t))
852 auto __ival = __builtin_bit_cast(_Int<int32_t>, __val);
853 return _Int<int16_t>(__ival._M_hi, __ival._M_lo);
857 auto __ival = __builtin_bit_cast(_Int<int64_t>, __val);
858 return _Int<int16_t>(__ival._M_hi, __ival._M_lo);
861 else if constexpr (__fmt == _M68k_80bit)
863 auto __ival = __builtin_bit_cast(_Int<int32_t>, __val);
864 return _Int<int16_t>(__ival._M_hi >> 16, __ival._M_lo);
866 else if constexpr (sizeof(_Tp) == 2 * sizeof(int64_t))
869 return __builtin_bit_cast(__int128, __val);
871 return __builtin_bit_cast(_Int<int64_t>, __val);
875 static_assert(sizeof(_Tp) == sizeof(int64_t),
876 "unsupported floating-point type");
880 template<typename _Tp>
881 static constexpr strong_ordering
882 _S_fp_cmp(_Tp __x, _Tp __y) noexcept
885 if (__builtin_isnan(__x) || __builtin_isnan(__y))
887 int __ix = (bool) __builtin_isnan(__x);
888 int __iy = (bool) __builtin_isnan(__y);
889 __ix *= __builtin_signbit(__x) ? -1 : 1;
890 __iy *= __builtin_signbit(__y) ? -1 : 1;
891 return __ix <=> __iy;
894 return __builtin_bit_cast(strong_ordering, __x <=> __y);
897 auto __ix = _S_fp_bits(__x);
898 auto __iy = _S_fp_bits(__y);
901 return strong_ordering::equal; // All bits are equal, we're done.
903#ifdef __cpp_using_enum
906 constexpr auto __fmt = _S_fp_fmt<_Tp>();
908 if constexpr (__fmt == _Dbldbl) // double-double
910 // Unpack the double-double into two parts.
911 // We never inspect the low double as a double, cast to integer.
912 struct _Unpacked { double _M_hi; int64_t _M_lo; };
913 auto __x2 = __builtin_bit_cast(_Unpacked, __x);
914 auto __y2 = __builtin_bit_cast(_Unpacked, __y);
916 // Compare the high doubles first and use result if unequal.
917 auto __cmp = _S_fp_cmp(__x2._M_hi, __y2._M_hi);
918 if (__cmp != strong_ordering::equal)
921 // For NaN the low double is unused, so if the high doubles
922 // are the same NaN, we don't need to compare the low doubles.
923 if (__builtin_isnan(__x2._M_hi))
924 return strong_ordering::equal;
925 // Similarly, if the low doubles are +zero or -zero (which is
926 // true for all infinities and some other values), we're done.
927 if (((__x2._M_lo | __y2._M_lo) & 0x7fffffffffffffffULL) == 0)
928 return strong_ordering::equal;
930 // Otherwise, compare the low parts.
931 return _S_compl(__x2._M_lo) <=> _S_compl(__y2._M_lo);
935 if constexpr (__fmt == _M68k_80bit)
937 // For m68k the MSB of the significand is ignored for the
938 // greatest exponent, so either 0 or 1 is valid there.
939 // Set it before comparing, so that we never have 0 there.
940 constexpr uint16_t __maxexp = 0x7fff;
941 if ((__ix._M_hi & __maxexp) == __maxexp)
942 __ix._M_lo |= 1ull << 63;
943 if ((__iy._M_hi & __maxexp) == __maxexp)
944 __iy._M_lo |= 1ull << 63;
948#if defined __hppa__ || (defined __mips__ && !defined __mips_nan2008)
949 // IEEE 754-1985 allowed the meaning of the quiet/signaling
950 // bit to be reversed. Flip that to give desired ordering.
951 if (__builtin_isnan(__x) && __builtin_isnan(__y))
953 using _Int = decltype(__ix);
955 constexpr int __nantype = __fmt == _Binary32 ? 22
956 : __fmt == _Binary64 ? 51
957 : __fmt == _Binary128 ? 111
959 constexpr _Int __bit = _Int(1) << __nantype;
965 return _S_compl(__ix) <=> _S_compl(__iy);
970 template<typename _Tp, __decayed_same_as<_Tp> _Up>
971 requires __strongly_ordered<_Tp, _Up>
972 constexpr strong_ordering
973 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
974 noexcept(_S_noexcept<_Tp, _Up>())
976 if constexpr (floating_point<decay_t<_Tp>>)
977 return _S_fp_cmp(__e, __f);
978 else if constexpr (__adl_strong<_Tp, _Up>)
979 return strong_ordering(strong_order(static_cast<_Tp&&>(__e),
980 static_cast<_Up&&>(__f)));
981 else if constexpr (__cmp3way<strong_ordering, _Tp, _Up>)
982 return compare_three_way()(static_cast<_Tp&&>(__e),
983 static_cast<_Up&&>(__f));
987 template<typename _Tp, typename _Up>
988 concept __weakly_ordered
989 = floating_point<remove_reference_t<_Tp>>
990 || __adl_weak<_Tp, _Up>
991 || __cmp3way<weak_ordering, _Tp, _Up>
992 || __strongly_ordered<_Tp, _Up>;
996 template<typename _Tp, typename _Up>
997 static constexpr bool
1000 if constexpr (floating_point<decay_t<_Tp>>)
1002 else if constexpr (__adl_weak<_Tp, _Up>)
1003 return noexcept(weak_ordering(weak_order(std::declval<_Tp>(),
1004 std::declval<_Up>())));
1005 else if constexpr (__cmp3way<weak_ordering, _Tp, _Up>)
1006 return noexcept(compare_three_way()(std::declval<_Tp>(),
1007 std::declval<_Up>()));
1008 else if constexpr (__strongly_ordered<_Tp, _Up>)
1009 return _Strong_order::_S_noexcept<_Tp, _Up>();
1012 friend class _Partial_order;
1013 friend class _Weak_fallback;
1016 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1017 requires __weakly_ordered<_Tp, _Up>
1018 constexpr weak_ordering
1019 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1020 noexcept(_S_noexcept<_Tp, _Up>())
1022 if constexpr (floating_point<decay_t<_Tp>>)
1023 return __compare::__fp_weak_ordering(__e, __f);
1024 else if constexpr (__adl_weak<_Tp, _Up>)
1025 return weak_ordering(weak_order(static_cast<_Tp&&>(__e),
1026 static_cast<_Up&&>(__f)));
1027 else if constexpr (__cmp3way<weak_ordering, _Tp, _Up>)
1028 return compare_three_way()(static_cast<_Tp&&>(__e),
1029 static_cast<_Up&&>(__f));
1030 else if constexpr (__strongly_ordered<_Tp, _Up>)
1031 return _Strong_order{}(static_cast<_Tp&&>(__e),
1032 static_cast<_Up&&>(__f));
1036 template<typename _Tp, typename _Up>
1037 concept __partially_ordered
1038 = __adl_partial<_Tp, _Up>
1039 || __cmp3way<partial_ordering, _Tp, _Up>
1040 || __weakly_ordered<_Tp, _Up>;
1042 class _Partial_order
1044 template<typename _Tp, typename _Up>
1045 static constexpr bool
1048 if constexpr (__adl_partial<_Tp, _Up>)
1049 return noexcept(partial_ordering(partial_order(std::declval<_Tp>(),
1050 std::declval<_Up>())));
1051 else if constexpr (__cmp3way<partial_ordering, _Tp, _Up>)
1052 return noexcept(compare_three_way()(std::declval<_Tp>(),
1053 std::declval<_Up>()));
1054 else if constexpr (__weakly_ordered<_Tp, _Up>)
1055 return _Weak_order::_S_noexcept<_Tp, _Up>();
1058 friend class _Partial_fallback;
1061 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1062 requires __partially_ordered<_Tp, _Up>
1063 constexpr partial_ordering
1064 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1065 noexcept(_S_noexcept<_Tp, _Up>())
1067 if constexpr (__adl_partial<_Tp, _Up>)
1068 return partial_ordering(partial_order(static_cast<_Tp&&>(__e),
1069 static_cast<_Up&&>(__f)));
1070 else if constexpr (__cmp3way<partial_ordering, _Tp, _Up>)
1071 return compare_three_way()(static_cast<_Tp&&>(__e),
1072 static_cast<_Up&&>(__f));
1073 else if constexpr (__weakly_ordered<_Tp, _Up>)
1074 return _Weak_order{}(static_cast<_Tp&&>(__e),
1075 static_cast<_Up&&>(__f));
1079 template<typename _Tp, typename _Up>
1080 concept __op_eq_lt = requires(_Tp&& __t, _Up&& __u)
1082 { static_cast<_Tp&&>(__t) == static_cast<_Up&&>(__u) }
1083 -> convertible_to<bool>;
1084 { static_cast<_Tp&&>(__t) < static_cast<_Up&&>(__u) }
1085 -> convertible_to<bool>;
1088 class _Strong_fallback
1090 template<typename _Tp, typename _Up>
1091 static constexpr bool
1094 if constexpr (__strongly_ordered<_Tp, _Up>)
1095 return _Strong_order::_S_noexcept<_Tp, _Up>();
1097 return noexcept(bool(std::declval<_Tp>() == std::declval<_Up>()))
1098 && noexcept(bool(std::declval<_Tp>() < std::declval<_Up>()));
1102 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1103 requires __strongly_ordered<_Tp, _Up> || __op_eq_lt<_Tp, _Up>
1104 constexpr strong_ordering
1105 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1106 noexcept(_S_noexcept<_Tp, _Up>())
1108 if constexpr (__strongly_ordered<_Tp, _Up>)
1109 return _Strong_order{}(static_cast<_Tp&&>(__e),
1110 static_cast<_Up&&>(__f));
1111 else // __op_eq_lt<_Tp, _Up>
1112 return static_cast<_Tp&&>(__e) == static_cast<_Up&&>(__f)
1113 ? strong_ordering::equal
1114 : static_cast<_Tp&&>(__e) < static_cast<_Up&&>(__f)
1115 ? strong_ordering::less
1116 : strong_ordering::greater;
1120 class _Weak_fallback
1122 template<typename _Tp, typename _Up>
1123 static constexpr bool
1126 if constexpr (__weakly_ordered<_Tp, _Up>)
1127 return _Weak_order::_S_noexcept<_Tp, _Up>();
1129 return noexcept(bool(std::declval<_Tp>() == std::declval<_Up>()))
1130 && noexcept(bool(std::declval<_Tp>() < std::declval<_Up>()));
1134 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1135 requires __weakly_ordered<_Tp, _Up> || __op_eq_lt<_Tp, _Up>
1136 constexpr weak_ordering
1137 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1138 noexcept(_S_noexcept<_Tp, _Up>())
1140 if constexpr (__weakly_ordered<_Tp, _Up>)
1141 return _Weak_order{}(static_cast<_Tp&&>(__e),
1142 static_cast<_Up&&>(__f));
1143 else // __op_eq_lt<_Tp, _Up>
1144 return static_cast<_Tp&&>(__e) == static_cast<_Up&&>(__f)
1145 ? weak_ordering::equivalent
1146 : static_cast<_Tp&&>(__e) < static_cast<_Up&&>(__f)
1147 ? weak_ordering::less
1148 : weak_ordering::greater;
1152 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1153 // 3465. compare_partial_order_fallback requires F < E
1154 template<typename _Tp, typename _Up>
1155 concept __op_eq_lt_lt = __op_eq_lt<_Tp, _Up>
1156 && requires(_Tp&& __t, _Up&& __u)
1158 { static_cast<_Up&&>(__u) < static_cast<_Tp&&>(__t) }
1159 -> convertible_to<bool>;
1162 class _Partial_fallback
1164 template<typename _Tp, typename _Up>
1165 static constexpr bool
1168 if constexpr (__partially_ordered<_Tp, _Up>)
1169 return _Partial_order::_S_noexcept<_Tp, _Up>();
1171 return noexcept(bool(std::declval<_Tp>() == std::declval<_Up>()))
1172 && noexcept(bool(std::declval<_Tp>() < std::declval<_Up>()));
1176 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1177 requires __partially_ordered<_Tp, _Up> || __op_eq_lt_lt<_Tp, _Up>
1178 constexpr partial_ordering
1179 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1180 noexcept(_S_noexcept<_Tp, _Up>())
1182 if constexpr (__partially_ordered<_Tp, _Up>)
1183 return _Partial_order{}(static_cast<_Tp&&>(__e),
1184 static_cast<_Up&&>(__f));
1185 else // __op_eq_lt_lt<_Tp, _Up>
1186 return static_cast<_Tp&&>(__e) == static_cast<_Up&&>(__f)
1187 ? partial_ordering::equivalent
1188 : static_cast<_Tp&&>(__e) < static_cast<_Up&&>(__f)
1189 ? partial_ordering::less
1190 : static_cast<_Up&&>(__f) < static_cast<_Tp&&>(__e)
1191 ? partial_ordering::greater
1192 : partial_ordering::unordered;
1195 } // namespace @endcond
1197 // [cmp.alg], comparison algorithms
1199 inline namespace _Cpo
1201 inline constexpr __compare::_Strong_order strong_order{};
1203 inline constexpr __compare::_Weak_order weak_order{};
1205 inline constexpr __compare::_Partial_order partial_order{};
1207 inline constexpr __compare::_Strong_fallback
1208 compare_strong_order_fallback{};
1210 inline constexpr __compare::_Weak_fallback
1211 compare_weak_order_fallback{};
1213 inline constexpr __compare::_Partial_fallback
1214 compare_partial_order_fallback{};
1217 /// @cond undocumented
1220 // [expos.only.func] synth-three-way
1221 inline constexpr struct _Synth3way
1223 template<typename _Tp, typename _Up>
1224 static constexpr bool
1225 _S_noexcept(const _Tp* __t = nullptr, const _Up* __u = nullptr)
1227 if constexpr (three_way_comparable_with<_Tp, _Up>)
1228 return noexcept(*__t <=> *__u);
1230 return noexcept(*__t < *__u) && noexcept(*__u < *__t);
1233 template<typename _Tp, typename _Up>
1236 operator()(const _Tp& __t, const _Up& __u) const
1237 noexcept(_S_noexcept<_Tp, _Up>())
1240 { __t < __u } -> __boolean_testable;
1241 { __u < __t } -> __boolean_testable;
1244 if constexpr (three_way_comparable_with<_Tp, _Up>)
1249 return weak_ordering::less;
1251 return weak_ordering::greater;
1253 return weak_ordering::equivalent;
1258 // [expos.only.func] synth-three-way-result
1259 template<typename _Tp, typename _Up = _Tp>
1261 = decltype(__detail::__synth3way(std::declval<_Tp&>(),
1262 std::declval<_Up&>()));
1263 } // namespace __detail
1266#if __glibcxx_type_order >= 202506L // C++ >= 26
1267 /// Total ordering of types.
1270 template<typename _Tp, typename _Up>
1273 static constexpr strong_ordering value = __builtin_type_order(_Tp, _Up);
1274 using value_type = strong_ordering;
1275 using type = type_order<_Tp, _Up>;
1276 constexpr operator value_type() const noexcept { return value; }
1277 constexpr value_type operator()() const noexcept { return value; }
1280 /// @ingroup variable_templates
1282 template<typename _Tp, typename _Up>
1283 inline constexpr strong_ordering type_order_v
1284 = __builtin_type_order(_Tp, _Up);
1285#endif // __glibcxx_type_order >= 202506L
1287#endif // __cpp_lib_three_way_comparison >= 201907L
1290#pragma GCC diagnostic pop