blob: cb62ebddb61e9a7f0c85a5af782f1afd20b1dd1d [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
2 * Copyright (c) 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
The Android Open Source Project2949f582009-03-03 19:30:46 -080020 */
21
22/*
23 * Macros to extract possibly-unaligned big-endian integral values.
24 */
25#ifdef LBL_ALIGN
26/*
27 * The processor doesn't natively handle unaligned loads.
28 */
JP Abgrall53f17a92014-02-12 14:02:41 -080029#if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
30 (defined(__alpha) || defined(__alpha__) || \
31 defined(__mips) || defined(__mips__))
32
The Android Open Source Project2949f582009-03-03 19:30:46 -080033/*
JP Abgrall53f17a92014-02-12 14:02:41 -080034 * This is a GCC-compatible compiler and we have __attribute__, which
35 * we assume that mean we have __attribute__((packed)), and this is
36 * MIPS or Alpha, which has instructions that can help when doing
37 * unaligned loads.
38 *
Elliott Hughes892a68b2015-10-19 14:43:53 -070039 * Declare packed structures containing a uint16_t and a uint32_t,
The Android Open Source Project2949f582009-03-03 19:30:46 -080040 * cast the pointer to point to one of those, and fetch through it;
41 * the GCC manual doesn't appear to explicitly say that
42 * __attribute__((packed)) causes the compiler to generate unaligned-safe
43 * code, but it apppears to do so.
44 *
JP Abgrall53f17a92014-02-12 14:02:41 -080045 * We do this in case the compiler can generate code using those
46 * instructions to do an unaligned load and pass stuff to "ntohs()" or
47 * "ntohl()", which might be better than than the code to fetch the
48 * bytes one at a time and assemble them. (That might not be the
49 * case on a little-endian platform, such as DEC's MIPS machines and
50 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
51 * inline.)
52 *
53 * We do this only for specific architectures because, for example,
54 * at least some versions of GCC, when compiling for 64-bit SPARC,
55 * generate code that assumes alignment if we do this.
56 *
57 * XXX - add other architectures and compilers as possible and
58 * appropriate.
59 *
60 * HP's C compiler, indicated by __HP_cc being defined, supports
61 * "#pragma unaligned N" in version A.05.50 and later, where "N"
62 * specifies a number of bytes at which the typedef on the next
63 * line is aligned, e.g.
64 *
65 * #pragma unalign 1
Elliott Hughes892a68b2015-10-19 14:43:53 -070066 * typedef uint16_t unaligned_uint16_t;
JP Abgrall53f17a92014-02-12 14:02:41 -080067 *
Elliott Hughes892a68b2015-10-19 14:43:53 -070068 * to define unaligned_uint16_t as a 16-bit unaligned data type.
JP Abgrall53f17a92014-02-12 14:02:41 -080069 * This could be presumably used, in sufficiently recent versions of
70 * the compiler, with macros similar to those below. This would be
71 * useful only if that compiler could generate better code for PA-RISC
72 * or Itanium than would be generated by a bunch of shifts-and-ORs.
73 *
74 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
75 * an __unaligned qualifier that can be applied to pointers to get the
76 * compiler to generate code that does unaligned loads and stores when
77 * dereferencing the pointer in question.
78 *
79 * XXX - what if the native C compiler doesn't support
80 * __attribute__((packed))? How can we get it to generate unaligned
81 * accesses for *specific* items?
The Android Open Source Project2949f582009-03-03 19:30:46 -080082 */
83typedef struct {
Elliott Hughes892a68b2015-10-19 14:43:53 -070084 uint16_t val;
85} __attribute__((packed)) unaligned_uint16_t;
The Android Open Source Project2949f582009-03-03 19:30:46 -080086
87typedef struct {
Elliott Hughes892a68b2015-10-19 14:43:53 -070088 uint32_t val;
89} __attribute__((packed)) unaligned_uint32_t;
The Android Open Source Project2949f582009-03-03 19:30:46 -080090
Elliott Hughes892a68b2015-10-19 14:43:53 -070091static inline uint16_t
JP Abgrall53f17a92014-02-12 14:02:41 -080092EXTRACT_16BITS(const void *p)
93{
Elliott Hughes892a68b2015-10-19 14:43:53 -070094 return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val));
JP Abgrall53f17a92014-02-12 14:02:41 -080095}
The Android Open Source Project2949f582009-03-03 19:30:46 -080096
Elliott Hughes892a68b2015-10-19 14:43:53 -070097static inline uint32_t
JP Abgrall53f17a92014-02-12 14:02:41 -080098EXTRACT_32BITS(const void *p)
99{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700100 return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
JP Abgrall53f17a92014-02-12 14:02:41 -0800101}
102
Elliott Hughes892a68b2015-10-19 14:43:53 -0700103static inline uint64_t
JP Abgrall53f17a92014-02-12 14:02:41 -0800104EXTRACT_64BITS(const void *p)
105{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700106 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 | \
107 ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0));
JP Abgrall53f17a92014-02-12 14:02:41 -0800108}
109
110#else /* have to do it a byte at a time */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800111/*
JP Abgrall53f17a92014-02-12 14:02:41 -0800112 * This isn't a GCC-compatible compiler, we don't have __attribute__,
113 * or we do but we don't know of any better way with this instruction
114 * set to do unaligned loads, so do unaligned loads of big-endian
The Android Open Source Project2949f582009-03-03 19:30:46 -0800115 * quantities the hard way - fetch the bytes one at a time and
116 * assemble them.
117 */
118#define EXTRACT_16BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700119 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
120 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800121#define EXTRACT_32BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700122 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
123 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
124 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
125 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800126#define EXTRACT_64BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700127 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
128 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
129 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
130 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
131 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
132 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
133 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
134 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
JP Abgrall53f17a92014-02-12 14:02:41 -0800135#endif /* must special-case unaligned accesses */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800136#else /* LBL_ALIGN */
137/*
138 * The processor natively handles unaligned loads, so we can just
139 * cast the pointer and fetch through it.
140 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700141static inline uint16_t
JP Abgrall53f17a92014-02-12 14:02:41 -0800142EXTRACT_16BITS(const void *p)
143{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700144 return ((uint16_t)ntohs(*(const uint16_t *)(p)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800145}
146
Elliott Hughes892a68b2015-10-19 14:43:53 -0700147static inline uint32_t
JP Abgrall53f17a92014-02-12 14:02:41 -0800148EXTRACT_32BITS(const void *p)
149{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700150 return ((uint32_t)ntohl(*(const uint32_t *)(p)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800151}
152
Elliott Hughes892a68b2015-10-19 14:43:53 -0700153static inline uint64_t
JP Abgrall53f17a92014-02-12 14:02:41 -0800154EXTRACT_64BITS(const void *p)
155{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700156 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 | \
157 ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
JP Abgrall53f17a92014-02-12 14:02:41 -0800158
159}
160
The Android Open Source Project2949f582009-03-03 19:30:46 -0800161#endif /* LBL_ALIGN */
162
163#define EXTRACT_24BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700164 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
165 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
166 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
167
168#define EXTRACT_40BITS(p) \
169 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
170 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
171 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
172 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
173 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
174
175#define EXTRACT_48BITS(p) \
176 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
177 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
178 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
179 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
180 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
181 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
182
183#define EXTRACT_56BITS(p) \
184 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
185 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
186 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
187 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
188 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
189 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
190 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800191
192/*
193 * Macros to extract possibly-unaligned little-endian integral values.
194 * XXX - do loads on little-endian machines that support unaligned loads?
195 */
196#define EXTRACT_LE_8BITS(p) (*(p))
197#define EXTRACT_LE_16BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700198 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
199 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800200#define EXTRACT_LE_32BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700201 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
202 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
203 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
204 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
JP Abgrall53f17a92014-02-12 14:02:41 -0800205#define EXTRACT_LE_24BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700206 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
207 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
208 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800209#define EXTRACT_LE_64BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700210 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
211 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
212 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
213 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
214 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
215 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
216 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
217 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))