| USRP Hardware Driver and Device Manual Version: 4.10.0.0 UHD and USRP Manual |

Loading...
Searching...
No Matches
byteswap.ipp
Go to the documentation of this file.
1//
2// Copyright 2010-2011 Ettus Research LLC
3// Copyright 2018 Ettus Research, a National Instruments Company
4//
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
7
8#pragma once
9
10/***********************************************************************
11 * Platform-specific implementation details for byteswap below:
12 **********************************************************************/
13#if defined(UHD_MSVC)
14# include <cstdlib>
15
16 UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
17{
18 return _byteswap_ushort(x);
19}
20
21 UHD_FORCE_INLINE uint32_t uhd::byteswap(uint32_t x)
22{
23 return _byteswap_ulong(x);
24}
25
26 UHD_FORCE_INLINE uint64_t uhd::byteswap(uint64_t x)
27{
28 return _byteswap_uint64(x);
29}
30
31#elif defined(UHD_PLATFORM_MACOS)
32# include <libkern/OSByteOrder.h>
33
34 UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
35{
36 return OSSwapInt16(x);
37}
38
39 UHD_FORCE_INLINE uint32_t uhd::byteswap(uint32_t x)
40{
41 return OSSwapInt32(x);
42}
43
44 UHD_FORCE_INLINE uint64_t uhd::byteswap(uint64_t x)
45{
46 return OSSwapInt64(x);
47}
48
49#elif defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 3
50
51 UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
52{
53 return (x >> 8) | (x << 8); // DNE return __builtin_bswap16(x);
54}
55
56 UHD_FORCE_INLINE uint32_t uhd::byteswap(uint32_t x)
57{
58 return __builtin_bswap32(x);
59}
60
61 UHD_FORCE_INLINE uint64_t uhd::byteswap(uint64_t x)
62{
63 return __builtin_bswap64(x);
64}
65
66#elif defined(UHD_PLATFORM_LINUX)
67# include <byteswap.h>
68
69 UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
70{
71 return bswap_16(x);
72}
73
74 UHD_FORCE_INLINE uint32_t uhd::byteswap(uint32_t x)
75{
76 return bswap_32(x);
77}
78
79 UHD_FORCE_INLINE uint64_t uhd::byteswap(uint64_t x)
80{
81 return bswap_64(x);
82}
83
84#else // http://www.koders.com/c/fidB93B34CD44F0ECF724F1A4EAE3854BA2FE692F59.aspx
85
86 UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
87{
88 return (x >> 8) | (x << 8);
89}
90
91 UHD_FORCE_INLINE uint32_t uhd::byteswap(uint32_t x)
92{
93 return (uint32_t(uhd::byteswap(uint16_t(x & 0xfffful))) << 16)
94 | (uhd::byteswap(uint16_t(x >> 16)));
95}
96
97 UHD_FORCE_INLINE uint64_t uhd::byteswap(uint64_t x)
98{
99 return (uint64_t(uhd::byteswap(uint32_t(x & 0xffffffffull))) << 32)
100 | (uhd::byteswap(uint32_t(x >> 32)));
101}
102
103#endif
104
105/***********************************************************************
106 * Define the templated network to/from host conversions
107 **********************************************************************/
108 namespace uhd {
109
110 template <typename T>
111 UHD_FORCE_INLINE T ntohx(T num)
112{
113#ifdef UHD_BIG_ENDIAN
114 return num;
115#else
116 return uhd::byteswap(num);
117#endif
118}
119
120 template <typename T>
121 UHD_FORCE_INLINE T htonx(T num)
122{
123#ifdef UHD_BIG_ENDIAN
124 return num;
125#else
126 return uhd::byteswap(num);
127#endif
128}
129
130 template <typename T>
131 UHD_FORCE_INLINE T wtohx(T num)
132{
133#ifdef UHD_BIG_ENDIAN
134 return uhd::byteswap(num);
135#else
136 return num;
137#endif
138}
139
140 template <typename T>
141 UHD_FORCE_INLINE T htowx(T num)
142{
143#ifdef UHD_BIG_ENDIAN
144 return uhd::byteswap(num);
145#else
146 return num;
147#endif
148}
149
150 template <endianness_t endianness, typename T>
151 UHD_FORCE_INLINE T htolx(T num)
152{
153#ifdef UHD_BIG_ENDIAN
154 if constexpr (endianness == ENDIANNESS_BIG) {
155 return num;
156 } else {
157 return uhd::byteswap(num);
158 }
159#else // if UHD_LITTLE_ENDIAN
160 if constexpr (endianness == ENDIANNESS_LITTLE) {
161 return num;
162 } else {
163 return uhd::byteswap(num);
164 }
165#endif
166}
167
168 template <endianness_t endianness, typename T>
169 UHD_FORCE_INLINE T ltohx(T num)
170{
171#ifdef UHD_BIG_ENDIAN
172 if constexpr (endianness == ENDIANNESS_BIG) {
173 return num;
174 } else {
175 return uhd::byteswap(num);
176 }
177#else // if UHD_LITTLE_ENDIAN
178 if constexpr (endianness == ENDIANNESS_LITTLE) {
179 return num;
180 } else {
181 return uhd::byteswap(num);
182 }
183#endif
184}
185
186} /* namespace uhd */
UHD_FORCE_INLINE
#define UHD_FORCE_INLINE
Definition config.hpp:115
uhd
Definition build_info.hpp:12
uhd::ntohx
T ntohx(T)
network to host: short, long, or long-long
Definition byteswap.ipp:111
uhd::byteswap
uint16_t byteswap(uint16_t)
perform a byteswap on a 16 bit integer
Definition byteswap.ipp:86
uhd::htowx
T htowx(T)
host to worknet: short, long, or long-long
Definition byteswap.ipp:141
uhd::htolx
T htolx(T num)
Host to link with configurable endianness (16-bit, 32-bit, or 64-bit).
Definition byteswap.ipp:151
uhd::wtohx
T wtohx(T)
worknet to host: short, long, or long-long
Definition byteswap.ipp:131
uhd::ltohx
T ltohx(T num)
Link with configurable endianness to host (16-bit, 32-bit, or 64-bit).
Definition byteswap.ipp:169
uhd::htonx
T htonx(T)
host to network: short, long, or long-long
Definition byteswap.ipp:121