Path: blob/main/contrib/llvm-project/libcxx/include/__utility/cmp.h
35236 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___UTILITY_CMP_H9#define _LIBCPP___UTILITY_CMP_H1011#include <__concepts/arithmetic.h>12#include <__config>13#include <__type_traits/is_signed.h>14#include <__type_traits/make_unsigned.h>15#include <limits>1617#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)18# pragma GCC system_header19#endif2021_LIBCPP_PUSH_MACROS22#include <__undef_macros>2324_LIBCPP_BEGIN_NAMESPACE_STD2526#if _LIBCPP_STD_VER >= 202728template <__libcpp_integer _Tp, __libcpp_integer _Up>29_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_equal(_Tp __t, _Up __u) noexcept {30if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)31return __t == __u;32else if constexpr (is_signed_v<_Tp>)33return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;34else35return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);36}3738template <__libcpp_integer _Tp, __libcpp_integer _Up>39_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_not_equal(_Tp __t, _Up __u) noexcept {40return !std::cmp_equal(__t, __u);41}4243template <__libcpp_integer _Tp, __libcpp_integer _Up>44_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less(_Tp __t, _Up __u) noexcept {45if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)46return __t < __u;47else if constexpr (is_signed_v<_Tp>)48return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;49else50return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);51}5253template <__libcpp_integer _Tp, __libcpp_integer _Up>54_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater(_Tp __t, _Up __u) noexcept {55return std::cmp_less(__u, __t);56}5758template <__libcpp_integer _Tp, __libcpp_integer _Up>59_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_less_equal(_Tp __t, _Up __u) noexcept {60return !std::cmp_greater(__t, __u);61}6263template <__libcpp_integer _Tp, __libcpp_integer _Up>64_LIBCPP_HIDE_FROM_ABI constexpr bool cmp_greater_equal(_Tp __t, _Up __u) noexcept {65return !std::cmp_less(__t, __u);66}6768template <__libcpp_integer _Tp, __libcpp_integer _Up>69_LIBCPP_HIDE_FROM_ABI constexpr bool in_range(_Up __u) noexcept {70return std::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&71std::cmp_greater_equal(__u, numeric_limits<_Tp>::min());72}7374#endif // _LIBCPP_STD_VER >= 207576_LIBCPP_END_NAMESPACE_STD7778_LIBCPP_POP_MACROS7980#endif // _LIBCPP___UTILITY_CMP_H818283