libstdc++
compare
Go to the documentation of this file.
1// -*- C++ -*- operator<=> three-way comparison support.
2
3// Copyright (C) 2019-2024 Free Software Foundation, Inc.
4//
5// This file is part of GCC.
6//
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)
10// any later version.
11//
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.
16//
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.
20
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/>.
25
26/** @file compare
27 * This is a Standard C++ Library header.
28 */
29
30#ifndef _COMPARE
31#define _COMPARE
32
33#pragma GCC system_header
34
35#define __glibcxx_want_three_way_comparison
36#include <bits/version.h>
37
38#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
39
40#include <concepts>
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44 // [cmp.categories], comparison category types
45
46 namespace __cmp_cat
47 {
48 using type = signed char;
49
50 enum class _Ord : type { equivalent = 0, less = -1, greater = 1 };
51
52 enum class _Ncmp : type { _Unordered = 2 };
53
54 struct __unspec
55 {
56 consteval __unspec(__unspec*) noexcept { }
57 };
58 }
59
60 class partial_ordering
61 {
62 // less=0xff, equiv=0x00, greater=0x01, unordered=0x02
63 __cmp_cat::type _M_value;
64
65 constexpr explicit
66 partial_ordering(__cmp_cat::_Ord __v) noexcept
67 : _M_value(__cmp_cat::type(__v))
68 { }
69
70 constexpr explicit
71 partial_ordering(__cmp_cat::_Ncmp __v) noexcept
72 : _M_value(__cmp_cat::type(__v))
73 { }
74
75 friend class weak_ordering;
76 friend class strong_ordering;
77
78 public:
79 // valid values
80 static const partial_ordering less;
81 static const partial_ordering equivalent;
82 static const partial_ordering greater;
83 static const partial_ordering unordered;
84
85 // comparisons
86 [[nodiscard]]
87 friend constexpr bool
88 operator==(partial_ordering __v, __cmp_cat::__unspec) noexcept
89 { return __v._M_value == 0; }
90
91 [[nodiscard]]
92 friend constexpr bool
93 operator==(partial_ordering, partial_ordering) noexcept = default;
94
95 [[nodiscard]]
96 friend constexpr bool
97 operator< (partial_ordering __v, __cmp_cat::__unspec) noexcept
98 { return __v._M_value == -1; }
99
100 [[nodiscard]]
101 friend constexpr bool
102 operator> (partial_ordering __v, __cmp_cat::__unspec) noexcept
103 { return __v._M_value == 1; }
104
105 [[nodiscard]]
106 friend constexpr bool
107 operator<=(partial_ordering __v, __cmp_cat::__unspec) noexcept
108 { return __v._M_value <= 0; }
109
110 [[nodiscard]]
111 friend constexpr bool
112 operator>=(partial_ordering __v, __cmp_cat::__unspec) noexcept
113 { return __cmp_cat::type(__v._M_value & 1) == __v._M_value; }
114
115 [[nodiscard]]
116 friend constexpr bool
117 operator< (__cmp_cat::__unspec, partial_ordering __v) noexcept
118 { return __v._M_value == 1; }
119
120 [[nodiscard]]
121 friend constexpr bool
122 operator> (__cmp_cat::__unspec, partial_ordering __v) noexcept
123 { return __v._M_value == -1; }
124
125 [[nodiscard]]
126 friend constexpr bool
127 operator<=(__cmp_cat::__unspec, partial_ordering __v) noexcept
128 { return __cmp_cat::type(__v._M_value & 1) == __v._M_value; }
129
130 [[nodiscard]]
131 friend constexpr bool
132 operator>=(__cmp_cat::__unspec, partial_ordering __v) noexcept
133 { return 0 >= __v._M_value; }
134
135 [[nodiscard]]
136 friend constexpr partial_ordering
137 operator<=>(partial_ordering __v, __cmp_cat::__unspec) noexcept
138 { return __v; }
139
140 [[nodiscard]]
141 friend constexpr partial_ordering
142 operator<=>(__cmp_cat::__unspec, partial_ordering __v) noexcept
143 {
144 if (__v._M_value & 1)
145 return partial_ordering(__cmp_cat::_Ord(-__v._M_value));
146 else
147 return __v;
148 }
149 };
150
151 // valid values' definitions
152 inline constexpr partial_ordering
153 partial_ordering::less(__cmp_cat::_Ord::less);
154
155 inline constexpr partial_ordering
156 partial_ordering::equivalent(__cmp_cat::_Ord::equivalent);
157
158 inline constexpr partial_ordering
159 partial_ordering::greater(__cmp_cat::_Ord::greater);
160
161 inline constexpr partial_ordering
162 partial_ordering::unordered(__cmp_cat::_Ncmp::_Unordered);
163
164 class weak_ordering
165 {
166 __cmp_cat::type _M_value;
167
168 constexpr explicit
169 weak_ordering(__cmp_cat::_Ord __v) noexcept : _M_value(__cmp_cat::type(__v))
170 { }
171
172 friend class strong_ordering;
173
174 public:
175 // valid values
176 static const weak_ordering less;
177 static const weak_ordering equivalent;
178 static const weak_ordering greater;
179
180 [[nodiscard]]
181 constexpr operator partial_ordering() const noexcept
182 { return partial_ordering(__cmp_cat::_Ord(_M_value)); }
183
184 // comparisons
185 [[nodiscard]]
186 friend constexpr bool
187 operator==(weak_ordering __v, __cmp_cat::__unspec) noexcept
188 { return __v._M_value == 0; }
189
190 [[nodiscard]]
191 friend constexpr bool
192 operator==(weak_ordering, weak_ordering) noexcept = default;
193
194 [[nodiscard]]
195 friend constexpr bool
196 operator< (weak_ordering __v, __cmp_cat::__unspec) noexcept
197 { return __v._M_value < 0; }
198
199 [[nodiscard]]
200 friend constexpr bool
201 operator> (weak_ordering __v, __cmp_cat::__unspec) noexcept
202 { return __v._M_value > 0; }
203
204 [[nodiscard]]
205 friend constexpr bool
206 operator<=(weak_ordering __v, __cmp_cat::__unspec) noexcept
207 { return __v._M_value <= 0; }
208
209 [[nodiscard]]
210 friend constexpr bool
211 operator>=(weak_ordering __v, __cmp_cat::__unspec) noexcept
212 { return __v._M_value >= 0; }
213
214 [[nodiscard]]
215 friend constexpr bool
216 operator< (__cmp_cat::__unspec, weak_ordering __v) noexcept
217 { return 0 < __v._M_value; }
218
219 [[nodiscard]]
220 friend constexpr bool
221 operator> (__cmp_cat::__unspec, weak_ordering __v) noexcept
222 { return 0 > __v._M_value; }
223
224 [[nodiscard]]
225 friend constexpr bool
226 operator<=(__cmp_cat::__unspec, weak_ordering __v) noexcept
227 { return 0 <= __v._M_value; }
228
229 [[nodiscard]]
230 friend constexpr bool
231 operator>=(__cmp_cat::__unspec, weak_ordering __v) noexcept
232 { return 0 >= __v._M_value; }
233
234 [[nodiscard]]
235 friend constexpr weak_ordering
236 operator<=>(weak_ordering __v, __cmp_cat::__unspec) noexcept
237 { return __v; }
238
239 [[nodiscard]]
240 friend constexpr weak_ordering
241 operator<=>(__cmp_cat::__unspec, weak_ordering __v) noexcept
242 { return weak_ordering(__cmp_cat::_Ord(-__v._M_value)); }
243 };
244
245 // valid values' definitions
246 inline constexpr weak_ordering
247 weak_ordering::less(__cmp_cat::_Ord::less);
248
249 inline constexpr weak_ordering
250 weak_ordering::equivalent(__cmp_cat::_Ord::equivalent);
251
252 inline constexpr weak_ordering
253 weak_ordering::greater(__cmp_cat::_Ord::greater);
254
255 class strong_ordering
256 {
257 __cmp_cat::type _M_value;
258
259 constexpr explicit
260 strong_ordering(__cmp_cat::_Ord __v) noexcept
261 : _M_value(__cmp_cat::type(__v))
262 { }
263
264 public:
265 // valid values
266 static const strong_ordering less;
267 static const strong_ordering equal;
268 static const strong_ordering equivalent;
269 static const strong_ordering greater;
270
271 [[nodiscard]]
272 constexpr operator partial_ordering() const noexcept
273 { return partial_ordering(__cmp_cat::_Ord(_M_value)); }
274
275 [[nodiscard]]
276 constexpr operator weak_ordering() const noexcept
277 { return weak_ordering(__cmp_cat::_Ord(_M_value)); }
278
279 // comparisons
280 [[nodiscard]]
281 friend constexpr bool
282 operator==(strong_ordering __v, __cmp_cat::__unspec) noexcept
283 { return __v._M_value == 0; }
284
285 [[nodiscard]]
286 friend constexpr bool
287 operator==(strong_ordering, strong_ordering) noexcept = default;
288
289 [[nodiscard]]
290 friend constexpr bool
291 operator< (strong_ordering __v, __cmp_cat::__unspec) noexcept
292 { return __v._M_value < 0; }
293
294 [[nodiscard]]
295 friend constexpr bool
296 operator> (strong_ordering __v, __cmp_cat::__unspec) noexcept
297 { return __v._M_value > 0; }
298
299 [[nodiscard]]
300 friend constexpr bool
301 operator<=(strong_ordering __v, __cmp_cat::__unspec) noexcept
302 { return __v._M_value <= 0; }
303
304 [[nodiscard]]
305 friend constexpr bool
306 operator>=(strong_ordering __v, __cmp_cat::__unspec) noexcept
307 { return __v._M_value >= 0; }
308
309 [[nodiscard]]
310 friend constexpr bool
311 operator< (__cmp_cat::__unspec, strong_ordering __v) noexcept
312 { return 0 < __v._M_value; }
313
314 [[nodiscard]]
315 friend constexpr bool
316 operator> (__cmp_cat::__unspec, strong_ordering __v) noexcept
317 { return 0 > __v._M_value; }
318
319 [[nodiscard]]
320 friend constexpr bool
321 operator<=(__cmp_cat::__unspec, strong_ordering __v) noexcept
322 { return 0 <= __v._M_value; }
323
324 [[nodiscard]]
325 friend constexpr bool
326 operator>=(__cmp_cat::__unspec, strong_ordering __v) noexcept
327 { return 0 >= __v._M_value; }
328
329 [[nodiscard]]
330 friend constexpr strong_ordering
331 operator<=>(strong_ordering __v, __cmp_cat::__unspec) noexcept
332 { return __v; }
333
334 [[nodiscard]]
335 friend constexpr strong_ordering
336 operator<=>(__cmp_cat::__unspec, strong_ordering __v) noexcept
337 { return strong_ordering(__cmp_cat::_Ord(-__v._M_value)); }
338 };
339
340 // valid values' definitions
341 inline constexpr strong_ordering
342 strong_ordering::less(__cmp_cat::_Ord::less);
343
344 inline constexpr strong_ordering
345 strong_ordering::equal(__cmp_cat::_Ord::equivalent);
346
347 inline constexpr strong_ordering
348 strong_ordering::equivalent(__cmp_cat::_Ord::equivalent);
349
350 inline constexpr strong_ordering
351 strong_ordering::greater(__cmp_cat::_Ord::greater);
352
353
354 // named comparison functions
355 [[nodiscard]]
356 constexpr bool
357 is_eq(partial_ordering __cmp) noexcept
358 { return __cmp == 0; }
359
360 [[nodiscard]]
361 constexpr bool
362 is_neq(partial_ordering __cmp) noexcept
363 { return __cmp != 0; }
364
365 [[nodiscard]]
366 constexpr bool
367 is_lt (partial_ordering __cmp) noexcept
368 { return __cmp < 0; }
369
370 [[nodiscard]]
371 constexpr bool
372 is_lteq(partial_ordering __cmp) noexcept
373 { return __cmp <= 0; }
374
375 [[nodiscard]]
376 constexpr bool
377 is_gt (partial_ordering __cmp) noexcept
378 { return __cmp > 0; }
379
380 [[nodiscard]]
381 constexpr bool
382 is_gteq(partial_ordering __cmp) noexcept
383 { return __cmp >= 0; }
384
385 namespace __detail
386 {
387 template<typename _Tp>
388 inline constexpr unsigned __cmp_cat_id = 1;
389 template<>
390 inline constexpr unsigned __cmp_cat_id<partial_ordering> = 2;
391 template<>
392 inline constexpr unsigned __cmp_cat_id<weak_ordering> = 4;
393 template<>
394 inline constexpr unsigned __cmp_cat_id<strong_ordering> = 8;
395
396 template<typename... _Ts>
397 constexpr auto __common_cmp_cat()
398 {
399 constexpr unsigned __cats = (__cmp_cat_id<_Ts> | ...);
400 // If any Ti is not a comparison category type, U is void.
401 if constexpr (__cats & 1)
402 return;
403 // Otherwise, if at least one Ti is std::partial_ordering,
404 // U is std::partial_ordering.
405 else if constexpr (bool(__cats & __cmp_cat_id<partial_ordering>))
406 return partial_ordering::equivalent;
407 // Otherwise, if at least one Ti is std::weak_ordering,
408 // U is std::weak_ordering.
409 else if constexpr (bool(__cats & __cmp_cat_id<weak_ordering>))
410 return weak_ordering::equivalent;
411 // Otherwise, U is std::strong_ordering.
412 else
413 return strong_ordering::equivalent;
414 }
415 } // namespace __detail
416
417 // [cmp.common], common comparison category type
418 template<typename... _Ts>
419 struct common_comparison_category
420 {
421 using type = decltype(__detail::__common_cmp_cat<_Ts...>());
422 };
423
424 // Partial specializations for one and zero argument cases.
425
426 template<typename _Tp>
427 struct common_comparison_category<_Tp>
428 { using type = void; };
429
430 template<>
431 struct common_comparison_category<partial_ordering>
432 { using type = partial_ordering; };
433
434 template<>
435 struct common_comparison_category<weak_ordering>
436 { using type = weak_ordering; };
437
438 template<>
439 struct common_comparison_category<strong_ordering>
440 { using type = strong_ordering; };
441
442 template<>
443 struct common_comparison_category<>
444 { using type = strong_ordering; };
445
446 template<typename... _Ts>
447 using common_comparison_category_t
448 = typename common_comparison_category<_Ts...>::type;
449
450#if __cpp_lib_three_way_comparison >= 201907L
451 // C++ >= 20 && impl_3way_comparison >= 201907 && lib_concepts
452 namespace __detail
453 {
454 template<typename _Tp, typename _Cat>
455 concept __compares_as
456 = same_as<common_comparison_category_t<_Tp, _Cat>, _Cat>;
457 } // namespace __detail
458
459 // [cmp.concept], concept three_way_comparable
460 template<typename _Tp, typename _Cat = partial_ordering>
461 concept three_way_comparable
462 = __detail::__weakly_eq_cmp_with<_Tp, _Tp>
463 && __detail::__partially_ordered_with<_Tp, _Tp>
464 && requires(const remove_reference_t<_Tp>& __a,
465 const remove_reference_t<_Tp>& __b)
466 {
467 { __a <=> __b } -> __detail::__compares_as<_Cat>;
468 };
469
470 template<typename _Tp, typename _Up, typename _Cat = partial_ordering>
471 concept three_way_comparable_with
472 = three_way_comparable<_Tp, _Cat>
473 && three_way_comparable<_Up, _Cat>
474 && common_reference_with<const remove_reference_t<_Tp>&,
476 && three_way_comparable<
478 const remove_reference_t<_Up>&>, _Cat>
479 && __detail::__weakly_eq_cmp_with<_Tp, _Up>
480 && __detail::__partially_ordered_with<_Tp, _Up>
481 && requires(const remove_reference_t<_Tp>& __t,
482 const remove_reference_t<_Up>& __u)
483 {
484 { __t <=> __u } -> __detail::__compares_as<_Cat>;
485 { __u <=> __t } -> __detail::__compares_as<_Cat>;
486 };
487
488 namespace __detail
489 {
490 template<typename _Tp, typename _Up>
491 using __cmp3way_res_t
492 = decltype(std::declval<_Tp>() <=> std::declval<_Up>());
493
494 // Implementation of std::compare_three_way_result.
495 // It is undefined for a program to add specializations of
496 // std::compare_three_way_result, so the std::compare_three_way_result_t
497 // alias ignores std::compare_three_way_result and uses
498 // __detail::__cmp3way_res_impl directly instead.
499 template<typename _Tp, typename _Up>
500 struct __cmp3way_res_impl
501 { };
502
503 template<typename _Tp, typename _Up>
504 requires requires { typename __cmp3way_res_t<__cref<_Tp>, __cref<_Up>>; }
505 struct __cmp3way_res_impl<_Tp, _Up>
506 {
507 using type = __cmp3way_res_t<__cref<_Tp>, __cref<_Up>>;
508 };
509 } // namespace __detail
510
511 /// [cmp.result], result of three-way comparison
512 template<typename _Tp, typename _Up = _Tp>
514 : __detail::__cmp3way_res_impl<_Tp, _Up>
515 { };
516
517 /// [cmp.result], result of three-way comparison
518 template<typename _Tp, typename _Up = _Tp>
520 = typename __detail::__cmp3way_res_impl<_Tp, _Up>::type;
521
522 namespace __detail
523 {
524 // BUILTIN-PTR-THREE-WAY(T, U)
525 // This determines whether t <=> u results in a call to a built-in
526 // operator<=> comparing pointers. It doesn't work for function pointers
527 // (PR 93628).
528 template<typename _Tp, typename _Up>
529 concept __3way_builtin_ptr_cmp
530 = requires(_Tp&& __t, _Up&& __u)
531 { static_cast<_Tp&&>(__t) <=> static_cast<_Up&&>(__u); }
532 && convertible_to<_Tp, const volatile void*>
533 && convertible_to<_Up, const volatile void*>
534 && __not_overloaded_spaceship<_Tp, _Up>;
535 } // namespace __detail
536
537 // _GLIBCXX_RESOLVE_LIB_DEFECTS
538 // 3530 BUILTIN-PTR-MEOW should not opt the type out of syntactic checks
539
540 // [cmp.object], typename compare_three_way
541 struct compare_three_way
542 {
543 template<typename _Tp, typename _Up>
544 requires three_way_comparable_with<_Tp, _Up>
545 constexpr auto
546 operator() [[nodiscard]] (_Tp&& __t, _Up&& __u) const
547 noexcept(noexcept(std::declval<_Tp>() <=> std::declval<_Up>()))
548 {
549 if constexpr (__detail::__3way_builtin_ptr_cmp<_Tp, _Up>)
550 {
551 auto __pt = static_cast<const volatile void*>(__t);
552 auto __pu = static_cast<const volatile void*>(__u);
553 if (std::__is_constant_evaluated())
554 return __pt <=> __pu;
555 auto __it = reinterpret_cast<__UINTPTR_TYPE__>(__pt);
556 auto __iu = reinterpret_cast<__UINTPTR_TYPE__>(__pu);
557 return __it <=> __iu;
558 }
559 else
560 return static_cast<_Tp&&>(__t) <=> static_cast<_Up&&>(__u);
561 }
562
563 using is_transparent = void;
564 };
565
566 /// @cond undocumented
567 // Namespace for helpers for the <compare> customization points.
568 namespace __compare
569 {
570 template<floating_point _Tp>
571 constexpr weak_ordering
572 __fp_weak_ordering(_Tp __e, _Tp __f)
573 {
574 // Returns an integer with the same sign as the argument, and magnitude
575 // indicating the classification: zero=1 subnorm=2 norm=3 inf=4 nan=5
576 auto __cat = [](_Tp __fp) -> int {
577 const int __sign = __builtin_signbit(__fp) ? -1 : 1;
578 if (__builtin_isnormal(__fp))
579 return (__fp == 0 ? 1 : 3) * __sign;
580 if (__builtin_isnan(__fp))
581 return 5 * __sign;
582 if (int __inf = __builtin_isinf_sign(__fp))
583 return 4 * __inf;
584 return 2 * __sign;
585 };
586
587 auto __po = __e <=> __f;
588 if (is_lt(__po))
589 return weak_ordering::less;
590 else if (is_gt(__po))
591 return weak_ordering::greater;
592 else if (__po == partial_ordering::equivalent)
593 return weak_ordering::equivalent;
594 else // unordered, at least one argument is NaN
595 {
596 // return -1 for negative nan, +1 for positive nan, 0 otherwise.
597 auto __isnan_sign = [](_Tp __fp) -> int {
598 return __builtin_isnan(__fp)
599 ? __builtin_signbit(__fp) ? -1 : 1
600 : 0;
601 };
602 auto __ord = __isnan_sign(__e) <=> __isnan_sign(__f);
603 if (is_eq(__ord))
604 return weak_ordering::equivalent;
605 else if (is_lt(__ord))
606 return weak_ordering::less;
607 else
608 return weak_ordering::greater;
609 }
610 }
611
612 void strong_order() = delete;
613
614 template<typename _Tp, typename _Up>
615 concept __adl_strong = requires(_Tp&& __t, _Up&& __u)
616 {
617 strong_ordering(strong_order(static_cast<_Tp&&>(__t),
618 static_cast<_Up&&>(__u)));
619 };
620
621 void weak_order() = delete;
622
623 template<typename _Tp, typename _Up>
624 concept __adl_weak = requires(_Tp&& __t, _Up&& __u)
625 {
626 weak_ordering(weak_order(static_cast<_Tp&&>(__t),
627 static_cast<_Up&&>(__u)));
628 };
629
630 void partial_order() = delete;
631
632 template<typename _Tp, typename _Up>
633 concept __adl_partial = requires(_Tp&& __t, _Up&& __u)
634 {
635 partial_ordering(partial_order(static_cast<_Tp&&>(__t),
636 static_cast<_Up&&>(__u)));
637 };
638
639 template<typename _Ord, typename _Tp, typename _Up>
640 concept __cmp3way = requires(_Tp&& __t, _Up&& __u, compare_three_way __c)
641 {
642 _Ord(__c(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)));
643 };
644
645 template<typename _Tp, typename _Up>
646 concept __strongly_ordered
647 = __adl_strong<_Tp, _Up>
648 || floating_point<remove_reference_t<_Tp>>
649 || __cmp3way<strong_ordering, _Tp, _Up>;
650
651 template<typename _Tp, typename _Up>
652 concept __decayed_same_as = same_as<decay_t<_Tp>, decay_t<_Up>>;
653
654 class _Strong_order
655 {
656 template<typename _Tp, typename _Up>
657 static constexpr bool
658 _S_noexcept()
659 {
660 if constexpr (floating_point<decay_t<_Tp>>)
661 return true;
662 else if constexpr (__adl_strong<_Tp, _Up>)
663 return noexcept(strong_ordering(strong_order(std::declval<_Tp>(),
665 else if constexpr (__cmp3way<strong_ordering, _Tp, _Up>)
666 return noexcept(compare_three_way()(std::declval<_Tp>(),
668 }
669
670 friend class _Weak_order;
671 friend class _Strong_fallback;
672
673 // Names for the supported floating-point representations.
674 enum class _Fp_fmt
675 {
676 _Binary16, _Binary32, _Binary64, _Binary128, // IEEE
677 _X86_80bit, // x86 80-bit extended precision
678 _M68k_80bit, // m68k 80-bit extended precision
679 _Dbldbl, // IBM 128-bit double-double
680 _Bfloat16, // std::bfloat16_t
681 };
682
683#ifndef __cpp_using_enum
684 // XXX Remove these once 'using enum' support is ubiquitous.
685 static constexpr _Fp_fmt _Binary16 = _Fp_fmt::_Binary16;
686 static constexpr _Fp_fmt _Binary32 = _Fp_fmt::_Binary32;
687 static constexpr _Fp_fmt _Binary64 = _Fp_fmt::_Binary64;
688 static constexpr _Fp_fmt _Binary128 = _Fp_fmt::_Binary128;
689 static constexpr _Fp_fmt _X86_80bit = _Fp_fmt::_X86_80bit;
690 static constexpr _Fp_fmt _M68k_80bit = _Fp_fmt::_M68k_80bit;
691 static constexpr _Fp_fmt _Dbldbl = _Fp_fmt::_Dbldbl;
692 static constexpr _Fp_fmt _Bfloat16 = _Fp_fmt::_Bfloat16;
693#endif
694
695 // Identify the format used by a floating-point type.
696 template<typename _Tp>
697 static consteval _Fp_fmt
698 _S_fp_fmt() noexcept
699 {
700#ifdef __cpp_using_enum
701 using enum _Fp_fmt;
702#endif
703
704 // Identify these formats first, then assume anything else is IEEE.
705 // N.B. ARM __fp16 alternative format can be handled as binary16.
706
707#ifdef __LONG_DOUBLE_IBM128__
708 if constexpr (__is_same(_Tp, long double))
709 return _Dbldbl;
710#elif defined __LONG_DOUBLE_IEEE128__ && defined __SIZEOF_IBM128__
711 if constexpr (__is_same(_Tp, __ibm128))
712 return _Dbldbl;
713#endif
714
715#if __LDBL_MANT_DIG__ == 64
716 if constexpr (__is_same(_Tp, long double))
717 return __LDBL_MIN_EXP__ == -16381 ? _X86_80bit : _M68k_80bit;
718#endif
719#ifdef __SIZEOF_FLOAT80__
720 if constexpr (__is_same(_Tp, __float80))
721 return _X86_80bit;
722#endif
723#ifdef __STDCPP_BFLOAT16_T__
724 if constexpr (__is_same(_Tp, decltype(0.0bf16)))
725 return _Bfloat16;
726#endif
727
728 constexpr int __width = sizeof(_Tp) * __CHAR_BIT__;
729
730 if constexpr (__width == 16) // IEEE binary16 (or ARM fp16).
731 return _Binary16;
732 else if constexpr (__width == 32) // IEEE binary32
733 return _Binary32;
734 else if constexpr (__width == 64) // IEEE binary64
735 return _Binary64;
736 else if constexpr (__width == 128) // IEEE binary128
737 return _Binary128;
738 }
739
740 // So we don't need to include <stdint.h> and pollute the namespace.
741 using int64_t = __INT64_TYPE__;
742 using int32_t = __INT32_TYPE__;
743 using int16_t = __INT16_TYPE__;
744 using uint64_t = __UINT64_TYPE__;
745 using uint16_t = __UINT16_TYPE__;
746
747 // Used to unpack floating-point types that do not fit into an integer.
748 template<typename _Tp>
749 struct _Int
750 {
751#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
752 uint64_t _M_lo;
753 _Tp _M_hi;
754#else
755 _Tp _M_hi;
756 uint64_t _M_lo;
757#endif
758
759 constexpr explicit
760 _Int(_Tp __hi, uint64_t __lo) noexcept : _M_hi(__hi)
761 { _M_lo = __lo; }
762
763 constexpr explicit
764 _Int(uint64_t __lo) noexcept : _M_hi(0)
765 { _M_lo = __lo; }
766
767 constexpr bool operator==(const _Int&) const = default;
768
769#if defined __hppa__ || (defined __mips__ && !defined __mips_nan2008)
770 consteval _Int
771 operator<<(int __n) const noexcept
772 {
773 // XXX this assumes n >= 64, which is true for the use below.
774 return _Int(static_cast<_Tp>(_M_lo << (__n - 64)), 0);
775 }
776#endif
777
778 constexpr _Int&
779 operator^=(const _Int& __rhs) noexcept
780 {
781 _M_hi ^= __rhs._M_hi;
782 _M_lo ^= __rhs._M_lo;
783 return *this;
784 }
785
786 constexpr strong_ordering
787 operator<=>(const _Int& __rhs) const noexcept
788 {
789 strong_ordering __cmp = _M_hi <=> __rhs._M_hi;
790 if (__cmp != strong_ordering::equal)
791 return __cmp;
792 return _M_lo <=> __rhs._M_lo;
793 }
794 };
795
796 template<typename _Tp>
797 static constexpr _Tp
798 _S_compl(_Tp __t) noexcept
799 {
800 constexpr int __width = sizeof(_Tp) * __CHAR_BIT__;
801 // Sign extend to get all ones or all zeros.
802 make_unsigned_t<_Tp> __sign = __t >> (__width - 1);
803 // If the sign bit was set, this flips all bits below it.
804 // This converts ones' complement to two's complement.
805 return __t ^ (__sign >> 1);
806 }
807
808 // As above but works on both parts of _Int<T>.
809 template<typename _Tp>
810 static constexpr _Int<_Tp>
811 _S_compl(_Int<_Tp> __t) noexcept
812 {
813 constexpr int __width = sizeof(_Tp) * __CHAR_BIT__;
814 make_unsigned_t<_Tp> __sign = __t._M_hi >> (__width - 1);
815 __t._M_hi ^= (__sign >> 1 );
816 uint64_t __sign64 = (_Tp)__sign;
817 __t._M_lo ^= __sign64;
818 return __t;
819 }
820
821 // Bit-cast a floating-point value to an unsigned integer.
822 template<typename _Tp>
823 constexpr static auto
824 _S_fp_bits(_Tp __val) noexcept
825 {
826 if constexpr (sizeof(_Tp) == sizeof(int64_t))
827 return __builtin_bit_cast(int64_t, __val);
828 else if constexpr (sizeof(_Tp) == sizeof(int32_t))
829 return __builtin_bit_cast(int32_t, __val);
830 else if constexpr (sizeof(_Tp) == sizeof(int16_t))
831 return __builtin_bit_cast(int16_t, __val);
832 else
833 {
834#ifdef __cpp_using_enum
835 using enum _Fp_fmt;
836#endif
837 constexpr auto __fmt = _S_fp_fmt<_Tp>();
838 if constexpr (__fmt == _X86_80bit || __fmt == _M68k_80bit)
839 {
840 if constexpr (sizeof(_Tp) == 3 * sizeof(int32_t))
841 {
842 auto __ival = __builtin_bit_cast(_Int<int32_t>, __val);
843 return _Int<int16_t>(__ival._M_hi, __ival._M_lo);
844 }
845 else
846 {
847 auto __ival = __builtin_bit_cast(_Int<int64_t>, __val);
848 return _Int<int16_t>(__ival._M_hi, __ival._M_lo);
849 }
850 }
851 else if constexpr (sizeof(_Tp) == 2 * sizeof(int64_t))
852 {
853#if __SIZEOF_INT128__
854 return __builtin_bit_cast(__int128, __val);
855#else
856 return __builtin_bit_cast(_Int<int64_t>, __val);
857#endif
858 }
859 else
860 static_assert(sizeof(_Tp) == sizeof(int64_t),
861 "unsupported floating-point type");
862 }
863 }
864
865 template<typename _Tp>
866 static constexpr strong_ordering
867 _S_fp_cmp(_Tp __x, _Tp __y) noexcept
868 {
869#ifdef __vax__
870 if (__builtin_isnan(__x) || __builtin_isnan(__y))
871 {
872 int __ix = (bool) __builtin_isnan(__x);
873 int __iy = (bool) __builtin_isnan(__y);
874 __ix *= __builtin_signbit(__x) ? -1 : 1;
875 __iy *= __builtin_signbit(__y) ? -1 : 1;
876 return __ix <=> __iy;
877 }
878 else
879 return __builtin_bit_cast(strong_ordering, __x <=> __y);
880#endif
881
882 auto __ix = _S_fp_bits(__x);
883 auto __iy = _S_fp_bits(__y);
884
885 if (__ix == __iy)
886 return strong_ordering::equal; // All bits are equal, we're done.
887
888#ifdef __cpp_using_enum
889 using enum _Fp_fmt;
890#endif
891 constexpr auto __fmt = _S_fp_fmt<_Tp>();
892
893 if constexpr (__fmt == _Dbldbl) // double-double
894 {
895 // Unpack the double-double into two parts.
896 // We never inspect the low double as a double, cast to integer.
897 struct _Unpacked { double _M_hi; int64_t _M_lo; };
898 auto __x2 = __builtin_bit_cast(_Unpacked, __x);
899 auto __y2 = __builtin_bit_cast(_Unpacked, __y);
900
901 // Compare the high doubles first and use result if unequal.
902 auto __cmp = _S_fp_cmp(__x2._M_hi, __y2._M_hi);
903 if (__cmp != strong_ordering::equal)
904 return __cmp;
905
906 // For NaN the low double is unused, so if the high doubles
907 // are the same NaN, we don't need to compare the low doubles.
908 if (__builtin_isnan(__x2._M_hi))
909 return strong_ordering::equal;
910 // Similarly, if the low doubles are +zero or -zero (which is
911 // true for all infinities and some other values), we're done.
912 if (((__x2._M_lo | __y2._M_lo) & 0x7fffffffffffffffULL) == 0)
913 return strong_ordering::equal;
914
915 // Otherwise, compare the low parts.
916 return _S_compl(__x2._M_lo) <=> _S_compl(__y2._M_lo);
917 }
918 else
919 {
920 if constexpr (__fmt == _M68k_80bit)
921 {
922 // For m68k the MSB of the significand is ignored for the
923 // greatest exponent, so either 0 or 1 is valid there.
924 // Set it before comparing, so that we never have 0 there.
925 constexpr uint16_t __maxexp = 0x7fff;
926 if ((__ix._M_hi & __maxexp) == __maxexp)
927 __ix._M_lo |= 1ull << 63;
928 if ((__iy._M_hi & __maxexp) == __maxexp)
929 __iy._M_lo |= 1ull << 63;
930 }
931 else
932 {
933#if defined __hppa__ || (defined __mips__ && !defined __mips_nan2008)
934 // IEEE 754-1985 allowed the meaning of the quiet/signaling
935 // bit to be reversed. Flip that to give desired ordering.
936 if (__builtin_isnan(__x) && __builtin_isnan(__y))
937 {
938 using _Int = decltype(__ix);
939
940 constexpr int __nantype = __fmt == _Binary32 ? 22
941 : __fmt == _Binary64 ? 51
942 : __fmt == _Binary128 ? 111
943 : -1;
944 constexpr _Int __bit = _Int(1) << __nantype;
945 __ix ^= __bit;
946 __iy ^= __bit;
947 }
948#endif
949 }
950 return _S_compl(__ix) <=> _S_compl(__iy);
951 }
952 }
953
954 public:
955 template<typename _Tp, __decayed_same_as<_Tp> _Up>
956 requires __strongly_ordered<_Tp, _Up>
957 constexpr strong_ordering
958 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
959 noexcept(_S_noexcept<_Tp, _Up>())
960 {
961 if constexpr (floating_point<decay_t<_Tp>>)
962 return _S_fp_cmp(__e, __f);
963 else if constexpr (__adl_strong<_Tp, _Up>)
964 return strong_ordering(strong_order(static_cast<_Tp&&>(__e),
965 static_cast<_Up&&>(__f)));
966 else if constexpr (__cmp3way<strong_ordering, _Tp, _Up>)
967 return compare_three_way()(static_cast<_Tp&&>(__e),
968 static_cast<_Up&&>(__f));
969 }
970 };
971
972 template<typename _Tp, typename _Up>
973 concept __weakly_ordered
974 = floating_point<remove_reference_t<_Tp>>
975 || __adl_weak<_Tp, _Up>
976 || __cmp3way<weak_ordering, _Tp, _Up>
977 || __strongly_ordered<_Tp, _Up>;
978
979 class _Weak_order
980 {
981 template<typename _Tp, typename _Up>
982 static constexpr bool
983 _S_noexcept()
984 {
985 if constexpr (floating_point<decay_t<_Tp>>)
986 return true;
987 else if constexpr (__adl_weak<_Tp, _Up>)
988 return noexcept(weak_ordering(weak_order(std::declval<_Tp>(),
990 else if constexpr (__cmp3way<weak_ordering, _Tp, _Up>)
991 return noexcept(compare_three_way()(std::declval<_Tp>(),
993 else if constexpr (__strongly_ordered<_Tp, _Up>)
994 return _Strong_order::_S_noexcept<_Tp, _Up>();
995 }
996
997 friend class _Partial_order;
998 friend class _Weak_fallback;
999
1000 public:
1001 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1002 requires __weakly_ordered<_Tp, _Up>
1003 constexpr weak_ordering
1004 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1005 noexcept(_S_noexcept<_Tp, _Up>())
1006 {
1007 if constexpr (floating_point<decay_t<_Tp>>)
1008 return __compare::__fp_weak_ordering(__e, __f);
1009 else if constexpr (__adl_weak<_Tp, _Up>)
1010 return weak_ordering(weak_order(static_cast<_Tp&&>(__e),
1011 static_cast<_Up&&>(__f)));
1012 else if constexpr (__cmp3way<weak_ordering, _Tp, _Up>)
1013 return compare_three_way()(static_cast<_Tp&&>(__e),
1014 static_cast<_Up&&>(__f));
1015 else if constexpr (__strongly_ordered<_Tp, _Up>)
1016 return _Strong_order{}(static_cast<_Tp&&>(__e),
1017 static_cast<_Up&&>(__f));
1018 }
1019 };
1020
1021 template<typename _Tp, typename _Up>
1022 concept __partially_ordered
1023 = __adl_partial<_Tp, _Up>
1024 || __cmp3way<partial_ordering, _Tp, _Up>
1025 || __weakly_ordered<_Tp, _Up>;
1026
1027 class _Partial_order
1028 {
1029 template<typename _Tp, typename _Up>
1030 static constexpr bool
1031 _S_noexcept()
1032 {
1033 if constexpr (__adl_partial<_Tp, _Up>)
1034 return noexcept(partial_ordering(partial_order(std::declval<_Tp>(),
1035 std::declval<_Up>())));
1036 else if constexpr (__cmp3way<partial_ordering, _Tp, _Up>)
1037 return noexcept(compare_three_way()(std::declval<_Tp>(),
1039 else if constexpr (__weakly_ordered<_Tp, _Up>)
1040 return _Weak_order::_S_noexcept<_Tp, _Up>();
1041 }
1042
1043 friend class _Partial_fallback;
1044
1045 public:
1046 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1047 requires __partially_ordered<_Tp, _Up>
1048 constexpr partial_ordering
1049 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1050 noexcept(_S_noexcept<_Tp, _Up>())
1051 {
1052 if constexpr (__adl_partial<_Tp, _Up>)
1053 return partial_ordering(partial_order(static_cast<_Tp&&>(__e),
1054 static_cast<_Up&&>(__f)));
1055 else if constexpr (__cmp3way<partial_ordering, _Tp, _Up>)
1056 return compare_three_way()(static_cast<_Tp&&>(__e),
1057 static_cast<_Up&&>(__f));
1058 else if constexpr (__weakly_ordered<_Tp, _Up>)
1059 return _Weak_order{}(static_cast<_Tp&&>(__e),
1060 static_cast<_Up&&>(__f));
1061 }
1062 };
1063
1064 template<typename _Tp, typename _Up>
1065 concept __op_eq_lt = requires(_Tp&& __t, _Up&& __u)
1066 {
1067 { static_cast<_Tp&&>(__t) == static_cast<_Up&&>(__u) }
1068 -> convertible_to<bool>;
1069 { static_cast<_Tp&&>(__t) < static_cast<_Up&&>(__u) }
1070 -> convertible_to<bool>;
1071 };
1072
1073 class _Strong_fallback
1074 {
1075 template<typename _Tp, typename _Up>
1076 static constexpr bool
1077 _S_noexcept()
1078 {
1079 if constexpr (__strongly_ordered<_Tp, _Up>)
1080 return _Strong_order::_S_noexcept<_Tp, _Up>();
1081 else
1082 return noexcept(bool(std::declval<_Tp>() == std::declval<_Up>()))
1083 && noexcept(bool(std::declval<_Tp>() < std::declval<_Up>()));
1084 }
1085
1086 public:
1087 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1088 requires __strongly_ordered<_Tp, _Up> || __op_eq_lt<_Tp, _Up>
1089 constexpr strong_ordering
1090 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1091 noexcept(_S_noexcept<_Tp, _Up>())
1092 {
1093 if constexpr (__strongly_ordered<_Tp, _Up>)
1094 return _Strong_order{}(static_cast<_Tp&&>(__e),
1095 static_cast<_Up&&>(__f));
1096 else // __op_eq_lt<_Tp, _Up>
1097 return static_cast<_Tp&&>(__e) == static_cast<_Up&&>(__f)
1098 ? strong_ordering::equal
1099 : static_cast<_Tp&&>(__e) < static_cast<_Up&&>(__f)
1100 ? strong_ordering::less
1101 : strong_ordering::greater;
1102 }
1103 };
1104
1105 class _Weak_fallback
1106 {
1107 template<typename _Tp, typename _Up>
1108 static constexpr bool
1109 _S_noexcept()
1110 {
1111 if constexpr (__weakly_ordered<_Tp, _Up>)
1112 return _Weak_order::_S_noexcept<_Tp, _Up>();
1113 else
1114 return noexcept(bool(std::declval<_Tp>() == std::declval<_Up>()))
1115 && noexcept(bool(std::declval<_Tp>() < std::declval<_Up>()));
1116 }
1117
1118 public:
1119 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1120 requires __weakly_ordered<_Tp, _Up> || __op_eq_lt<_Tp, _Up>
1121 constexpr weak_ordering
1122 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1123 noexcept(_S_noexcept<_Tp, _Up>())
1124 {
1125 if constexpr (__weakly_ordered<_Tp, _Up>)
1126 return _Weak_order{}(static_cast<_Tp&&>(__e),
1127 static_cast<_Up&&>(__f));
1128 else // __op_eq_lt<_Tp, _Up>
1129 return static_cast<_Tp&&>(__e) == static_cast<_Up&&>(__f)
1130 ? weak_ordering::equivalent
1131 : static_cast<_Tp&&>(__e) < static_cast<_Up&&>(__f)
1132 ? weak_ordering::less
1133 : weak_ordering::greater;
1134 }
1135 };
1136
1137 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1138 // 3465. compare_partial_order_fallback requires F < E
1139 template<typename _Tp, typename _Up>
1140 concept __op_eq_lt_lt = __op_eq_lt<_Tp, _Up>
1141 && requires(_Tp&& __t, _Up&& __u)
1142 {
1143 { static_cast<_Up&&>(__u) < static_cast<_Tp&&>(__t) }
1144 -> convertible_to<bool>;
1145 };
1146
1147 class _Partial_fallback
1148 {
1149 template<typename _Tp, typename _Up>
1150 static constexpr bool
1151 _S_noexcept()
1152 {
1153 if constexpr (__partially_ordered<_Tp, _Up>)
1154 return _Partial_order::_S_noexcept<_Tp, _Up>();
1155 else
1156 return noexcept(bool(std::declval<_Tp>() == std::declval<_Up>()))
1157 && noexcept(bool(std::declval<_Tp>() < std::declval<_Up>()));
1158 }
1159
1160 public:
1161 template<typename _Tp, __decayed_same_as<_Tp> _Up>
1162 requires __partially_ordered<_Tp, _Up> || __op_eq_lt_lt<_Tp, _Up>
1163 constexpr partial_ordering
1164 operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
1165 noexcept(_S_noexcept<_Tp, _Up>())
1166 {
1167 if constexpr (__partially_ordered<_Tp, _Up>)
1168 return _Partial_order{}(static_cast<_Tp&&>(__e),
1169 static_cast<_Up&&>(__f));
1170 else // __op_eq_lt_lt<_Tp, _Up>
1171 return static_cast<_Tp&&>(__e) == static_cast<_Up&&>(__f)
1172 ? partial_ordering::equivalent
1173 : static_cast<_Tp&&>(__e) < static_cast<_Up&&>(__f)
1174 ? partial_ordering::less
1175 : static_cast<_Up&&>(__f) < static_cast<_Tp&&>(__e)
1176 ? partial_ordering::greater
1177 : partial_ordering::unordered;
1178 }
1179 };
1180 } // namespace @endcond
1181
1182 // [cmp.alg], comparison algorithms
1183
1184 inline namespace _Cpo
1185 {
1186 inline constexpr __compare::_Strong_order strong_order{};
1187
1188 inline constexpr __compare::_Weak_order weak_order{};
1189
1190 inline constexpr __compare::_Partial_order partial_order{};
1191
1192 inline constexpr __compare::_Strong_fallback
1193 compare_strong_order_fallback{};
1194
1195 inline constexpr __compare::_Weak_fallback
1196 compare_weak_order_fallback{};
1197
1198 inline constexpr __compare::_Partial_fallback
1199 compare_partial_order_fallback{};
1200 }
1201
1202 /// @cond undocumented
1203 namespace __detail
1204 {
1205 // [expos.only.func] synth-three-way
1206 inline constexpr struct _Synth3way
1207 {
1208 template<typename _Tp, typename _Up>
1209 static constexpr bool
1210 _S_noexcept(const _Tp* __t = nullptr, const _Up* __u = nullptr)
1211 {
1212 if constexpr (three_way_comparable_with<_Tp, _Up>)
1213 return noexcept(*__t <=> *__u);
1214 else
1215 return noexcept(*__t < *__u) && noexcept(*__u < *__t);
1216 }
1217
1218 template<typename _Tp, typename _Up>
1219 [[nodiscard]]
1220 constexpr auto
1221 operator()(const _Tp& __t, const _Up& __u) const
1222 noexcept(_S_noexcept<_Tp, _Up>())
1223 requires requires
1224 {
1225 { __t < __u } -> __boolean_testable;
1226 { __u < __t } -> __boolean_testable;
1227 }
1228 {
1229 if constexpr (three_way_comparable_with<_Tp, _Up>)
1230 return __t <=> __u;
1231 else
1232 {
1233 if (__t < __u)
1234 return weak_ordering::less;
1235 else if (__u < __t)
1236 return weak_ordering::greater;
1237 else
1238 return weak_ordering::equivalent;
1239 }
1240 }
1241 } __synth3way = {};
1242
1243 // [expos.only.func] synth-three-way-result
1244 template<typename _Tp, typename _Up = _Tp>
1245 using __synth3way_t
1246 = decltype(__detail::__synth3way(std::declval<_Tp&>(),
1248 } // namespace __detail
1249 /// @endcond
1250#endif // __cpp_lib_three_way_comparison >= 201907L
1251} // namespace std
1252
1253#endif // C++20
1254
1255#endif // _COMPARE
typename common_reference< _Tp... >::type common_reference_t
Definition type_traits:3927
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1731
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2483
ISO C++ entities toplevel namespace is std.
typename __detail::__cmp3way_res_impl< _Tp, _Up >::type compare_three_way_result_t
[cmp.result], result of three-way comparison
Definition compare:518
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1687
Implementation details not part of the namespace std interface.
[cmp.result], result of three-way comparison
Definition compare:514