blob: 5969c22574b5a83cd9718c13ba7861da8d11e38d [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/*
Elliott Hughescec480a2017-12-19 16:54:57 -080023 * For 8-bit values; provided for the sake of completeness. Byte order
24 * isn't relevant, and alignment isn't an issue.
The Android Open Source Project2949f582009-03-03 19:30:46 -080025 */
Elliott Hughescec480a2017-12-19 16:54:57 -080026#define EXTRACT_8BITS(p) (*(p))
27#define EXTRACT_LE_8BITS(p) (*(p))
28
29/*
30 * Inline functions or macros to extract possibly-unaligned big-endian
31 * integral values.
32 */
33#include "funcattrs.h"
34
35/*
36 * If we have versions of GCC or Clang that support an __attribute__
37 * to say "if we're building with unsigned behavior sanitization,
38 * don't complain about undefined behavior in this function", we
39 * label these functions with that attribute - we *know* it's undefined
40 * in the C standard, but we *also* know it does what we want with
41 * the ISA we're targeting and the compiler we're using.
42 *
43 * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined));
44 * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether
45 * GCC or Clang first had __attribute__((no_sanitize(XXX)).
46 *
47 * For Clang, we check for __attribute__((no_sanitize(XXX)) with
48 * __has_attribute, as there are versions of Clang that support
49 * __attribute__((no_sanitize("undefined")) but don't support
50 * __attribute__((no_sanitize_undefined)).
51 *
52 * We define this here, rather than in funcattrs.h, because we
53 * only want it used here, we don't want it to be broadly used.
54 * (Any printer will get this defined, but this should at least
55 * make it harder for people to find.)
56 */
57#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409)
58#define UNALIGNED_OK __attribute__((no_sanitize_undefined))
59#elif __has_attribute(no_sanitize)
60#define UNALIGNED_OK __attribute__((no_sanitize("undefined")))
61#else
62#define UNALIGNED_OK
63#endif
64
The Android Open Source Project2949f582009-03-03 19:30:46 -080065#ifdef LBL_ALIGN
66/*
67 * The processor doesn't natively handle unaligned loads.
68 */
JP Abgrall53f17a92014-02-12 14:02:41 -080069#if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
70 (defined(__alpha) || defined(__alpha__) || \
71 defined(__mips) || defined(__mips__))
72
The Android Open Source Project2949f582009-03-03 19:30:46 -080073/*
Elliott Hughescec480a2017-12-19 16:54:57 -080074* This is a GCC-compatible compiler and we have __attribute__, which
JP Abgrall53f17a92014-02-12 14:02:41 -080075 * we assume that mean we have __attribute__((packed)), and this is
76 * MIPS or Alpha, which has instructions that can help when doing
77 * unaligned loads.
78 *
Elliott Hughes892a68b2015-10-19 14:43:53 -070079 * Declare packed structures containing a uint16_t and a uint32_t,
The Android Open Source Project2949f582009-03-03 19:30:46 -080080 * cast the pointer to point to one of those, and fetch through it;
81 * the GCC manual doesn't appear to explicitly say that
82 * __attribute__((packed)) causes the compiler to generate unaligned-safe
83 * code, but it apppears to do so.
84 *
JP Abgrall53f17a92014-02-12 14:02:41 -080085 * We do this in case the compiler can generate code using those
86 * instructions to do an unaligned load and pass stuff to "ntohs()" or
87 * "ntohl()", which might be better than than the code to fetch the
88 * bytes one at a time and assemble them. (That might not be the
89 * case on a little-endian platform, such as DEC's MIPS machines and
90 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
91 * inline.)
92 *
93 * We do this only for specific architectures because, for example,
94 * at least some versions of GCC, when compiling for 64-bit SPARC,
95 * generate code that assumes alignment if we do this.
96 *
97 * XXX - add other architectures and compilers as possible and
98 * appropriate.
99 *
100 * HP's C compiler, indicated by __HP_cc being defined, supports
101 * "#pragma unaligned N" in version A.05.50 and later, where "N"
102 * specifies a number of bytes at which the typedef on the next
103 * line is aligned, e.g.
104 *
105 * #pragma unalign 1
Elliott Hughes892a68b2015-10-19 14:43:53 -0700106 * typedef uint16_t unaligned_uint16_t;
JP Abgrall53f17a92014-02-12 14:02:41 -0800107 *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700108 * to define unaligned_uint16_t as a 16-bit unaligned data type.
JP Abgrall53f17a92014-02-12 14:02:41 -0800109 * This could be presumably used, in sufficiently recent versions of
110 * the compiler, with macros similar to those below. This would be
111 * useful only if that compiler could generate better code for PA-RISC
112 * or Itanium than would be generated by a bunch of shifts-and-ORs.
113 *
114 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
115 * an __unaligned qualifier that can be applied to pointers to get the
116 * compiler to generate code that does unaligned loads and stores when
117 * dereferencing the pointer in question.
118 *
119 * XXX - what if the native C compiler doesn't support
120 * __attribute__((packed))? How can we get it to generate unaligned
121 * accesses for *specific* items?
The Android Open Source Project2949f582009-03-03 19:30:46 -0800122 */
123typedef struct {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700124 uint16_t val;
125} __attribute__((packed)) unaligned_uint16_t;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800126
127typedef struct {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700128 uint32_t val;
129} __attribute__((packed)) unaligned_uint32_t;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800130
Elliott Hughescec480a2017-12-19 16:54:57 -0800131UNALIGNED_OK static inline uint16_t
JP Abgrall53f17a92014-02-12 14:02:41 -0800132EXTRACT_16BITS(const void *p)
133{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700134 return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val));
JP Abgrall53f17a92014-02-12 14:02:41 -0800135}
The Android Open Source Project2949f582009-03-03 19:30:46 -0800136
Elliott Hughescec480a2017-12-19 16:54:57 -0800137UNALIGNED_OK static inline uint32_t
JP Abgrall53f17a92014-02-12 14:02:41 -0800138EXTRACT_32BITS(const void *p)
139{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700140 return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
JP Abgrall53f17a92014-02-12 14:02:41 -0800141}
142
Elliott Hughescec480a2017-12-19 16:54:57 -0800143UNALIGNED_OK static inline uint64_t
JP Abgrall53f17a92014-02-12 14:02:41 -0800144EXTRACT_64BITS(const void *p)
145{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700146 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 |
Elliott Hughes892a68b2015-10-19 14:43:53 -0700147 ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0));
JP Abgrall53f17a92014-02-12 14:02:41 -0800148}
149
150#else /* have to do it a byte at a time */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800151/*
JP Abgrall53f17a92014-02-12 14:02:41 -0800152 * This isn't a GCC-compatible compiler, we don't have __attribute__,
153 * or we do but we don't know of any better way with this instruction
154 * set to do unaligned loads, so do unaligned loads of big-endian
The Android Open Source Project2949f582009-03-03 19:30:46 -0800155 * quantities the hard way - fetch the bytes one at a time and
156 * assemble them.
157 */
158#define EXTRACT_16BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700159 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
160 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800161#define EXTRACT_32BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700162 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
163 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
164 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
165 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800166#define EXTRACT_64BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700167 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
168 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
169 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
170 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
171 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
172 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
173 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
174 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
JP Abgrall53f17a92014-02-12 14:02:41 -0800175#endif /* must special-case unaligned accesses */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800176#else /* LBL_ALIGN */
177/*
178 * The processor natively handles unaligned loads, so we can just
179 * cast the pointer and fetch through it.
180 */
Elliott Hughescec480a2017-12-19 16:54:57 -0800181static inline uint16_t UNALIGNED_OK
JP Abgrall53f17a92014-02-12 14:02:41 -0800182EXTRACT_16BITS(const void *p)
183{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700184 return ((uint16_t)ntohs(*(const uint16_t *)(p)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800185}
186
Elliott Hughescec480a2017-12-19 16:54:57 -0800187static inline uint32_t UNALIGNED_OK
JP Abgrall53f17a92014-02-12 14:02:41 -0800188EXTRACT_32BITS(const void *p)
189{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700190 return ((uint32_t)ntohl(*(const uint32_t *)(p)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800191}
192
Elliott Hughescec480a2017-12-19 16:54:57 -0800193static inline uint64_t UNALIGNED_OK
JP Abgrall53f17a92014-02-12 14:02:41 -0800194EXTRACT_64BITS(const void *p)
195{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700196 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 |
Elliott Hughes892a68b2015-10-19 14:43:53 -0700197 ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
JP Abgrall53f17a92014-02-12 14:02:41 -0800198
199}
200
The Android Open Source Project2949f582009-03-03 19:30:46 -0800201#endif /* LBL_ALIGN */
202
203#define EXTRACT_24BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700204 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
205 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
206 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
207
208#define EXTRACT_40BITS(p) \
209 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
210 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
211 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
212 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
213 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
214
215#define EXTRACT_48BITS(p) \
216 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
217 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
218 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
219 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
220 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
221 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
222
223#define EXTRACT_56BITS(p) \
224 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
225 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
226 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
227 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
228 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
229 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
230 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800231
232/*
233 * Macros to extract possibly-unaligned little-endian integral values.
234 * XXX - do loads on little-endian machines that support unaligned loads?
235 */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800236#define EXTRACT_LE_16BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700237 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
238 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800239#define EXTRACT_LE_32BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700240 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
241 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
242 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
243 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
JP Abgrall53f17a92014-02-12 14:02:41 -0800244#define EXTRACT_LE_24BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700245 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
246 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
247 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800248#define EXTRACT_LE_64BITS(p) \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700249 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
250 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
251 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
252 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
253 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
254 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
255 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
256 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700257
258/*
259 * Macros to check the presence of the values in question.
260 */
261#define ND_TTEST_8BITS(p) ND_TTEST2(*(p), 1)
262#define ND_TCHECK_8BITS(p) ND_TCHECK2(*(p), 1)
263
264#define ND_TTEST_16BITS(p) ND_TTEST2(*(p), 2)
265#define ND_TCHECK_16BITS(p) ND_TCHECK2(*(p), 2)
266
267#define ND_TTEST_24BITS(p) ND_TTEST2(*(p), 3)
268#define ND_TCHECK_24BITS(p) ND_TCHECK2(*(p), 3)
269
270#define ND_TTEST_32BITS(p) ND_TTEST2(*(p), 4)
271#define ND_TCHECK_32BITS(p) ND_TCHECK2(*(p), 4)
272
273#define ND_TTEST_40BITS(p) ND_TTEST2(*(p), 5)
274#define ND_TCHECK_40BITS(p) ND_TCHECK2(*(p), 5)
275
276#define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6)
277#define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6)
278
279#define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7)
280#define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7)
281
282#define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8)
283#define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8)
Elliott Hughescec480a2017-12-19 16:54:57 -0800284
285#define ND_TTEST_128BITS(p) ND_TTEST2(*(p), 16)
286#define ND_TCHECK_128BITS(p) ND_TCHECK2(*(p), 16)