OSDN Git Service

the 16/32 swap funcs were swapped #875
[uclinux-h8/uClibc.git] / utils / bswap.h
1 #ifndef _BSWAP_H
2 #define _BSWAP_H 1
3
4 #if !defined(__BYTE_ORDER) && defined(BYTE_ORDER)
5 #  define __BYTE_ORDER = BYTE_ORDER
6 #endif
7
8 #ifndef __BYTE_ORDER
9 #ifdef __linux__
10 #include <endian.h>
11 #else
12 #define __LITTLE_ENDIAN 1234    /* least-significant byte first (vax, pc) */
13 #define __BIG_ENDIAN    4321    /* most-significant byte first (IBM, net) */
14 #define __PDP_ENDIAN    3412    /* LSB first in word, MSW first in long (pdp) */
15
16 #if defined(sun386) || defined(i386)
17 #define __BYTE_ORDER    __LITTLE_ENDIAN
18 #endif
19
20 #if defined(sparc)
21 #define __BYTE_ORDER    __BIG_ENDIAN
22 #endif
23
24 #endif /* __linux__ */
25 #endif /* __BYTE_ORDER */
26
27
28 #ifndef __BYTE_ORDER
29 # error "Undefined __BYTE_ORDER"
30 #endif
31
32 #ifdef __linux__
33 #include <byteswap.h>
34 #else
35
36 static inline uint16_t bswap_16(uint16_t x)
37 {
38         return ((((x) & 0xff00) >> 8) | \
39                 (((x) & 0x00ff) << 8));
40 }
41 static inline uint32_t bswap_32(uint32_t x)
42 {
43         return ((((x) & 0xff000000) >> 24) | \
44                 (((x) & 0x00ff0000) >>  8) | \
45                 (((x) & 0x0000ff00) <<  8) | \
46                 (((x) & 0x000000ff) << 24));
47 }
48 #endif
49
50 #endif