USRP Hardware Driver and USRP Manual Version: 4.7.0.0-0-unknown
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
17UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
18{
19 return _byteswap_ushort(x);
20}
21
22UHD_INLINE uint32_t uhd::byteswap(uint32_t x)
23{
24 return _byteswap_ulong(x);
25}
26
27UHD_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
35UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
36{
37 return OSSwapInt16(x);
38}
39
40UHD_INLINE uint32_t uhd::byteswap(uint32_t x)
41{
42 return OSSwapInt32(x);
43}
44
45UHD_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
52UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
53{
54 return (x >> 8) | (x << 8); // DNE return __builtin_bswap16(x);
55}
56
57UHD_INLINE uint32_t uhd::byteswap(uint32_t x)
58{
59 return __builtin_bswap32(x);
60}
61
62UHD_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
70UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
71{
72 return bswap_16(x);
73}
74
75UHD_INLINE uint32_t uhd::byteswap(uint32_t x)
76{
77 return bswap_32(x);
78}
79
80UHD_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
87UHD_INLINE uint16_t uhd::byteswap(uint16_t x)
88{
89 return (x >> 8) | (x << 8);
90}
91
92UHD_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
98UHD_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>
113{
114#ifdef UHD_BIG_ENDIAN
115 return num;
116#else
117 return uhd::byteswap(num);
118#endif
119}
120
121template <typename T>
123{
124#ifdef UHD_BIG_ENDIAN
125 return num;
126#else
127 return uhd::byteswap(num);
128#endif
129}
130
131template <typename T>
133{
134#ifdef UHD_BIG_ENDIAN
135 return uhd::byteswap(num);
136#else
137 return num;
138#endif
139}
140
141template <typename T>
143{
144#ifdef UHD_BIG_ENDIAN
145 return uhd::byteswap(num);
146#else
147 return num;
148#endif
149}
150
151} /* namespace uhd */
#define UHD_INLINE
Definition config.h:65
Definition build_info.hpp:12
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:87
T htowx(T)
host to worknet: short, long, or long-long
Definition byteswap.ipp:142
T wtohx(T)
worknet to host: short, long, or long-long
Definition byteswap.ipp:132
T htonx(T)
host to network: short, long, or long-long
Definition byteswap.ipp:122