Skip to content

Commit

Permalink
backport to_underlying (#2853)
Browse files Browse the repository at this point in the history
* backport `to_underlying` to C++11
* disable test on bit field enums for pre gcc 9.3 due to unmuteable warnings
  • Loading branch information
davebayer authored Nov 19, 2024
1 parent f9d56e5 commit c5573f1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion c2h/include/c2h/catch2_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ struct Catch::StringMaker<cudaError>
{
static auto convert(cudaError e) -> std::string
{
return std::to_string(cuda::std::__to_underlying(e)) + " (" + cudaGetErrorString(e) + ")";
return std::to_string(cuda::std::to_underlying(e)) + " (" + cudaGetErrorString(e) + ")";
}
};

Expand Down
10 changes: 1 addition & 9 deletions libcudacxx/include/cuda/std/__utility/to_underlying.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,11 @@

_LIBCUDACXX_BEGIN_NAMESPACE_STD

template <class _Tp>
_LIBCUDACXX_HIDE_FROM_ABI constexpr typename underlying_type<_Tp>::type __to_underlying(_Tp __val) noexcept
{
return static_cast<typename underlying_type<_Tp>::type>(__val);
}

#if _CCCL_STD_VER > 2020
template <class _Tp>
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr underlying_type_t<_Tp> to_underlying(_Tp __val) noexcept
{
return _CUDA_VSTD::__to_underlying(__val);
return static_cast<underlying_type_t<_Tp>>(__val);
}
#endif

_LIBCUDACXX_END_NAMESPACE_STD

Expand Down
5 changes: 3 additions & 2 deletions 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 // !_CCCL_COMPILER(NVRTC)

#define __cccl_lib_to_underlying 202102L

#if _CCCL_STD_VER >= 2014
# define __cccl_lib_bit_cast 201806L
# define __cccl_lib_chrono_udls 201304L
Expand Down Expand Up @@ -244,8 +246,7 @@
// # define __cccl_lib_stdatomic_h 202011L
// # define __cccl_lib_string_contains 202011L
// # define __cccl_lib_string_resize_and_overwrite 202110L
# define __cccl_lib_to_underlying 202102L
# define __cccl_lib_unreachable 202202L
# define __cccl_lib_unreachable 202202L

#endif // _CCCL_STD_VER >= 2023

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
//
//===----------------------------------------------------------------------===//

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

// [utility.underlying], to_underlying
// template <class T>
// constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
// constexpr underlying_type_t<T> to_underlying( T value ) noexcept;

#include <cuda/std/cassert>
#include <cuda/std/cstdint>
Expand All @@ -20,6 +18,10 @@

#include "test_macros.h"

#if defined(TEST_COMPILER_GCC) && ((TEST_GCC_VER >= 900 && TEST_GCC_VER < 903) || TEST_GCC_VER < 804)
# define OMIT_BITFIELD_ENUMS 1
#endif

enum class e_default
{
a = 0,
Expand Down Expand Up @@ -54,14 +56,16 @@ enum class e_bool : cuda::std::uint8_t
t = 1
};

#if !OMIT_BITFIELD_ENUMS
struct WithBitfieldEnums
{
e_default e1 : 3;
e_ushort e2 : 6;
e_bool e3 : 1;
};
#endif // !OMIT_BITFIELD_ENUMS

__host__ __device__ constexpr bool test()
__host__ __device__ TEST_CONSTEXPR_CXX14 bool test()
{
ASSERT_NOEXCEPT(cuda::std::to_underlying(e_default::a));
ASSERT_SAME_TYPE(int, decltype(cuda::std::to_underlying(e_default::a)));
Expand All @@ -88,21 +92,26 @@ __host__ __device__ constexpr bool test()
assert(cuda::std::numeric_limits<int>::min() == cuda::std::to_underlying(enum_min));
assert(cuda::std::numeric_limits<int>::max() == cuda::std::to_underlying(enum_max));

WithBitfieldEnums bf;
#if !OMIT_BITFIELD_ENUMS
WithBitfieldEnums bf{};
bf.e1 = static_cast<e_default>(3);
bf.e2 = e_ushort::e;
bf.e3 = e_bool::t;
assert(3 == cuda::std::to_underlying(bf.e1));
assert(25 == cuda::std::to_underlying(bf.e2));
assert(1 == cuda::std::to_underlying(bf.e3));
#endif // !OMIT_BITFIELD_ENUMS

return true;
}

int main(int, char**)
{
test();
static_assert(test());

#if TEST_STD_VER >= 2014
static_assert(test(), "");
#endif // TEST_STD_VER >= 2014

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
//
//===----------------------------------------------------------------------===//

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

// [utility.underlying], to_underlying
// template <class T>
// constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
// constexpr underlying_type_t<T> to_underlying( T value ) noexcept;

#include <cuda/std/utility>

Expand Down

0 comments on commit c5573f1

Please sign in to comment.