USRP Hardware Driver and Device Manual Version: 4.10.0.0_release
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
16UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
17{
18 return _byteswap_ushort(x);
19}
20
21UHD_FORCE_INLINE uint32_t uhd::byteswap(uint32_t x)
22{
23 return _byteswap_ulong(x);
24}
25
26UHD_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
34UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
35{
36 return OSSwapInt16(x);
37}
38
39UHD_FORCE_INLINE uint32_t uhd::byteswap(uint32_t x)
40{
41 return OSSwapInt32(x);
42}
43
44UHD_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
51UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
52{
53 return (x >> 8) | (x << 8); // DNE return __builtin_bswap16(x);
54}
55
56UHD_FORCE_INLINE uint32_t uhd::byteswap(uint32_t x)
57{
58 return __builtin_bswap32(x);
59}
60
61UHD_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
69UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
70{
71 return bswap_16(x);
72}
73
74UHD_FORCE_INLINE uint32_t uhd::byteswap(uint32_t x)
75{
76 return bswap_32(x);
77}
78
79UHD_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
86UHD_FORCE_INLINE uint16_t uhd::byteswap(uint16_t x)
87{
88 return (x >> 8) | (x << 8);
89}
90
91UHD_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
97UHD_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 **********************************************************************/
108namespace uhd {
109
110template <typename T>
112{
113#ifdef UHD_BIG_ENDIAN
114 return num;
115#else
116 return uhd::byteswap(num);
117#endif
118}
119
120template <typename T>
122{
123#ifdef UHD_BIG_ENDIAN
124 return num;
125#else
126 return uhd::byteswap(num);
127#endif
128}
129
130template <typename T>
132{
133#ifdef UHD_BIG_ENDIAN
134 return uhd::byteswap(num);
135#else
136 return num;
137#endif
138}
139
140template <typename T>
142{
143#ifdef UHD_BIG_ENDIAN
144 return uhd::byteswap(num);
145#else
146 return num;
147#endif
148}
149
150template <endianness_t endianness, typename T>
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
168template <endianness_t endianness, typename T>
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 */
#define UHD_FORCE_INLINE
Definition config.hpp:115
Definition build_info.hpp:12
T ntohx(T)
network to host: short, long, or long-long
Definition byteswap.ipp:111
uint16_t byteswap(uint16_t)
perform a byteswap on a 16 bit integer
Definition byteswap.ipp:86
T htowx(T)
host to worknet: short, long, or long-long
Definition byteswap.ipp:141
T htolx(T num)
Host to link with configurable endianness (16-bit, 32-bit, or 64-bit).
Definition byteswap.ipp:151
T wtohx(T)
worknet to host: short, long, or long-long
Definition byteswap.ipp:131
T ltohx(T num)
Link with configurable endianness to host (16-bit, 32-bit, or 64-bit).
Definition byteswap.ipp:169
T htonx(T)
host to network: short, long, or long-long
Definition byteswap.ipp:121