extract.h revision 1.9
1/* 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. 20 */ 21 22 23/* 24 * If we have versions of GCC or Clang that support an __attribute__ 25 * to say "if we're building with unsigned behavior sanitization, 26 * don't complain about undefined behavior in this function", we 27 * label these functions with that attribute - we *know* it's undefined 28 * in the C standard, but we *also* know it does what we want with 29 * the ISA we're targeting and the compiler we're using. 30 * 31 * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined)); 32 * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether 33 * GCC or Clang first had __attribute__((no_sanitize(XXX)). 34 * 35 * For Clang, we check for __attribute__((no_sanitize(XXX)) with 36 * __has_attribute, as there are versions of Clang that support 37 * __attribute__((no_sanitize("undefined")) but don't support 38 * __attribute__((no_sanitize_undefined)). 39 * 40 * We define this here, rather than in funcattrs.h, because we 41 * only want it used here, we don't want it to be broadly used. 42 * (Any printer will get this defined, but this should at least 43 * make it harder for people to find.) 44 */ 45#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409) 46#define UNALIGNED_OK __attribute__((no_sanitize_undefined)) 47#elif __has_attribute(no_sanitize) 48#define UNALIGNED_OK __attribute__((no_sanitize("undefined"))) 49#else 50#define UNALIGNED_OK 51#endif 52 53#ifdef __NetBSD__ 54#include <string.h> 55 56/* 57 * Do it the portable way and let the compiler optimize the code 58 */ 59static inline uint16_t EXTRACT_16BITS(const void *p) 60{ 61 uint16_t t; 62 memcpy(&t, p, sizeof(t)); 63 return ntohs(t); 64} 65 66static inline uint32_t EXTRACT_24BITS(const void *p) 67{ 68 uint8_t t[3]; 69 memcpy(t, p, sizeof(t)); 70 return 71 ((uint32_t)t[0] << 16) | 72 ((uint32_t)t[1] << 8) | 73 t[2]; 74} 75 76static inline uint32_t EXTRACT_32BITS(const void *p) 77{ 78 uint32_t t; 79 memcpy(&t, p, sizeof(t)); 80 return ntohl(t); 81} 82 83 84static inline uint64_t EXTRACT_48BITS(const void *p) 85{ 86 uint8_t t[6]; 87 memcpy(t, p, sizeof(t)); 88 return 89 ((uint64_t)t[0] << 40) | 90 ((uint64_t)t[1] << 32) | 91 ((uint64_t)t[2] << 24) | 92 ((uint64_t)t[3] << 16) | 93 ((uint64_t)t[4] << 8) | 94 t[5]; 95} 96 97static inline uint64_t EXTRACT_64BITS(const void *p) 98{ 99 uint32_t t[2]; 100 memcpy(&t[0], p, sizeof(t[0])); 101 memcpy(&t[1], (const uint8_t *)p + sizeof(t[0]), sizeof(t[1])); 102 return ((uint64_t)ntohl(t[0]) << 32) | ntohl(t[1]); 103} 104 105static inline uint8_t EXTRACT_LE_8BITS(const void *p) 106{ 107 uint8_t t[1]; 108 memcpy(t, p, sizeof(t)); 109 return t[0]; 110} 111 112static inline uint16_t EXTRACT_LE_16BITS(const void *p) 113{ 114 uint8_t t[2]; 115 memcpy(t, p, sizeof(t)); 116 return 117 ((uint16_t)t[1] << 8) | 118 t[0]; 119} 120 121static inline uint32_t EXTRACT_LE_24BITS(const void *p) 122{ 123 uint8_t t[3]; 124 memcpy(t, p, sizeof(t)); 125 return 126 ((uint32_t)t[2] << 16) | 127 ((uint32_t)t[1] << 8) | 128 t[0]; 129} 130 131static inline uint32_t EXTRACT_LE_32BITS(const void *p) 132{ 133 uint8_t t[4]; 134 memcpy(t, p, sizeof(t)); 135 return 136 ((uint32_t)t[3] << 24) | 137 ((uint32_t)t[2] << 16) | 138 ((uint32_t)t[1] << 8) | 139 t[0]; 140} 141 142static inline uint64_t EXTRACT_LE_64BITS(const void *p) 143{ 144 uint8_t t[8]; 145 memcpy(&t, p, sizeof(t)); 146 return 147 ((uint64_t)t[7] << 56) | 148 ((uint64_t)t[6] << 48) | 149 ((uint64_t)t[5] << 40) | 150 ((uint64_t)t[4] << 32) | 151 ((uint64_t)t[3] << 24) | 152 ((uint64_t)t[2] << 16) | 153 ((uint64_t)t[1] << 8) | 154 t[0]; 155} 156 157#define EXTRACT_8BITS(p) EXTRACT_LE_8BITS(p) 158 159#else /* Fast & Loose */ 160/* 161 * For 8-bit values; provided for the sake of completeness. Byte order 162 * isn't relevant, and alignment isn't an issue. 163 */ 164#define EXTRACT_8BITS(p) (*(p)) 165#define EXTRACT_LE_8BITS(p) (*(p)) 166 167/* 168 * Inline functions or macros to extract possibly-unaligned big-endian 169 * integral values. 170 */ 171#include "funcattrs.h" 172 173#ifdef LBL_ALIGN 174/* 175 * The processor doesn't natively handle unaligned loads. 176 */ 177#if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \ 178 (defined(__alpha) || defined(__alpha__) || \ 179 defined(__mips) || defined(__mips__)) 180 181/* 182* This is a GCC-compatible compiler and we have __attribute__, which 183 * we assume that mean we have __attribute__((packed)), and this is 184 * MIPS or Alpha, which has instructions that can help when doing 185 * unaligned loads. 186 * 187 * Declare packed structures containing a uint16_t and a uint32_t, 188 * cast the pointer to point to one of those, and fetch through it; 189 * the GCC manual doesn't appear to explicitly say that 190 * __attribute__((packed)) causes the compiler to generate unaligned-safe 191 * code, but it apppears to do so. 192 * 193 * We do this in case the compiler can generate code using those 194 * instructions to do an unaligned load and pass stuff to "ntohs()" or 195 * "ntohl()", which might be better than than the code to fetch the 196 * bytes one at a time and assemble them. (That might not be the 197 * case on a little-endian platform, such as DEC's MIPS machines and 198 * Alpha machines, where "ntohs()" and "ntohl()" might not be done 199 * inline.) 200 * 201 * We do this only for specific architectures because, for example, 202 * at least some versions of GCC, when compiling for 64-bit SPARC, 203 * generate code that assumes alignment if we do this. 204 * 205 * XXX - add other architectures and compilers as possible and 206 * appropriate. 207 * 208 * HP's C compiler, indicated by __HP_cc being defined, supports 209 * "#pragma unaligned N" in version A.05.50 and later, where "N" 210 * specifies a number of bytes at which the typedef on the next 211 * line is aligned, e.g. 212 * 213 * #pragma unalign 1 214 * typedef uint16_t unaligned_uint16_t; 215 * 216 * to define unaligned_uint16_t as a 16-bit unaligned data type. 217 * This could be presumably used, in sufficiently recent versions of 218 * the compiler, with macros similar to those below. This would be 219 * useful only if that compiler could generate better code for PA-RISC 220 * or Itanium than would be generated by a bunch of shifts-and-ORs. 221 * 222 * DEC C, indicated by __DECC being defined, has, at least on Alpha, 223 * an __unaligned qualifier that can be applied to pointers to get the 224 * compiler to generate code that does unaligned loads and stores when 225 * dereferencing the pointer in question. 226 * 227 * XXX - what if the native C compiler doesn't support 228 * __attribute__((packed))? How can we get it to generate unaligned 229 * accesses for *specific* items? 230 */ 231typedef struct { 232 uint16_t val; 233} __attribute__((packed)) unaligned_uint16_t; 234 235typedef struct { 236 uint32_t val; 237} __attribute__((packed)) unaligned_uint32_t; 238 239UNALIGNED_OK static inline uint16_t 240EXTRACT_16BITS(const void *p) 241{ 242 return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val)); 243} 244 245UNALIGNED_OK static inline uint32_t 246EXTRACT_32BITS(const void *p) 247{ 248 return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val)); 249} 250 251UNALIGNED_OK static inline uint64_t 252EXTRACT_64BITS(const void *p) 253{ 254 return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 | 255 ((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0)); 256} 257 258#else /* have to do it a byte at a time */ 259/* 260 * This isn't a GCC-compatible compiler, we don't have __attribute__, 261 * or we do but we don't know of any better way with this instruction 262 * set to do unaligned loads, so do unaligned loads of big-endian 263 * quantities the hard way - fetch the bytes one at a time and 264 * assemble them. 265 */ 266#define EXTRACT_16BITS(p) \ 267 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \ 268 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0))) 269#define EXTRACT_32BITS(p) \ 270 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \ 271 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \ 272 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \ 273 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0))) 274#define EXTRACT_64BITS(p) \ 275 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \ 276 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \ 277 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \ 278 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \ 279 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \ 280 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \ 281 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \ 282 ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0))) 283#endif /* must special-case unaligned accesses */ 284#else /* LBL_ALIGN */ 285/* 286 * The processor natively handles unaligned loads, so we can just 287 * cast the pointer and fetch through it. 288 */ 289static inline uint16_t UNALIGNED_OK 290EXTRACT_16BITS(const void *p) 291{ 292 return ((uint16_t)ntohs(*(const uint16_t *)(p))); 293} 294 295static inline uint32_t UNALIGNED_OK 296EXTRACT_32BITS(const void *p) 297{ 298 return ((uint32_t)ntohl(*(const uint32_t *)(p))); 299} 300 301static inline uint64_t UNALIGNED_OK 302EXTRACT_64BITS(const void *p) 303{ 304 return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 | 305 ((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0)); 306 307} 308 309#endif /* LBL_ALIGN */ 310 311#define EXTRACT_24BITS(p) \ 312 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \ 313 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 314 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0))) 315 316#define EXTRACT_40BITS(p) \ 317 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \ 318 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \ 319 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 320 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \ 321 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0))) 322 323#define EXTRACT_48BITS(p) \ 324 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \ 325 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \ 326 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \ 327 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \ 328 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \ 329 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0))) 330 331#define EXTRACT_56BITS(p) \ 332 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \ 333 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \ 334 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \ 335 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 336 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \ 337 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \ 338 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0))) 339 340/* 341 * Macros to extract possibly-unaligned little-endian integral values. 342 * XXX - do loads on little-endian machines that support unaligned loads? 343 */ 344#define EXTRACT_LE_16BITS(p) \ 345 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 346 ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0))) 347#define EXTRACT_LE_32BITS(p) \ 348 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 349 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 350 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 351 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0))) 352#define EXTRACT_LE_24BITS(p) \ 353 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 354 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 355 ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0))) 356#define EXTRACT_LE_64BITS(p) \ 357 ((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \ 358 ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \ 359 ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \ 360 ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \ 361 ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \ 362 ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \ 363 ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \ 364 ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0))) 365 366#endif /* __NetBSD__ */ 367 368/* 369 * Macros to check the presence of the values in question. 370 */ 371#define ND_TTEST_8BITS(p) ND_TTEST2(*(p), 1) 372#define ND_TCHECK_8BITS(p) ND_TCHECK2(*(p), 1) 373 374#define ND_TTEST_16BITS(p) ND_TTEST2(*(p), 2) 375#define ND_TCHECK_16BITS(p) ND_TCHECK2(*(p), 2) 376 377#define ND_TTEST_24BITS(p) ND_TTEST2(*(p), 3) 378#define ND_TCHECK_24BITS(p) ND_TCHECK2(*(p), 3) 379 380#define ND_TTEST_32BITS(p) ND_TTEST2(*(p), 4) 381#define ND_TCHECK_32BITS(p) ND_TCHECK2(*(p), 4) 382 383#define ND_TTEST_40BITS(p) ND_TTEST2(*(p), 5) 384#define ND_TCHECK_40BITS(p) ND_TCHECK2(*(p), 5) 385 386#define ND_TTEST_48BITS(p) ND_TTEST2(*(p), 6) 387#define ND_TCHECK_48BITS(p) ND_TCHECK2(*(p), 6) 388 389#define ND_TTEST_56BITS(p) ND_TTEST2(*(p), 7) 390#define ND_TCHECK_56BITS(p) ND_TCHECK2(*(p), 7) 391 392#define ND_TTEST_64BITS(p) ND_TTEST2(*(p), 8) 393#define ND_TCHECK_64BITS(p) ND_TCHECK2(*(p), 8) 394 395#define ND_TTEST_128BITS(p) ND_TTEST2(*(p), 16) 396#define ND_TCHECK_128BITS(p) ND_TCHECK2(*(p), 16) 397