Skip to content

Commit c796a7f

Browse files
authored
Move libcudacxx endian macros to cccl (NVIDIA#4429)
* Move libcudacxx endian macros to cccl * fix review, add `_CCCL_WARNING`, default to little endian if we fail to detect the endianness
1 parent 848b524 commit c796a7f

File tree

6 files changed

+58
-84
lines changed

6 files changed

+58
-84
lines changed

libcudacxx/include/cuda/std/__bit/endian.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,9 @@ _LIBCUDACXX_BEGIN_NAMESPACE_STD
2525

2626
enum class endian
2727
{
28-
little = 0xDEAD,
29-
big = 0xFACE,
30-
#if defined(_LIBCUDACXX_LITTLE_ENDIAN)
31-
native = little
32-
#elif defined(_LIBCUDACXX_BIG_ENDIAN)
33-
native = big
34-
#else
35-
native = 0xCAFE
36-
#endif
28+
little = _CCCL_ENDIAN_LITTLE(),
29+
big = _CCCL_ENDIAN_BIG(),
30+
native = _CCCL_ENDIAN_NATIVE(),
3731
};
3832

3933
_LIBCUDACXX_END_NAMESPACE_STD

libcudacxx/include/cuda/std/__cccl/architecture.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#ifndef __CCCL_ARCH_H
1212
#define __CCCL_ARCH_H
1313

14+
#include <cuda/std/__cccl/compiler.h>
15+
#include <cuda/std/__cccl/preprocessor.h>
16+
1417
// The header provides the following macros to determine the host architecture:
1518
//
1619
// _CCCL_ARCH(ARM64) ARM64
@@ -36,4 +39,40 @@
3639

3740
#define _CCCL_ARCH(...) _CCCL_ARCH_##__VA_ARGS__##_()
3841

42+
// Determine the endianness
43+
44+
#define _CCCL_ENDIAN_LITTLE() 0xDEAD
45+
#define _CCCL_ENDIAN_BIG() 0xFACE
46+
#define _CCCL_ENDIAN_PDP() 0xBEEF
47+
48+
#if _CCCL_COMPILER(NVRTC) || (_CCCL_COMPILER(MSVC) && (_CCCL_ARCH(X86_64) || _CCCL_ARCH(ARM64))) || __LITTLE_ENDIAN__
49+
# define _CCCL_ENDIAN_NATIVE() _CCCL_ENDIAN_LITTLE()
50+
#elif __BIG_ENDIAN__
51+
# define _CCCL_ENDIAN_NATIVE() _CCCL_ENDIAN_BIG()
52+
#elif defined(__BYTE_ORDER__)
53+
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
54+
# define _CCCL_ENDIAN_NATIVE() _CCCL_ENDIAN_LITTLE()
55+
# elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
56+
# define _CCCL_ENDIAN_NATIVE() _CCCL_ENDIAN_PDP()
57+
# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
58+
# define _CCCL_ENDIAN_NATIVE() _CCCL_ENDIAN_BIG()
59+
# endif // __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
60+
#elif _CCCL_HAS_INCLUDE(<endian.h>)
61+
# include <endian.h>
62+
# if __BYTE_ORDER == __LITTLE_ENDIAN
63+
# define _CCCL_ENDIAN_NATIVE() _CCCL_ENDIAN_LITTLE()
64+
# elif __BYTE_ORDER == __PDP_ENDIAN
65+
# define _CCCL_ENDIAN_NATIVE() _CCCL_ENDIAN_PDP()
66+
# elif __BYTE_ORDER == __BIG_ENDIAN
67+
# define _CCCL_ENDIAN_NATIVE() _CCCL_ENDIAN_BIG()
68+
# endif // __BYTE_ORDER == __BIG_ENDIAN
69+
#endif // ^^^ has endian.h ^^^
70+
71+
#if !defined(_CCCL_ENDIAN_NATIVE)
72+
_CCCL_WARNING("failed to determine the endianness of the host architecture, defaulting to little-endian")
73+
# define _CCCL_ENDIAN_NATIVE() _CCCL_ENDIAN_LITTLE()
74+
#endif // !_CCCL_ENDIAN_NATIVE
75+
76+
#define _CCCL_ENDIAN(_NAME) (_CCCL_ENDIAN_NATIVE() == _CCCL_ENDIAN_##_NAME())
77+
3978
#endif // __CCCL_ARCH_H

libcudacxx/include/cuda/std/__cccl/compiler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,10 @@
181181

182182
#define _CCCL_PRAGMA_NOUNROLL() _CCCL_PRAGMA_UNROLL(1)
183183

184+
#if _CCCL_COMPILER(MSVC)
185+
# define _CCCL_WARNING(_MSG) _CCCL_PRAGMA(message(__FILE__ ":" _CCCL_TO_STRING(__LINE__) ": warning: " _MSG))
186+
#else // ^^^ _CCCL_COMPILER(MSVC) ^^^ / vvv !_CCCL_COMPILER(MSVC) vvv
187+
# define _CCCL_WARNING(_MSG) _CCCL_PRAGMA(GCC warning _MSG)
188+
#endif // !_CCCL_COMPILER(MSVC)
189+
184190
#endif // __CCCL_COMPILER_H

libcudacxx/include/cuda/std/detail/libcxx/include/__config

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -38,59 +38,6 @@ extern "C++" {
3838
# define __has_extension(__x) 0
3939
# endif
4040

41-
# ifdef __LITTLE_ENDIAN__
42-
# if __LITTLE_ENDIAN__
43-
# define _LIBCUDACXX_LITTLE_ENDIAN
44-
# endif // __LITTLE_ENDIAN__
45-
# endif // __LITTLE_ENDIAN__
46-
47-
# ifdef __BIG_ENDIAN__
48-
# if __BIG_ENDIAN__
49-
# define _LIBCUDACXX_BIG_ENDIAN
50-
# endif // __BIG_ENDIAN__
51-
# endif // __BIG_ENDIAN__
52-
53-
# ifdef __BYTE_ORDER__
54-
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
55-
# define _LIBCUDACXX_LITTLE_ENDIAN
56-
# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
57-
# define _LIBCUDACXX_BIG_ENDIAN
58-
# endif // __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
59-
# endif // __BYTE_ORDER__
60-
61-
# if defined(_WIN32)
62-
# define _LIBCUDACXX_LITTLE_ENDIAN
63-
# if (defined(_M_AMD64) || defined(__x86_64__)) || (defined(_M_ARM) || defined(__arm__))
64-
# define _LIBCUDACXX_HAS_BITSCAN64
65-
# endif
66-
67-
// Some CRT APIs are unavailable to store apps
68-
# if defined(WINAPI_FAMILY)
69-
# include <winapifamily.h>
70-
# if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) \
71-
&& (!defined(WINAPI_PARTITION_SYSTEM) || !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_SYSTEM))
72-
# define _LIBCUDACXX_WINDOWS_STORE_APP
73-
# endif
74-
# endif
75-
# endif // defined(_WIN32)
76-
77-
# ifndef _LIBCUDACXX_LITTLE_ENDIAN
78-
# if _CCCL_COMPILER(NVRTC)
79-
# define _LIBCUDACXX_LITTLE_ENDIAN
80-
# endif
81-
# endif // _LIBCUDACXX_LITTLE_ENDIAN
82-
83-
# if !defined(_LIBCUDACXX_LITTLE_ENDIAN) && !defined(_LIBCUDACXX_BIG_ENDIAN)
84-
# include <endian.h>
85-
# if __BYTE_ORDER == __LITTLE_ENDIAN
86-
# define _LIBCUDACXX_LITTLE_ENDIAN
87-
# elif __BYTE_ORDER == __BIG_ENDIAN
88-
# define _LIBCUDACXX_BIG_ENDIAN
89-
# else // __BYTE_ORDER == __BIG_ENDIAN
90-
# error unable to determine endian
91-
# endif
92-
# endif // !defined(_LIBCUDACXX_LITTLE_ENDIAN) && !defined(_LIBCUDACXX_BIG_ENDIAN)
93-
9441
# if _CCCL_HAS_ATTRIBUTE(__no_sanitize__) && !_CCCL_COMPILER(GCC)
9542
# define _LIBCUDACXX_NO_CFI __attribute__((__no_sanitize__("cfi")))
9643
# else

libcudacxx/test/libcudacxx/std/numerics/bit/bit.endian/endian.pass.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,30 @@
1010
// <cuda/std/bit>
1111

1212
#include <cuda/std/bit>
13-
// #include <cuda/std/cstring>
1413
#include <cuda/std/cassert>
1514
#include <cuda/std/cstdint>
15+
#include <cuda/std/cstring>
1616
#include <cuda/std/type_traits>
1717

1818
#include "test_macros.h"
1919

2020
int main(int, char**)
2121
{
22-
static_assert(cuda::std::is_enum<cuda::std::endian>::value, "");
23-
24-
// Check that E is a scoped enum by checking for conversions.
25-
typedef cuda::std::underlying_type<cuda::std::endian>::type UT;
26-
static_assert(!cuda::std::is_convertible<cuda::std::endian, UT>::value, "");
22+
static_assert(cuda::std::is_scoped_enum_v<cuda::std::endian>);
2723

2824
// test that the enumeration values exist
29-
static_assert(cuda::std::endian::little == cuda::std::endian::little, "");
30-
static_assert(cuda::std::endian::big == cuda::std::endian::big, "");
31-
static_assert(cuda::std::endian::native == cuda::std::endian::native, "");
32-
static_assert(cuda::std::endian::little != cuda::std::endian::big, "");
33-
34-
// Technically not required, but true on all existing machines
35-
static_assert(
36-
cuda::std::endian::native == cuda::std::endian::little || cuda::std::endian::native == cuda::std::endian::big, "");
25+
static_assert(cuda::std::endian::little == cuda::std::endian::little);
26+
static_assert(cuda::std::endian::big == cuda::std::endian::big);
27+
static_assert(cuda::std::endian::native == cuda::std::endian::native);
28+
static_assert(cuda::std::endian::little != cuda::std::endian::big);
3729

3830
// Try to check at runtime
3931
{
40-
uint32_t i = 0x01020304;
32+
cuda::std::uint32_t i = 0x01020304;
4133
char c[4];
42-
static_assert(sizeof(i) == sizeof(c), "");
43-
memcpy(c, &i, sizeof(c));
34+
static_assert(sizeof(i) == sizeof(c));
4435

36+
cuda::std::memcpy(c, &i, sizeof(c));
4537
assert((c[0] == 1) == (cuda::std::endian::native == cuda::std::endian::big));
4638
}
4739

thrust/thrust/detail/config/cpp_dialect.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@
4141
#define THRUST_CPP_DIALECT _CCCL_STD_VER
4242

4343
// Define THRUST_COMPILER_DEPRECATION macro:
44-
#if _CCCL_COMPILER(MSVC) || _CCCL_COMPILER(NVRTC)
45-
# define THRUST_COMP_DEPR_IMPL(msg) _CCCL_PRAGMA(message(__FILE__ ":" _CCCL_TO_STRING(__LINE__) ": warning: " #msg))
46-
#else // clang / gcc:
47-
# define THRUST_COMP_DEPR_IMPL(msg) _CCCL_PRAGMA(GCC warning #msg)
48-
#endif
44+
#define THRUST_COMP_DEPR_IMPL(msg) _CCCL_WARNING(#msg)
4945

5046
// Compiler checks:
5147
// clang-format off

0 commit comments

Comments
 (0)