2 * Copyright (c) 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 static inline uint16_t get_unaligned_u16(const uint16_t *);
25 static inline uint32_t get_unaligned_u32(const uint32_t *);
26 static inline uint64_t get_unaligned_u64(const uint64_t *);
27 static inline void put_unaligned_u16(uint16_t *, uint16_t);
28 static inline void put_unaligned_u32(uint32_t *, uint32_t);
29 static inline void put_unaligned_u64(uint64_t *, uint64_t);
32 /* GCC implementations. */
33 #define GCC_UNALIGNED_ACCESSORS(SIZE) \
34 struct unaligned_u##SIZE { \
35 uint##SIZE##_t x __attribute__((__packed__)); \
37 static inline struct unaligned_u##SIZE * \
38 unaligned_u##SIZE(const uint##SIZE##_t *p) \
40 return (struct unaligned_u##SIZE *) p; \
43 static inline uint##SIZE##_t \
44 get_unaligned_u##SIZE(const uint##SIZE##_t *p) \
46 return unaligned_u##SIZE(p)->x; \
50 put_unaligned_u##SIZE(uint##SIZE##_t *p, uint##SIZE##_t x) \
52 unaligned_u##SIZE(p)->x = x; \
55 GCC_UNALIGNED_ACCESSORS(16);
56 GCC_UNALIGNED_ACCESSORS(32);
57 GCC_UNALIGNED_ACCESSORS(64);
59 /* Generic implementations. */
61 static inline uint16_t get_unaligned_u16(const uint16_t *p_)
63 const uint8_t *p = (const uint8_t *) p_;
64 return ntohs((p[0] << 8) | p[1]);
67 static inline void put_unaligned_u16(uint16_t *p_, uint16_t x_)
69 uint8_t *p = (uint8_t *) p_;
70 uint16_t x = ntohs(x_);
76 static inline uint32_t get_unaligned_u32(const uint32_t *p_)
78 const uint8_t *p = (const uint8_t *) p_;
79 return ntohl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
82 static inline void put_unaligned_u32(uint32_t *p_, uint32_t x_)
84 uint8_t *p = (uint8_t *) p_;
85 uint32_t x = ntohl(x_);
93 static inline uint64_t get_unaligned_u64(const uint64_t *p_)
95 const uint8_t *p = (const uint8_t *) p_;
96 return ntohll(((uint64_t) p[0] << 56)
97 | ((uint64_t) p[1] << 48)
98 | ((uint64_t) p[2] << 40)
99 | ((uint64_t) p[3] << 32)
106 static inline void put_unaligned_u64(uint64_t *p_, uint64_t x_)
108 uint8_t *p = (uint8_t *) p_;
109 uint64_t x = ntohll(x_);
122 #endif /* unaligned.h */