cmp.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___UTILITY_CMP_H
  9. #define _LIBCPP___UTILITY_CMP_H
  10. #include <__concepts/arithmetic.h>
  11. #include <__config>
  12. #include <__type_traits/is_signed.h>
  13. #include <__type_traits/make_unsigned.h>
  14. #include <limits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_PUSH_MACROS
  19. #include <__undef_macros>
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. #if _LIBCPP_STD_VER >= 20
  22. template <__libcpp_integer _Tp, __libcpp_integer _Up>
  23. _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_equal(_Tp __t, _Up __u) noexcept {
  24. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  25. return __t == __u;
  26. else if constexpr (is_signed_v<_Tp>)
  27. return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;
  28. else
  29. return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
  30. }
  31. template <__libcpp_integer _Tp, __libcpp_integer _Up>
  32. _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_not_equal(_Tp __t, _Up __u) noexcept {
  33. return !std::cmp_equal(__t, __u);
  34. }
  35. template <__libcpp_integer _Tp, __libcpp_integer _Up>
  36. _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less(_Tp __t, _Up __u) noexcept {
  37. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  38. return __t < __u;
  39. else if constexpr (is_signed_v<_Tp>)
  40. return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;
  41. else
  42. return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
  43. }
  44. template <__libcpp_integer _Tp, __libcpp_integer _Up>
  45. _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater(_Tp __t, _Up __u) noexcept {
  46. return std::cmp_less(__u, __t);
  47. }
  48. template <__libcpp_integer _Tp, __libcpp_integer _Up>
  49. _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less_equal(_Tp __t, _Up __u) noexcept {
  50. return !std::cmp_greater(__t, __u);
  51. }
  52. template <__libcpp_integer _Tp, __libcpp_integer _Up>
  53. _LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater_equal(_Tp __t, _Up __u) noexcept {
  54. return !std::cmp_less(__t, __u);
  55. }
  56. template <__libcpp_integer _Tp, __libcpp_integer _Up>
  57. _LIBCPP_HIDE_FROM_ABI constexpr bool in_range(_Up __u) noexcept {
  58. return std::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&
  59. std::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
  60. }
  61. #endif // _LIBCPP_STD_VER >= 20
  62. _LIBCPP_END_NAMESPACE_STD
  63. _LIBCPP_POP_MACROS
  64. #endif // _LIBCPP___UTILITY_CMP_H