Debian Codegraph @

src/include/xgboost/byteswap.h

raw symbols SCIP
1 /**
2 * Copyright 2022-2026, XGBoost Contributors
3 */
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 return __nv_bswap16(v);
27 }
28
29 template <>
30 inline __device__ std::uint32_t ByteSwap(std::uint32_t v) {
31 return __nv_bswap32(v);
32 }
33
34 template <>
35 inline __device__ std::uint64_t ByteSwap(std::uint64_t v) {
36 return __nv_bswap64(v);
37 }
38
39 #elif defined(__GLIBC__)
40 // Host gcc/clang
41 template <typename T>
42 T ByteSwap(T v);
43
44 template <>
45 inline std::uint16_t ByteSwap(std::uint16_t v) {
46 return __builtin_bswap16(v);
47 }
48
49 template <>
50 inline std::uint32_t ByteSwap(std::uint32_t v) {
51 return __builtin_bswap32(v);
52 }
53
54 template <>
55 inline std::uint64_t ByteSwap(std::uint64_t v) {
56 return __builtin_bswap64(v);
57 }
58
59 #elif defined(xgboost_IS_WIN) && !defined(__MINGW32__)
60 // MSVC
61 template <typename T>
62 T ByteSwap(T v);
63
64 template <>
65 inline std::uint16_t ByteSwap(std::uint16_t v) {
66 return _byteswap_ushort(v);
67 }
68
69 template <>
70 inline std::uint32_t ByteSwap(std::uint32_t v) {
71 return _byteswap_ulong(v);
72 }
73
74 template <>
75 inline std::uint64_t ByteSwap(std::uint64_t v) {
76 return _byteswap_uint64(v);
77 }
78
79 #else
80
81 template <typename T>
82 T ByteSwap(T v) {
83 dmlc::ByteSwap(&v, sizeof(v), 1);
84 return v;
85 }
86
87 #endif // defined(__CUDA_ARCH__)
88 } // namespace xgboost