Skip to content

Commit a791939

Browse files
authored
make compiler version comparison utility generic (#2952)
1 parent 0b36a7d commit a791939

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

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

+27-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,34 @@
1111
#ifndef __CCCL_COMPILER_H
1212
#define __CCCL_COMPILER_H
1313

14+
// Utility to compare version numbers. To use:
15+
// 1) Define a macro that makes a version number from major and minor numbers, e. g.:
16+
// #define MYPRODUCT_MAKE_VERSION(_MAJOR, _MINOR) (_MAJOR * 100 + _MINOR)
17+
// 2) Define a macro that you will use to compare versions, e. g.:
18+
// #define MYPRODUCT(...) _CCCL_VERSION_COMPARE(MYPRODUCT, MYPRODUCT_##__VA_ARGS__)
19+
// Signatures:
20+
// MYPRODUCT(_PROD) - is the product _PROD version non-zero?
21+
// MYPRODUCT(_PROD, _OP, _MAJOR) - compare the product _PROD version to _MAJOR using operator _OP
22+
// MYPRODUCT(_PROD, _OP, _MAJOR, _MINOR) - compare the product _PROD version to _MAJOR._MINOR using operator _OP
23+
#define _CCCL_VERSION_COMPARE_1(_PREFIX, _VER) (_VER != 0)
24+
#define _CCCL_VERSION_COMPARE_3(_PREFIX, _VER, _OP, _MAJOR) _CCCL_VERSION_COMPARE_4(_PREFIX, _VER, _OP, _MAJOR, 0)
25+
#define _CCCL_VERSION_COMPARE_4(_PREFIX, _VER, _OP, _MAJOR, _MINOR) \
26+
(_CCCL_VERSION_COMPARE_1(_PREFIX, _VER) && (_VER _OP _PREFIX##_MAKE_VERSION(_MAJOR, _MINOR)))
27+
#define _CCCL_VERSION_SELECT_COUNT(_ARG1, _ARG2, _ARG3, _ARG4, _ARG5, ...) _ARG5
28+
#define _CCCL_VERSION_SELECT2(_ARGS) _CCCL_VERSION_SELECT_COUNT _ARGS
29+
// MSVC traditonal preprocessor requires an extra level of indirection
30+
#define _CCCL_VERSION_SELECT(...) \
31+
_CCCL_VERSION_SELECT2( \
32+
(__VA_ARGS__, \
33+
_CCCL_VERSION_COMPARE_4, \
34+
_CCCL_VERSION_COMPARE_3, \
35+
_CCCL_VERSION_COMPARE_BAD_ARG_COUNT, \
36+
_CCCL_VERSION_COMPARE_1, \
37+
_CCCL_VERSION_COMPARE_BAD_ARG_COUNT))
38+
#define _CCCL_VERSION_COMPARE(_PREFIX, ...) _CCCL_VERSION_SELECT(__VA_ARGS__)(_PREFIX, __VA_ARGS__)
39+
1440
#define _CCCL_COMPILER_MAKE_VERSION(_MAJOR, _MINOR) (_MAJOR * 100 + _MINOR)
41+
#define _CCCL_COMPILER(...) _CCCL_VERSION_COMPARE(_CCCL_COMPILER, _CCCL_COMPILER_##__VA_ARGS__)
1542

1643
// Determine the host compiler and its version
1744
#if defined(__INTEL_COMPILER)
@@ -39,24 +66,6 @@
3966
# define _CCCL_COMPILER_NVRTC _CCCL_COMPILER_MAKE_VERSION(__CUDACC_VER_MAJOR__, __CUDACC_VER_MINOR__)
4067
#endif
4168

42-
#define _CCCL_COMPILER_COMPARE_VERSION_1(_COMP) _COMP
43-
#define _CCCL_COMPILER_COMPARE_VERSION_3(_COMP, _OP, _MAJOR) _CCCL_COMPILER_COMPARE_VERSION_4(_COMP, _OP, _MAJOR, 0)
44-
#define _CCCL_COMPILER_COMPARE_VERSION_4(_COMP, _OP, _MAJOR, _MINOR) \
45-
(_COMP && (_COMP _OP _CCCL_COMPILER_MAKE_VERSION(_MAJOR, _MINOR)))
46-
47-
#define _CCCL_COMPILER_SELECT_COUNT(_ARG1, _ARG2, _ARG3, _ARG4, _ARG5, ...) _ARG5
48-
#define _CCCL_COMPILER_SELECT2(_ARGS) _CCCL_COMPILER_SELECT_COUNT _ARGS
49-
// MSVC traditonal preprocessor requires an extra level of indirection
50-
#define _CCCL_COMPILER_SELECT(...) \
51-
_CCCL_COMPILER_SELECT2( \
52-
(__VA_ARGS__, \
53-
_CCCL_COMPILER_COMPARE_VERSION_4, \
54-
_CCCL_COMPILER_COMPARE_VERSION_3, \
55-
_CCCL_COMPILER_COMPARE_BAD_ARG_COUNT, \
56-
_CCCL_COMPILER_COMPARE_VERSION_1, \
57-
_CCCL_COMPILER_COMPARE_BAD_ARG_COUNT))
58-
#define _CCCL_COMPILER(...) _CCCL_COMPILER_SELECT(_CCCL_COMPILER_##__VA_ARGS__)(_CCCL_COMPILER_##__VA_ARGS__)
59-
6069
// Determine the cuda compiler
6170
#if defined(__NVCC__)
6271
# define _CCCL_CUDA_COMPILER_NVCC

0 commit comments

Comments
 (0)