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