USRP Hardware Driver and USRP Manual: byteswap.ipp Source File

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