1 /******************************************************************************
2  * Copyright (c) 2011 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12 
13 /*
14  * Common byteorder (endianness) macros
15  */
16 
17 #ifndef BYTEORDER_H
18 #define BYTEORDER_H
19 
20 #include <stdint.h>
21 
22 typedef uint16_t le16;
23 typedef uint32_t le32;
24 typedef uint64_t le64;
25 
26 static inline uint16_t bswap_16 (uint16_t x)
27 {
28 	return __builtin_bswap16(x);
29 }
30 
31 static inline uint32_t bswap_32 (uint32_t x)
32 {
33 	return __builtin_bswap32(x);
34 }
35 
36 static inline uint64_t bswap_64 (uint64_t x)
37 {
38 	return __builtin_bswap64(x);
39 }
40 
41 static inline void bswap_16p (uint16_t *x)
42 {
43 	*x = __builtin_bswap16(*x);
44 }
45 
46 static inline void bswap_32p (uint32_t *x)
47 {
48 	*x = __builtin_bswap32(*x);
49 }
50 
51 static inline void bswap_64p (uint64_t *x)
52 {
53 	*x = __builtin_bswap64(*x);
54 }
55 
56 
57 /* gcc defines __BIG_ENDIAN__ on big endian targets */
58 #ifdef __BIG_ENDIAN__
59 
60 #define cpu_to_be16(x) (x)
61 #define cpu_to_be32(x) (x)
62 #define cpu_to_be64(x) (x)
63 
64 #define be16_to_cpu(x) (x)
65 #define be32_to_cpu(x) (x)
66 #define be64_to_cpu(x) (x)
67 
68 #define le16_to_cpu(x) bswap_16(x)
69 #define le32_to_cpu(x) bswap_32(x)
70 #define le64_to_cpu(x) bswap_64(x)
71 
72 #define cpu_to_le16(x) bswap_16(x)
73 #define cpu_to_le32(x) bswap_32(x)
74 #define cpu_to_le64(x) bswap_64(x)
75 
76 #else
77 
78 #define cpu_to_be16(x) bswap_16(x)
79 #define cpu_to_be32(x) bswap_32(x)
80 #define cpu_to_be64(x) bswap_64(x)
81 
82 #define be16_to_cpu(x) bswap_16(x)
83 #define be32_to_cpu(x) bswap_32(x)
84 #define be64_to_cpu(x) bswap_64(x)
85 
86 #define le16_to_cpu(x) (x)
87 #define le32_to_cpu(x) (x)
88 #define le64_to_cpu(x) (x)
89 
90 #define cpu_to_le16(x) (x)
91 #define cpu_to_le32(x) (x)
92 #define cpu_to_le64(x) (x)
93 
94 #endif  /* __BIG_ENDIAN__ */
95 
96 #endif  /* BYTEORDER_H */
97