tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
cmp_less.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2024 Tobias Hienzsch
3
4#ifndef TETL_UTILITY_CMP_LESS_HPP
5#define TETL_UTILITY_CMP_LESS_HPP
6
7#include <etl/_concepts/builtin_integer.hpp>
8#include <etl/_type_traits/is_signed.hpp>
9#include <etl/_type_traits/make_unsigned.hpp>
10
11namespace etl {
12
13/// Compare the values of two integers t and u. Unlike builtin comparison
14/// operators, negative signed integers always compare less than (and not equal
15/// to) unsigned integers: the comparison is safe against lossy integer
16/// conversion.
17///
18/// https://en.cppreference.com/w/cpp/utility/intcmp
19///
20/// \ingroup utility
21template <builtin_integer T, builtin_integer U>
22[[nodiscard]] constexpr auto cmp_less(T t, U u) noexcept -> bool
23{
24 using UT = etl::make_unsigned_t<T>;
25 using UU = etl::make_unsigned_t<U>;
26 if constexpr (etl::is_signed_v<T> == etl::is_signed_v<U>) {
27 return t < u;
28 } else if constexpr (etl::is_signed_v<T>) {
29 return t < 0 ? true : UT(t) < u;
30 } else {
31 return u < 0 ? false : t < UU(u);
32 }
33}
34
35} // namespace etl
36
37#endif // TETL_UTILITY_CMP_LESS_HPP
constexpr auto cmp_less(T t, U u) noexcept -> bool
Compare the values of two integers t and u. Unlike builtin comparison operators, negative signed inte...
Definition cmp_less.hpp:22
Definition adjacent_find.hpp:9