xgboost
byteswap.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <dmlc/endian.h> // for ByteSwap
7 #include <xgboost/base.h>
8 #include <xgboost/windefs.h>
9 
10 #include <cstdint>
11 
12 #if defined(xgboost_IS_WIN)
13 
14 #include <cstdlib> // for _byteswap_uint64, _byteswap_ulong, _byteswap_ushort
15 
16 #endif // defined(xgboost_IS_WIN)
17 
18 namespace xgboost {
19 #if defined(__CUDA_ARCH__)
20 // CUDA kernel version
21 template <typename T>
22 [[nodiscard]] __device__ T ByteSwap(T v);
23 
24 template <>
25 inline __device__ std::uint16_t ByteSwap(std::uint16_t v) {
26 #if defined(__clang__)
27  return __builtin_bswap16(v);
28 #else
29  return __nv_bswap16(v);
30 #endif
31 }
32 
33 template <>
34 inline __device__ std::uint32_t ByteSwap(std::uint32_t v) {
35 #if defined(__clang__)
36  return __builtin_bswap32(v);
37 #else
38  return __nv_bswap32(v);
39 #endif
40 }
41 
42 template <>
43 inline __device__ std::uint64_t ByteSwap(std::uint64_t v) {
44 #if defined(__clang__)
45  return __builtin_bswap64(v);
46 #else
47  return __nv_bswap64(v);
48 #endif
49 }
50 
51 #elif defined(__GLIBC__)
52 // Host gcc/clang
53 template <typename T>
54 T ByteSwap(T v);
55 
56 template <>
57 inline std::uint16_t ByteSwap(std::uint16_t v) {
58  return __builtin_bswap16(v);
59 }
60 
61 template <>
62 inline std::uint32_t ByteSwap(std::uint32_t v) {
63  return __builtin_bswap32(v);
64 }
65 
66 template <>
67 inline std::uint64_t ByteSwap(std::uint64_t v) {
68  return __builtin_bswap64(v);
69 }
70 
71 #elif defined(xgboost_IS_WIN) && !defined(__MINGW32__)
72 // MSVC
73 template <typename T>
74 T ByteSwap(T v);
75 
76 template <>
77 inline std::uint16_t ByteSwap(std::uint16_t v) {
78  return _byteswap_ushort(v);
79 }
80 
81 template <>
82 inline std::uint32_t ByteSwap(std::uint32_t v) {
83  return _byteswap_ulong(v);
84 }
85 
86 template <>
87 inline std::uint64_t ByteSwap(std::uint64_t v) {
88  return _byteswap_uint64(v);
89 }
90 
91 #else
92 
93 template <typename T>
94 T ByteSwap(T v) {
95  dmlc::ByteSwap(&v, sizeof(v), 1);
96  return v;
97 }
98 
99 #endif // defined(__CUDA_ARCH__)
100 } // namespace xgboost
Defines configuration macros and basic types for xgboost.
Learner interface that integrates objective, gbm and evaluation together. This is the user facing XGB...
Definition: base.h:89
T ByteSwap(T v)
Definition: byteswap.h:94