Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / lib / usrp2_bytesex.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #ifndef INCLUDED_USRP2_BYTESEX_H
19 #define INCLUDED_USRP2_BYTESEX_H
20
21 // The USRP2 speaks big-endian...
22 // Use the standard include files or provide substitutions for
23 // htons and friends
24
25 #if defined(HAVE_ARPA_INET_H)
26 #include <arpa/inet.h>
27 #elif defined(HAVE_NETINET_IN_H)
28 #include <netinet/in.h>
29 #else
30 #include <stdint.h>
31
32 #ifdef WORDS_BIGENDIAN  // nothing to do...
33
34 static inline uint32_t htonl(uint32_t x){ return x; }
35 static inline uint16_t htons(uint16_t x){ return x; }
36 static inline uint32_t ntohl(uint32_t x){ return x; }
37 static inline uint16_t ntohs(uint16_t x){ return x; }
38
39 #else
40
41 #ifdef HAVE_BYTESWAP_H
42 #include <byteswap.h>
43 #else
44
45 static inline uint16_t
46 bswap_16 (uint16_t x)
47 {
48   return ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8));
49 }
50
51 static inline uint32_t
52 bswap_32 (uint32_t x)
53 {
54   return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) \
55         | (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24));
56 }
57 #endif
58
59 static inline uint32_t htonl(uint32_t x){ return bswap_32(x); }
60 static inline uint16_t htons(uint16_t x){ return bswap_16(x); }
61 static inline uint32_t ntohl(uint32_t x){ return bswap_32(x); }
62 static inline uint16_t ntohs(uint16_t x){ return bswap_16(x); }
63
64 #endif
65 #endif
66 #endif /* INCLUDED_USRP2_BYTESEX_H */