compare 37 KB

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