Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport std integer comparison functions to C++11 #2805

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
613b062
backport std integer comparison functions to C++11
davebayer Nov 13, 2024
ae61685
Merge branch 'main' into backport_std_int_comp_funcs
davebayer Nov 13, 2024
76c88ba
fix sfinae
davebayer Nov 13, 2024
e14acb2
add missing `integral_constant.h` include
davebayer Nov 13, 2024
d8d3df9
add missing `_LIBCUDACXX_HIDE_FROM_ABI`
davebayer Nov 13, 2024
0812f59
change target c++ version to 14, use `_CCCL_TRAIT` for standard type …
davebayer Nov 14, 2024
7783358
fix typo
davebayer Nov 14, 2024
f6dc313
suppress MSVC warnings
davebayer Nov 14, 2024
d1360b7
Address review feedback
miscco Nov 15, 2024
dd928c6
Add missing include
miscco Nov 15, 2024
518920f
Fix invalid initialization issue with helper struct
miscco Nov 15, 2024
b2aacd8
Make tests work in C++14
miscco Nov 15, 2024
d560a77
Try and inhibit warning
miscco Nov 15, 2024
5e4be4a
Move implementation of `_LIBCUDACXX_TEMPLATE` to CCCL
miscco Nov 15, 2024
1a424d6
Just avoid the whole warning entirely and let the optimizer do the thing
miscco Nov 15, 2024
be9b176
Merge branch 'main' into pr/davebayer/2805
miscco Nov 15, 2024
bab2c75
Merge branch 'cccl_template_emulation' into pr/davebayer/2805
miscco Nov 15, 2024
40988e8
Make it work in C++11
miscco Nov 15, 2024
e97795a
Avoid old nvcc issues
miscco Nov 15, 2024
abfa826
Pacify MSVC
miscco Nov 15, 2024
f44ac1b
Merge branch 'main' into backport_std_int_comp_funcs
davebayer Nov 17, 2024
3fdfab3
Move implementation of `_LIBCUDACXX_TEMPLATE` to CCCL
miscco Nov 15, 2024
11cd4e7
Move back to the `concept_macros` file
miscco Nov 18, 2024
6093df2
Merge commit '11cd4e7ac1738f0ba3808a6e58be29317d1e58f4' into pr/daveb…
miscco Nov 18, 2024
4266526
Fix formatting
miscco Nov 18, 2024
01e610c
fix concept macros include
davebayer Nov 18, 2024
f2a175a
Merge branch 'main' into backport_std_int_comp_funcs
davebayer Nov 21, 2024
18e562d
Merge branch 'main' into backport_std_int_comp_funcs
miscco Nov 22, 2024
9422d15
Merge branch 'main' into backport_std_int_comp_funcs
davebayer Nov 25, 2024
a7a39e1
[pre-commit.ci] auto code formatting
pre-commit-ci[bot] Nov 25, 2024
4f25558
fix compilation for old nvcc
davebayer Nov 26, 2024
eaa609e
Merge branch 'main' into backport_std_int_comp_funcs
davebayer Nov 26, 2024
a4231e1
Drop newline
miscco Nov 26, 2024
8381859
fix msvc warnings once again
davebayer Nov 26, 2024
890cb8a
change implementation of `if constexpr`
davebayer Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 77 additions & 34 deletions libcudacxx/include/cuda/std/__utility/cmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#endif // no system header

#include <cuda/std/__type_traits/disjunction.h>
#include <cuda/std/__type_traits/enable_if.h>
#include <cuda/std/__type_traits/integral_constant.h>
#include <cuda/std/__type_traits/is_integral.h>
#include <cuda/std/__type_traits/is_same.h>
#include <cuda/std/__type_traits/is_signed.h>
Expand All @@ -33,94 +35,135 @@ _CCCL_PUSH_MACROS

_LIBCUDACXX_BEGIN_NAMESPACE_STD

#if _CCCL_STD_VER > 2017
template <class _Tp, class... _Up>
struct _IsSameAsAny : _Or<_IsSame<_Tp, _Up>...>
{};

template <class _Tp>
concept __is_safe_integral_cmp =
is_integral_v<_Tp>
&& !_IsSameAsAny<_Tp,
bool,
char,
char16_t,
char32_t
# ifndef _LIBCUDACXX_NO_HAS_CHAR8_T
,
char8_t
# endif
# ifndef _LIBCUDACXX_HAS_NO_WIDE_CHARACTERS
,
wchar_t
# endif
>::value;

template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool cmp_equal(_Tp __t, _Up __u) noexcept
struct __is_safe_integral_cmp
: bool_constant<is_integral<_Tp>::value
&& !_IsSameAsAny<_Tp,
bool,
char,
char16_t,
char32_t
#ifndef _LIBCUDACXX_NO_HAS_CHAR8_T
,
char8_t
#endif
#ifndef _LIBCUDACXX_HAS_NO_WIDE_CHARACTERS
,
wchar_t
#endif
>::value>
{};

struct __cmp_equal_impl
{
if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
template <class _Tp, class _Up, enable_if_t<is_signed<_Tp>::value && is_signed<_Up>::value, int> = 0>
davebayer marked this conversation as resolved.
Show resolved Hide resolved
_LIBCUDACXX_HIDE_FROM_ABI static constexpr bool __do_cmp(_Tp __t, _Up __u) noexcept
{
return __t == __u;
}
else if constexpr (is_signed_v<_Tp>)

template <
class _Tp,
class _Up,
enable_if_t<(is_signed<_Tp>::value && !is_signed<_Up>::value) || (!is_signed<_Tp>::value && is_signed<_Up>::value),
int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI static constexpr bool __do_cmp(_Tp __t, _Up __u) noexcept
{
return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;
}
else

template <class _Tp, class _Up, enable_if_t<!is_signed<_Tp>::value && !is_signed<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI static constexpr bool __do_cmp(_Tp __t, _Up __u) noexcept
{
return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
}
};

miscco marked this conversation as resolved.
Show resolved Hide resolved
template <class _Tp,
class _Up,
enable_if_t<__is_safe_integral_cmp<_Tp>::value && __is_safe_integral_cmp<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool cmp_equal(_Tp __t, _Up __u) noexcept
{
return __cmp_equal_impl::__do_cmp(__t, __u);
}

template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
template <class _Tp,
class _Up,
enable_if_t<__is_safe_integral_cmp<_Tp>::value && __is_safe_integral_cmp<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool cmp_not_equal(_Tp __t, _Up __u) noexcept
{
return !_CUDA_VSTD::cmp_equal(__t, __u);
}

template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool cmp_less(_Tp __t, _Up __u) noexcept
struct __cmp_less_impl
{
if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
template <class _Tp, class _Up, enable_if_t<is_signed<_Tp>::value && is_signed<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI static constexpr bool __do_cmp(_Tp __t, _Up __u) noexcept
{
return __t < __u;
}
else if constexpr (is_signed_v<_Tp>)

template <
class _Tp,
class _Up,
enable_if_t<(is_signed<_Tp>::value && !is_signed<_Up>::value) || (!is_signed<_Tp>::value && is_signed<_Up>::value),
int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI static constexpr bool __do_cmp(_Tp __t, _Up __u) noexcept
{
return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;
}
else

template <class _Tp, class _Up, enable_if_t<!is_signed<_Tp>::value && !is_signed<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI static constexpr bool __do_cmp(_Tp __t, _Up __u) noexcept
{
return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
}
};

miscco marked this conversation as resolved.
Show resolved Hide resolved
template <class _Tp,
class _Up,
enable_if_t<__is_safe_integral_cmp<_Tp>::value && __is_safe_integral_cmp<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool cmp_less(_Tp __t, _Up __u) noexcept
{
return __cmp_less_impl::__do_cmp(__t, __u);
}

template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
template <class _Tp,
class _Up,
enable_if_t<__is_safe_integral_cmp<_Tp>::value && __is_safe_integral_cmp<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool cmp_greater(_Tp __t, _Up __u) noexcept
{
return _CUDA_VSTD::cmp_less(__u, __t);
}

template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
template <class _Tp,
class _Up,
enable_if_t<__is_safe_integral_cmp<_Tp>::value && __is_safe_integral_cmp<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool cmp_less_equal(_Tp __t, _Up __u) noexcept
{
return !_CUDA_VSTD::cmp_greater(__t, __u);
}

template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
template <class _Tp,
class _Up,
enable_if_t<__is_safe_integral_cmp<_Tp>::value && __is_safe_integral_cmp<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool cmp_greater_equal(_Tp __t, _Up __u) noexcept
{
return !_CUDA_VSTD::cmp_less(__t, __u);
}

template <__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
template <class _Tp,
class _Up,
enable_if_t<__is_safe_integral_cmp<_Tp>::value && __is_safe_integral_cmp<_Up>::value, int> = 0>
_LIBCUDACXX_HIDE_FROM_ABI constexpr bool in_range(_Up __u) noexcept
{
return _CUDA_VSTD::cmp_less_equal(__u, numeric_limits<_Tp>::max())
&& _CUDA_VSTD::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
}
#endif // _CCCL_STD_VER > 2017

_LIBCUDACXX_END_NAMESPACE_STD

Expand Down
3 changes: 2 additions & 1 deletion libcudacxx/include/cuda/std/version
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
# include <ciso646> // otherwise go for the smallest possible header
#endif

#define __cccl_lib_integer_comparison_functions 202002L

miscco marked this conversation as resolved.
Show resolved Hide resolved
#if _CCCL_STD_VER >= 2014
# define __cccl_lib_bit_cast 201806L
# define __cccl_lib_chrono_udls 201304L
Expand Down Expand Up @@ -174,7 +176,6 @@
// # define __cccl_lib_format 202106L
// # define __cccl_lib_generic_unordered_lookup 201811L
// # define __cccl_lib_int_pow2 202002L
// # define __cccl_lib_integer_comparison_functions 202002L
// # define __cccl_lib_interpolate 201902L
# ifdef _CCCL_BUILTIN_IS_CONSTANT_EVALUATED
# define __cccl_lib_is_constant_evaluated 201811L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

miscco marked this conversation as resolved.
Show resolved Hide resolved
// <utility>

// template<class T, class U>
// constexpr bool cmp_equal(T t, U u) noexcept; // C++20
// constexpr bool cmp_equal(T t, U u) noexcept;

#include <cuda/std/cassert>
#include <cuda/std/limits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

miscco marked this conversation as resolved.
Show resolved Hide resolved
// <utility>

// constexpr bool cmp_greater(T t, U u) noexcept; // C++20
// constexpr bool cmp_greater(T t, U u) noexcept;

#include <cuda/std/cassert>
#include <cuda/std/limits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

miscco marked this conversation as resolved.
Show resolved Hide resolved
// <utility>

// constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20
// constexpr bool cmp_greater_equal(T t, U u) noexcept;

#include <cuda/std/cassert>
#include <cuda/std/limits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

miscco marked this conversation as resolved.
Show resolved Hide resolved
// <utility>

// template<class T, class U>
// constexpr bool cmp_less(T t, U u) noexcept; // C++20
// constexpr bool cmp_less(T t, U u) noexcept;

#include <cuda/std/cassert>
#include <cuda/std/limits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

miscco marked this conversation as resolved.
Show resolved Hide resolved
// <utility>

// constexpr bool cmp_less_equal(T t, U u) noexcept; // C++20
// constexpr bool cmp_less_equal(T t, U u) noexcept;

#include <cuda/std/cassert>
#include <cuda/std/limits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

miscco marked this conversation as resolved.
Show resolved Hide resolved
// <utility>

// template<class T, class U>
// constexpr bool cmp_not_equal(T t, U u) noexcept; // C++20
// constexpr bool cmp_not_equal(T t, U u) noexcept;

#include <cuda/std/cassert>
#include <cuda/std/limits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,28 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

miscco marked this conversation as resolved.
Show resolved Hide resolved
// <utility>

// template<class T, class U>
// constexpr bool cmp_equal(T t, U u) noexcept; // C++20
// constexpr bool cmp_equal(T t, U u) noexcept;

// template<class T, class U>
// constexpr bool cmp_not_equal(T t, U u) noexcept; // C++20
// constexpr bool cmp_not_equal(T t, U u) noexcept;

// template<class T, class U>
// constexpr bool cmp_less(T t, U u) noexcept; // C++20
// constexpr bool cmp_less(T t, U u) noexcept;

// template<class T, class U>
// constexpr bool cmp_less_equal(T t, U u) noexcept; // C++20
// constexpr bool cmp_less_equal(T t, U u) noexcept;

// template<class T, class U>
// constexpr bool cmp_greater(T t, U u) noexcept; // C++20
// constexpr bool cmp_greater(T t, U u) noexcept;

// template<class T, class U>
// constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20
// constexpr bool cmp_greater_equal(T t, U u) noexcept;

// template<class R, class T>
// constexpr bool in_range(T t) noexcept; // C++20
// constexpr bool in_range(T t) noexcept;

#include <cuda/std/cstddef>
#include <cuda/std/utility>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

miscco marked this conversation as resolved.
Show resolved Hide resolved
// <utility>

// template<class R, class T>
// constexpr bool in_range(T t) noexcept; // C++20
// constexpr bool in_range(T t) noexcept;

#include <cuda/std/cassert>
#include <cuda/std/cstdint>
Expand Down