init
[protos/xbee-avr.git] / aversive / endian.h
1 /*
2  *  Copyright Droids Corporation (2011)
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 2 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, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: md5c.c,v 1.3.4.1 2006-11-26 21:06:02 zer0 Exp $
19  *
20  */
21
22 #ifndef _AVERSIVE_ENDIAN_H_
23 #define _AVERSIVE_ENDIAN_H_
24
25 static inline uint16_t bswap16(uint16_t x)
26 {
27         return (uint16_t)(((x & 0x00ffU) << 8) |
28                           ((x & 0xff00U) >> 8));
29 }
30
31 static inline uint32_t bswap32(uint32_t x)
32 {
33         return  ((x & 0x000000ffUL) << 24) |
34                 ((x & 0x0000ff00UL) << 8) |
35                 ((x & 0x00ff0000UL) >> 8) |
36                 ((x & 0xff000000UL) >> 24);
37 }
38
39 static inline uint64_t bswap64(uint64_t x)
40 {
41         return  ((x & 0x00000000000000ffULL) << 56) |
42                 ((x & 0x000000000000ff00ULL) << 40) |
43                 ((x & 0x0000000000ff0000ULL) << 24) |
44                 ((x & 0x00000000ff000000ULL) <<  8) |
45                 ((x & 0x000000ff00000000ULL) >>  8) |
46                 ((x & 0x0000ff0000000000ULL) >> 24) |
47                 ((x & 0x00ff000000000000ULL) >> 40) |
48                 ((x & 0xff00000000000000ULL) >> 56);
49 }
50
51 #if BYTE_ORDER == LITTLE_ENDIAN
52 #define ntohs(x) bswap16(x)
53 #define ntohl(x) bswap32(x)
54 #define ntohll(x) bswap64(x)
55 #else
56 #define ntohs(x) (x)
57 #define ntohl(x) (x)
58 #define ntohll(x) (x)
59 #endif
60
61 #define htons ntohs
62 #define htonl ntohl
63 #define htonll ntohll
64
65 #endif