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