Statistics
| Branch: | Tag: | Revision:

root / host / include / uhd / utils / byteswap.ipp @ 4caf9db3

History | View | Annotate | Download (4.01 KB)

1
//
2
// Copyright 2010-2011 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#ifndef INCLUDED_UHD_UTILS_BYTESWAP_IPP
19
#define INCLUDED_UHD_UTILS_BYTESWAP_IPP
20

    
21
/***********************************************************************
22
 * Platform-specific implementation details for byteswap below:
23
 **********************************************************************/
24
#if defined(BOOST_MSVC) //http://msdn.microsoft.com/en-us/library/a3140177%28VS.80%29.aspx
25
    #include <cstdlib>
26

    
27
    UHD_INLINE boost::uint16_t uhd::byteswap(boost::uint16_t x){
28
        return _byteswap_ushort(x);
29
    }
30

    
31
    UHD_INLINE boost::uint32_t uhd::byteswap(boost::uint32_t x){
32
        return _byteswap_ulong(x);
33
    }
34

    
35
    UHD_INLINE boost::uint64_t uhd::byteswap(boost::uint64_t x){
36
        return _byteswap_uint64(x);
37
    }
38

    
39
#elif defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 2
40

    
41
    UHD_INLINE boost::uint16_t uhd::byteswap(boost::uint16_t x){
42
        return (x>>8) | (x<<8); //DNE return __builtin_bswap16(x);
43
    }
44

    
45
    UHD_INLINE boost::uint32_t uhd::byteswap(boost::uint32_t x){
46
        return __builtin_bswap32(x);
47
    }
48

    
49
    UHD_INLINE boost::uint64_t uhd::byteswap(boost::uint64_t x){
50
        return __builtin_bswap64(x);
51
    }
52

    
53
#elif defined(UHD_PLATFORM_MACOS)
54
    #include <libkern/OSByteOrder.h>
55

    
56
    UHD_INLINE boost::uint16_t uhd::byteswap(boost::uint16_t x){
57
        return OSSwapInt16(x);
58
    }
59

    
60
    UHD_INLINE boost::uint32_t uhd::byteswap(boost::uint32_t x){
61
        return OSSwapInt32(x);
62
    }
63

    
64
    UHD_INLINE boost::uint64_t uhd::byteswap(boost::uint64_t x){
65
        return OSSwapInt64(x);
66
    }
67

    
68
#elif defined(UHD_PLATFORM_LINUX)
69
    #include <byteswap.h>
70

    
71
    UHD_INLINE boost::uint16_t uhd::byteswap(boost::uint16_t x){
72
        return bswap_16(x);
73
    }
74

    
75
    UHD_INLINE boost::uint32_t uhd::byteswap(boost::uint32_t x){
76
        return bswap_32(x);
77
    }
78

    
79
    UHD_INLINE boost::uint64_t uhd::byteswap(boost::uint64_t x){
80
        return bswap_64(x);
81
    }
82

    
83
#else //http://www.koders.com/c/fidB93B34CD44F0ECF724F1A4EAE3854BA2FE692F59.aspx
84

    
85
    UHD_INLINE boost::uint16_t uhd::byteswap(boost::uint16_t x){
86
        return (x>>8) | (x<<8);
87
    }
88

    
89
    UHD_INLINE boost::uint32_t uhd::byteswap(boost::uint32_t x){
90
        return (boost::uint32_t(uhd::byteswap(boost::uint16_t(x&0xfffful)))<<16) | (uhd::byteswap(boost::uint16_t(x>>16)));
91
    }
92

    
93
    UHD_INLINE boost::uint64_t uhd::byteswap(boost::uint64_t x){
94
        return (boost::uint64_t(uhd::byteswap(boost::uint32_t(x&0xffffffffull)))<<32) | (uhd::byteswap(boost::uint32_t(x>>32)));
95
    }
96

    
97
#endif
98

    
99
/***********************************************************************
100
 * Define the templated network to/from host conversions
101
 **********************************************************************/
102
#include <boost/detail/endian.hpp>
103

    
104
template<typename T> UHD_INLINE T uhd::ntohx(T num){
105
    #ifdef BOOST_BIG_ENDIAN
106
        return num;
107
    #else
108
        return uhd::byteswap(num);
109
    #endif
110
}
111

    
112
template<typename T> UHD_INLINE T uhd::htonx(T num){
113
    #ifdef BOOST_BIG_ENDIAN
114
        return num;
115
    #else
116
        return uhd::byteswap(num);
117
    #endif
118
}
119

    
120
template<typename T> UHD_INLINE T uhd::wtohx(T num){
121
    #ifdef BOOST_BIG_ENDIAN
122
        return uhd::byteswap(num);
123
    #else
124
        return num;
125
    #endif
126
}
127

    
128
template<typename T> UHD_INLINE T uhd::htowx(T num){
129
    #ifdef BOOST_BIG_ENDIAN
130
        return uhd::byteswap(num);
131
    #else
132
        return num;
133
    #endif
134
}
135

    
136
#endif /* INCLUDED_UHD_UTILS_BYTESWAP_IPP */