USRP Hardware Driver and USRP Manual Version: 3.15.0.0-MacPorts-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#ifndef INCLUDED_UHD_UTILS_BYTESWAP_IPP
9#define INCLUDED_UHD_UTILS_BYTESWAP_IPP
10
11/***********************************************************************
12 * Platform-specific implementation details for byteswap below:
13 **********************************************************************/
14#if defined( \
15 BOOST_MSVC) // http://msdn.microsoft.com/en-us/library/a3140177%28VS.80%29.aspx
16# include <cstdlib>
17
18UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
19{
20 return _byteswap_ushort(x);
21}
22
23UHD_INLINE uint32_t uhd::byteswap(uint32_t x)
24{
25 return _byteswap_ulong(x);
26}
27
28UHD_INLINE uint64_t uhd::byteswap(uint64_t x)
29{
30 return _byteswap_uint64(x);
31}
32
33#elif defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 3
34
35UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
36{
37 return (x >> 8) | (x << 8); // DNE return __builtin_bswap16(x);
38}
39
40UHD_INLINE uint32_t uhd::byteswap(uint32_t x)
41{
42 return __builtin_bswap32(x);
43}
44
45UHD_INLINE uint64_t uhd::byteswap(uint64_t x)
46{
47 return __builtin_bswap64(x);
48}
49
50#elif defined(UHD_PLATFORM_MACOS)
51# include <libkern/OSByteOrder.h>
52
53UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
54{
55 return OSSwapInt16(x);
56}
57
58UHD_INLINE uint32_t uhd::byteswap(uint32_t x)
59{
60 return OSSwapInt32(x);
61}
62
63UHD_INLINE uint64_t uhd::byteswap(uint64_t x)
64{
65 return OSSwapInt64(x);
66}
67
68#elif defined(UHD_PLATFORM_LINUX)
69# include <byteswap.h>
70
71UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
72{
73 return bswap_16(x);
74}
75
76UHD_INLINE uint32_t uhd::byteswap(uint32_t x)
77{
78 return bswap_32(x);
79}
80
81UHD_INLINE uint64_t uhd::byteswap(uint64_t x)
82{
83 return bswap_64(x);
84}
85
86#else // http://www.koders.com/c/fidB93B34CD44F0ECF724F1A4EAE3854BA2FE692F59.aspx
87
88UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
89{
90 return (x >> 8) | (x << 8);
91}
92
93UHD_INLINE uint32_t uhd::byteswap(uint32_t x)
94{
95 return (uint32_t(uhd::byteswap(uint16_t(x & 0xfffful))) << 16)
96 | (uhd::byteswap(uint16_t(x >> 16)));
97}
98
99UHD_INLINE uint64_t uhd::byteswap(uint64_t x)
100{
101 return (uint64_t(uhd::byteswap(uint32_t(x & 0xffffffffull))) << 32)
102 | (uhd::byteswap(uint32_t(x >> 32)));
103}
104
105#endif
106
107/***********************************************************************
108 * Define the templated network to/from host conversions
109 **********************************************************************/
110namespace uhd {
111
112template <typename T> UHD_INLINE T ntohx(T num)
113{
114#ifdef UHD_BIG_ENDIAN
115 return num;
116#else
117 return uhd::byteswap(num);
118#endif
119}
120
121template <typename T> UHD_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
130template <typename T> UHD_INLINE T wtohx(T num)
131{
132#ifdef UHD_BIG_ENDIAN
133 return uhd::byteswap(num);
134#else
135 return num;
136#endif
137}
138
139template <typename T> UHD_INLINE T htowx(T num)
140{
141#ifdef UHD_BIG_ENDIAN
142 return uhd::byteswap(num);
143#else
144 return num;
145#endif
146}
147
148} /* namespace uhd */
149
150#endif /* INCLUDED_UHD_UTILS_BYTESWAP_IPP */
#define UHD_INLINE
Definition config.h:53
Definition build_info.hpp:13
T ntohx(T)
network to host: short, long, or long-long
Definition byteswap.ipp:112
uint16_t byteswap(uint16_t)
perform a byteswap on a 16 bit integer
Definition byteswap.ipp:88
T htowx(T)
host to worknet: short, long, or long-long
Definition byteswap.ipp:139
T wtohx(T)
worknet to host: short, long, or long-long
Definition byteswap.ipp:130
T htonx(T)
host to network: short, long, or long-long
Definition byteswap.ipp:121