5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #define get_unaligned(ptr) \
27 struct __attribute__((packed)) { \
29 } *__p = (typeof(__p)) (ptr); \
33 #define put_unaligned(val, ptr) \
35 struct __attribute__((packed)) { \
37 } *__p = (typeof(__p)) (ptr); \
41 #if __BYTE_ORDER == __LITTLE_ENDIAN
42 static inline uint64_t get_le64(const void *ptr)
44 return get_unaligned((const uint64_t *) ptr);
47 static inline uint64_t get_be64(const void *ptr)
49 return bswap_64(get_unaligned((const uint64_t *) ptr));
52 static inline uint32_t get_le32(const void *ptr)
54 return get_unaligned((const uint32_t *) ptr);
57 static inline uint32_t get_be32(const void *ptr)
59 return bswap_32(get_unaligned((const uint32_t *) ptr));
62 static inline uint16_t get_le16(const void *ptr)
64 return get_unaligned((const uint16_t *) ptr);
67 static inline uint16_t get_be16(const void *ptr)
69 return bswap_16(get_unaligned((const uint16_t *) ptr));
72 static inline void put_be16(uint16_t val, void *ptr)
74 put_unaligned(bswap_16(val), (uint16_t *) ptr);
77 static inline void put_be32(uint32_t val, void *ptr)
79 put_unaligned(bswap_32(val), (uint32_t *) ptr);
82 static inline void put_le16(uint16_t val, void *ptr)
84 put_unaligned(val, (uint16_t *) ptr);
87 static inline void put_le32(uint32_t val, void *ptr)
89 put_unaligned(val, (uint32_t *) ptr);
92 static inline void put_be64(uint64_t val, void *ptr)
94 put_unaligned(bswap_64(val), (uint64_t *) ptr);
97 static inline void put_le64(uint64_t val, void *ptr)
99 put_unaligned(val, (uint64_t *) ptr);
101 #elif __BYTE_ORDER == __BIG_ENDIAN
102 static inline uint64_t get_le64(const void *ptr)
104 return bswap_64(get_unaligned((const uint64_t *) ptr));
107 static inline uint64_t get_be64(const void *ptr)
109 return get_unaligned((const uint64_t *) ptr);
112 static inline uint32_t get_le32(const void *ptr)
114 return bswap_32(get_unaligned((const uint32_t *) ptr));
117 static inline uint32_t get_be32(const void *ptr)
119 return get_unaligned((const uint32_t *) ptr);
122 static inline uint16_t get_le16(const void *ptr)
124 return bswap_16(get_unaligned((const uint16_t *) ptr));
127 static inline uint16_t get_be16(const void *ptr)
129 return get_unaligned((const uint16_t *) ptr);
132 static inline void put_be16(uint16_t val, void *ptr)
134 put_unaligned(val, (uint16_t *) ptr);
137 static inline void put_be32(uint32_t val, void *ptr)
139 put_unaligned(val, (uint32_t *) ptr);
142 static inline void put_le16(uint16_t val, void *ptr)
144 put_unaligned(bswap_16(val), (uint16_t *) ptr);
147 static inline void put_le32(uint32_t val, void *ptr)
149 put_unaligned(bswap_32(val), (uint32_t *) ptr);
152 static inline void put_be64(uint64_t val, void *ptr)
154 put_unaligned(val, (uint64_t *) ptr);
157 static inline void put_le64(uint64_t val, void *ptr)
159 put_unaligned(bswap_64(val), (uint64_t *) ptr);
162 #error "Unknown byte order"