2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2009, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief Handle unaligned data access
23 #ifndef _ASTERISK_UNALIGNED_H
24 #define _ASTERISK_UNALIGNED_H
26 #if defined(__cplusplus) || defined(c_plusplus)
31 /* If we just tell GCC what's going on, we can trust it to behave optimally */
32 static inline uint64_t get_unaligned_uint64(const void *p)
34 const struct { uint64_t d; } __attribute__((packed)) *pp = p;
38 static inline unsigned int get_unaligned_uint32(const void *p)
40 const struct { unsigned int d; } __attribute__((packed)) *pp = p;
44 static inline unsigned short get_unaligned_uint16(const void *p)
46 const struct { unsigned short d; } __attribute__((packed)) *pp = p;
51 static inline void put_unaligned_uint64(void *p, uint64_t datum)
53 unsigned char *cp = p;
55 cp[0] = (datum >> 56) & 0xff;
56 cp[1] = (datum >> 48) & 0xff;
57 cp[2] = (datum >> 40) & 0xff;
58 cp[3] = (datum >> 32) & 0xff;
59 cp[4] = (datum >> 24) & 0xff;
60 cp[5] = (datum >> 16) & 0xff;
61 cp[6] = (datum >> 8) & 0xff;
62 cp[7] = (datum >> 0) & 0xff;
65 static inline void put_unaligned_uint32(void *p, unsigned int datum)
67 struct { unsigned int d; } __attribute__((packed)) *pp = p;
72 static inline void put_unaligned_uint16(void *p, unsigned short datum)
74 struct { unsigned short d; } __attribute__((packed)) *pp = p;
78 #elif defined(SOLARIS) && defined(__sparc__)
79 static inline uint64_t get_unaligned_uint64(const void *p)
81 const unsigned char *cp = p;
84 (((uint64_t) cp[0]) << 56) |
85 (((uint64_t) cp[1]) << 48) |
86 (((uint64_t) cp[2]) << 40) |
87 (((uint64_t) cp[3]) << 32) |
88 (((uint64_t) cp[4]) << 24) |
89 (((uint64_t) cp[5]) << 16) |
90 (((uint64_t) cp[6]) << 8) |
91 (((uint64_t) cp[7]) << 0);
94 static inline unsigned int get_unaligned_uint32(const void *p)
96 const unsigned char *cp = p;
98 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3];
101 static inline unsigned short get_unaligned_uint16(const void *p)
103 const unsigned char *cp = p;
105 return (cp[0] << 8) | cp[1] ;
108 static inline void put_unaligned_uint64(void *p, uint64_t datum)
110 unsigned char *cp = p;
112 cp[0] = (datum >> 56) & 0xff;
113 cp[1] = (datum >> 48) & 0xff;
114 cp[2] = (datum >> 40) & 0xff;
115 cp[3] = (datum >> 32) & 0xff;
116 cp[4] = (datum >> 24) & 0xff;
117 cp[5] = (datum >> 16) & 0xff;
118 cp[6] = (datum >> 8) & 0xff;
119 cp[7] = (datum >> 0) & 0xff;
122 static inline void put_unaligned_uint32(void *p, unsigned int datum)
124 unsigned char *cp = p;
132 static inline void put_unaligned_uint16(void *p, unsigned int datum)
134 unsigned char *cp = p;
139 #else /* Not GCC, not Solaris/SPARC. Assume we can handle direct load/store. */
140 #define get_unaligned_uint64(p) (*((uint64_t *)(p)))
141 #define get_unaligned_uint32(p) (*((unsigned int *)(p)))
142 #define get_unaligned_uint16(p) (*((unsigned short *)(p)))
143 #define put_unaligned_uint64(p,d) do { uint64_t *__P = (p); *__P = d; } while(0)
144 #define put_unaligned_uint32(p,d) do { unsigned int *__P = (p); *__P = d; } while(0)
145 #define put_unaligned_uint16(p,d) do { unsigned short *__P = (p); *__P = d; } while(0)
148 #if defined(__cplusplus) || defined(c_plusplus)
153 #endif /* _ASTERISK_UNALIGNED_H */