hipcub/libcxx.hpp Source File

hipcub/libcxx.hpp Source File#

hipCUB: hipcub/libcxx.hpp Source File
libcxx.hpp
1 // MIT License
2 //
3 // Copyright (c) 2026 Advanced Micro Devices, Inc. All rights reserved.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef HIPCUB_LIBCXX_HPP_
24 #define HIPCUB_LIBCXX_HPP_
25 
26 #pragma once
27 
28 // This is a utility file that helps managing which
29 // 'std' implementation we're using. The provided
30 // macros are for internal use only and may change
31 // in future versions.
32 //
33 // Example usage:
34 // #include _HIPCUB_STD_INCLUDE(optional)
35 // using optional_int = _HIPCUB_STD::optional<int>;
36 
37 // Minimum version that we depend on.
38 #define _HIPCUB_REQUIRED_LIBCXX_VERSION_MAJOR 3
39 #define _HIPCUB_REQUIRED_LIBCXX_VERSION_MINOR 0
40 #define _HIPCUB_REQUIRED_LIBCXX_VERSION_PATCH 0
41 
42 #define _HIPCUB_REQUIRED_LIBCXX_VERSION \
43  _HIPCUB_REQUIRED_LIBCXX_VERSION_MAJOR * 1000000 + _HIPCUB_REQUIRED_LIBCXX_VERSION_MINOR * 1000 \
44  + _HIPCUB_REQUIRED_LIBCXX_VERSION_PATCH
45 
46 #ifdef __has_include
47  #define HIPCUB_HAS_INCLUDE(_X) __has_include(_X)
48 #else
49  #define HIPCUB_HAS_INCLUDE(_X) 0
50 #endif
51 
52 #define _HIPCUB_STRINGIFY_IMPL(x) #x
53 #define _HIPCUB_STRINGIFY(x) _HIPCUB_STRINGIFY_IMPL(x)
54 
55 // clang-format off
56 
57 // If the '::cuda::std' namespace from 'libcudacxx' or 'libhipcxx' is available.
58 #if HIPCUB_HAS_INCLUDE(<cuda/std/version>)
59  #include <cuda/std/version>
60  // If version matches and '_CUDA_VSTD' is available.
61  #if defined(_LIBCUDACXX_CUDA_API_VERSION) && (_LIBCUDACXX_CUDA_API_VERSION >= _HIPCUB_REQUIRED_LIBCXX_VERSION) && defined(_CUDA_VSTD)
62  #define _HIPCUB_LIBCXX_INCLUDE(LIB) _HIPCUB_STRINGIFY(cuda/LIB)
63  #define _HIPCUB_STD_INCLUDE(LIB) _HIPCUB_STRINGIFY(cuda/std/LIB)
64  #define _HIPCUB_LIBCXX ::cuda
65  #define _HIPCUB_STD _CUDA_VSTD
66  #define _HIPCUB_HAS_DEVICE_SYSTEM_STD 1
67  #define _HIPCUB_STD_NAMESPACE_BEGIN _LIBCUDACXX_BEGIN_NAMESPACE_STD
68  #define _HIPCUB_STD_NAMESPACE_END _LIBCUDACXX_END_NAMESPACE_STD
69  #endif
70 #endif
71 // Otherwise, if the '::hip::std' namespace from 'libhipcxx' is available.
72 #if !defined(_HIPCUB_HAS_DEVICE_SYSTEM_STD) && HIPCUB_HAS_INCLUDE(<hip/std/version>)
73  #include <hip/std/version>
74  // If version matches and '_CUDA_VSTD' is available.
75  #if defined(_LIBCUDACXX_CUDA_API_VERSION) && (_LIBCUDACXX_CUDA_API_VERSION >= _HIPCUB_REQUIRED_LIBCXX_VERSION) && defined(_CUDA_VSTD)
76  #define _HIPCUB_LIBCXX_INCLUDE(LIB) _HIPCUB_STRINGIFY(hip/LIB)
77  #define _HIPCUB_STD_INCLUDE(LIB) _HIPCUB_STRINGIFY(hip/std/LIB)
78  // In 'libhipcxx' the '::hip' namespace is synonymous with '::cuda'.
79  #define _HIPCUB_LIBCXX ::hip
80  // In 'libhipcxx' the macro '_CUDA_VSTD' is also defined.
81  #define _HIPCUB_STD _CUDA_VSTD
82  #define _HIPCUB_HAS_DEVICE_SYSTEM_STD 1
83  #define _HIPCUB_STD_NAMESPACE_BEGIN _LIBCUDACXX_BEGIN_NAMESPACE_STD
84  #define _HIPCUB_STD_NAMESPACE_END _LIBCUDACXX_END_NAMESPACE_STD
85  #endif
86 #endif
87 
88 // If 'libcudacxx' or 'libhipcxx' is not found, use fallback.
89 #ifndef _HIPCUB_HAS_DEVICE_SYSTEM_STD
90  #define _HIPCUB_LIBCXX_INCLUDE(LIB) _HIPCUB_STRINGIFY(LIB)
91  #define _HIPCUB_STD_INCLUDE(LIB) _HIPCUB_STRINGIFY(LIB)
92  #define _HIPCUB_LIBCXX
93  #define _HIPCUB_STD ::std
94  #define _HIPCUB_HAS_DEVICE_SYSTEM_STD 0
95  #define _HIPCUB_STD_NAMESPACE_BEGIN \
96  namespace std \
97  {
98  #define _HIPCUB_STD_NAMESPACE_END }
99 #endif
100 
101 // clang-format on
102 
103 #endif // HIPCUB_LIBCXX_HPP_